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