Move select() and struct timeval to LFP bindings
[iolib.git] / src / syscalls / ffi-types-unix.lisp
blobbd4eb37788ea0ae81b6ac2e7206099ddb853f2a0
1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Grovel definitions for *NIX systems.
4 ;;;
6 (in-package :iolib.syscalls)
8 #+linux
9 (define "_GNU_SOURCE")
11 ;;; Largefile support on linux
12 ;;; TODO: check if these flags are required on solaris too
13 #+linux
14 (progn
15 (define "_LARGEFILE_SOURCE")
16 (define "_LARGEFILE64_SOURCE")
17 (define "_FILE_OFFSET_BITS" 64))
19 (include "stdlib.h" "errno.h" "sys/types.h" "sys/stat.h" "sys/mman.h"
20 "fcntl.h" "signal.h" "unistd.h" "limits.h" "time.h" "sys/select.h"
21 "sys/poll.h" "sys/ioctl.h" "sys/resource.h" "pwd.h" "grp.h"
22 "dirent.h" "sys/utsname.h" "sys/wait.h")
24 #+linux
25 (include "sys/epoll.h" "sys/syscall.h")
27 #+bsd
28 (include "sys/event.h" "sys/time.h") ; for kqueue
31 ;;;-------------------------------------------------------------------------
32 ;;; POSIX types
33 ;;;-------------------------------------------------------------------------
35 (ctype size-t "size_t")
36 (ctype ssize-t "ssize_t")
37 (ctype pid-t "pid_t")
38 (ctype uid-t "uid_t")
39 (ctype gid-t "gid_t")
40 (ctype off-t "off_t")
41 (ctype mode-t "mode_t")
44 ;;;-------------------------------------------------------------------------
45 ;;; Open()
46 ;;;-------------------------------------------------------------------------
48 (constant (o-rdonly "O_RDONLY"))
49 (constant (o-wronly "O_WRONLY"))
50 (constant (o-rdwr "O_RDWR"))
51 (constant (o-creat "O_CREAT"))
52 (constant (o-excl "O_EXCL"))
53 (constant (o-trunc "O_TRUNC"))
54 (constant (o-append "O_APPEND"))
56 (constant (o-noctty "O_NOCTTY"))
57 (constant (o-nonblock "O_NONBLOCK"))
58 (constant (o-ndelay "O_NDELAY"))
59 (constant (o-sync "O_SYNC"))
60 (constant (o-nofollow "O_NOFOLLOW"))
61 (constant (o-async "O_ASYNC"))
62 (constant (o-cloexec "O_CLOEXEC"))
66 ;;;-------------------------------------------------------------------------
67 ;;; Lseek()
68 ;;;-------------------------------------------------------------------------
70 (constant (seek-set "SEEK_SET"))
71 (constant (seek-cur "SEEK_CUR"))
72 (constant (seek-end "SEEK_END"))
75 ;;;-------------------------------------------------------------------------
76 ;;; Access()
77 ;;;-------------------------------------------------------------------------
79 (constant (r-ok "R_OK"))
80 (constant (w-ok "W_OK"))
81 (constant (x-ok "X_OK"))
82 (constant (f-ok "F_OK"))
85 ;;;-------------------------------------------------------------------------
86 ;;; time.h
87 ;;;-------------------------------------------------------------------------
89 (ctype time-t "time_t")
90 (ctype suseconds-t "suseconds_t")
92 #-darwin
93 (progn
94 (ctype clockid-t "clockid_t")
95 (constant (clock-monotonic "CLOCK_MONOTONIC"))
96 (constant (clock-realtime "CLOCK_REALTIME")))
98 (cstruct timespec "struct timespec"
99 "UNIX time specification in seconds and nanoseconds."
100 (sec "tv_sec" :type time-t)
101 (nsec "tv_nsec" :type :long))
104 ;;;-------------------------------------------------------------------------
105 ;;; sys/stat.h
106 ;;;-------------------------------------------------------------------------
108 (ctype dev-t "dev_t")
109 (ctype ino-t "ino_t")
111 (ctype nlink-t "nlink_t")
112 (ctype blksize-t "blksize_t")
113 (ctype blkcnt-t "blkcnt_t")
115 (cstruct stat "struct stat"
116 (dev "st_dev" :type dev-t)
117 (ino "st_ino" :type ino-t)
118 (mode "st_mode" :type mode-t)
119 (nlink "st_nlink" :type nlink-t)
120 (uid "st_uid" :type uid-t)
121 (gid "st_gid" :type gid-t)
122 (rdev "st_rdev" :type dev-t)
123 (size "st_size" :type off-t)
124 (blksize "st_blksize" :type blkcnt-t)
125 (blocks "st_blocks" :type blksize-t)
126 (atime "st_atime" :type time-t)
127 (mtime "st_mtime" :type time-t)
128 (ctime "st_ctime" :type time-t))
130 #+linux
131 (constant (+stat-version+ "_STAT_VER"))
133 (constant (s-irwxu "S_IRWXU")
134 :documentation "read, write, execute/search by owner")
135 (constant (s-irusr "S_IRUSR") :documentation "read permission, owner")
136 (constant (s-iwusr "S_IWUSR") :documentation "write permission, owner")
137 (constant (s-ixusr "S_IXUSR") :documentation "execute/search permission, owner")
138 (constant (s-ifmt "S_IFMT") :documentation "bitmask for type of entry")
139 (constant (s-ififo "S_IFIFO") :documentation "named pipe, aka fifo")
140 (constant (s-ifchr "S_IFCHR") :documentation "special character-device")
141 (constant (s-ifdir "S_IFDIR") :documentation "directory")
142 (constant (s-ifblk "S_IFBLK") :documentation "special block-device")
143 (constant (s-ifreg "S_IFREG") :documentation "regular file")
144 (constant (s-ifwht "S_IFWHT") :documentation "whiteout" :optional t)
145 (constant (s-iread "S_IREAD"))
146 (constant (s-iwrite "S_IWRITE"))
147 (constant (s-iexec "S_IEXEC"))
149 (constant (s-irwxg "S_IRWXG")
150 :documentation "read, write, execute/search by group")
151 (constant (s-irgrp "S_IRGRP") :documentation "read permission, group")
152 (constant (s-iwgrp "S_IWGRP") :documentation "write permission, group")
153 (constant (s-ixgrp "S_IXGRP")
154 :documentation "execute/search permission, group")
155 (constant (s-irwxo "S_IRWXO")
156 :documentation "read, write, execute/search by others")
157 (constant (s-iroth "S_IROTH") :documentation "read permission, others")
158 (constant (s-iwoth "S_IWOTH") :documentation "write permission, others")
159 (constant (s-ixoth "S_IXOTH")
160 :documentation "execute/search permission, others")
161 (constant (s-isuid "S_ISUID") :documentation "set-user-ID on execution")
162 (constant (s-isgid "S_ISGID") :documentation "set-group-ID on execution")
163 (constant (s-isvtx "S_ISVTX")
164 :documentation "'sticky' bit, many meanings, nonportable")
165 (constant (s-iflnk "S_IFLNK") :documentation "symbolic link")
166 (constant (s-ifsock "S_IFSOCK") :documentation "socket")
168 (constant (path-max "PATH_MAX" "MAXPATHLEN"))
171 ;;;-------------------------------------------------------------------------
172 ;;; signal.h
173 ;;;-------------------------------------------------------------------------
175 (constant (sighup "SIGHUP") :documentation "terminal line hangup.")
176 (constant (sigquit "SIGQUIT") :documentation "quit program.")
177 (constant (sigtrap "SIGTRAP") :documentation "trace trap.")
178 #-linux
179 (constant (sigemt "SIGEMT") :documentation "emulate instruction executed.")
180 (constant (sigkill "SIGKILL") :documentation "kill program.")
181 (constant (sigbus "SIGBUS") :documentation "bus error.")
182 (constant (sigsys "SIGSYS") :documentation "non-existent system call invoked.")
183 (constant (sigpipe "SIGPIPE") :documentation "write on a pipe with no reader.")
184 (constant (sigalrm "SIGALRM") :documentation "real-timeimer expired.")
185 (constant (sigurg "SIGURG")
186 :documentation "urgent condition present on socket.")
187 (constant (sigstop "SIGSTOP")
188 :documentation "stop (cannot be caught or ignored).")
189 (constant (sigtstp "SIGTSTP")
190 :documentation "stop signal generated from keyboard.")
191 (constant (sigcont "SIGCONT") :documentation "continue after stop.")
192 (constant (sigchld "SIGCHLD") :documentation "child status has changed.")
193 (constant (sigcld "SIGCLD") :documentation "child status has changed.") ; same as above
194 (constant (sigttin "SIGTTIN")
195 :documentation "background read attempted from control terminal.")
196 (constant (sigttou "SIGTTOU")
197 :documentation "background write attempted from control terminal.")
198 (constant (sigio "SIGIO")
199 :documentation "I/O is possible on a descriptor (see fcntl(2)).")
200 (constant (sigxcpu "SIGXCPU")
201 :documentation "cpuime limit exceeded (see setrlimit(2)).")
202 (constant (sigxfsz "SIGXFSZ")
203 :documentation "file size limit exceeded (see setrlimit(2)).")
204 (constant (sigvtalrm "SIGVTALRM")
205 :documentation "virtualime alarm (see setitimer(2)).")
206 (constant (sigprof "SIGPROF")
207 :documentation "profilingimer alarm (see setitimer(2)).")
208 (constant (sigwinch "SIGWINCH") :documentation "Window size change.")
209 #-linux
210 (constant (siginfo "SIGINFO") :documentation "status request from keyboard.")
211 (constant (sigusr1 "SIGUSR1") :documentation "User defined signal 1.")
212 (constant (sigusr2 "SIGUSR2") :documentation "User defined signal 2.")
213 #+linux
214 (progn
215 (constant (sigrtmin "SIGRTMIN")
216 :documentation "Smallest real-time signal number.")
217 (constant (sigrtmax "SIGRTMAX")
218 :documentation "Largest real-time signal number."))
220 (constant (sig-ign "SIG_IGN"))
221 (constant (sig-dfl "SIG_DFL"))
223 (cstruct sigaction "struct sigaction"
224 (handler "sa_handler" :type :pointer)
225 (sigaction "sa_sigaction" :type :pointer)
226 (mask "sa_mask" :type :unsigned-long) ;; actual type can be structure or array...
227 (flags "sa_flags" :type :int))
229 (constant (sa-nocldstop "SA_NOCLDSTOP"))
230 (constant (sa-nocldwait "SA_NOCLDWAIT"))
231 (constant (sa-nodefer "SA_NODEFER"))
232 (constant (sa-onstack "SA_ONSTACK"))
233 (constant (sa-resethand "SA_RESETHAND"))
234 (constant (sa-restart "SA_RESTART"))
235 (constant (sa-siginfo "SA_SIGINFO"))
239 ;;;-------------------------------------------------------------------------
240 ;;; sys/syscalls.h
241 ;;;-------------------------------------------------------------------------
243 #+linux (constant (sys-gettid "SYS_gettid"))
246 ;;;-------------------------------------------------------------------------
247 ;;; unistd.h
248 ;;;-------------------------------------------------------------------------
250 (ctype useconds-t "useconds_t")
253 ;;;-------------------------------------------------------------------------
254 ;;; Fcntl()
255 ;;;-------------------------------------------------------------------------
257 (constant (f-dupfd "F_DUPFD"))
258 (constant (f-getfd "F_GETFD"))
259 (constant (f-setfd "F_SETFD"))
260 (constant (f-getfl "F_GETFL"))
261 (constant (f-setfl "F_SETFL"))
262 (constant (f-getlk "F_GETLK"))
263 (constant (f-setlk "F_SETLK"))
264 (constant (f-setlkw "F_SETLKW"))
265 (constant (f-getown "F_GETOWN"))
266 (constant (f-setown "F_SETOWN"))
267 (constant (f-rdlck "F_RDLCK"))
268 (constant (f-wrlck "F_WRLCK"))
269 (constant (f-unlck "F_UNLCK"))
271 #+linux
272 (progn
273 (constant (f-getsig "F_GETSIG"))
274 (constant (f-setsig "F_SETSIG"))
275 (constant (f-setlease "F_SETLEASE"))
276 (constant (f-getlease "F_GETLEASE")))
279 ;;;-------------------------------------------------------------------------
280 ;;; Mmap()
281 ;;;-------------------------------------------------------------------------
283 (constant (prot-none "PROT_NONE") :documentation "mmap: no protection")
284 (constant (prot-read "PROT_READ") :documentation "mmap: read protection")
285 (constant (prot-write "PROT_WRITE") :documentation "mmap: write protection")
286 (constant (prot-exec "PROT_EXEC") :documentation "mmap: execute protection")
287 (constant (map-shared "MAP_SHARED") :documentation "mmap: shared memory")
288 (constant (map-private "MAP_PRIVATE") :documentation "mmap: private mapping")
289 (constant (map-fixed "MAP_FIXED") :documentation "mmap: map at location")
290 (constant (map-failed "MAP_FAILED") :documentation "mmap: failure")
293 ;;;-------------------------------------------------------------------------
294 ;;; Poll()
295 ;;;-------------------------------------------------------------------------
297 (ctype nfds-t "nfds_t")
299 (cstruct pollfd "struct pollfd"
300 "Poll file descriptor activity specification structure."
301 (fd "fd" :type :int)
302 (events "events" :type :short)
303 (revents "revents" :type :short))
305 (constant (pollin "POLLIN"))
306 (constant (pollrdnorm "POLLRDNORM"))
307 (constant (pollrdband "POLLRDBAND"))
308 (constant (pollpri "POLLPRI"))
309 (constant (pollout "POLLOUT"))
310 (constant (pollwrnorm "POLLWRNORM"))
311 (constant (pollwrband "POLLWRBAND"))
312 (constant (pollerr "POLLERR"))
313 #+linux (constant (pollrdhup "POLLRDHUP"))
314 (constant (pollhup "POLLHUP"))
315 (constant (pollnval "POLLNVAL"))
318 ;;;-------------------------------------------------------------------------
319 ;;; from sys/epoll.h
320 ;;;-------------------------------------------------------------------------
322 #+linux
323 (progn
324 (cunion epoll-data "epoll_data_t"
325 (ptr "ptr" :type :pointer)
326 (fd "fd" :type :int)
327 (u32 "u32" :type :uint32)
328 (u64 "u64" :type :uint64))
330 (cstruct epoll-event "struct epoll_event"
331 (events "events" :type :uint32)
332 (data "data" :type epoll-data))
334 (constant (epoll-ctl-add "EPOLL_CTL_ADD"))
335 (constant (epoll-ctl-del "EPOLL_CTL_DEL"))
336 (constant (epoll-ctl-mod "EPOLL_CTL_MOD"))
338 (constant (epollin "EPOLLIN"))
339 (constant (epollrdnorm "EPOLLRDNORM"))
340 (constant (epollrdband "EPOLLRDBAND"))
341 (constant (epollpri "EPOLLPRI"))
342 (constant (epollout "EPOLLOUT"))
343 (constant (epollwrnorm "EPOLLWRNORM"))
344 (constant (epollwrband "EPOLLWRBAND"))
345 (constant (epollerr "EPOLLERR"))
346 (constant (epollhup "EPOLLHUP"))
347 (constant (epollmsg "EPOLLMSG"))
348 (constant (epolloneshot "EPOLLONESHOT"))
349 (constant (epollet "EPOLLET")))
352 ;;;-------------------------------------------------------------------------
353 ;;; from sys/event.h
354 ;;;-------------------------------------------------------------------------
356 #+bsd
357 (progn
358 (ctype intptr-t "intptr_t")
359 (ctype uintptr-t "uintptr_t")
361 (cstruct kevent "struct kevent"
362 (ident "ident" :type uintptr-t)
363 (filter "filter" :type #-netbsd :short
364 #+netbsd :uint32)
365 (flags "flags" :type #-netbsd :unsigned-short
366 #+netbsd :uint32)
367 (fflags "fflags" :type #-netbsd :unsigned-int
368 #+netbsd :uint32)
369 (data "data" :type #-netbsd intptr-t
370 #+netbsd :int64)
371 (udata "udata" :type :pointer))
373 ;; kevent() flags
374 (constant (ev-add "EV_ADD"))
375 (constant (ev-enable "EV_ENABLE"))
376 (constant (ev-disable "EV_DISABLE"))
377 (constant (ev-delete "EV_DELETE"))
378 (constant (ev-oneshot "EV_ONESHOT"))
379 (constant (ev-clear "EV_CLEAR"))
380 (constant (ev-eof "EV_EOF"))
381 (constant (ev-error "EV_ERROR"))
383 ;; kevent() filter flags
384 (constant (evfilt-read "EVFILT_READ"))
385 (constant (evfilt-write "EVFILT_WRITE"))
386 (constant (evfilt-aio "EVFILT_AIO"))
387 (constant (evfilt-vnode "EVFILT_VNODE"))
388 (constant (evfilt-proc "EVFILT_PROC"))
389 (constant (evfilt-signal "EVFILT_SIGNAL"))
390 (constant (evfilt-timer "EVFILT_TIMER"))
391 #-darwin (constant (evfilt-netdev "EVFILT_NETDEV"))
393 ;; EVFILT_VNODE options
394 (constant (note-delete "NOTE_DELETE"))
395 (constant (note-write "NOTE_WRITE"))
396 (constant (note-extend "NOTE_EXTEND"))
397 (constant (note-attrib "NOTE_ATTRIB"))
398 (constant (note-link "NOTE_LINK"))
399 (constant (note-rename "NOTE_RENAME"))
400 (constant (note-revoke "NOTE_REVOKE"))
402 ;; EVFILT_PROC options
403 (constant (note-exit "NOTE_EXIT"))
404 (constant (note-fork "NOTE_FORK"))
405 (constant (note-exec "NOTE_EXEC"))
406 (constant (note-track "NOTE_TRACK"))
407 (constant (note-trackerr "NOTE_TRACKERR"))
409 ;; EVFILT_NETDEV options
410 #-darwin
411 (progn
412 (constant (note-linkup "NOTE_LINKUP"))
413 (constant (note-linkdown "NOTE_LINKDOWN"))
414 (constant (note-linkinv "NOTE_LINKINV"))))
417 ;;;-------------------------------------------------------------------------
418 ;;; dirent.h
419 ;;;-------------------------------------------------------------------------
421 ;; Apparently POSIX 1003.1-2001 (according to linux manpages) only
422 ;; requires d_name. Sigh. I guess we should assemble some decent
423 ;; wrapper functions. No, struct members can't be optional at this
424 ;; point.
425 (cstruct dirent "struct dirent"
426 ;; POSIX actually requires this to be d_ino
427 (fileno "d_fileno" :type #-freebsd ino-t #+freebsd :uint32)
428 (type "d_type" :type :uint8)
429 (name "d_name" :type :uint8 :count :auto))
431 ;;; filetypes set in d_type slot of struct dirent
432 (constant (dt-unknown "DT_UNKNOWN"))
433 (constant (dt-fifo "DT_FIFO"))
434 (constant (dt-chr "DT_CHR"))
435 (constant (dt-dir "DT_DIR"))
436 (constant (dt-blk "DT_BLK"))
437 (constant (dt-reg "DT_REG"))
438 (constant (dt-lnk "DT_LNK"))
439 (constant (dt-sock "DT_SOCK"))
440 (constant (dt-wht "DT_WHT"))
443 ;;;-------------------------------------------------------------------------
444 ;;; Ioctl()
445 ;;;-------------------------------------------------------------------------
447 (constant (fionbio "FIONBIO"))
448 (constant (fionread "FIONREAD"))
451 ;;;-------------------------------------------------------------------------
452 ;;; sys/resource.h
453 ;;;-------------------------------------------------------------------------
455 (ctype rlim-t "rlim_t")
456 (ctype id-t "id_t")
458 (cstruct rlimit "struct rlimit"
459 (cur "rlim_cur" :type rlim-t)
460 (max "rlim_max" :type rlim-t))
462 (cstruct rusage "struct rusage"
463 (utime "ru_utime" :type timeval)
464 (stime "ru_stime" :type timeval)
465 (maxrss "ru_maxrss" :type :long)
466 (ixrss "ru_ixrss" :type :long)
467 (idrss "ru_idrss" :type :long)
468 (isrss "ru_isrss" :type :long)
469 (minflt "ru_minflt" :type :long)
470 (majflt "ru_majflt" :type :long)
471 (nswap "ru_nswap" :type :long)
472 (inblock "ru_inblock" :type :long)
473 (oublock "ru_oublock" :type :long)
474 (msgsnd "ru_msgsnd" :type :long)
475 (msgrcv "ru_msgrcv" :type :long)
476 (nsignals "ru_nsignals" :type :long)
477 (nvcsw "ru_nvcsw" :type :long)
478 (nivcsw "ru_nivcsw" :type :long))
480 (constant (prio-process "PRIO_PROCESS"))
481 (constant (prio-pgrp "PRIO_PGRP"))
482 (constant (prio-user "PRIO_USER"))
483 (constant (rlim-infinity "RLIM_INFINITY"))
484 (constant (rusage-self "RUSAGE_SELF"))
485 (constant (rusage-children "RUSAGE_CHILDREN"))
486 (constant (rlimit-as "RLIMIT_AS"))
487 (constant (rlimit-core "RLIMIT_CORE"))
488 (constant (rlimit-cpu "RLIMIT_CPU"))
489 (constant (rlimit-data "RLIMIT_DATA"))
490 (constant (rlimit-fsize "RLIMIT_FSIZE"))
491 (constant (rlimit-memlock "RLIMIT_MEMLOCK"))
492 (constant (rlimit-nofile "RLIMIT_NOFILE"))
493 (constant (rlimit-nproc "RLIMIT_NPROC"))
494 (constant (rlimit-rss "RLIMIT_RSS"))
495 (constant (rlimit-stack "RLIMIT_STACK"))
497 #+linux
498 (progn
499 (constant (rlim-saved-max "RLIM_SAVED_MAX"))
500 (constant (rlim-saved-cur "RLIM_SAVED_CUR"))
501 (constant (rlimit-locks "RLIMIT_LOCKS"))
502 (constant (rlimit-msgqueue "RLIMIT_MSGQUEUE"))
503 (constant (rlimit-nlimits "RLIMIT_NLIMITS"))
504 (constant (rlimit-nice "RLIMIT_NICE"))
505 (constant (rlimit-rtprio "RLIMIT_RTPRIO"))
506 (constant (rlimit-sigpending "RLIMIT_SIGPENDING")))
508 #+freebsd
509 (constant (rlimit-sbsize "RLIMIT_SBSIZE"))
512 ;;;-------------------------------------------------------------------------
513 ;;; sys/utsname.h
514 ;;;-------------------------------------------------------------------------
516 (cstruct utsname "struct utsname"
517 (sysname "sysname" :type :char)
518 (nodename "nodename" :type :char)
519 (release "release" :type :char)
520 (version "version" :type :char)
521 (machine "machine" :type :char))
524 ;;;-------------------------------------------------------------------------
525 ;;; pwd.h
526 ;;;-------------------------------------------------------------------------
528 (cstruct passwd-entry "struct passwd"
529 (name "pw_name" :type sstring)
530 (passwd "pw_passwd" :type sstring)
531 (uid "pw_uid" :type uid-t)
532 (gid "pw_gid" :type gid-t)
533 (gecos "pw_gecos" :type sstring)
534 (dir "pw_dir" :type sstring)
535 (shell "pw_shell" :type sstring))
538 ;;;-------------------------------------------------------------------------
539 ;;; grp.h
540 ;;;-------------------------------------------------------------------------
542 (cstruct group-entry "struct group"
543 (name "gr_name" :type sstring)
544 (passwd "gr_passwd" :type sstring)
545 (gid "gr_gid" :type gid-t)
546 (mem "gr_mem" :type :pointer))