0.9.18.23:
[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       (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))
17         (unless instance
18           (setf instance (make-instance ',name)))
19         ,@(loop for slotd in slots
20                 ;; FIXME: slotds in source are more complicated in general
21                 ;;
22                 ;; FIXME: baroque construction of intricate fragility
23                 for array-length = (getf (cdr slotd) :array-length)
24                 if array-length
25                   collect `(progn
26                              (let ((array (make-array ,array-length)))
27                                (setf (slot-value instance ',(car slotd))
28                                      array)
29                                (dotimes (i ,array-length)
30                                  (setf (aref array i)
31                                        (sb-alien:deref
32                                         (sb-alien:slot alien ',(car slotd))
33                                         i)))))
34                 else
35                   collect `(setf (slot-value instance ',(car slotd))
36                                  (sb-alien:slot alien ',(car slotd))))
37         instance)
38       (defun ,to-alien (instance &optional alien)
39         (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
40                  (type ,name instance))
41         (unless alien
42           (setf alien (sb-alien:make-alien ,alien-type)))
43         ,@(loop for slotd in slots
44                 for array-length = (getf (cdr slotd) :array-length)
45                 if array-length
46                   collect `(progn
47                              (let ((array (slot-value instance ',(car slotd))))
48                                (dotimes (i ,array-length)
49                                  (setf (sb-alien:deref
50                                         (sb-alien:slot alien ',(car slotd))
51                                         i)
52                                        (aref array i)))))
53                 else
54                   collect `(setf (sb-alien:slot alien ',(car slotd))
55                                  (slot-value instance ',(car slotd)))))
56       (find-class ',name))))
57
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))))))
64
65 (defun syscall-error ()
66   (error 'sb-posix:syscall-error :errno (get-errno)))
67
68 (declaim (inline never-fails))
69 (defun never-fails (&rest args)
70   (declare (ignore args))
71   nil)
72
73 ;;; filesystem access
74 (defmacro define-call* (name &rest arguments)
75   #-win32 `(define-call ,name ,@arguments)
76   #+win32 `(define-call ,(concatenate 'string "_" name) ,@arguments))
77
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)
87               (whence int))
88 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
89 (macrolet ((def (x)
90                `(progn
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))
97                    (if 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).
109   not
110   (dir (* t)))
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)
115
116 #-win32
117 (progn
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
136   ;; interface
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))
146     (if argp
147         (etypecase arg
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))
159     (if argp
160         (etypecase arg
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)))
164
165   ;; uid, gid
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))
182
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))
195
196 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
197
198 #-win32
199 (progn
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))))
205           (pid (alien-funcall
206                 (extern-alien "wait" (function pid-t (* int)))
207                 (sb-sys:vector-sap ptr))))
208      (if (minusp pid)
209          (syscall-error)
210          (values pid (aref ptr 0))))))
211
212 #-win32
213 (progn
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))))
221           (pid (alien-funcall
222                 (extern-alien "waitpid" (function pid-t
223                                                   pid-t (* int) int))
224                 pid (sb-sys:vector-sap ptr) options)))
225      (if (minusp pid)
226          (syscall-error)
227          (values pid (aref ptr 0)))))
228  ;; waitpid macros
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)))
237
238 ;;; mmap, msync
239 #-win32
240 (progn
241  (define-call "mmap" sb-sys:system-area-pointer
242    (lambda (res)
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))
246
247  (define-call "munmap" int minusp
248    (start sb-sys:system-area-pointer) (length unsigned))
249
250 (define-call "msync" int minusp
251   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
252
253 #-win32
254 (define-call "getpagesize" int minusp)
255 #+win32
256 ;;; KLUDGE: This could be taken from GetSystemInfo
257 (export (defun getpagesize () 4096))
258
259 ;;; passwd database
260 #-win32
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)))
269
270 (defmacro define-pw-call (name arg type)
271   #-win32
272   ;; FIXME: this isn't the documented way of doing this, surely?
273   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
274     `(progn
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)))
279           (if (null r)
280               r
281               (alien-to-passwd r)))))))
282
283 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
284 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
285
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)))
297
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)))
301     `(progn
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)
310                     a-stat)))
311             (when (minusp r)
312               (syscall-error))
313             (alien-to-stat a-stat stat)))))))
314
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" 
319                   pathname filename
320                   (function int c-string (* alien-stat)))
321
322 #-win32
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
326 #+win32
327 (progn
328   (declaim (inline lstat))
329   (export (defun lstat (filename &optional stat)
330             (if stat (stat filename stat) (stat filename)))))
331
332 (define-stat-call #-(or win32 netbsd) "fstat" #+(or win32 netbsd) "_fstat"
333                   fd file-descriptor
334                   (function int int (* alien-stat)))
335
336
337 ;;; mode flags
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))
345
346 #-win32
347 (progn
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))
352    (unless 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))))
358      (when (minusp r)
359        (syscall-error)))
360    (values (aref filedes2 0) (aref filedes2 1))))
361
362 #-win32
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)))
369
370 #-win32
371 (progn
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
380                   (extern-alien
381                    "tcsetattr"
382                    (function int int int (* alien-termios)))
383                   fd actions a-termios)))
384          (when (minusp r)
385            (syscall-error)))
386        (values))))
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)))
395                (file-descriptor fd)
396                a-termios)))
397        (when (minusp r)
398          (syscall-error))
399        (setf termios (alien-to-termios a-termios termios))))
400    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))
409                a-termios
410                speed)))
411        (when (minusp r)
412          (syscall-error))
413        (setf termios (alien-to-termios a-termios termios))))
414    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))
423                a-termios
424                speed)))
425        (when (minusp r)
426          (syscall-error))
427        (setf termios (alien-to-termios a-termios termios))))
428    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)))
437                     a-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)))
446                     a-termios))))
447
448 ;;; environment
449
450 (export 'getenv :sb-posix)
451 (defun getenv (name)
452   (let ((r (alien-funcall
453             (extern-alien "getenv" (function (* char) c-string))
454             name)))
455     (declare (type (alien (* char)) r))
456     (unless (null-alien r)
457       (cast r c-string))))
458 (define-call "putenv" int minusp (string c-string))