1 (cl:in-package :sb-posix-internal)
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 (defun ,to-protocol (alien &optional instance)
14 (declare (type (sb-alien:alien (* ,alien-type)) alien)
15 (type (or null ,name) instance))
17 (setf instance (make-instance ',name)))
18 ,@(loop for slotd in slots
19 ;; FIXME: slotds in source are more complicated in general
21 ;; FIXME: baroque construction of intricate fragility
22 for array-length = (getf (cdr slotd) :array-length)
25 (let ((array (make-array ,array-length)))
26 (setf (slot-value instance ',(car slotd))
28 (dotimes (i ,array-length)
31 (sb-alien:slot alien ',(car slotd))
34 collect `(setf (slot-value instance ',(car slotd))
35 (sb-alien:slot alien ',(car slotd))))
37 (defun ,to-alien (instance &optional alien)
38 (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
39 (type ,name instance))
41 (setf alien (sb-alien:make-alien ,alien-type)))
42 ,@(loop for slotd in slots
43 for array-length = (getf (cdr slotd) :array-length)
46 (let ((array (slot-value instance ',(car slotd))))
47 (dotimes (i ,array-length)
49 (sb-alien:slot alien ',(car slotd))
53 collect `(setf (sb-alien:slot alien ',(car slotd))
54 (slot-value instance ',(car slotd)))))
55 (find-class ',name))))
57 (define-condition sb-posix:syscall-error (error)
58 ((errno :initarg :errno :reader sb-posix:syscall-errno))
59 (:report (lambda (c s)
60 (let ((errno (sb-posix:syscall-errno c)))
61 (format s "System call error ~A (~A)"
62 errno (sb-int:strerror errno))))))
64 (defun syscall-error ()
65 (error 'sb-posix:syscall-error :errno (get-errno)))
67 (declaim (inline never-fails))
68 (defun never-fails (&rest args)
69 (declare (ignore args))
74 (define-call "access" int minusp (pathname filename) (mode int))
75 (define-call "chdir" int minusp (pathname filename))
76 (define-call "chmod" int minusp (pathname filename) (mode sb-posix::mode-t))
77 (define-call "chown" int minusp (pathname filename)
78 (owner sb-posix::uid-t) (group sb-posix::gid-t))
79 (define-call "chroot" int minusp (pathname filename))
80 (define-call "close" int minusp (fd file-descriptor))
81 (define-call "creat" int minusp (pathname filename) (mode sb-posix::mode-t))
82 (define-call "dup" int minusp (oldfd file-descriptor))
83 (define-call "dup2" int minusp (oldfd file-descriptor) (newfd file-descriptor))
84 (define-call "fchdir" int minusp (fd file-descriptor))
85 (define-call "fchmod" int minusp (fd file-descriptor) (mode sb-posix::mode-t))
86 (define-call "fchown" int minusp (fd file-descriptor)
87 (owner sb-posix::uid-t) (group sb-posix::gid-t))
88 (define-call "fdatasync" int minusp (fd file-descriptor))
89 (define-call "ftruncate" int minusp (fd file-descriptor) (length sb-posix::off-t))
90 (define-call "fsync" int minusp (fd file-descriptor))
91 (define-call "lchown" int minusp (pathname filename)
92 (owner sb-posix::uid-t) (group sb-posix::gid-t))
93 (define-call "link" int minusp (oldpath filename) (newpath filename))
94 (define-call "lseek" sb-posix::off-t minusp (fd file-descriptor) (offset sb-posix::off-t) (whence int))
95 (define-call "mkdir" int minusp (pathname filename) (mode sb-posix::mode-t))
96 (define-call "mkfifo" int minusp (pathname filename) (mode sb-posix::mode-t))
97 (define-call-internally open-with-mode "open" int minusp (pathname filename) (flags int) (mode sb-posix::mode-t))
98 (define-call-internally open-without-mode "open" int minusp (pathname filename) (flags int))
99 (define-entry-point "open" (pathname flags &optional (mode nil mode-supplied))
101 (open-with-mode pathname flags mode)
102 (open-without-mode pathname flags)))
103 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
104 (define-call "rename" int minusp (oldpath filename) (newpath filename))
105 (define-call "rmdir" int minusp (pathname filename))
106 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
107 (define-call "sync" void never-fails)
108 (define-call "truncate" int minusp (pathname filename) (length sb-posix::off-t))
109 (define-call "unlink" int minusp (pathname filename))
110 (define-call "mkstemp" int minusp (template c-string))
112 (define-call-internally ioctl-without-arg "ioctl" int minusp (fd file-descriptor) (cmd int))
113 (define-call-internally ioctl-with-int-arg "ioctl" int minusp (fd file-descriptor) (cmd int) (arg int))
114 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp (fd file-descriptor) (cmd int) (arg alien-pointer-to-anything-or-nil))
115 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
118 ((alien int) (ioctl-with-int-arg fd cmd arg))
119 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
120 (ioctl-without-arg fd cmd)))
122 (define-call-internally fcntl-without-arg "fcntl" int minusp (fd file-descriptor) (cmd int))
123 (define-call-internally fcntl-with-int-arg "fcntl" int minusp (fd file-descriptor) (cmd int) (arg int))
124 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp (fd file-descriptor) (cmd int) (arg alien-pointer-to-anything-or-nil))
125 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
128 ((alien int) (fcntl-with-int-arg fd cmd arg))
129 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
130 (fcntl-without-arg fd cmd)))
132 (define-call "opendir" (* t) null-alien (pathname filename))
133 (define-call "readdir" (* sb-posix::dirent)
134 ;; readdir() has the worst error convention in the world. It's just
135 ;; too painful to support. (return is NULL _and_ errno "unchanged"
136 ;; is not an error, it's EOF).
139 (define-call "closedir" int minusp (dir (* t)))
141 (define-call "umask" sb-posix::mode-t never-fails (mode sb-posix::mode-t))
145 (define-call "geteuid" sb-posix::uid-t never-fails) ; "always successful", it says
146 (define-call "getresuid" sb-posix::uid-t never-fails)
147 (define-call "getuid" sb-posix::uid-t never-fails)
148 (define-call "seteuid" int minusp (uid sb-posix::uid-t))
149 (define-call "setfsuid" int minusp (uid sb-posix::uid-t))
150 (define-call "setreuid" int minusp
151 (ruid sb-posix::uid-t) (euid sb-posix::uid-t))
152 (define-call "setresuid" int minusp
153 (ruid sb-posix::uid-t) (euid sb-posix::uid-t)
154 (suid sb-posix::uid-t))
155 (define-call "setuid" int minusp (uid sb-posix::uid-t))
157 (define-call "getegid" sb-posix::gid-t never-fails)
158 (define-call "getgid" sb-posix::gid-t never-fails)
159 (define-call "getresgid" sb-posix::gid-t never-fails)
160 (define-call "setegid" int minusp (gid sb-posix::gid-t))
161 (define-call "setfsgid" int minusp (gid sb-posix::gid-t))
162 (define-call "setgid" int minusp (gid sb-posix::gid-t))
163 (define-call "setregid" int minusp
164 (rgid sb-posix::gid-t) (egid sb-posix::gid-t))
165 (define-call "setresgid" int minusp
166 (rgid sb-posix::gid-t)
167 (egid sb-posix::gid-t) (sgid sb-posix::gid-t))
169 ;;; processes, signals
170 (define-call "alarm" int never-fails (seconds unsigned))
171 (define-call "fork" sb-posix::pid-t minusp)
172 (define-call "getpgid" sb-posix::pid-t minusp (pid sb-posix::pid-t))
173 (define-call "getpid" sb-posix::pid-t never-fails)
174 (define-call "getppid" sb-posix::pid-t never-fails)
175 (define-call "getpgrp" sb-posix::pid-t never-fails)
176 (define-call "getsid" sb-posix::pid-t minusp (pid sb-posix::pid-t))
177 (define-call "kill" int minusp (pid sb-posix::pid-t) (signal int))
178 (define-call "killpg" int minusp (pgrp int) (signal int))
179 (define-call "pause" int minusp)
180 (define-call "setpgid" int minusp
181 (pid sb-posix::pid-t) (pgid sb-posix::pid-t))
182 (define-call "setpgrp" int minusp)
184 (export 'sb-posix::wait :sb-posix)
185 (declaim (inline sb-posix::wait))
186 (defun sb-posix::wait (&optional statusptr)
187 (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
188 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
190 (extern-alien "wait" (function sb-posix::pid-t (* int)))
191 (sb-sys:vector-sap ptr))))
194 (values pid (aref ptr 0)))))
196 (export 'sb-posix::waitpid :sb-posix)
197 (declaim (inline sb-posix::waitpid))
198 (defun sb-posix::waitpid (pid options &optional statusptr)
199 (declare (type (sb-alien:alien sb-posix::pid-t) pid)
200 (type (sb-alien:alien int) options)
201 (type (or null (simple-array (signed-byte 32) (1))) statusptr))
202 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
204 (extern-alien "waitpid" (function sb-posix::pid-t
205 sb-posix::pid-t (* int) int))
206 pid (sb-sys:vector-sap ptr) options)))
209 (values pid (aref ptr 0)))))
212 (define-call "wifexited" boolean never-fails (status int))
213 (define-call "wexitstatus" int never-fails (status int))
214 (define-call "wifsignaled" boolean never-fails (status int))
215 (define-call "wtermsig" int never-fails (status int))
216 (define-call "wifstopped" boolean never-fails (status int))
217 (define-call "wstopsig" int never-fails (status int))
218 #+nil ; see alien/waitpid-macros.c
219 (define-call "wifcontinued" boolean never-fails (status int))
222 (define-call "mmap" sb-sys:system-area-pointer
224 (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
225 (addr sap-or-nil) (length unsigned) (prot unsigned)
226 (flags unsigned) (fd file-descriptor) (offset sb-posix::off-t))
228 (define-call "munmap" int minusp
229 (start sb-sys:system-area-pointer) (length unsigned))
231 (define-call "msync" int minusp
232 (addr sb-sys:system-area-pointer) (length unsigned) (flags int))
234 (define-call "getpagesize" int minusp)
236 (define-protocol-class sb-posix::stat sb-posix::alien-stat ()
237 ((sb-posix::mode :initarg :mode :accessor sb-posix:stat-mode)
238 (sb-posix::ino :initarg :ino :accessor sb-posix:stat-ino)
239 (sb-posix::dev :initarg :dev :accessor sb-posix:stat-dev)
240 (sb-posix::nlink :initarg :nlink :accessor sb-posix:stat-nlink)
241 (sb-posix::uid :initarg :uid :accessor sb-posix:stat-uid)
242 (sb-posix::gid :initarg :gid :accessor sb-posix:stat-gid)
243 (sb-posix::size :initarg :size :accessor sb-posix:stat-size)
244 (sb-posix::atime :initarg :atime :accessor sb-posix:stat-atime)
245 (sb-posix::mtime :initarg :mtime :accessor sb-posix:stat-mtime)
246 (sb-posix::ctime :initarg :ctime :accessor sb-posix:stat-ctime)))
248 (defmacro define-stat-call (name arg designator-fun type)
249 ;; FIXME: this isn't the documented way of doing this, surely?
250 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
252 (export ',lisp-name :sb-posix)
253 (declaim (inline ,lisp-name))
254 (defun ,lisp-name (,arg &optional stat)
255 (declare (type (or null (sb-alien:alien (* sb-posix::alien-stat))) stat))
256 (sb-posix::with-alien-stat a-stat ()
257 (let ((r (alien-funcall
258 (extern-alien ,name ,type)
259 (,designator-fun ,arg)
263 (alien-to-stat a-stat stat)))))))
265 (define-stat-call "stat" pathname sb-posix::filename
266 (function int c-string (* sb-posix::alien-stat)))
267 (define-stat-call "lstat" pathname sb-posix::filename
268 (function int c-string (* sb-posix::alien-stat)))
269 (define-stat-call "fstat" fd sb-posix::file-descriptor
270 (function int int (* sb-posix::alien-stat)))
274 (define-call "s_isreg" boolean never-fails (mode sb-posix::mode-t))
275 (define-call "s_isdir" boolean never-fails (mode sb-posix::mode-t))
276 (define-call "s_ischr" boolean never-fails (mode sb-posix::mode-t))
277 (define-call "s_isblk" boolean never-fails (mode sb-posix::mode-t))
278 (define-call "s_isfifo" boolean never-fails (mode sb-posix::mode-t))
279 (define-call "s_islnk" boolean never-fails (mode sb-posix::mode-t))
280 (define-call "s_issock" boolean never-fails (mode sb-posix::mode-t))
282 (export 'sb-posix::pipe :sb-posix)
283 (declaim (inline sb-posix::pipe))
284 (defun sb-posix::pipe (&optional filedes2)
285 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
287 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
288 (let ((r (alien-funcall
289 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
290 (extern-alien "pipe" (function int (* int)))
291 (sb-sys:vector-sap filedes2))))
294 (values (aref filedes2 0) (aref filedes2 1)))
296 (define-protocol-class sb-posix::termios sb-posix::alien-termios ()
297 ((sb-posix::iflag :initarg :iflag :accessor sb-posix:termios-iflag)
298 (sb-posix::oflag :initarg :oflag :accessor sb-posix:termios-oflag)
299 (sb-posix::cflag :initarg :cflag :accessor sb-posix:termios-cflag)
300 (sb-posix::lflag :initarg :lflag :accessor sb-posix:termios-lflag)
301 (sb-posix::cc :initarg :cc :accessor sb-posix:termios-cc :array-length sb-posix::nccs)))
303 (export 'sb-posix::tcsetattr :sb-posix)
304 (declaim (inline sb-posix::tcsetattr))
305 (defun sb-posix::tcsetattr (fd actions termios)
306 (sb-posix::with-alien-termios a-termios ()
307 (termios-to-alien termios a-termios)
308 (let ((fd (sb-posix::file-descriptor fd)))
309 (let* ((r (alien-funcall
312 (function int int int (* sb-posix::alien-termios)))
313 fd actions a-termios)))
317 (export 'sb-posix::tcgetattr :sb-posix)
318 (declaim (inline sb-posix::tcgetattr))
319 (defun sb-posix::tcgetattr (fd &optional termios)
320 (sb-posix::with-alien-termios a-termios ()
321 (let ((r (alien-funcall
322 (extern-alien "tcgetattr"
323 (function int int (* sb-posix::alien-termios)))
324 (sb-posix::file-descriptor fd)
328 (setf termios (alien-to-termios a-termios termios))))
333 (export 'sb-posix::getenv :sb-posix)
334 (defun sb-posix::getenv (name)
335 (let ((r (alien-funcall
336 (extern-alien "getenv" (function (* char) c-string))
338 (declare (type (alien (* char)) r))
339 (unless (null-alien r)
341 (define-call "putenv" int minusp (string c-string))