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" off-t minusp (fd file-descriptor) (offset off-t)
88 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
91 (define-call-internally open-with-mode ,x int minusp
92 (pathname filename) (flags int) (mode mode-t))
93 (define-call-internally open-without-mode ,x int minusp
94 (pathname filename) (flags int))
95 (define-entry-point ,x
96 (pathname flags &optional (mode nil mode-supplied))
98 (open-with-mode pathname flags mode)
99 (open-without-mode pathname flags))))))
100 (def #-win32 "open" #+win32 "_open"))
101 (define-call "rename" int minusp (oldpath filename) (newpath filename))
102 (define-call* "rmdir" int minusp (pathname filename))
103 (define-call* "unlink" int minusp (pathname filename))
104 (define-call "opendir" (* t) null-alien (pathname filename))
105 (define-call "readdir" (* dirent)
106 ;; readdir() has the worst error convention in the world. It's just
107 ;; too painful to support. (return is NULL _and_ errno "unchanged"
108 ;; is not an error, it's EOF).
111 (define-call "closedir" int minusp (dir (* t)))
112 ;; need to do this here because we can't do it in the DEFPACKAGE
113 (define-call* "umask" mode-t never-fails (mode mode-t))
114 (define-call* "getpid" pid-t never-fails)
118 (define-call "chown" int minusp (pathname filename)
119 (owner uid-t) (group gid-t))
120 (define-call "chroot" int minusp (pathname filename))
121 (define-call "fchdir" int minusp (fd file-descriptor))
122 (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
123 (define-call "fchown" int minusp (fd file-descriptor)
124 (owner uid-t) (group gid-t))
125 (define-call "fdatasync" int minusp (fd file-descriptor))
126 (define-call "ftruncate" int minusp (fd file-descriptor) (length off-t))
127 (define-call "fsync" int minusp (fd file-descriptor))
128 (define-call "lchown" int minusp (pathname filename)
129 (owner uid-t) (group gid-t))
130 (define-call "link" int minusp (oldpath filename) (newpath filename))
131 (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
132 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
133 (define-call "sync" void never-fails)
134 (define-call "truncate" int minusp (pathname filename) (length off-t))
135 ;; FIXME: Windows does have _mktemp, which has a slightlty different
137 (define-call "mkstemp" int minusp (template c-string))
138 (define-call-internally ioctl-without-arg "ioctl" int minusp
139 (fd file-descriptor) (cmd int))
140 (define-call-internally ioctl-with-int-arg "ioctl" int minusp
141 (fd file-descriptor) (cmd int) (arg int))
142 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
143 (fd file-descriptor) (cmd int)
144 (arg alien-pointer-to-anything-or-nil))
145 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
148 ((alien int) (ioctl-with-int-arg fd cmd arg))
149 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
150 (ioctl-without-arg fd cmd)))
151 (define-call-internally fcntl-without-arg "fcntl" int minusp
152 (fd file-descriptor) (cmd int))
153 (define-call-internally fcntl-with-int-arg "fcntl" int minusp
154 (fd file-descriptor) (cmd int) (arg int))
155 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
156 (fd file-descriptor) (cmd int)
157 (arg alien-pointer-to-anything-or-nil))
158 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
161 ((alien int) (fcntl-with-int-arg fd cmd arg))
162 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
163 (fcntl-without-arg fd cmd)))
166 (define-call "geteuid" uid-t never-fails) ; "always successful", it says
167 (define-call "getresuid" uid-t never-fails)
168 (define-call "getuid" uid-t never-fails)
169 (define-call "seteuid" int minusp (uid uid-t))
170 (define-call "setfsuid" int minusp (uid uid-t))
171 (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
172 (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
173 (define-call "setuid" int minusp (uid uid-t))
174 (define-call "getegid" gid-t never-fails)
175 (define-call "getgid" gid-t never-fails)
176 (define-call "getresgid" gid-t never-fails)
177 (define-call "setegid" int minusp (gid gid-t))
178 (define-call "setfsgid" int minusp (gid gid-t))
179 (define-call "setgid" int minusp (gid gid-t))
180 (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
181 (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
183 ;; processes, signals
184 (define-call "alarm" int never-fails (seconds unsigned))
185 (define-call "fork" pid-t minusp)
186 (define-call "getpgid" pid-t minusp (pid pid-t))
187 (define-call "getppid" pid-t never-fails)
188 (define-call "getpgrp" pid-t never-fails)
189 (define-call "getsid" pid-t minusp (pid pid-t))
190 (define-call "kill" int minusp (pid pid-t) (signal int))
191 (define-call "killpg" int minusp (pgrp int) (signal int))
192 (define-call "pause" int minusp)
193 (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
194 (define-call "setpgrp" int minusp))
196 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
200 (export 'wait :sb-posix)
201 (declaim (inline wait))
202 (defun wait (&optional statusptr)
203 (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
204 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
206 (extern-alien "wait" (function pid-t (* int)))
207 (sb-sys:vector-sap ptr))))
210 (values pid (aref ptr 0))))))
214 (export 'waitpid :sb-posix)
215 (declaim (inline waitpid))
216 (defun waitpid (pid options &optional statusptr)
217 (declare (type (sb-alien:alien pid-t) pid)
218 (type (sb-alien:alien int) options)
219 (type (or null (simple-array (signed-byte 32) (1))) statusptr))
220 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
222 (extern-alien "waitpid" (function pid-t
224 pid (sb-sys:vector-sap ptr) options)))
227 (values pid (aref ptr 0)))))
229 (define-call "wifexited" boolean never-fails (status int))
230 (define-call "wexitstatus" int never-fails (status int))
231 (define-call "wifsignaled" boolean never-fails (status int))
232 (define-call "wtermsig" int never-fails (status int))
233 (define-call "wifstopped" boolean never-fails (status int))
234 (define-call "wstopsig" int never-fails (status int))
235 #+nil ; see alien/waitpid-macros.c
236 (define-call "wifcontinued" boolean never-fails (status int)))
241 (define-call "mmap" sb-sys:system-area-pointer
243 (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
244 (addr sap-or-nil) (length unsigned) (prot unsigned)
245 (flags unsigned) (fd file-descriptor) (offset off-t))
247 (define-call "munmap" int minusp
248 (start sb-sys:system-area-pointer) (length unsigned))
250 (define-call "msync" int minusp
251 (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
254 (define-call "getpagesize" int minusp)
256 ;;; KLUDGE: This could be taken from GetSystemInfo
257 (export (defun getpagesize () 4096))
261 (define-protocol-class passwd alien-passwd ()
262 ((name :initarg :name :accessor passwd-name)
263 (passwd :initarg :passwd :accessor passwd-passwd)
264 (uid :initarg :uid :accessor passwd-uid)
265 (gid :initarg :gid :accessor passwd-gid)
266 (gecos :initarg :gecos :accessor passwd-gecos)
267 (dir :initarg :dir :accessor passwd-dir)
268 (shell :initarg :shell :accessor passwd-shell)))
270 (defmacro define-pw-call (name arg type)
272 ;; FIXME: this isn't the documented way of doing this, surely?
273 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
275 (export ',lisp-name :sb-posix)
276 (declaim (inline ,lisp-name))
277 (defun ,lisp-name (,arg)
278 (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
281 (alien-to-passwd r)))))))
283 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
284 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
286 (define-protocol-class stat alien-stat ()
287 ((mode :initarg :mode :accessor stat-mode)
288 (ino :initarg :ino :accessor stat-ino)
289 (dev :initarg :dev :accessor stat-dev)
290 (nlink :initarg :nlink :accessor stat-nlink)
291 (uid :initarg :uid :accessor stat-uid)
292 (gid :initarg :gid :accessor stat-gid)
293 (size :initarg :size :accessor stat-size)
294 (atime :initarg :atime :accessor stat-atime)
295 (mtime :initarg :mtime :accessor stat-mtime)
296 (ctime :initarg :ctime :accessor stat-ctime)))
298 (defmacro define-stat-call (name arg designator-fun type)
299 ;; FIXME: this isn't the documented way of doing this, surely?
300 (let ((lisp-name (lisp-for-c-symbol name)))
302 (export ',lisp-name :sb-posix)
303 (declaim (inline ,lisp-name))
304 (defun ,lisp-name (,arg &optional stat)
305 (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
306 (with-alien-stat a-stat ()
307 (let ((r (alien-funcall
308 (extern-alien ,name ,type)
309 (,designator-fun ,arg)
313 (alien-to-stat a-stat stat)))))))
315 ;; Note: _stat, _lstat, and _fstat for NetBSD are provided in
316 ;; src/runtime/bsd-os.c. See comments in that file
317 ;; for an explanation. -- RMK 2006-10-15
318 (define-stat-call #-(or win32 netbsd) "stat" #+(or win32 netbsd) "_stat"
320 (function int c-string (* alien-stat)))
323 (define-stat-call #-netbsd "lstat" #+netbsd "_lstat" pathname filename
324 (function int c-string (* alien-stat)))
325 ;;; No symbolic links on Windows, so use stat
328 (declaim (inline lstat))
329 (export (defun lstat (filename &optional stat)
330 (if stat (stat filename stat) (stat filename)))))
332 (define-stat-call #-(or win32 netbsd) "fstat" #+(or win32 netbsd) "_fstat"
334 (function int int (* alien-stat)))
338 (define-call "s_isreg" boolean never-fails (mode mode-t))
339 (define-call "s_isdir" boolean never-fails (mode mode-t))
340 (define-call "s_ischr" boolean never-fails (mode mode-t))
341 (define-call "s_isblk" boolean never-fails (mode mode-t))
342 (define-call "s_isfifo" boolean never-fails (mode mode-t))
343 (define-call "s_islnk" boolean never-fails (mode mode-t))
344 (define-call "s_issock" boolean never-fails (mode mode-t))
348 (export 'pipe :sb-posix)
349 (declaim (inline pipe))
350 (defun pipe (&optional filedes2)
351 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
353 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
354 (let ((r (alien-funcall
355 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
356 (extern-alien "pipe" (function int (* int)))
357 (sb-sys:vector-sap filedes2))))
360 (values (aref filedes2 0) (aref filedes2 1))))
363 (define-protocol-class termios alien-termios ()
364 ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
365 (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
366 (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
367 (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
368 (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
372 (export 'tcsetattr :sb-posix)
373 (declaim (inline tcsetattr))
374 (defun tcsetattr (fd actions termios)
375 (declare (type termios termios))
376 (with-alien-termios a-termios ()
377 (termios-to-alien termios a-termios)
378 (let ((fd (file-descriptor fd)))
379 (let* ((r (alien-funcall
382 (function int int int (* alien-termios)))
383 fd actions a-termios)))
387 (export 'tcgetattr :sb-posix)
388 (declaim (inline tcgetattr))
389 (defun tcgetattr (fd &optional termios)
390 (declare (type (or null termios) termios))
391 (with-alien-termios a-termios ()
392 (let ((r (alien-funcall
393 (extern-alien "tcgetattr"
394 (function int int (* alien-termios)))
399 (setf termios (alien-to-termios a-termios termios))))
401 (export 'cfsetispeed :sb-posix)
402 (declaim (inline cfsetispeed))
403 (defun cfsetispeed (speed &optional termios)
404 (declare (type (or null termios) termios))
405 (with-alien-termios a-termios ()
406 (let ((r (alien-funcall
407 (extern-alien "cfsetispeed"
408 (function int (* alien-termios) speed-t))
413 (setf termios (alien-to-termios a-termios termios))))
415 (export 'cfsetospeed :sb-posix)
416 (declaim (inline cfsetospeed))
417 (defun cfsetospeed (speed &optional termios)
418 (declare (type (or null termios) termios))
419 (with-alien-termios a-termios ()
420 (let ((r (alien-funcall
421 (extern-alien "cfsetospeed"
422 (function int (* alien-termios) speed-t))
427 (setf termios (alien-to-termios a-termios termios))))
429 (export 'cfgetispeed :sb-posix)
430 (declaim (inline cfgetispeed))
431 (defun cfgetispeed (termios)
432 (declare (type termios termios))
433 (with-alien-termios a-termios ()
434 (termios-to-alien termios a-termios)
435 (alien-funcall (extern-alien "cfgetispeed"
436 (function speed-t (* alien-termios)))
438 (export 'cfgetospeed :sb-posix)
439 (declaim (inline cfgetospeed))
440 (defun cfgetospeed (termios)
441 (declare (type termios termios))
442 (with-alien-termios a-termios ()
443 (termios-to-alien termios a-termios)
444 (alien-funcall (extern-alien "cfgetospeed"
445 (function speed-t (* alien-termios)))
450 (export 'getenv :sb-posix)
452 (let ((r (alien-funcall
453 (extern-alien "getenv" (function (* char) c-string))
455 (declare (type (alien (* char)) r))
456 (unless (null-alien r)
458 (define-call "putenv" int minusp (string c-string))