1.0.1.32: More syscall wrappers on NetBSD, to work around C preprocessor abuse
[sbcl.git] / contrib / sb-posix / interface.lisp
1 (cl:in-package :sb-posix)
2
3 (defmacro define-protocol-class
4     (name alien-type superclasses slots &rest options)
5   (let ((to-protocol (intern (format nil "ALIEN-TO-~A" name)))
6         (to-alien (intern (format nil "~A-TO-ALIEN" name))))
7     `(progn
8       (defclass ,name ,superclasses
9         ,(loop for slotd in slots
10                collect (ldiff slotd (member :array-length slotd)))
11         ,@options)
12       (declaim (inline ,to-alien ,to-protocol))
13       (declaim (inline ,to-protocol ,to-alien))
14       (defun ,to-protocol (alien &optional instance)
15         (declare (type (sb-alien:alien (* ,alien-type)) alien)
16                  (type (or null ,name) instance))
17         (unless instance
18           (setf instance (make-instance ',name)))
19         ,@(loop for slotd in slots
20                 ;; FIXME: slotds in source are more complicated in general
21                 ;;
22                 ;; FIXME: baroque construction of intricate fragility
23                 for array-length = (getf (cdr slotd) :array-length)
24                 if array-length
25                   collect `(progn
26                              (let ((array (make-array ,array-length)))
27                                (setf (slot-value instance ',(car slotd))
28                                      array)
29                                (dotimes (i ,array-length)
30                                  (setf (aref array i)
31                                        (sb-alien:deref
32                                         (sb-alien:slot alien ',(car slotd))
33                                         i)))))
34                 else
35                   collect `(setf (slot-value instance ',(car slotd))
36                                  (sb-alien:slot alien ',(car slotd))))
37         instance)
38       (defun ,to-alien (instance &optional alien)
39         (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
40                  (type ,name instance))
41         (unless alien
42           (setf alien (sb-alien:make-alien ,alien-type)))
43         ,@(loop for slotd in slots
44                 for array-length = (getf (cdr slotd) :array-length)
45                 if array-length
46                   collect `(progn
47                              (let ((array (slot-value instance ',(car slotd))))
48                                (dotimes (i ,array-length)
49                                  (setf (sb-alien:deref
50                                         (sb-alien:slot alien ',(car slotd))
51                                         i)
52                                        (aref array i)))))
53                 else
54                   collect `(setf (sb-alien:slot alien ',(car slotd))
55                                  (slot-value instance ',(car slotd)))))
56       (find-class ',name))))
57
58 (define-condition sb-posix:syscall-error (error)
59   ((errno :initarg :errno :reader sb-posix:syscall-errno))
60   (:report (lambda (c s)
61              (let ((errno (sb-posix:syscall-errno c)))
62                (format s "System call error ~A (~A)"
63                        errno (sb-int:strerror errno))))))
64
65 (defun syscall-error ()
66   (error 'sb-posix:syscall-error :errno (get-errno)))
67
68 (declaim (inline never-fails))
69 (defun never-fails (&rest args)
70   (declare (ignore args))
71   nil)
72
73 ;;; filesystem access
74 (defmacro define-call* (name &rest arguments)
75   #-win32 `(define-call ,name ,@arguments)
76   #+win32 `(define-call ,(if (consp name)
77                              `(,(concatenate 'string "_" (car name))
78                                 ,@(cdr name))
79                              (concatenate 'string "_" name))
80                ,@arguments))
81
82 (define-call* "access" int minusp (pathname filename) (mode int))
83 (define-call* "chdir" int minusp (pathname filename))
84 (define-call* "chmod" int minusp (pathname filename) (mode mode-t))
85 (define-call* "close" int minusp (fd file-descriptor))
86 (define-call* "creat" int minusp (pathname filename) (mode mode-t))
87 (define-call* "dup" int minusp (oldfd file-descriptor))
88 (define-call* "dup2" int minusp (oldfd file-descriptor)
89               (newfd file-descriptor))
90 (define-call* ("lseek" :options :largefile)
91     off-t minusp (fd file-descriptor) (offset off-t)
92     (whence int))
93 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
94 (macrolet ((def (x)
95                `(progn
96                  (define-call-internally open-with-mode ,x int minusp
97                    (pathname filename) (flags int) (mode mode-t))
98                  (define-call-internally open-without-mode ,x int minusp
99                    (pathname filename) (flags int))
100                  (define-entry-point ,x
101                      (pathname flags &optional (mode nil mode-supplied))
102                    (if mode-supplied
103                        (open-with-mode pathname flags mode)
104                        (open-without-mode pathname flags))))))
105     (def #-win32 "open" #+win32 "_open"))
106 (define-call "rename" int minusp (oldpath filename) (newpath filename))
107 (define-call* "rmdir" int minusp (pathname filename))
108 (define-call* "unlink" int minusp (pathname filename))
109 (define-call #-netbsd "opendir" #+netbsd "_opendir"
110     (* t) null-alien (pathname filename))
111 (define-call (#-netbsd "readdir" #+netbsd "_readdir" :options :largefile)
112   (* dirent)
113   ;; readdir() has the worst error convention in the world.  It's just
114   ;; too painful to support.  (return is NULL _and_ errno "unchanged"
115   ;; is not an error, it's EOF).
116   not
117   (dir (* t)))
118 (define-call "closedir" int minusp (dir (* t)))
119 ;; need to do this here because we can't do it in the DEFPACKAGE
120 (define-call* "umask" mode-t never-fails (mode mode-t))
121 (define-call* "getpid" pid-t never-fails)
122
123 #-win32
124 (progn
125   (define-call "chown" int minusp (pathname filename)
126                (owner uid-t) (group gid-t))
127   (define-call "chroot" int minusp (pathname filename))
128   (define-call "fchdir" int minusp (fd file-descriptor))
129   (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
130   (define-call "fchown" int minusp (fd file-descriptor)
131                (owner uid-t)  (group gid-t))
132   (define-call "fdatasync" int minusp (fd file-descriptor))
133   (define-call ("ftruncate" :options :largefile)
134       int minusp (fd file-descriptor) (length off-t))
135   (define-call "fsync" int minusp (fd file-descriptor))
136   (define-call "lchown" int minusp (pathname filename)
137                (owner uid-t)  (group gid-t))
138   (define-call "link" int minusp (oldpath filename) (newpath filename))
139   (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
140   (define-call "symlink" int minusp (oldpath filename) (newpath filename))
141   (define-call "sync" void never-fails)
142   (define-call ("truncate" :options :largefile)
143       int minusp (pathname filename) (length off-t))
144   ;; FIXME: Windows does have _mktemp, which has a slightlty different
145   ;; interface
146   (defun mkstemp (template)
147     ;; we are emulating sb-alien's charset conversion for strings
148     ;; here, to accommodate for the call-by-reference nature of
149     ;; mkstemp's template strings.
150     (let ((arg (sb-ext:string-to-octets
151                 (filename template)
152                 :external-format sb-alien::*default-c-string-external-format*)))
153       (sb-sys:with-pinned-objects (arg)
154         (let ((result (alien-funcall (extern-alien "mkstemp"
155                                                    (function int c-string))
156                                      (sap-alien (sb-alien::vector-sap arg)
157                                                 (* char)))))
158           (when (minusp result)
159             (syscall-error))
160           (values result
161                   (sb-ext:octets-to-string
162                    arg
163                    :external-format sb-alien::*default-c-string-external-format*))))))
164   (define-call-internally ioctl-without-arg "ioctl" int minusp
165                           (fd file-descriptor) (cmd int))
166   (define-call-internally ioctl-with-int-arg "ioctl" int minusp
167                           (fd file-descriptor) (cmd int) (arg int))
168   (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
169                           (fd file-descriptor) (cmd int)
170                           (arg alien-pointer-to-anything-or-nil))
171   (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
172     (if argp
173         (etypecase arg
174           ((alien int) (ioctl-with-int-arg fd cmd arg))
175           ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
176         (ioctl-without-arg fd cmd)))
177   (define-call-internally fcntl-without-arg "fcntl" int minusp
178                           (fd file-descriptor) (cmd int))
179   (define-call-internally fcntl-with-int-arg "fcntl" int minusp
180                           (fd file-descriptor) (cmd int) (arg int))
181   (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
182                           (fd file-descriptor) (cmd int)
183                           (arg alien-pointer-to-anything-or-nil))
184   (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
185     (if argp
186         (etypecase arg
187           ((alien int) (fcntl-with-int-arg fd cmd arg))
188           ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
189         (fcntl-without-arg fd cmd)))
190
191   ;; uid, gid
192   (define-call "geteuid" uid-t never-fails) ; "always successful", it says
193   (define-call "getresuid" uid-t never-fails)
194   (define-call "getuid" uid-t never-fails)
195   (define-call "seteuid" int minusp (uid uid-t))
196   (define-call "setfsuid" int minusp (uid uid-t))
197   (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
198   (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
199   (define-call "setuid" int minusp (uid uid-t))
200   (define-call "getegid" gid-t never-fails)
201   (define-call "getgid" gid-t never-fails)
202   (define-call "getresgid" gid-t never-fails)
203   (define-call "setegid" int minusp (gid gid-t))
204   (define-call "setfsgid" int minusp (gid gid-t))
205   (define-call "setgid" int minusp (gid gid-t))
206   (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
207   (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
208
209   ;; processes, signals
210   (define-call "alarm" int never-fails (seconds unsigned))
211
212
213
214   #+mach-exception-handler
215   (progn
216     ;; FIXME this is a lie, of course this can fail, but there's no
217     ;; error handling here yet!
218     (define-call "setup_mach_exceptions" void never-fails)
219     (define-call ("posix_fork" :c-name "fork") pid-t minusp)
220     (defun fork ()
221       (let ((pid (posix-fork)))
222         (when (= pid 0)
223           (setup-mach-exceptions))
224         pid))
225     (export 'fork :sb-posix))
226
227   #-mach-exception-handler
228   (define-call "fork" pid-t minusp)
229
230   (define-call "getpgid" pid-t minusp (pid pid-t))
231   (define-call "getppid" pid-t never-fails)
232   (define-call "getpgrp" pid-t never-fails)
233   (define-call "getsid" pid-t minusp  (pid pid-t))
234   (define-call "kill" int minusp (pid pid-t) (signal int))
235   (define-call "killpg" int minusp (pgrp int) (signal int))
236   (define-call "pause" int minusp)
237   (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
238   (define-call "setpgrp" int minusp))
239
240 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
241
242 #-win32
243 (progn
244  (export 'wait :sb-posix)
245  (declaim (inline wait))
246  (defun wait (&optional statusptr)
247    (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
248    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
249           (pid (alien-funcall
250                 (extern-alien "wait" (function pid-t (* int)))
251                 (sb-sys:vector-sap ptr))))
252      (if (minusp pid)
253          (syscall-error)
254          (values pid (aref ptr 0))))))
255
256 #-win32
257 (progn
258  (export 'waitpid :sb-posix)
259  (declaim (inline waitpid))
260  (defun waitpid (pid options &optional statusptr)
261    (declare (type (sb-alien:alien pid-t) pid)
262             (type (sb-alien:alien int) options)
263             (type (or null (simple-array (signed-byte 32) (1))) statusptr))
264    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
265           (pid (alien-funcall
266                 (extern-alien "waitpid" (function pid-t
267                                                   pid-t (* int) int))
268                 pid (sb-sys:vector-sap ptr) options)))
269      (if (minusp pid)
270          (syscall-error)
271          (values pid (aref ptr 0)))))
272  ;; waitpid macros
273  (define-call "wifexited" boolean never-fails (status int))
274  (define-call "wexitstatus" int never-fails (status int))
275  (define-call "wifsignaled" boolean never-fails (status int))
276  (define-call "wtermsig" int never-fails (status int))
277  (define-call "wifstopped" boolean never-fails (status int))
278  (define-call "wstopsig" int never-fails (status int))
279  #+nil ; see alien/waitpid-macros.c
280  (define-call "wifcontinued" boolean never-fails (status int)))
281
282 ;;; mmap, msync
283 #-win32
284 (progn
285  (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
286    (lambda (res)
287      (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
288    (addr sap-or-nil) (length unsigned) (prot unsigned)
289    (flags unsigned) (fd file-descriptor) (offset off-t))
290
291  (define-call "munmap" int minusp
292    (start sb-sys:system-area-pointer) (length unsigned))
293
294 (define-call "msync" int minusp
295   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
296
297 #-win32
298 (define-call "getpagesize" int minusp)
299 #+win32
300 ;;; KLUDGE: This could be taken from GetSystemInfo
301 (export (defun getpagesize () 4096))
302
303 ;;; passwd database
304 #-win32
305 (define-protocol-class passwd alien-passwd ()
306   ((name :initarg :name :accessor passwd-name)
307    (passwd :initarg :passwd :accessor passwd-passwd)
308    (uid :initarg :uid :accessor passwd-uid)
309    (gid :initarg :gid :accessor passwd-gid)
310    (gecos :initarg :gecos :accessor passwd-gecos)
311    (dir :initarg :dir :accessor passwd-dir)
312    (shell :initarg :shell :accessor passwd-shell)))
313
314 (defmacro define-pw-call (name arg type)
315   #-win32
316   ;; FIXME: this isn't the documented way of doing this, surely?
317   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
318     `(progn
319       (export ',lisp-name :sb-posix)
320       (declaim (inline ,lisp-name))
321       (defun ,lisp-name (,arg)
322         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
323           (if (null r)
324               r
325               (alien-to-passwd r)))))))
326
327 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
328 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
329
330 (define-protocol-class stat alien-stat ()
331   ((mode :initarg :mode :accessor stat-mode)
332    (ino :initarg :ino :accessor stat-ino)
333    (dev :initarg :dev :accessor stat-dev)
334    (nlink :initarg :nlink :accessor stat-nlink)
335    (uid :initarg :uid :accessor stat-uid)
336    (gid :initarg :gid :accessor stat-gid)
337    (size :initarg :size :accessor stat-size)
338    (atime :initarg :atime :accessor stat-atime)
339    (mtime :initarg :mtime :accessor stat-mtime)
340    (ctime :initarg :ctime :accessor stat-ctime)))
341
342 (defmacro define-stat-call (name arg designator-fun type)
343   ;; FIXME: this isn't the documented way of doing this, surely?
344   (let ((lisp-name (lisp-for-c-symbol name)))
345     `(progn
346       (export ',lisp-name :sb-posix)
347       (declaim (inline ,lisp-name))
348       (defun ,lisp-name (,arg &optional stat)
349         (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
350         (with-alien-stat a-stat ()
351           (let ((r (alien-funcall
352                     (extern-alien ,(real-c-name (list name :options :largefile)) ,type)
353                     (,designator-fun ,arg)
354                     a-stat)))
355             (when (minusp r)
356               (syscall-error))
357             (alien-to-stat a-stat stat)))))))
358
359 ;; Note: _stat, _lstat, and _fstat for NetBSD are provided in
360 ;; src/runtime/bsd-os.c.  See comments in that file
361 ;; for an explanation. -- RMK 2006-10-15
362 (define-stat-call #-(or win32 netbsd) "stat" #+(or win32 netbsd) "_stat"
363                   pathname filename
364                   (function int c-string (* alien-stat)))
365
366 #-win32
367 (define-stat-call #-netbsd "lstat" #+netbsd "_lstat"
368                   pathname filename
369                   (function int c-string (* alien-stat)))
370 ;;; No symbolic links on Windows, so use stat
371 #+win32
372 (progn
373   (declaim (inline lstat))
374   (export (defun lstat (filename &optional stat)
375             (if stat (stat filename stat) (stat filename)))))
376
377 (define-stat-call #-(or win32 netbsd) "fstat" #+(or win32 netbsd) "_fstat"
378                   fd file-descriptor
379                   (function int int (* alien-stat)))
380
381
382 ;;; mode flags
383 (define-call "s_isreg" boolean never-fails (mode mode-t))
384 (define-call "s_isdir" boolean never-fails (mode mode-t))
385 (define-call "s_ischr" boolean never-fails (mode mode-t))
386 (define-call "s_isblk" boolean never-fails (mode mode-t))
387 (define-call "s_isfifo" boolean never-fails (mode mode-t))
388 (define-call "s_islnk" boolean never-fails (mode mode-t))
389 (define-call "s_issock" boolean never-fails (mode mode-t))
390
391 #-win32
392 (progn
393  (export 'pipe :sb-posix)
394  (declaim (inline pipe))
395  (defun pipe (&optional filedes2)
396    (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
397    (unless filedes2
398      (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
399    (let ((r (alien-funcall
400              ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
401              (extern-alien "pipe" (function int (* int)))
402              (sb-sys:vector-sap filedes2))))
403      (when (minusp r)
404        (syscall-error)))
405    (values (aref filedes2 0) (aref filedes2 1))))
406
407 #-win32
408 (define-protocol-class termios alien-termios ()
409   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
410    (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
411    (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
412    (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
413    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
414
415 #-win32
416 (progn
417  (export 'tcsetattr :sb-posix)
418  (declaim (inline tcsetattr))
419  (defun tcsetattr (fd actions termios)
420    (declare (type termios termios))
421    (with-alien-termios a-termios ()
422      (termios-to-alien termios a-termios)
423      (let ((fd (file-descriptor fd)))
424        (let* ((r (alien-funcall
425                   (extern-alien
426                    "tcsetattr"
427                    (function int int int (* alien-termios)))
428                   fd actions a-termios)))
429          (when (minusp r)
430            (syscall-error)))
431        (values))))
432  (export 'tcgetattr :sb-posix)
433  (declaim (inline tcgetattr))
434  (defun tcgetattr (fd &optional termios)
435    (declare (type (or null termios) termios))
436    (with-alien-termios a-termios ()
437      (let ((r (alien-funcall
438                (extern-alien "tcgetattr"
439                              (function int int (* alien-termios)))
440                (file-descriptor fd)
441                a-termios)))
442        (when (minusp r)
443          (syscall-error))
444        (setf termios (alien-to-termios a-termios termios))))
445    termios)
446  (export 'cfsetispeed :sb-posix)
447  (declaim (inline cfsetispeed))
448  (defun cfsetispeed (speed &optional termios)
449    (declare (type (or null termios) termios))
450    (with-alien-termios a-termios ()
451      (let ((r (alien-funcall
452                (extern-alien "cfsetispeed"
453                              (function int (* alien-termios) speed-t))
454                a-termios
455                speed)))
456        (when (minusp r)
457          (syscall-error))
458        (setf termios (alien-to-termios a-termios termios))))
459    termios)
460  (export 'cfsetospeed :sb-posix)
461  (declaim (inline cfsetospeed))
462  (defun cfsetospeed (speed &optional termios)
463    (declare (type (or null termios) termios))
464    (with-alien-termios a-termios ()
465      (let ((r (alien-funcall
466                (extern-alien "cfsetospeed"
467                              (function int (* alien-termios) speed-t))
468                a-termios
469                speed)))
470        (when (minusp r)
471          (syscall-error))
472        (setf termios (alien-to-termios a-termios termios))))
473    termios)
474  (export 'cfgetispeed :sb-posix)
475  (declaim (inline cfgetispeed))
476  (defun cfgetispeed (termios)
477    (declare (type termios termios))
478    (with-alien-termios a-termios ()
479      (termios-to-alien termios a-termios)
480      (alien-funcall (extern-alien "cfgetispeed"
481                                   (function speed-t (* alien-termios)))
482                     a-termios)))
483  (export 'cfgetospeed :sb-posix)
484  (declaim (inline cfgetospeed))
485  (defun cfgetospeed (termios)
486    (declare (type termios termios))
487    (with-alien-termios a-termios ()
488      (termios-to-alien termios a-termios)
489      (alien-funcall (extern-alien "cfgetospeed"
490                                  (function speed-t (* alien-termios)))
491                     a-termios))))
492
493
494 #-win32
495 (progn
496   (export 'time :sb-posix)
497   (defun time ()
498     (let ((result (alien-funcall (extern-alien "time"
499                                                (function time-t (* time-t)))
500                                  nil)))
501       (if (minusp result)
502           (syscall-error)
503           result)))
504   (export 'utime :sb-posix)
505   (defun utime (filename &optional access-time modification-time)
506     (let ((fun (extern-alien "utime" (function int c-string
507                                                (* alien-utimbuf))))
508           (name (filename filename)))
509       (if (not (and access-time modification-time))
510           (alien-funcall fun name nil)
511           (with-alien ((utimbuf (struct alien-utimbuf)))
512             (setf (slot utimbuf 'actime) (or access-time 0)
513                   (slot utimbuf 'modtime) (or modification-time 0))
514             (let ((result (alien-funcall fun name (alien-sap utimbuf))))
515               (if (minusp result)
516                   (syscall-error)
517                   result))))))
518   (export 'utimes :sb-posix)
519   (defun utimes (filename &optional access-time modification-time)
520     (flet ((seconds-and-useconds (time)
521              (multiple-value-bind (integer fractional)
522                  (cl:truncate time)
523                (values integer (cl:truncate (* fractional 1000000)))))
524            (maybe-syscall-error (value)
525              (if (minusp value)
526                  (syscall-error)
527                  value)))
528       (let ((fun (extern-alien "utimes" (function int c-string
529                                                   (* (array alien-timeval 2)))))
530             (name (filename filename)))
531         (if (not (and access-time modification-time))
532             (maybe-syscall-error (alien-funcall fun name nil))
533             (with-alien ((buf (array alien-timeval 2)))
534               (let ((actime (deref buf 0))
535                     (modtime (deref buf 1)))
536                 (setf (values (slot actime 'sec)
537                               (slot actime 'usec))
538                       (seconds-and-useconds (or access-time 0))
539                       (values (slot modtime 'sec)
540                               (slot modtime 'usec))
541                       (seconds-and-useconds (or modification-time 0)))
542                 (maybe-syscall-error (alien-funcall fun name
543                                                     (alien-sap buf))))))))))
544
545
546 ;;; environment
547
548 (export 'getenv :sb-posix)
549 (defun getenv (name)
550   (let ((r (alien-funcall
551             (extern-alien "getenv" (function (* char) c-string))
552             name)))
553     (declare (type (alien (* char)) r))
554     (unless (null-alien r)
555       (cast r c-string))))
556 (define-call "putenv" int minusp (string c-string))
557
558 ;;; syslog
559 #-win32
560 (progn
561   (export 'openlog :sb-posix)
562   (export 'syslog :sb-posix)
563   (export 'closelog :sb-posix)
564   (defun openlog (ident options &optional (facility log-user))
565     (alien-funcall (extern-alien
566                     "openlog" (function void c-string int int))
567                    ident options facility))
568   (defun syslog (priority format &rest args)
569     "Send a message to the syslog facility, with severity level
570 PRIORITY.  The message will be formatted as by CL:FORMAT (rather
571 than C's printf) with format string FORMAT and arguments ARGS."
572     (flet ((syslog1 (priority message)
573              (alien-funcall (extern-alien
574                              "syslog" (function void int c-string c-string))
575                             priority "%s" message)))
576       (syslog1 priority (apply #'format nil format args))))
577   (define-call "closelog" void never-fails))
578