1 (cl:in-package :sb-posix)
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))))
8 (defclass ,name ,superclasses
9 ,(loop for slotd in slots
10 collect (ldiff slotd (member :array-length slotd)))
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))
18 (setf instance (make-instance ',name)))
19 ,@(loop for slotd in slots
20 ;; FIXME: slotds in source are more complicated in general
22 ;; FIXME: baroque construction of intricate fragility
23 for array-length = (getf (cdr slotd) :array-length)
26 (let ((array (make-array ,array-length)))
27 (setf (slot-value instance ',(car slotd))
29 (dotimes (i ,array-length)
32 (sb-alien:slot alien ',(car slotd))
35 collect `(setf (slot-value instance ',(car slotd))
36 (sb-alien:slot alien ',(car slotd))))
38 (defun ,to-alien (instance &optional alien)
39 (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
40 (type ,name instance))
42 (setf alien (sb-alien:make-alien ,alien-type)))
43 ,@(loop for slotd in slots
44 for array-length = (getf (cdr slotd) :array-length)
47 (let ((array (slot-value instance ',(car slotd))))
48 (dotimes (i ,array-length)
50 (sb-alien:slot alien ',(car slotd))
54 collect `(setf (sb-alien:slot alien ',(car slotd))
55 (slot-value instance ',(car slotd)))))
56 (find-class ',name))))
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))))))
65 (defun syscall-error ()
66 (error 'sb-posix:syscall-error :errno (get-errno)))
68 (declaim (inline never-fails))
69 (defun never-fails (&rest args)
70 (declare (ignore args))
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))
79 (concatenate 'string "_" name))
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)
93 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
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))
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 "opendir" (* t) null-alien (pathname filename))
110 (define-call ("readdir" :options :largefile) (* dirent)
111 ;; readdir() has the worst error convention in the world. It's just
112 ;; too painful to support. (return is NULL _and_ errno "unchanged"
113 ;; is not an error, it's EOF).
116 (define-call "closedir" int minusp (dir (* t)))
117 ;; need to do this here because we can't do it in the DEFPACKAGE
118 (define-call* "umask" mode-t never-fails (mode mode-t))
119 (define-call* "getpid" pid-t never-fails)
123 (define-call "chown" int minusp (pathname filename)
124 (owner uid-t) (group gid-t))
125 (define-call "chroot" int minusp (pathname filename))
126 (define-call "fchdir" int minusp (fd file-descriptor))
127 (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
128 (define-call "fchown" int minusp (fd file-descriptor)
129 (owner uid-t) (group gid-t))
130 (define-call "fdatasync" int minusp (fd file-descriptor))
131 (define-call ("ftruncate" :options :largefile)
132 int minusp (fd file-descriptor) (length off-t))
133 (define-call "fsync" int minusp (fd file-descriptor))
134 (define-call "lchown" int minusp (pathname filename)
135 (owner uid-t) (group gid-t))
136 (define-call "link" int minusp (oldpath filename) (newpath filename))
137 (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
138 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
139 (define-call "sync" void never-fails)
140 (define-call ("truncate" :options :largefile)
141 int minusp (pathname filename) (length off-t))
142 ;; FIXME: Windows does have _mktemp, which has a slightlty different
144 (defun mkstemp (template)
145 ;; we are emulating sb-alien's charset conversion for strings
146 ;; here, to accommodate for the call-by-reference nature of
147 ;; mkstemp's template strings.
148 (let ((arg (sb-ext:string-to-octets
150 :external-format sb-alien::*default-c-string-external-format*)))
151 (sb-sys:with-pinned-objects (arg)
152 (let ((result (alien-funcall (extern-alien "mkstemp"
153 (function int c-string))
154 (sap-alien (sb-alien::vector-sap arg)
156 (when (minusp result)
159 (sb-ext:octets-to-string
161 :external-format sb-alien::*default-c-string-external-format*))))))
162 (define-call-internally ioctl-without-arg "ioctl" int minusp
163 (fd file-descriptor) (cmd int))
164 (define-call-internally ioctl-with-int-arg "ioctl" int minusp
165 (fd file-descriptor) (cmd int) (arg int))
166 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
167 (fd file-descriptor) (cmd int)
168 (arg alien-pointer-to-anything-or-nil))
169 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
172 ((alien int) (ioctl-with-int-arg fd cmd arg))
173 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
174 (ioctl-without-arg fd cmd)))
175 (define-call-internally fcntl-without-arg "fcntl" int minusp
176 (fd file-descriptor) (cmd int))
177 (define-call-internally fcntl-with-int-arg "fcntl" int minusp
178 (fd file-descriptor) (cmd int) (arg int))
179 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
180 (fd file-descriptor) (cmd int)
181 (arg alien-pointer-to-anything-or-nil))
182 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
185 ((alien int) (fcntl-with-int-arg fd cmd arg))
186 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
187 (fcntl-without-arg fd cmd)))
190 (define-call "geteuid" uid-t never-fails) ; "always successful", it says
191 (define-call "getresuid" uid-t never-fails)
192 (define-call "getuid" uid-t never-fails)
193 (define-call "seteuid" int minusp (uid uid-t))
194 (define-call "setfsuid" int minusp (uid uid-t))
195 (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
196 (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
197 (define-call "setuid" int minusp (uid uid-t))
198 (define-call "getegid" gid-t never-fails)
199 (define-call "getgid" gid-t never-fails)
200 (define-call "getresgid" gid-t never-fails)
201 (define-call "setegid" int minusp (gid gid-t))
202 (define-call "setfsgid" int minusp (gid gid-t))
203 (define-call "setgid" int minusp (gid gid-t))
204 (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
205 (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
207 ;; processes, signals
208 (define-call "alarm" int never-fails (seconds unsigned))
212 #+mach-exception-handler
214 ;; FIXME this is a lie, of course this can fail, but there's no
215 ;; error handling here yet!
216 (define-call "setup_mach_exceptions" void never-fails)
217 (define-call ("posix_fork" :c-name "fork") pid-t minusp)
219 (let ((pid (posix-fork)))
221 (setup-mach-exceptions))
223 (export 'fork :sb-posix))
225 #-mach-exception-handler
226 (define-call "fork" pid-t minusp)
228 (define-call "getpgid" pid-t minusp (pid pid-t))
229 (define-call "getppid" pid-t never-fails)
230 (define-call "getpgrp" pid-t never-fails)
231 (define-call "getsid" pid-t minusp (pid pid-t))
232 (define-call "kill" int minusp (pid pid-t) (signal int))
233 (define-call "killpg" int minusp (pgrp int) (signal int))
234 (define-call "pause" int minusp)
235 (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
236 (define-call "setpgrp" int minusp))
238 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
242 (export 'wait :sb-posix)
243 (declaim (inline wait))
244 (defun wait (&optional statusptr)
245 (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
246 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
248 (extern-alien "wait" (function pid-t (* int)))
249 (sb-sys:vector-sap ptr))))
252 (values pid (aref ptr 0))))))
256 (export 'waitpid :sb-posix)
257 (declaim (inline waitpid))
258 (defun waitpid (pid options &optional statusptr)
259 (declare (type (sb-alien:alien pid-t) pid)
260 (type (sb-alien:alien int) options)
261 (type (or null (simple-array (signed-byte 32) (1))) statusptr))
262 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
264 (extern-alien "waitpid" (function pid-t
266 pid (sb-sys:vector-sap ptr) options)))
269 (values pid (aref ptr 0)))))
271 (define-call "wifexited" boolean never-fails (status int))
272 (define-call "wexitstatus" int never-fails (status int))
273 (define-call "wifsignaled" boolean never-fails (status int))
274 (define-call "wtermsig" int never-fails (status int))
275 (define-call "wifstopped" boolean never-fails (status int))
276 (define-call "wstopsig" int never-fails (status int))
277 #+nil ; see alien/waitpid-macros.c
278 (define-call "wifcontinued" boolean never-fails (status int)))
283 (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
285 (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
286 (addr sap-or-nil) (length unsigned) (prot unsigned)
287 (flags unsigned) (fd file-descriptor) (offset off-t))
289 (define-call "munmap" int minusp
290 (start sb-sys:system-area-pointer) (length unsigned))
292 (define-call "msync" int minusp
293 (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
296 (define-call "getpagesize" int minusp)
298 ;;; KLUDGE: This could be taken from GetSystemInfo
299 (export (defun getpagesize () 4096))
303 (define-protocol-class passwd alien-passwd ()
304 ((name :initarg :name :accessor passwd-name)
305 (passwd :initarg :passwd :accessor passwd-passwd)
306 (uid :initarg :uid :accessor passwd-uid)
307 (gid :initarg :gid :accessor passwd-gid)
308 (gecos :initarg :gecos :accessor passwd-gecos)
309 (dir :initarg :dir :accessor passwd-dir)
310 (shell :initarg :shell :accessor passwd-shell)))
312 (defmacro define-pw-call (name arg type)
314 ;; FIXME: this isn't the documented way of doing this, surely?
315 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
317 (export ',lisp-name :sb-posix)
318 (declaim (inline ,lisp-name))
319 (defun ,lisp-name (,arg)
320 (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
323 (alien-to-passwd r)))))))
325 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
326 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
328 (define-protocol-class stat alien-stat ()
329 ((mode :initarg :mode :accessor stat-mode)
330 (ino :initarg :ino :accessor stat-ino)
331 (dev :initarg :dev :accessor stat-dev)
332 (nlink :initarg :nlink :accessor stat-nlink)
333 (uid :initarg :uid :accessor stat-uid)
334 (gid :initarg :gid :accessor stat-gid)
335 (size :initarg :size :accessor stat-size)
336 (atime :initarg :atime :accessor stat-atime)
337 (mtime :initarg :mtime :accessor stat-mtime)
338 (ctime :initarg :ctime :accessor stat-ctime)))
340 (defmacro define-stat-call (name arg designator-fun type)
341 ;; FIXME: this isn't the documented way of doing this, surely?
342 (let ((lisp-name (lisp-for-c-symbol name)))
344 (export ',lisp-name :sb-posix)
345 (declaim (inline ,lisp-name))
346 (defun ,lisp-name (,arg &optional stat)
347 (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
348 (with-alien-stat a-stat ()
349 (let ((r (alien-funcall
350 (extern-alien ,(real-c-name (list name :options :largefile)) ,type)
351 (,designator-fun ,arg)
355 (alien-to-stat a-stat stat)))))))
357 ;; Note: _stat, _lstat, and _fstat for NetBSD are provided in
358 ;; src/runtime/bsd-os.c. See comments in that file
359 ;; for an explanation. -- RMK 2006-10-15
360 (define-stat-call #-(or win32 netbsd) "stat" #+(or win32 netbsd) "_stat"
362 (function int c-string (* alien-stat)))
365 (define-stat-call #-netbsd "lstat" #+netbsd "_lstat"
367 (function int c-string (* alien-stat)))
368 ;;; No symbolic links on Windows, so use stat
371 (declaim (inline lstat))
372 (export (defun lstat (filename &optional stat)
373 (if stat (stat filename stat) (stat filename)))))
375 (define-stat-call #-(or win32 netbsd) "fstat" #+(or win32 netbsd) "_fstat"
377 (function int int (* alien-stat)))
381 (define-call "s_isreg" boolean never-fails (mode mode-t))
382 (define-call "s_isdir" boolean never-fails (mode mode-t))
383 (define-call "s_ischr" boolean never-fails (mode mode-t))
384 (define-call "s_isblk" boolean never-fails (mode mode-t))
385 (define-call "s_isfifo" boolean never-fails (mode mode-t))
386 (define-call "s_islnk" boolean never-fails (mode mode-t))
387 (define-call "s_issock" boolean never-fails (mode mode-t))
391 (export 'pipe :sb-posix)
392 (declaim (inline pipe))
393 (defun pipe (&optional filedes2)
394 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
396 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
397 (let ((r (alien-funcall
398 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
399 (extern-alien "pipe" (function int (* int)))
400 (sb-sys:vector-sap filedes2))))
403 (values (aref filedes2 0) (aref filedes2 1))))
406 (define-protocol-class termios alien-termios ()
407 ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
408 (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
409 (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
410 (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
411 (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
415 (export 'tcsetattr :sb-posix)
416 (declaim (inline tcsetattr))
417 (defun tcsetattr (fd actions termios)
418 (declare (type termios termios))
419 (with-alien-termios a-termios ()
420 (termios-to-alien termios a-termios)
421 (let ((fd (file-descriptor fd)))
422 (let* ((r (alien-funcall
425 (function int int int (* alien-termios)))
426 fd actions a-termios)))
430 (export 'tcgetattr :sb-posix)
431 (declaim (inline tcgetattr))
432 (defun tcgetattr (fd &optional termios)
433 (declare (type (or null termios) termios))
434 (with-alien-termios a-termios ()
435 (let ((r (alien-funcall
436 (extern-alien "tcgetattr"
437 (function int int (* alien-termios)))
442 (setf termios (alien-to-termios a-termios termios))))
444 (export 'cfsetispeed :sb-posix)
445 (declaim (inline cfsetispeed))
446 (defun cfsetispeed (speed &optional termios)
447 (declare (type (or null termios) termios))
448 (with-alien-termios a-termios ()
449 (let ((r (alien-funcall
450 (extern-alien "cfsetispeed"
451 (function int (* alien-termios) speed-t))
456 (setf termios (alien-to-termios a-termios termios))))
458 (export 'cfsetospeed :sb-posix)
459 (declaim (inline cfsetospeed))
460 (defun cfsetospeed (speed &optional termios)
461 (declare (type (or null termios) termios))
462 (with-alien-termios a-termios ()
463 (let ((r (alien-funcall
464 (extern-alien "cfsetospeed"
465 (function int (* alien-termios) speed-t))
470 (setf termios (alien-to-termios a-termios termios))))
472 (export 'cfgetispeed :sb-posix)
473 (declaim (inline cfgetispeed))
474 (defun cfgetispeed (termios)
475 (declare (type termios termios))
476 (with-alien-termios a-termios ()
477 (termios-to-alien termios a-termios)
478 (alien-funcall (extern-alien "cfgetispeed"
479 (function speed-t (* alien-termios)))
481 (export 'cfgetospeed :sb-posix)
482 (declaim (inline cfgetospeed))
483 (defun cfgetospeed (termios)
484 (declare (type termios termios))
485 (with-alien-termios a-termios ()
486 (termios-to-alien termios a-termios)
487 (alien-funcall (extern-alien "cfgetospeed"
488 (function speed-t (* alien-termios)))
494 (export 'time :sb-posix)
496 (let ((result (alien-funcall (extern-alien "time"
497 (function time-t (* time-t)))
502 (export 'utime :sb-posix)
503 (defun utime (filename &optional access-time modification-time)
504 (let ((fun (extern-alien "utime" (function int c-string
506 (name (filename filename)))
507 (if (not (and access-time modification-time))
508 (alien-funcall fun name nil)
509 (with-alien ((utimbuf (struct alien-utimbuf)))
510 (setf (slot utimbuf 'actime) (or access-time 0)
511 (slot utimbuf 'modtime) (or modification-time 0))
512 (let ((result (alien-funcall fun name (alien-sap utimbuf))))
516 (export 'utimes :sb-posix)
517 (defun utimes (filename &optional access-time modification-time)
518 (flet ((seconds-and-useconds (time)
519 (multiple-value-bind (integer fractional)
521 (values integer (cl:truncate (* fractional 1000000)))))
522 (maybe-syscall-error (value)
526 (let ((fun (extern-alien "utimes" (function int c-string
527 (* (array alien-timeval 2)))))
528 (name (filename filename)))
529 (if (not (and access-time modification-time))
530 (maybe-syscall-error (alien-funcall fun name nil))
531 (with-alien ((buf (array alien-timeval 2)))
532 (let ((actime (deref buf 0))
533 (modtime (deref buf 1)))
534 (setf (values (slot actime 'sec)
536 (seconds-and-useconds (or access-time 0))
537 (values (slot modtime 'sec)
538 (slot modtime 'usec))
539 (seconds-and-useconds (or modification-time 0)))
540 (maybe-syscall-error (alien-funcall fun name
541 (alien-sap buf))))))))))
546 (export 'getenv :sb-posix)
548 (let ((r (alien-funcall
549 (extern-alien "getenv" (function (* char) c-string))
551 (declare (type (alien (* char)) r))
552 (unless (null-alien r)
554 (define-call "putenv" int minusp (string c-string))