1.0.1.1:
[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" :options :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" :options :largefile) (* 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" :options :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" :options :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
193
194
195   #+mach-exception-handler
196   (progn
197     ;; FIXME this is a lie, of course this can fail, but there's no
198     ;; error handling here yet!
199     (define-call "setup_mach_exceptions" void never-fails)
200     (define-call ("posix_fork" :c-name "fork") pid-t minusp)
201     (defun fork ()
202       (let ((pid (posix-fork)))
203         (when (= pid 0)
204           (setup-mach-exceptions))
205         pid))
206     (export 'fork :sb-posix))
207
208   #-mach-exception-handler
209   (define-call "fork" pid-t minusp)
210
211   (define-call "getpgid" pid-t minusp (pid pid-t))
212   (define-call "getppid" pid-t never-fails)
213   (define-call "getpgrp" pid-t never-fails)
214   (define-call "getsid" pid-t minusp  (pid pid-t))
215   (define-call "kill" int minusp (pid pid-t) (signal int))
216   (define-call "killpg" int minusp (pgrp int) (signal int))
217   (define-call "pause" int minusp)
218   (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
219   (define-call "setpgrp" int minusp))
220
221 ;;(define-call "readlink" int minusp (path filename) (buf (* t)) (len int))
222
223 #-win32
224 (progn
225  (export 'wait :sb-posix)
226  (declaim (inline wait))
227  (defun wait (&optional statusptr)
228    (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
229    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
230           (pid (alien-funcall
231                 (extern-alien "wait" (function pid-t (* int)))
232                 (sb-sys:vector-sap ptr))))
233      (if (minusp pid)
234          (syscall-error)
235          (values pid (aref ptr 0))))))
236
237 #-win32
238 (progn
239  (export 'waitpid :sb-posix)
240  (declaim (inline waitpid))
241  (defun waitpid (pid options &optional statusptr)
242    (declare (type (sb-alien:alien pid-t) pid)
243             (type (sb-alien:alien int) options)
244             (type (or null (simple-array (signed-byte 32) (1))) statusptr))
245    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
246           (pid (alien-funcall
247                 (extern-alien "waitpid" (function pid-t
248                                                   pid-t (* int) int))
249                 pid (sb-sys:vector-sap ptr) options)))
250      (if (minusp pid)
251          (syscall-error)
252          (values pid (aref ptr 0)))))
253  ;; waitpid macros
254  (define-call "wifexited" boolean never-fails (status int))
255  (define-call "wexitstatus" int never-fails (status int))
256  (define-call "wifsignaled" boolean never-fails (status int))
257  (define-call "wtermsig" int never-fails (status int))
258  (define-call "wifstopped" boolean never-fails (status int))
259  (define-call "wstopsig" int never-fails (status int))
260  #+nil ; see alien/waitpid-macros.c
261  (define-call "wifcontinued" boolean never-fails (status int)))
262
263 ;;; mmap, msync
264 #-win32
265 (progn
266  (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
267    (lambda (res)
268      (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
269    (addr sap-or-nil) (length unsigned) (prot unsigned)
270    (flags unsigned) (fd file-descriptor) (offset off-t))
271
272  (define-call "munmap" int minusp
273    (start sb-sys:system-area-pointer) (length unsigned))
274
275 (define-call "msync" int minusp
276   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
277
278 #-win32
279 (define-call "getpagesize" int minusp)
280 #+win32
281 ;;; KLUDGE: This could be taken from GetSystemInfo
282 (export (defun getpagesize () 4096))
283
284 ;;; passwd database
285 #-win32
286 (define-protocol-class passwd alien-passwd ()
287   ((name :initarg :name :accessor passwd-name)
288    (passwd :initarg :passwd :accessor passwd-passwd)
289    (uid :initarg :uid :accessor passwd-uid)
290    (gid :initarg :gid :accessor passwd-gid)
291    (gecos :initarg :gecos :accessor passwd-gecos)
292    (dir :initarg :dir :accessor passwd-dir)
293    (shell :initarg :shell :accessor passwd-shell)))
294
295 (defmacro define-pw-call (name arg type)
296   #-win32
297   ;; FIXME: this isn't the documented way of doing this, surely?
298   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
299     `(progn
300       (export ',lisp-name :sb-posix)
301       (declaim (inline ,lisp-name))
302       (defun ,lisp-name (,arg)
303         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
304           (if (null r)
305               r
306               (alien-to-passwd r)))))))
307
308 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
309 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
310
311 (define-protocol-class stat alien-stat ()
312   ((mode :initarg :mode :accessor stat-mode)
313    (ino :initarg :ino :accessor stat-ino)
314    (dev :initarg :dev :accessor stat-dev)
315    (nlink :initarg :nlink :accessor stat-nlink)
316    (uid :initarg :uid :accessor stat-uid)
317    (gid :initarg :gid :accessor stat-gid)
318    (size :initarg :size :accessor stat-size)
319    (atime :initarg :atime :accessor stat-atime)
320    (mtime :initarg :mtime :accessor stat-mtime)
321    (ctime :initarg :ctime :accessor stat-ctime)))
322
323 (defmacro define-stat-call (name arg designator-fun type)
324   ;; FIXME: this isn't the documented way of doing this, surely?
325   (let ((lisp-name (lisp-for-c-symbol name)))
326     `(progn
327       (export ',lisp-name :sb-posix)
328       (declaim (inline ,lisp-name))
329       (defun ,lisp-name (,arg &optional stat)
330         (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
331         (with-alien-stat a-stat ()
332           (let ((r (alien-funcall
333                     (extern-alien ,(real-c-name (list name :options :largefile)) ,type)
334                     (,designator-fun ,arg)
335                     a-stat)))
336             (when (minusp r)
337               (syscall-error))
338             (alien-to-stat a-stat stat)))))))
339
340 ;; Note: _stat, _lstat, and _fstat for NetBSD are provided in
341 ;; src/runtime/bsd-os.c.  See comments in that file
342 ;; for an explanation. -- RMK 2006-10-15
343 (define-stat-call #-(or win32 netbsd) "stat" #+(or win32 netbsd) "_stat"
344                   pathname filename
345                   (function int c-string (* alien-stat)))
346
347 #-win32
348 (define-stat-call #-netbsd "lstat" #+netbsd "_lstat"
349                   pathname filename
350                   (function int c-string (* alien-stat)))
351 ;;; No symbolic links on Windows, so use stat
352 #+win32
353 (progn
354   (declaim (inline lstat))
355   (export (defun lstat (filename &optional stat)
356             (if stat (stat filename stat) (stat filename)))))
357
358 (define-stat-call #-(or win32 netbsd) "fstat" #+(or win32 netbsd) "_fstat"
359                   fd file-descriptor
360                   (function int int (* alien-stat)))
361
362
363 ;;; mode flags
364 (define-call "s_isreg" boolean never-fails (mode mode-t))
365 (define-call "s_isdir" boolean never-fails (mode mode-t))
366 (define-call "s_ischr" boolean never-fails (mode mode-t))
367 (define-call "s_isblk" boolean never-fails (mode mode-t))
368 (define-call "s_isfifo" boolean never-fails (mode mode-t))
369 (define-call "s_islnk" boolean never-fails (mode mode-t))
370 (define-call "s_issock" boolean never-fails (mode mode-t))
371
372 #-win32
373 (progn
374  (export 'pipe :sb-posix)
375  (declaim (inline pipe))
376  (defun pipe (&optional filedes2)
377    (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
378    (unless filedes2
379      (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
380    (let ((r (alien-funcall
381              ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
382              (extern-alien "pipe" (function int (* int)))
383              (sb-sys:vector-sap filedes2))))
384      (when (minusp r)
385        (syscall-error)))
386    (values (aref filedes2 0) (aref filedes2 1))))
387
388 #-win32
389 (define-protocol-class termios alien-termios ()
390   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
391    (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
392    (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
393    (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
394    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
395
396 #-win32
397 (progn
398  (export 'tcsetattr :sb-posix)
399  (declaim (inline tcsetattr))
400  (defun tcsetattr (fd actions termios)
401    (declare (type termios termios))
402    (with-alien-termios a-termios ()
403      (termios-to-alien termios a-termios)
404      (let ((fd (file-descriptor fd)))
405        (let* ((r (alien-funcall
406                   (extern-alien
407                    "tcsetattr"
408                    (function int int int (* alien-termios)))
409                   fd actions a-termios)))
410          (when (minusp r)
411            (syscall-error)))
412        (values))))
413  (export 'tcgetattr :sb-posix)
414  (declaim (inline tcgetattr))
415  (defun tcgetattr (fd &optional termios)
416    (declare (type (or null termios) termios))
417    (with-alien-termios a-termios ()
418      (let ((r (alien-funcall
419                (extern-alien "tcgetattr"
420                              (function int int (* alien-termios)))
421                (file-descriptor fd)
422                a-termios)))
423        (when (minusp r)
424          (syscall-error))
425        (setf termios (alien-to-termios a-termios termios))))
426    termios)
427  (export 'cfsetispeed :sb-posix)
428  (declaim (inline cfsetispeed))
429  (defun cfsetispeed (speed &optional termios)
430    (declare (type (or null termios) termios))
431    (with-alien-termios a-termios ()
432      (let ((r (alien-funcall
433                (extern-alien "cfsetispeed"
434                              (function int (* alien-termios) speed-t))
435                a-termios
436                speed)))
437        (when (minusp r)
438          (syscall-error))
439        (setf termios (alien-to-termios a-termios termios))))
440    termios)
441  (export 'cfsetospeed :sb-posix)
442  (declaim (inline cfsetospeed))
443  (defun cfsetospeed (speed &optional termios)
444    (declare (type (or null termios) termios))
445    (with-alien-termios a-termios ()
446      (let ((r (alien-funcall
447                (extern-alien "cfsetospeed"
448                              (function int (* alien-termios) speed-t))
449                a-termios
450                speed)))
451        (when (minusp r)
452          (syscall-error))
453        (setf termios (alien-to-termios a-termios termios))))
454    termios)
455  (export 'cfgetispeed :sb-posix)
456  (declaim (inline cfgetispeed))
457  (defun cfgetispeed (termios)
458    (declare (type termios termios))
459    (with-alien-termios a-termios ()
460      (termios-to-alien termios a-termios)
461      (alien-funcall (extern-alien "cfgetispeed"
462                                   (function speed-t (* alien-termios)))
463                     a-termios)))
464  (export 'cfgetospeed :sb-posix)
465  (declaim (inline cfgetospeed))
466  (defun cfgetospeed (termios)
467    (declare (type termios termios))
468    (with-alien-termios a-termios ()
469      (termios-to-alien termios a-termios)
470      (alien-funcall (extern-alien "cfgetospeed"
471                                  (function speed-t (* alien-termios)))
472                     a-termios))))
473
474
475 #-win32
476 (progn
477   (export 'time :sb-posix)
478   (defun time ()
479     (let ((result (alien-funcall (extern-alien "time"
480                                                (function time-t (* time-t)))
481                                  nil)))
482       (if (minusp result)
483           (syscall-error)
484           result)))
485   (export 'utime :sb-posix)
486   (defun utime (filename &optional access-time modification-time)
487     (let ((fun (extern-alien "utime" (function int c-string
488                                                (* alien-utimbuf))))
489           (name (filename filename)))
490       (if (not (and access-time modification-time))
491           (alien-funcall fun name nil)
492           (with-alien ((utimbuf (struct alien-utimbuf)))
493             (setf (slot utimbuf 'actime) (or access-time 0)
494                   (slot utimbuf 'modtime) (or modification-time 0))
495             (let ((result (alien-funcall fun name (alien-sap utimbuf))))
496               (if (minusp result)
497                   (syscall-error)
498                   result))))))
499   (export 'utimes :sb-posix)
500   (defun utimes (filename &optional access-time modification-time)
501     (flet ((seconds-and-useconds (time)
502              (multiple-value-bind (integer fractional)
503                  (cl:truncate time)
504                (values integer (cl:truncate (* fractional 1000000)))))
505            (maybe-syscall-error (value)
506              (if (minusp value)
507                  (syscall-error)
508                  value)))
509       (let ((fun (extern-alien "utimes" (function int c-string
510                                                   (* (array alien-timeval 2)))))
511             (name (filename filename)))
512         (if (not (and access-time modification-time))
513             (maybe-syscall-error (alien-funcall fun name nil))
514             (with-alien ((buf (array alien-timeval 2)))
515               (let ((actime (deref buf 0))
516                     (modtime (deref buf 1)))
517                 (setf (values (slot actime 'sec)
518                               (slot actime 'usec))
519                       (seconds-and-useconds (or access-time 0))
520                       (values (slot modtime 'sec)
521                               (slot modtime 'usec))
522                       (seconds-and-useconds (or modification-time 0)))
523                 (maybe-syscall-error (alien-funcall fun name
524                                                     (alien-sap buf))))))))))
525
526
527 ;;; environment
528
529 (export 'getenv :sb-posix)
530 (defun getenv (name)
531   (let ((r (alien-funcall
532             (extern-alien "getenv" (function (* char) c-string))
533             name)))
534     (declare (type (alien (* char)) r))
535     (unless (null-alien r)
536       (cast r c-string))))
537 (define-call "putenv" int minusp (string c-string))