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