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 "close" int minusp (fd file-descriptor))
80 (define-call "creat" int minusp (pathname filename) (mode sb-posix::mode-t))
81 (define-call "dup" int minusp (oldfd file-descriptor))
82 (define-call "dup2" int minusp (oldfd file-descriptor) (newfd file-descriptor))
83 (define-call "fchdir" int minusp (fd file-descriptor))
84 (define-call "fchmod" int minusp (fd file-descriptor) (mode sb-posix::mode-t))
85 (define-call "fchown" int minusp (fd file-descriptor)
86 (owner sb-posix::uid-t) (group sb-posix::gid-t))
87 (define-call "fdatasync" int minusp (fd file-descriptor))
88 (define-call "ftruncate" int minusp (fd file-descriptor) (length sb-posix::off-t))
89 (define-call "fsync" int minusp (fd file-descriptor))
90 (define-call "lchown" int minusp (pathname filename)
91 (owner sb-posix::uid-t) (group sb-posix::gid-t))
92 (define-call "link" int minusp (oldpath filename) (newpath filename))
93 (define-call "lseek" sb-posix::off-t minusp (fd file-descriptor) (offset sb-posix::off-t) (whence int))
94 (define-call "mkdir" int minusp (pathname filename) (mode sb-posix::mode-t))
95 (define-call "mkfifo" int minusp (pathname filename) (mode sb-posix::mode-t))
96 (define-call-internally open-with-mode "open" int minusp (pathname filename) (flags int) (mode sb-posix::mode-t))
97 (define-call-internally open-without-mode "open" int minusp (pathname filename) (flags int))
98 (define-entry-point "open" (pathname flags &optional (mode nil mode-supplied))
100 (open-with-mode pathname flags mode)
101 (open-without-mode pathname flags)))
102 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
103 (define-call "rename" int minusp (oldpath filename) (newpath filename))
104 (define-call "rmdir" int minusp (pathname filename))
105 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
106 (define-call "sync" void never-fails)
107 (define-call "truncate" int minusp (pathname filename) (length sb-posix::off-t))
108 (define-call "unlink" int minusp (pathname filename))
110 (define-call-internally ioctl-without-arg "ioctl" int minusp (fd file-descriptor) (cmd int))
111 (define-call-internally ioctl-with-int-arg "ioctl" int minusp (fd file-descriptor) (cmd int) (arg int))
112 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp (fd file-descriptor) (cmd int) (arg alien-pointer-to-anything-or-nil))
113 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
116 ((alien int) (ioctl-with-int-arg fd cmd arg))
117 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
118 (ioctl-without-arg fd cmd)))
120 (define-call-internally fcntl-without-arg "fcntl" int minusp (fd file-descriptor) (cmd int))
121 (define-call-internally fcntl-with-int-arg "fcntl" int minusp (fd file-descriptor) (cmd int) (arg int))
122 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp (fd file-descriptor) (cmd int) (arg alien-pointer-to-anything-or-nil))
123 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
126 ((alien int) (fcntl-with-int-arg fd cmd arg))
127 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
128 (fcntl-without-arg fd cmd)))
130 (define-call "opendir" (* t) null-alien (pathname filename))
131 (define-call "readdir" (* sb-posix::dirent)
132 ;; readdir() has the worst error convention in the world. It's just
133 ;; too painful to support. (return is NULL _and_ errno "unchanged"
134 ;; is not an error, it's EOF).
137 (define-call "closedir" int minusp (dir (* t)))
139 (define-call "umask" sb-posix::mode-t never-fails (mode sb-posix::mode-t))
143 (define-call "geteuid" sb-posix::uid-t never-fails) ; "always successful", it says
144 (define-call "getresuid" sb-posix::uid-t never-fails)
145 (define-call "getuid" sb-posix::uid-t never-fails)
146 (define-call "seteuid" int minusp (uid sb-posix::uid-t))
147 (define-call "setfsuid" int minusp (uid sb-posix::uid-t))
148 (define-call "setreuid" int minusp
149 (ruid sb-posix::uid-t) (euid sb-posix::uid-t))
150 (define-call "setresuid" int minusp
151 (ruid sb-posix::uid-t) (euid sb-posix::uid-t)
152 (suid sb-posix::uid-t))
153 (define-call "setuid" int minusp (uid sb-posix::uid-t))
155 (define-call "getegid" sb-posix::gid-t never-fails)
156 (define-call "getgid" sb-posix::gid-t never-fails)
157 (define-call "getresgid" sb-posix::gid-t never-fails)
158 (define-call "setegid" int minusp (gid sb-posix::gid-t))
159 (define-call "setfsgid" int minusp (gid sb-posix::gid-t))
160 (define-call "setgid" int minusp (gid sb-posix::gid-t))
161 (define-call "setregid" int minusp
162 (rgid sb-posix::gid-t) (egid sb-posix::gid-t))
163 (define-call "setresgid" int minusp
164 (rgid sb-posix::gid-t)
165 (egid sb-posix::gid-t) (sgid sb-posix::gid-t))
167 ;;; processes, signals
168 (define-call "alarm" int never-fails (seconds unsigned))
169 (define-call "getpgid" sb-posix::pid-t minusp (pid sb-posix::pid-t))
170 (define-call "getpid" sb-posix::pid-t never-fails)
171 (define-call "getppid" sb-posix::pid-t never-fails)
172 (define-call "getpgrp" sb-posix::pid-t never-fails)
173 (define-call "getsid" sb-posix::pid-t minusp (pid sb-posix::pid-t))
174 (define-call "kill" int minusp (pid sb-posix::pid-t) (signal int))
175 (define-call "killpg" int minusp (pgrp int) (signal int))
176 (define-call "pause" int minusp)
177 (define-call "setpgid" int minusp
178 (pid sb-posix::pid-t) (pgid sb-posix::pid-t))
179 (define-call "setpgrp" int minusp)
182 (define-call "mmap" sb-sys:system-area-pointer
183 ;; KLUDGE: #XFFFFFFFF is (void *)-1, which is the charming return
184 ;; value of mmap on failure. Except on 64 bit systems ...
186 (= (sb-sys:sap-int res) #-alpha #XFFFFFFFF #+alpha #xffffffffffffffff))
187 (addr sap-or-nil) (length unsigned) (prot unsigned)
188 (flags unsigned) (fd file-descriptor) (offset sb-posix::off-t))
190 (define-call "munmap" int minusp
191 (start sb-sys:system-area-pointer) (length unsigned))
193 (define-call "msync" int minusp
194 (addr sb-sys:system-area-pointer) (length unsigned) (flags int))
196 (define-call "getpagesize" int minusp)
198 (define-protocol-class sb-posix::stat sb-posix::alien-stat ()
199 ((sb-posix::mode :initarg :mode :accessor sb-posix:stat-mode)
200 (sb-posix::ino :initarg :ino :accessor sb-posix:stat-ino)
201 (sb-posix::dev :initarg :dev :accessor sb-posix:stat-dev)
202 (sb-posix::nlink :initarg :nlink :accessor sb-posix:stat-nlink)
203 (sb-posix::uid :initarg :uid :accessor sb-posix:stat-uid)
204 (sb-posix::gid :initarg :gid :accessor sb-posix:stat-gid)
205 (sb-posix::size :initarg :size :accessor sb-posix:stat-size)
206 (sb-posix::atime :initarg :atime :accessor sb-posix:stat-atime)
207 (sb-posix::mtime :initarg :mtime :accessor sb-posix:stat-mtime)
208 (sb-posix::ctime :initarg :ctime :accessor sb-posix:stat-ctime)))
210 (defmacro define-stat-call (name arg designator-fun type)
211 ;; FIXME: this isn't the documented way of doing this, surely?
212 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
214 (export ',lisp-name :sb-posix)
215 (declaim (inline ,lisp-name))
216 (defun ,lisp-name (,arg &optional stat)
217 (declare (type (or null (sb-alien:alien (* sb-posix::alien-stat))) stat))
218 (sb-posix::with-alien-stat a-stat ()
219 (let ((r (alien-funcall
220 (extern-alien ,name ,type)
221 (,designator-fun ,arg)
225 (alien-to-stat a-stat stat)))))))
227 (define-stat-call "stat" pathname sb-posix::filename
228 (function int c-string (* sb-posix::alien-stat)))
229 (define-stat-call "lstat" pathname sb-posix::filename
230 (function int c-string (* sb-posix::alien-stat)))
231 (define-stat-call "fstat" fd sb-posix::file-descriptor
232 (function int int (* sb-posix::alien-stat)))
236 (define-call "s_isreg" boolean never-fails (mode sb-posix::mode-t))
237 (define-call "s_isdir" boolean never-fails (mode sb-posix::mode-t))
238 (define-call "s_ischr" boolean never-fails (mode sb-posix::mode-t))
239 (define-call "s_isblk" boolean never-fails (mode sb-posix::mode-t))
240 (define-call "s_isfifo" boolean never-fails (mode sb-posix::mode-t))
241 (define-call "s_islnk" boolean never-fails (mode sb-posix::mode-t))
242 (define-call "s_issock" boolean never-fails (mode sb-posix::mode-t))
244 (export 'sb-posix::pipe :sb-posix)
245 (declaim (inline sb-posix::pipe))
246 (defun sb-posix::pipe (&optional filedes2)
247 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
249 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
250 (let ((r (alien-funcall
251 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
252 (extern-alien "pipe" (function int (* int)))
253 (sb-sys:vector-sap filedes2))))
256 (values (aref filedes2 0) (aref filedes2 1)))
258 (define-protocol-class sb-posix::termios sb-posix::alien-termios ()
259 ((sb-posix::iflag :initarg :iflag :accessor sb-posix:termios-iflag)
260 (sb-posix::oflag :initarg :oflag :accessor sb-posix:termios-oflag)
261 (sb-posix::cflag :initarg :cflag :accessor sb-posix:termios-cflag)
262 (sb-posix::lflag :initarg :lflag :accessor sb-posix:termios-lflag)
263 (sb-posix::cc :initarg :cc :accessor sb-posix:termios-cc :array-length sb-posix::nccs)))
265 (export 'sb-posix::tcsetattr :sb-posix)
266 (declaim (inline sb-posix::tcsetattr))
267 (defun sb-posix::tcsetattr (fd actions termios)
268 (sb-posix::with-alien-termios a-termios ()
269 (termios-to-alien termios a-termios)
270 (let ((fd (sb-posix::file-descriptor fd)))
271 (let* ((r (alien-funcall
274 (function int int int (* sb-posix::alien-termios)))
275 fd actions a-termios)))
279 (export 'sb-posix::tcgetattr :sb-posix)
280 (declaim (inline sb-posix::tcgetattr))
281 (defun sb-posix::tcgetattr (fd &optional termios)
282 (sb-posix::with-alien-termios a-termios ()
283 (let ((r (alien-funcall
284 (extern-alien "tcgetattr"
285 (function int int (* sb-posix::alien-termios)))
286 (sb-posix::file-descriptor fd)
290 (setf termios (alien-to-termios a-termios termios))))