0.9.18.46:
[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" :largefile)
87     off-t minusp (fd file-descriptor) (offset off-t)
88     (whence int))
89 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
90 (macrolet ((def (x)
91                `(progn
92                  (define-call-internally open-with-mode ,x int minusp
93                    (pathname filename) (flags int) (mode mode-t))
94                  (define-call-internally open-without-mode ,x int minusp
95                    (pathname filename) (flags int))
96                  (define-entry-point ,x
97                      (pathname flags &optional (mode nil mode-supplied))
98                    (if mode-supplied
99                        (open-with-mode pathname flags mode)
100                        (open-without-mode pathname flags))))))
101     (def #-win32 "open" #+win32 "_open"))
102 (define-call "rename" int minusp (oldpath filename) (newpath filename))
103 (define-call* "rmdir" int minusp (pathname filename))
104 (define-call* "unlink" int minusp (pathname filename))
105 (define-call "opendir" (* t) null-alien (pathname filename))
106 (define-call "readdir" (* dirent)
107   ;; readdir() has the worst error convention in the world.  It's just
108   ;; too painful to support.  (return is NULL _and_ errno "unchanged"
109   ;; is not an error, it's EOF).
110   not
111   (dir (* t)))
112 (define-call "closedir" int minusp (dir (* t)))
113 ;; need to do this here because we can't do it in the DEFPACKAGE
114 (define-call* "umask" mode-t never-fails (mode mode-t))
115 (define-call* "getpid" pid-t never-fails)
116
117 #-win32
118 (progn
119   (define-call "chown" int minusp (pathname filename)
120                (owner uid-t) (group gid-t))
121   (define-call "chroot" int minusp (pathname filename))
122   (define-call "fchdir" int minusp (fd file-descriptor))
123   (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
124   (define-call "fchown" int minusp (fd file-descriptor)
125              (owner uid-t)  (group gid-t))
126   (define-call "fdatasync" int minusp (fd file-descriptor))
127   (define-call ("ftruncate" :largefile)
128       int minusp (fd file-descriptor) (length off-t))
129   (define-call "fsync" int minusp (fd file-descriptor))
130   (define-call "lchown" int minusp (pathname filename)
131                (owner uid-t)  (group gid-t))
132   (define-call "link" int minusp (oldpath filename) (newpath filename))
133   (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
134   (define-call "symlink" int minusp (oldpath filename) (newpath filename))
135   (define-call "sync" void never-fails)
136   (define-call ("truncate" :largefile)
137       int minusp (pathname filename) (length off-t))
138   ;; FIXME: Windows does have _mktemp, which has a slightlty different
139   ;; interface
140   (define-call "mkstemp" int minusp (template c-string))
141   (define-call-internally ioctl-without-arg "ioctl" int minusp
142                           (fd file-descriptor) (cmd int))
143   (define-call-internally ioctl-with-int-arg "ioctl" int minusp
144                           (fd file-descriptor) (cmd int) (arg int))
145   (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
146                           (fd file-descriptor) (cmd int)
147                           (arg alien-pointer-to-anything-or-nil))
148   (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
149     (if argp
150         (etypecase arg
151           ((alien int) (ioctl-with-int-arg fd cmd arg))
152           ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
153         (ioctl-without-arg fd cmd)))
154   (define-call-internally fcntl-without-arg "fcntl" int minusp
155                           (fd file-descriptor) (cmd int))
156   (define-call-internally fcntl-with-int-arg "fcntl" int minusp
157                           (fd file-descriptor) (cmd int) (arg int))
158   (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
159                           (fd file-descriptor) (cmd int)
160                           (arg alien-pointer-to-anything-or-nil))
161   (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
162     (if argp
163         (etypecase arg
164           ((alien int) (fcntl-with-int-arg fd cmd arg))
165           ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
166         (fcntl-without-arg fd cmd)))
167
168   ;; uid, gid
169   (define-call "geteuid" uid-t never-fails) ; "always successful", it says
170   (define-call "getresuid" uid-t never-fails)
171   (define-call "getuid" uid-t never-fails)
172   (define-call "seteuid" int minusp (uid uid-t))
173   (define-call "setfsuid" int minusp (uid uid-t))
174   (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
175   (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
176   (define-call "setuid" int minusp (uid uid-t))
177   (define-call "getegid" gid-t never-fails)
178   (define-call "getgid" gid-t never-fails)
179   (define-call "getresgid" gid-t never-fails)
180   (define-call "setegid" int minusp (gid gid-t))
181   (define-call "setfsgid" int minusp (gid gid-t))
182   (define-call "setgid" int minusp (gid gid-t))
183   (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
184   (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
185
186   ;; processes, signals
187   (define-call "alarm" int never-fails (seconds unsigned))
188   (define-call "fork" pid-t minusp)
189   (define-call "getpgid" pid-t minusp (pid pid-t))
190   (define-call "getppid" pid-t never-fails)
191   (define-call "getpgrp" pid-t never-fails)
192   (define-call "getsid" pid-t minusp  (pid pid-t))
193   (define-call "kill" int minusp (pid pid-t) (signal int))
194   (define-call "killpg" int minusp (pgrp int) (signal int))
195   (define-call "pause" int minusp)
196   (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
197   (define-call "setpgrp" int minusp))
198
199 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
200
201 #-win32
202 (progn
203  (export 'wait :sb-posix)
204  (declaim (inline wait))
205  (defun wait (&optional statusptr)
206    (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
207    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
208           (pid (alien-funcall
209                 (extern-alien "wait" (function pid-t (* int)))
210                 (sb-sys:vector-sap ptr))))
211      (if (minusp pid)
212          (syscall-error)
213          (values pid (aref ptr 0))))))
214
215 #-win32
216 (progn
217  (export 'waitpid :sb-posix)
218  (declaim (inline waitpid))
219  (defun waitpid (pid options &optional statusptr)
220    (declare (type (sb-alien:alien pid-t) pid)
221             (type (sb-alien:alien int) options)
222             (type (or null (simple-array (signed-byte 32) (1))) statusptr))
223    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
224           (pid (alien-funcall
225                 (extern-alien "waitpid" (function pid-t
226                                                   pid-t (* int) int))
227                 pid (sb-sys:vector-sap ptr) options)))
228      (if (minusp pid)
229          (syscall-error)
230          (values pid (aref ptr 0)))))
231  ;; waitpid macros
232  (define-call "wifexited" boolean never-fails (status int))
233  (define-call "wexitstatus" int never-fails (status int))
234  (define-call "wifsignaled" boolean never-fails (status int))
235  (define-call "wtermsig" int never-fails (status int))
236  (define-call "wifstopped" boolean never-fails (status int))
237  (define-call "wstopsig" int never-fails (status int))
238  #+nil ; see alien/waitpid-macros.c
239  (define-call "wifcontinued" boolean never-fails (status int)))
240
241 ;;; mmap, msync
242 #-win32
243 (progn
244  (define-call ("mmap" :largefile) sb-sys:system-area-pointer
245    (lambda (res)
246      (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
247    (addr sap-or-nil) (length unsigned) (prot unsigned)
248    (flags unsigned) (fd file-descriptor) (offset off-t))
249
250  (define-call "munmap" int minusp
251    (start sb-sys:system-area-pointer) (length unsigned))
252
253 (define-call "msync" int minusp
254   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
255
256 #-win32
257 (define-call "getpagesize" int minusp)
258 #+win32
259 ;;; KLUDGE: This could be taken from GetSystemInfo
260 (export (defun getpagesize () 4096))
261
262 ;;; passwd database
263 #-win32
264 (define-protocol-class passwd alien-passwd ()
265   ((name :initarg :name :accessor passwd-name)
266    (passwd :initarg :passwd :accessor passwd-passwd)
267    (uid :initarg :uid :accessor passwd-uid)
268    (gid :initarg :gid :accessor passwd-gid)
269    (gecos :initarg :gecos :accessor passwd-gecos)
270    (dir :initarg :dir :accessor passwd-dir)
271    (shell :initarg :shell :accessor passwd-shell)))
272
273 (defmacro define-pw-call (name arg type)
274   #-win32
275   ;; FIXME: this isn't the documented way of doing this, surely?
276   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
277     `(progn
278       (export ',lisp-name :sb-posix)
279       (declaim (inline ,lisp-name))
280       (defun ,lisp-name (,arg)
281         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
282           (if (null r)
283               r
284               (alien-to-passwd r)))))))
285
286 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
287 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
288
289 (define-protocol-class stat alien-stat ()
290   ((mode :initarg :mode :accessor stat-mode)
291    (ino :initarg :ino :accessor stat-ino)
292    (dev :initarg :dev :accessor stat-dev)
293    (nlink :initarg :nlink :accessor stat-nlink)
294    (uid :initarg :uid :accessor stat-uid)
295    (gid :initarg :gid :accessor stat-gid)
296    (size :initarg :size :accessor stat-size)
297    (atime :initarg :atime :accessor stat-atime)
298    (mtime :initarg :mtime :accessor stat-mtime)
299    (ctime :initarg :ctime :accessor stat-ctime)))
300
301 (defmacro define-stat-call (name arg designator-fun type)
302   ;; FIXME: this isn't the documented way of doing this, surely?
303   (let ((lisp-name (lisp-for-c-symbol name)))
304     `(progn
305       (export ',lisp-name :sb-posix)
306       (declaim (inline ,lisp-name))
307       (defun ,lisp-name (,arg &optional stat)
308         (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
309         (with-alien-stat a-stat ()
310           (let ((r (alien-funcall
311                     (extern-alien ,(real-c-name (list name :largefile)) ,type)
312                     (,designator-fun ,arg)
313                     a-stat)))
314             (when (minusp r)
315               (syscall-error))
316             (alien-to-stat a-stat stat)))))))
317
318 ;; Note: _stat, _lstat, and _fstat for NetBSD are provided in
319 ;; src/runtime/bsd-os.c.  See comments in that file
320 ;; for an explanation. -- RMK 2006-10-15
321 (define-stat-call #-(or win32 netbsd) "stat" #+(or win32 netbsd) "_stat"
322                   pathname filename
323                   (function int c-string (* alien-stat)))
324
325 #-win32
326 (define-stat-call #-netbsd "lstat" #+netbsd "_lstat"
327                   pathname filename
328                   (function int c-string (* alien-stat)))
329 ;;; No symbolic links on Windows, so use stat
330 #+win32
331 (progn
332   (declaim (inline lstat))
333   (export (defun lstat (filename &optional stat)
334             (if stat (stat filename stat) (stat filename)))))
335
336 (define-stat-call #-(or win32 netbsd) "fstat" #+(or win32 netbsd) "_fstat"
337                   fd file-descriptor
338                   (function int int (* alien-stat)))
339
340
341 ;;; mode flags
342 (define-call "s_isreg" boolean never-fails (mode mode-t))
343 (define-call "s_isdir" boolean never-fails (mode mode-t))
344 (define-call "s_ischr" boolean never-fails (mode mode-t))
345 (define-call "s_isblk" boolean never-fails (mode mode-t))
346 (define-call "s_isfifo" boolean never-fails (mode mode-t))
347 (define-call "s_islnk" boolean never-fails (mode mode-t))
348 (define-call "s_issock" boolean never-fails (mode mode-t))
349
350 #-win32
351 (progn
352  (export 'pipe :sb-posix)
353  (declaim (inline pipe))
354  (defun pipe (&optional filedes2)
355    (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
356    (unless filedes2
357      (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
358    (let ((r (alien-funcall
359              ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
360              (extern-alien "pipe" (function int (* int)))
361              (sb-sys:vector-sap filedes2))))
362      (when (minusp r)
363        (syscall-error)))
364    (values (aref filedes2 0) (aref filedes2 1))))
365
366 #-win32
367 (define-protocol-class termios alien-termios ()
368   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
369    (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
370    (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
371    (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
372    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
373
374 #-win32
375 (progn
376  (export 'tcsetattr :sb-posix)
377  (declaim (inline tcsetattr))
378  (defun tcsetattr (fd actions termios)
379    (declare (type termios termios))
380    (with-alien-termios a-termios ()
381      (termios-to-alien termios a-termios)
382      (let ((fd (file-descriptor fd)))
383        (let* ((r (alien-funcall
384                   (extern-alien
385                    "tcsetattr"
386                    (function int int int (* alien-termios)))
387                   fd actions a-termios)))
388          (when (minusp r)
389            (syscall-error)))
390        (values))))
391  (export 'tcgetattr :sb-posix)
392  (declaim (inline tcgetattr))
393  (defun tcgetattr (fd &optional termios)
394    (declare (type (or null termios) termios))
395    (with-alien-termios a-termios ()
396      (let ((r (alien-funcall
397                (extern-alien "tcgetattr"
398                              (function int int (* alien-termios)))
399                (file-descriptor fd)
400                a-termios)))
401        (when (minusp r)
402          (syscall-error))
403        (setf termios (alien-to-termios a-termios termios))))
404    termios)
405  (export 'cfsetispeed :sb-posix)
406  (declaim (inline cfsetispeed))
407  (defun cfsetispeed (speed &optional termios)
408    (declare (type (or null termios) termios))
409    (with-alien-termios a-termios ()
410      (let ((r (alien-funcall
411                (extern-alien "cfsetispeed"
412                              (function int (* alien-termios) speed-t))
413                a-termios
414                speed)))
415        (when (minusp r)
416          (syscall-error))
417        (setf termios (alien-to-termios a-termios termios))))
418    termios)
419  (export 'cfsetospeed :sb-posix)
420  (declaim (inline cfsetospeed))
421  (defun cfsetospeed (speed &optional termios)
422    (declare (type (or null termios) termios))
423    (with-alien-termios a-termios ()
424      (let ((r (alien-funcall
425                (extern-alien "cfsetospeed"
426                              (function int (* alien-termios) speed-t))
427                a-termios
428                speed)))
429        (when (minusp r)
430          (syscall-error))
431        (setf termios (alien-to-termios a-termios termios))))
432    termios)
433  (export 'cfgetispeed :sb-posix)
434  (declaim (inline cfgetispeed))
435  (defun cfgetispeed (termios)
436    (declare (type termios termios))
437    (with-alien-termios a-termios ()
438      (termios-to-alien termios a-termios)
439      (alien-funcall (extern-alien "cfgetispeed"
440                                   (function speed-t (* alien-termios)))
441                     a-termios)))
442  (export 'cfgetospeed :sb-posix)
443  (declaim (inline cfgetospeed))
444  (defun cfgetospeed (termios)
445    (declare (type termios termios))
446    (with-alien-termios a-termios ()
447      (termios-to-alien termios a-termios)
448      (alien-funcall (extern-alien "cfgetospeed"
449                                  (function speed-t (* alien-termios)))
450                     a-termios))))
451
452
453 #-win32
454 (progn
455   (export 'time :sb-posix)
456   (defun time ()
457     (let ((result (alien-funcall (extern-alien "time"
458                                                (function time-t (* time-t)))
459                                  nil)))
460       (if (minusp result)
461           (syscall-error)
462           result)))
463   (export 'utime :sb-posix)
464   (defun utime (filename &optional access-time modification-time)
465     (let ((fun (extern-alien "utime" (function int c-string
466                                                (* alien-utimbuf))))
467           (name (filename filename)))
468       (if (not (and access-time modification-time))
469           (alien-funcall fun name nil)
470           (with-alien ((utimbuf (struct alien-utimbuf)))
471             (setf (slot utimbuf 'actime) (or access-time 0)
472                   (slot utimbuf 'modtime) (or modification-time 0))
473             (let ((result (alien-funcall fun name (alien-sap utimbuf))))
474               (if (minusp result)
475                   (syscall-error)
476                   result))))))
477   (export 'utimes :sb-posix)
478   (defun utimes (filename &optional access-time modification-time)
479     (flet ((seconds-and-useconds (time)
480              (multiple-value-bind (integer fractional)
481                  (cl:truncate time)
482                (values integer (cl:truncate (* fractional 1000000)))))
483            (maybe-syscall-error (value)
484              (if (minusp value)
485                  (syscall-error)
486                  value)))
487       (let ((fun (extern-alien "utimes" (function int c-string
488                                                   (* (array alien-timeval 2)))))
489             (name (filename filename)))
490         (if (not (and access-time modification-time))
491             (maybe-syscall-error (alien-funcall fun name nil))
492             (with-alien ((buf (array alien-timeval 2)))
493               (let ((actime (deref buf 0))
494                     (modtime (deref buf 1)))
495                 (setf (values (slot actime 'sec)
496                               (slot actime 'usec))
497                       (seconds-and-useconds (or access-time 0))
498                       (values (slot modtime 'sec)
499                               (slot modtime 'usec))
500                       (seconds-and-useconds (or modification-time 0)))
501                 (maybe-syscall-error (alien-funcall fun name
502                                                     (alien-sap buf))))))))))
503
504
505 ;;; environment
506
507 (export 'getenv :sb-posix)
508 (defun getenv (name)
509   (let ((r (alien-funcall
510             (extern-alien "getenv" (function (* char) c-string))
511             name)))
512     (declare (type (alien (* char)) r))
513     (unless (null-alien r)
514       (cast r c-string))))
515 (define-call "putenv" int minusp (string c-string))