0.9.8.35:
[sbcl.git] / contrib / sb-posix / interface.lisp
1 (cl:in-package :sb-posix)
2
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))))
7     `(progn
8       (defclass ,name ,superclasses
9         ,(loop for slotd in slots
10                collect (ldiff slotd (member :array-length slotd)))
11         ,@options)
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))
16         (unless instance
17           (setf instance (make-instance ',name)))
18         ,@(loop for slotd in slots
19                 ;; FIXME: slotds in source are more complicated in general
20                 ;;
21                 ;; FIXME: baroque construction of intricate fragility
22                 for array-length = (getf (cdr slotd) :array-length)
23                 if array-length
24                   collect `(progn
25                              (let ((array (make-array ,array-length)))
26                                (setf (slot-value instance ',(car slotd))
27                                      array)
28                                (dotimes (i ,array-length)
29                                  (setf (aref array i)
30                                        (sb-alien:deref
31                                         (sb-alien:slot alien ',(car slotd))
32                                         i)))))
33                 else
34                   collect `(setf (slot-value instance ',(car slotd))
35                                  (sb-alien:slot alien ',(car slotd))))
36         instance)
37       (defun ,to-alien (instance &optional alien)
38         (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
39                  (type ,name instance))
40         (unless alien
41           (setf alien (sb-alien:make-alien ,alien-type)))
42         ,@(loop for slotd in slots
43                 for array-length = (getf (cdr slotd) :array-length)
44                 if array-length
45                   collect `(progn
46                              (let ((array (slot-value instance ',(car slotd))))
47                                (dotimes (i ,array-length)
48                                  (setf (sb-alien:deref
49                                         (sb-alien:slot alien ',(car slotd))
50                                         i)
51                                        (aref array i)))))
52                 else
53                   collect `(setf (sb-alien:slot alien ',(car slotd))
54                                  (slot-value instance ',(car slotd)))))
55       (find-class ',name))))
56
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))))))
63
64 (defun syscall-error ()
65   (error 'sb-posix:syscall-error :errno (get-errno)))
66
67 (declaim (inline never-fails))
68 (defun never-fails (&rest args)
69   (declare (ignore args))
70   nil)
71
72 ;;; filesystem access
73
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 mode-t))
77 (define-call "chown" int minusp (pathname filename)
78              (owner uid-t)  (group 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 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 mode-t))
86 (define-call "fchown" int minusp (fd file-descriptor)
87              (owner uid-t)  (group gid-t))
88 (define-call "fdatasync" int minusp (fd file-descriptor))
89 (define-call "ftruncate" int minusp (fd file-descriptor) (length off-t))
90 (define-call "fsync" int minusp (fd file-descriptor))
91 (define-call "lchown" int minusp (pathname filename)
92              (owner uid-t)  (group gid-t))
93 (define-call "link" int minusp (oldpath filename) (newpath filename))
94 (define-call "lseek" off-t minusp (fd file-descriptor) (offset off-t) (whence int))
95 (define-call "mkdir" int minusp (pathname filename) (mode mode-t))
96 (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
97 (define-call-internally open-with-mode "open" int minusp (pathname filename) (flags int) (mode 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))
100   (if 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 off-t))
109 (define-call "unlink" int minusp (pathname filename))
110 (define-call "mkstemp" int minusp (template c-string))
111
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))
116   (if argp
117       (etypecase arg
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)))
121
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))
126   (if argp
127       (etypecase arg
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)))
131
132 (define-call "opendir" (* t) null-alien (pathname filename))
133 (define-call "readdir" (* 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).
137   not
138   (dir (* t)))
139 (define-call "closedir" int minusp (dir (* t)))
140 ;; need to do this here because we can't do it in the DEFPACKAGE
141
142 (define-call "umask" mode-t never-fails (mode mode-t))
143
144 ;;; uid, gid
145
146 (define-call "geteuid" uid-t never-fails) ; "always successful", it says
147 (define-call "getresuid" uid-t never-fails)
148 (define-call "getuid" uid-t never-fails)
149 (define-call "seteuid" int minusp (uid uid-t))
150 (define-call "setfsuid" int minusp (uid uid-t))
151 (define-call "setreuid" int minusp
152              (ruid uid-t) (euid uid-t))
153 (define-call "setresuid" int minusp
154              (ruid uid-t) (euid uid-t)
155              (suid uid-t))
156 (define-call "setuid" int minusp (uid uid-t))
157
158 (define-call "getegid" gid-t never-fails)
159 (define-call "getgid" gid-t never-fails)
160 (define-call "getresgid" gid-t never-fails)
161 (define-call "setegid" int minusp (gid gid-t))
162 (define-call "setfsgid" int minusp (gid gid-t))
163 (define-call "setgid" int minusp (gid gid-t))
164 (define-call "setregid" int minusp
165              (rgid gid-t) (egid gid-t))
166 (define-call "setresgid" int minusp
167              (rgid gid-t)
168              (egid gid-t) (sgid gid-t))
169
170 ;;; processes, signals
171 (define-call "alarm" int never-fails (seconds unsigned))
172 (define-call "fork" pid-t minusp)
173 (define-call "getpgid" pid-t minusp (pid pid-t))
174 (define-call "getpid" pid-t never-fails)
175 (define-call "getppid" pid-t never-fails)
176 (define-call "getpgrp" pid-t never-fails)
177 (define-call "getsid" pid-t minusp  (pid pid-t))
178 (define-call "kill" int minusp (pid pid-t) (signal int))
179 (define-call "killpg" int minusp (pgrp int) (signal int))
180 (define-call "pause" int minusp)
181 (define-call "setpgid" int minusp
182              (pid pid-t) (pgid pid-t))
183 (define-call "setpgrp" int minusp)
184
185 (export 'wait :sb-posix)
186 (declaim (inline wait))
187 (defun wait (&optional statusptr)
188   (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
189   (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
190          (pid (alien-funcall
191                (extern-alien "wait" (function pid-t (* int)))
192                (sb-sys:vector-sap ptr))))
193     (if (minusp pid)
194         (syscall-error)
195         (values pid (aref ptr 0)))))
196
197 (export 'waitpid :sb-posix)
198 (declaim (inline waitpid))
199 (defun waitpid (pid options &optional statusptr)
200   (declare (type (sb-alien:alien pid-t) pid)
201            (type (sb-alien:alien int) options)
202            (type (or null (simple-array (signed-byte 32) (1))) statusptr))
203   (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
204          (pid (alien-funcall
205                (extern-alien "waitpid" (function pid-t
206                                                  pid-t (* int) int))
207                              pid (sb-sys:vector-sap ptr) options)))
208          (if (minusp pid)
209              (syscall-error)
210              (values pid (aref ptr 0)))))
211
212 ;; waitpid macros
213 (define-call "wifexited" boolean never-fails (status int))
214 (define-call "wexitstatus" int never-fails (status int))
215 (define-call "wifsignaled" boolean never-fails (status int))
216 (define-call "wtermsig" int never-fails (status int))
217 (define-call "wifstopped" boolean never-fails (status int))
218 (define-call "wstopsig" int never-fails (status int))
219 #+nil ; see alien/waitpid-macros.c
220 (define-call "wifcontinued" boolean never-fails (status int))
221
222 ;;; mmap, msync
223 (define-call "mmap" sb-sys:system-area-pointer
224   (lambda (res)
225     (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
226   (addr sap-or-nil) (length unsigned) (prot unsigned)
227   (flags unsigned) (fd file-descriptor) (offset off-t))
228
229 (define-call "munmap" int minusp
230   (start sb-sys:system-area-pointer) (length unsigned))
231
232 (define-call "msync" int minusp
233   (addr sb-sys:system-area-pointer) (length unsigned) (flags int))
234
235 (define-call "getpagesize" int minusp)
236
237 ;;; passwd database
238 (define-protocol-class passwd alien-passwd ()
239   ((name :initarg :name :accessor passwd-name)
240    (passwd :initarg :passwd :accessor passwd-passwd)
241    (uid :initarg :uid :accessor passwd-uid)
242    (gid :initarg :gid :accessor passwd-gid)
243    (gecos :initarg :gecos :accessor passwd-gecos)
244    (dir :initarg :dir :accessor passwd-dir)
245    (shell :initarg :shell :accessor passwd-shell)))
246
247 (defmacro define-pw-call (name arg type)
248   ;; FIXME: this isn't the documented way of doing this, surely?
249   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
250     `(progn
251       (export ',lisp-name :sb-posix)
252       (declaim (inline ,lisp-name))
253       (defun ,lisp-name (,arg)
254         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
255           (if (null r)
256               r
257               (alien-to-passwd r)))))))
258
259 (define-pw-call "getpwnam" login-name
260                 (function (* alien-passwd) c-string))
261 (define-pw-call "getpwuid" uid
262                 (function (* alien-passwd) uid-t))
263
264 (define-protocol-class stat alien-stat ()
265   ((mode :initarg :mode :accessor stat-mode)
266    (ino :initarg :ino :accessor stat-ino)
267    (dev :initarg :dev :accessor stat-dev)
268    (nlink :initarg :nlink :accessor stat-nlink)
269    (uid :initarg :uid :accessor stat-uid)
270    (gid :initarg :gid :accessor stat-gid)
271    (size :initarg :size :accessor stat-size)
272    (atime :initarg :atime :accessor stat-atime)
273    (mtime :initarg :mtime :accessor stat-mtime)
274    (ctime :initarg :ctime :accessor stat-ctime)))
275
276 (defmacro define-stat-call (name arg designator-fun type)
277   ;; FIXME: this isn't the documented way of doing this, surely?
278   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
279     `(progn
280       (export ',lisp-name :sb-posix)
281       (declaim (inline ,lisp-name))
282       (defun ,lisp-name (,arg &optional stat)
283         (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
284         (with-alien-stat a-stat ()
285           (let ((r (alien-funcall
286                     (extern-alien ,name ,type)
287                     (,designator-fun ,arg)
288                     a-stat)))
289             (when (minusp r)
290               (syscall-error))
291             (alien-to-stat a-stat stat)))))))
292
293 (define-stat-call "stat" pathname filename
294                   (function int c-string (* alien-stat)))
295 (define-stat-call "lstat" pathname filename
296                   (function int c-string (* alien-stat)))
297 (define-stat-call "fstat" fd file-descriptor
298                   (function int int (* alien-stat)))
299
300
301 ;;; mode flags
302 (define-call "s_isreg" boolean never-fails (mode mode-t))
303 (define-call "s_isdir" boolean never-fails (mode mode-t))
304 (define-call "s_ischr" boolean never-fails (mode mode-t))
305 (define-call "s_isblk" boolean never-fails (mode mode-t))
306 (define-call "s_isfifo" boolean never-fails (mode mode-t))
307 (define-call "s_islnk" boolean never-fails (mode mode-t))
308 (define-call "s_issock" boolean never-fails (mode mode-t))
309
310 (export 'pipe :sb-posix)
311 (declaim (inline pipe))
312 (defun pipe (&optional filedes2)
313   (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
314   (unless filedes2
315     (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
316   (let ((r (alien-funcall
317             ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
318             (extern-alien "pipe" (function int (* int)))
319             (sb-sys:vector-sap filedes2))))
320     (when (minusp r)
321       (syscall-error)))
322   (values (aref filedes2 0) (aref filedes2 1)))
323
324 (define-protocol-class termios alien-termios ()
325   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
326    (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
327    (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
328    (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
329    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
330
331 (export 'tcsetattr :sb-posix)
332 (declaim (inline tcsetattr))
333 (defun tcsetattr (fd actions termios)
334   (with-alien-termios a-termios ()
335     (termios-to-alien termios a-termios)
336     (let ((fd (file-descriptor fd)))
337       (let* ((r (alien-funcall
338                  (extern-alien
339                   "tcsetattr"
340                   (function int int int (* alien-termios)))
341                  fd actions a-termios)))
342         (when (minusp r)
343           (syscall-error)))
344       (values))))
345 (export 'tcgetattr :sb-posix)
346 (declaim (inline tcgetattr))
347 (defun tcgetattr (fd &optional termios)
348   (with-alien-termios a-termios ()
349     (let ((r (alien-funcall
350               (extern-alien "tcgetattr"
351                             (function int int (* alien-termios)))
352               (file-descriptor fd)
353               a-termios)))
354       (when (minusp r)
355         (syscall-error))
356       (setf termios (alien-to-termios a-termios termios))))
357   termios)
358
359 ;;; environment
360
361 (export 'getenv :sb-posix)
362 (defun getenv (name)
363   (let ((r (alien-funcall
364             (extern-alien "getenv" (function (* char) c-string))
365             name)))
366     (declare (type (alien (* char)) r))
367     (unless (null-alien r)
368       (cast r c-string))))
369 (define-call "putenv" int minusp (string c-string))