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 ,(concatenate 'string "_" name) ,@arguments))
78 (define-call* "access" int minusp (pathname filename) (mode int))
79 (define-call* "chdir" int minusp (pathname filename))
80 (define-call* "chmod" int minusp (pathname filename) (mode mode-t))
81 (define-call* "close" int minusp (fd file-descriptor))
82 (define-call* "creat" int minusp (pathname filename) (mode mode-t))
83 (define-call* "dup" int minusp (oldfd file-descriptor))
84 (define-call* "dup2" int minusp (oldfd file-descriptor)
85 (newfd file-descriptor))
86 (define-call* ("lseek" :largefile)
87 off-t minusp (fd file-descriptor) (offset off-t)
89 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
92 (define-call-internally open-with-mode ,x int minusp
93 (pathname filename) (flags int) (mode mode-t))
94 (define-call-internally open-without-mode ,x int minusp
95 (pathname filename) (flags int))
96 (define-entry-point ,x
97 (pathname flags &optional (mode nil mode-supplied))
99 (open-with-mode pathname flags mode)
100 (open-without-mode pathname flags))))))
101 (def #-win32 "open" #+win32 "_open"))
102 (define-call "rename" int minusp (oldpath filename) (newpath filename))
103 (define-call* "rmdir" int minusp (pathname filename))
104 (define-call* "unlink" int minusp (pathname filename))
105 (define-call "opendir" (* t) null-alien (pathname filename))
106 (define-call "readdir" (* dirent)
107 ;; readdir() has the worst error convention in the world. It's just
108 ;; too painful to support. (return is NULL _and_ errno "unchanged"
109 ;; is not an error, it's EOF).
112 (define-call "closedir" int minusp (dir (* t)))
113 ;; need to do this here because we can't do it in the DEFPACKAGE
114 (define-call* "umask" mode-t never-fails (mode mode-t))
115 (define-call* "getpid" pid-t never-fails)
119 (define-call "chown" int minusp (pathname filename)
120 (owner uid-t) (group gid-t))
121 (define-call "chroot" int minusp (pathname filename))
122 (define-call "fchdir" int minusp (fd file-descriptor))
123 (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
124 (define-call "fchown" int minusp (fd file-descriptor)
125 (owner uid-t) (group gid-t))
126 (define-call "fdatasync" int minusp (fd file-descriptor))
127 (define-call ("ftruncate" :largefile)
128 int minusp (fd file-descriptor) (length off-t))
129 (define-call "fsync" int minusp (fd file-descriptor))
130 (define-call "lchown" int minusp (pathname filename)
131 (owner uid-t) (group gid-t))
132 (define-call "link" int minusp (oldpath filename) (newpath filename))
133 (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
134 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
135 (define-call "sync" void never-fails)
136 (define-call ("truncate" :largefile)
137 int minusp (pathname filename) (length off-t))
138 ;; FIXME: Windows does have _mktemp, which has a slightlty different
140 (define-call "mkstemp" int minusp (template c-string))
141 (define-call-internally ioctl-without-arg "ioctl" int minusp
142 (fd file-descriptor) (cmd int))
143 (define-call-internally ioctl-with-int-arg "ioctl" int minusp
144 (fd file-descriptor) (cmd int) (arg int))
145 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
146 (fd file-descriptor) (cmd int)
147 (arg alien-pointer-to-anything-or-nil))
148 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
151 ((alien int) (ioctl-with-int-arg fd cmd arg))
152 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
153 (ioctl-without-arg fd cmd)))
154 (define-call-internally fcntl-without-arg "fcntl" int minusp
155 (fd file-descriptor) (cmd int))
156 (define-call-internally fcntl-with-int-arg "fcntl" int minusp
157 (fd file-descriptor) (cmd int) (arg int))
158 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
159 (fd file-descriptor) (cmd int)
160 (arg alien-pointer-to-anything-or-nil))
161 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
164 ((alien int) (fcntl-with-int-arg fd cmd arg))
165 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
166 (fcntl-without-arg fd cmd)))
169 (define-call "geteuid" uid-t never-fails) ; "always successful", it says
170 (define-call "getresuid" uid-t never-fails)
171 (define-call "getuid" uid-t never-fails)
172 (define-call "seteuid" int minusp (uid uid-t))
173 (define-call "setfsuid" int minusp (uid uid-t))
174 (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
175 (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
176 (define-call "setuid" int minusp (uid uid-t))
177 (define-call "getegid" gid-t never-fails)
178 (define-call "getgid" gid-t never-fails)
179 (define-call "getresgid" gid-t never-fails)
180 (define-call "setegid" int minusp (gid gid-t))
181 (define-call "setfsgid" int minusp (gid gid-t))
182 (define-call "setgid" int minusp (gid gid-t))
183 (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
184 (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
186 ;; processes, signals
187 (define-call "alarm" int never-fails (seconds unsigned))
188 (define-call "fork" pid-t minusp)
189 (define-call "getpgid" pid-t minusp (pid pid-t))
190 (define-call "getppid" pid-t never-fails)
191 (define-call "getpgrp" pid-t never-fails)
192 (define-call "getsid" pid-t minusp (pid pid-t))
193 (define-call "kill" int minusp (pid pid-t) (signal int))
194 (define-call "killpg" int minusp (pgrp int) (signal int))
195 (define-call "pause" int minusp)
196 (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
197 (define-call "setpgrp" int minusp))
199 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
203 (export 'wait :sb-posix)
204 (declaim (inline wait))
205 (defun wait (&optional statusptr)
206 (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
207 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
209 (extern-alien "wait" (function pid-t (* int)))
210 (sb-sys:vector-sap ptr))))
213 (values pid (aref ptr 0))))))
217 (export 'waitpid :sb-posix)
218 (declaim (inline waitpid))
219 (defun waitpid (pid options &optional statusptr)
220 (declare (type (sb-alien:alien pid-t) pid)
221 (type (sb-alien:alien int) options)
222 (type (or null (simple-array (signed-byte 32) (1))) statusptr))
223 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
225 (extern-alien "waitpid" (function pid-t
227 pid (sb-sys:vector-sap ptr) options)))
230 (values pid (aref ptr 0)))))
232 (define-call "wifexited" boolean never-fails (status int))
233 (define-call "wexitstatus" int never-fails (status int))
234 (define-call "wifsignaled" boolean never-fails (status int))
235 (define-call "wtermsig" int never-fails (status int))
236 (define-call "wifstopped" boolean never-fails (status int))
237 (define-call "wstopsig" int never-fails (status int))
238 #+nil ; see alien/waitpid-macros.c
239 (define-call "wifcontinued" boolean never-fails (status int)))
244 (define-call ("mmap" :largefile) sb-sys:system-area-pointer
246 (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
247 (addr sap-or-nil) (length unsigned) (prot unsigned)
248 (flags unsigned) (fd file-descriptor) (offset off-t))
250 (define-call "munmap" int minusp
251 (start sb-sys:system-area-pointer) (length unsigned))
253 (define-call "msync" int minusp
254 (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
257 (define-call "getpagesize" int minusp)
259 ;;; KLUDGE: This could be taken from GetSystemInfo
260 (export (defun getpagesize () 4096))
264 (define-protocol-class passwd alien-passwd ()
265 ((name :initarg :name :accessor passwd-name)
266 (passwd :initarg :passwd :accessor passwd-passwd)
267 (uid :initarg :uid :accessor passwd-uid)
268 (gid :initarg :gid :accessor passwd-gid)
269 (gecos :initarg :gecos :accessor passwd-gecos)
270 (dir :initarg :dir :accessor passwd-dir)
271 (shell :initarg :shell :accessor passwd-shell)))
273 (defmacro define-pw-call (name arg type)
275 ;; FIXME: this isn't the documented way of doing this, surely?
276 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
278 (export ',lisp-name :sb-posix)
279 (declaim (inline ,lisp-name))
280 (defun ,lisp-name (,arg)
281 (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
284 (alien-to-passwd r)))))))
286 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
287 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
289 (define-protocol-class stat alien-stat ()
290 ((mode :initarg :mode :accessor stat-mode)
291 (ino :initarg :ino :accessor stat-ino)
292 (dev :initarg :dev :accessor stat-dev)
293 (nlink :initarg :nlink :accessor stat-nlink)
294 (uid :initarg :uid :accessor stat-uid)
295 (gid :initarg :gid :accessor stat-gid)
296 (size :initarg :size :accessor stat-size)
297 (atime :initarg :atime :accessor stat-atime)
298 (mtime :initarg :mtime :accessor stat-mtime)
299 (ctime :initarg :ctime :accessor stat-ctime)))
301 (defmacro define-stat-call (name arg designator-fun type)
302 ;; FIXME: this isn't the documented way of doing this, surely?
303 (let ((lisp-name (lisp-for-c-symbol name)))
305 (export ',lisp-name :sb-posix)
306 (declaim (inline ,lisp-name))
307 (defun ,lisp-name (,arg &optional stat)
308 (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
309 (with-alien-stat a-stat ()
310 (let ((r (alien-funcall
311 (extern-alien ,(real-c-name (list name :largefile)) ,type)
312 (,designator-fun ,arg)
316 (alien-to-stat a-stat stat)))))))
318 ;; Note: _stat, _lstat, and _fstat for NetBSD are provided in
319 ;; src/runtime/bsd-os.c. See comments in that file
320 ;; for an explanation. -- RMK 2006-10-15
321 (define-stat-call #-(or win32 netbsd) "stat" #+(or win32 netbsd) "_stat"
323 (function int c-string (* alien-stat)))
326 (define-stat-call #-netbsd "lstat" #+netbsd "_lstat"
328 (function int c-string (* alien-stat)))
329 ;;; No symbolic links on Windows, so use stat
332 (declaim (inline lstat))
333 (export (defun lstat (filename &optional stat)
334 (if stat (stat filename stat) (stat filename)))))
336 (define-stat-call #-(or win32 netbsd) "fstat" #+(or win32 netbsd) "_fstat"
338 (function int int (* alien-stat)))
342 (define-call "s_isreg" boolean never-fails (mode mode-t))
343 (define-call "s_isdir" boolean never-fails (mode mode-t))
344 (define-call "s_ischr" boolean never-fails (mode mode-t))
345 (define-call "s_isblk" boolean never-fails (mode mode-t))
346 (define-call "s_isfifo" boolean never-fails (mode mode-t))
347 (define-call "s_islnk" boolean never-fails (mode mode-t))
348 (define-call "s_issock" boolean never-fails (mode mode-t))
352 (export 'pipe :sb-posix)
353 (declaim (inline pipe))
354 (defun pipe (&optional filedes2)
355 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
357 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
358 (let ((r (alien-funcall
359 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
360 (extern-alien "pipe" (function int (* int)))
361 (sb-sys:vector-sap filedes2))))
364 (values (aref filedes2 0) (aref filedes2 1))))
367 (define-protocol-class termios alien-termios ()
368 ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
369 (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
370 (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
371 (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
372 (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
376 (export 'tcsetattr :sb-posix)
377 (declaim (inline tcsetattr))
378 (defun tcsetattr (fd actions termios)
379 (declare (type termios termios))
380 (with-alien-termios a-termios ()
381 (termios-to-alien termios a-termios)
382 (let ((fd (file-descriptor fd)))
383 (let* ((r (alien-funcall
386 (function int int int (* alien-termios)))
387 fd actions a-termios)))
391 (export 'tcgetattr :sb-posix)
392 (declaim (inline tcgetattr))
393 (defun tcgetattr (fd &optional termios)
394 (declare (type (or null termios) termios))
395 (with-alien-termios a-termios ()
396 (let ((r (alien-funcall
397 (extern-alien "tcgetattr"
398 (function int int (* alien-termios)))
403 (setf termios (alien-to-termios a-termios termios))))
405 (export 'cfsetispeed :sb-posix)
406 (declaim (inline cfsetispeed))
407 (defun cfsetispeed (speed &optional termios)
408 (declare (type (or null termios) termios))
409 (with-alien-termios a-termios ()
410 (let ((r (alien-funcall
411 (extern-alien "cfsetispeed"
412 (function int (* alien-termios) speed-t))
417 (setf termios (alien-to-termios a-termios termios))))
419 (export 'cfsetospeed :sb-posix)
420 (declaim (inline cfsetospeed))
421 (defun cfsetospeed (speed &optional termios)
422 (declare (type (or null termios) termios))
423 (with-alien-termios a-termios ()
424 (let ((r (alien-funcall
425 (extern-alien "cfsetospeed"
426 (function int (* alien-termios) speed-t))
431 (setf termios (alien-to-termios a-termios termios))))
433 (export 'cfgetispeed :sb-posix)
434 (declaim (inline cfgetispeed))
435 (defun cfgetispeed (termios)
436 (declare (type termios termios))
437 (with-alien-termios a-termios ()
438 (termios-to-alien termios a-termios)
439 (alien-funcall (extern-alien "cfgetispeed"
440 (function speed-t (* alien-termios)))
442 (export 'cfgetospeed :sb-posix)
443 (declaim (inline cfgetospeed))
444 (defun cfgetospeed (termios)
445 (declare (type termios termios))
446 (with-alien-termios a-termios ()
447 (termios-to-alien termios a-termios)
448 (alien-funcall (extern-alien "cfgetospeed"
449 (function speed-t (* alien-termios)))
455 (export 'time :sb-posix)
457 (let ((result (alien-funcall (extern-alien "time"
458 (function time-t (* time-t)))
463 (export 'utime :sb-posix)
464 (defun utime (filename &optional access-time modification-time)
465 (let ((fun (extern-alien "utime" (function int c-string
467 (name (filename filename)))
468 (if (not (and access-time modification-time))
469 (alien-funcall fun name nil)
470 (with-alien ((utimbuf (struct alien-utimbuf)))
471 (setf (slot utimbuf 'actime) (or access-time 0)
472 (slot utimbuf 'modtime) (or modification-time 0))
473 (let ((result (alien-funcall fun name (alien-sap utimbuf))))
477 (export 'utimes :sb-posix)
478 (defun utimes (filename &optional access-time modification-time)
479 (flet ((seconds-and-useconds (time)
480 (multiple-value-bind (integer fractional)
482 (values integer (cl:truncate (* fractional 1000000)))))
483 (maybe-syscall-error (value)
487 (let ((fun (extern-alien "utimes" (function int c-string
488 (* (array alien-timeval 2)))))
489 (name (filename filename)))
490 (if (not (and access-time modification-time))
491 (maybe-syscall-error (alien-funcall fun name nil))
492 (with-alien ((buf (array alien-timeval 2)))
493 (let ((actime (deref buf 0))
494 (modtime (deref buf 1)))
495 (setf (values (slot actime 'sec)
497 (seconds-and-useconds (or access-time 0))
498 (values (slot modtime 'sec)
499 (slot modtime 'usec))
500 (seconds-and-useconds (or modification-time 0)))
501 (maybe-syscall-error (alien-funcall fun name
502 (alien-sap buf))))))))))
507 (export 'getenv :sb-posix)
509 (let ((r (alien-funcall
510 (extern-alien "getenv" (function (* char) c-string))
512 (declare (type (alien (* char)) r))
513 (unless (null-alien r)
515 (define-call "putenv" int minusp (string c-string))