-
-(define-call "umask" mode-t never-fails (mode mode-t))
-
-;;; uid, gid
-
-(define-call "geteuid" uid-t never-fails) ; "always successful", it says
-(define-call "getresuid" uid-t never-fails)
-(define-call "getuid" uid-t never-fails)
-(define-call "seteuid" int minusp (uid uid-t))
-(define-call "setfsuid" int minusp (uid uid-t))
-(define-call "setreuid" int minusp
- (ruid uid-t) (euid uid-t))
-(define-call "setresuid" int minusp
- (ruid uid-t) (euid uid-t)
- (suid uid-t))
-(define-call "setuid" int minusp (uid uid-t))
-
-(define-call "getegid" gid-t never-fails)
-(define-call "getgid" gid-t never-fails)
-(define-call "getresgid" gid-t never-fails)
-(define-call "setegid" int minusp (gid gid-t))
-(define-call "setfsgid" int minusp (gid gid-t))
-(define-call "setgid" int minusp (gid gid-t))
-(define-call "setregid" int minusp
- (rgid gid-t) (egid gid-t))
-(define-call "setresgid" int minusp
- (rgid gid-t)
- (egid gid-t) (sgid gid-t))
-
-;;; processes, signals
-(define-call "alarm" int never-fails (seconds unsigned))
-(define-call "fork" pid-t minusp)
-(define-call "getpgid" pid-t minusp (pid pid-t))
-(define-call "getpid" pid-t never-fails)
-(define-call "getppid" pid-t never-fails)
-(define-call "getpgrp" pid-t never-fails)
-(define-call "getsid" pid-t minusp (pid pid-t))
-(define-call "kill" int minusp (pid pid-t) (signal int))
-(define-call "killpg" int minusp (pgrp int) (signal int))
-(define-call "pause" int minusp)
-(define-call "setpgid" int minusp
- (pid pid-t) (pgid pid-t))
-(define-call "setpgrp" int minusp)
-
-(export 'wait :sb-posix)
-(declaim (inline wait))
-(defun wait (&optional statusptr)
- (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
- (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
- (pid (alien-funcall
- (extern-alien "wait" (function pid-t (* int)))
- (sb-sys:vector-sap ptr))))
- (if (minusp pid)
- (syscall-error)
- (values pid (aref ptr 0)))))
-
-(export 'waitpid :sb-posix)
-(declaim (inline waitpid))
-(defun waitpid (pid options &optional statusptr)
- (declare (type (sb-alien:alien pid-t) pid)
- (type (sb-alien:alien int) options)
- (type (or null (simple-array (signed-byte 32) (1))) statusptr))
- (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
- (pid (alien-funcall
- (extern-alien "waitpid" (function pid-t
- pid-t (* int) int))
- pid (sb-sys:vector-sap ptr) options)))
- (if (minusp pid)
- (syscall-error)
- (values pid (aref ptr 0)))))
-
-;; waitpid macros
-(define-call "wifexited" boolean never-fails (status int))
-(define-call "wexitstatus" int never-fails (status int))
-(define-call "wifsignaled" boolean never-fails (status int))
-(define-call "wtermsig" int never-fails (status int))
-(define-call "wifstopped" boolean never-fails (status int))
-(define-call "wstopsig" int never-fails (status int))
-#+nil ; see alien/waitpid-macros.c
-(define-call "wifcontinued" boolean never-fails (status int))
+(define-call* "umask" mode-t never-fails (mode mode-t))
+(define-call* "getpid" pid-t never-fails)
+
+#-win32
+(progn
+ (define-call "chown" int minusp (pathname filename)
+ (owner uid-t) (group gid-t))
+ (define-call "chroot" int minusp (pathname filename))
+ (define-call "fchdir" int minusp (fd file-descriptor))
+ (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
+ (define-call "fchown" int minusp (fd file-descriptor)
+ (owner uid-t) (group gid-t))
+ (define-call "fdatasync" int minusp (fd file-descriptor))
+ (define-call ("ftruncate" :options :largefile)
+ int minusp (fd file-descriptor) (length off-t))
+ (define-call "fsync" int minusp (fd file-descriptor))
+ (define-call "lchown" int minusp (pathname filename)
+ (owner uid-t) (group gid-t))
+ (define-call "link" int minusp (oldpath filename) (newpath filename))
+ (define-call "lockf" int minusp (fd file-descriptor) (cmd int) (len off-t))
+ (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
+ (define-call "symlink" int minusp (oldpath filename) (newpath filename))
+ (define-call "sync" void never-fails)
+ (define-call ("truncate" :options :largefile)
+ int minusp (pathname filename) (length off-t))
+ #-win32
+ (macrolet ((def-mk*temp (lisp-name c-name result-type errorp dirp values)
+ (declare (ignore dirp))
+ (if (sb-sys:find-foreign-symbol-address c-name)
+ `(progn
+ (defun ,lisp-name (template)
+ (let* ((external-format sb-alien::*default-c-string-external-format*)
+ (arg (sb-ext:string-to-octets
+ (filename template)
+ :external-format external-format
+ :null-terminate t)))
+ (sb-sys:with-pinned-objects (arg)
+ ;; accommodate for the call-by-reference
+ ;; nature of mks/dtemp's template strings.
+ (let ((result (alien-funcall (extern-alien ,c-name
+ (function ,result-type system-area-pointer))
+ (sb-alien::vector-sap arg))))
+ (when (,errorp result)
+ (syscall-error ',lisp-name))
+ ;; FIXME: We'd rather return pathnames, but other
+ ;; SB-POSIX functions like this return strings...
+ (let ((pathname (sb-ext:octets-to-string
+ arg :external-format external-format
+ :end (1- (length arg)))))
+ ,(if values
+ '(values result pathname)
+ 'pathname))))))
+ (export ',lisp-name))
+ `(progn
+ (defun ,lisp-name (template)
+ (declare (ignore template))
+ (unsupported-error ',lisp-name ,c-name))
+ (define-compiler-macro ,lisp-name (&whole form template)
+ (declare (ignore template))
+ (unsupported-warning ',lisp-name ,c-name)
+ form)
+ (export ',lisp-name)))))
+ (def-mk*temp mktemp "mktemp" (* char) null-alien nil nil)
+ ;; FIXME: Windows does have _mktemp, which has a slightly different
+ ;; interface
+ (def-mk*temp mkstemp "mkstemp" int minusp nil t)
+ ;; FIXME: What about Windows?
+ (def-mk*temp mkdtemp "mkdtemp" (* char) null-alien t nil))
+ (define-call-internally ioctl-without-arg "ioctl" int minusp
+ (fd file-descriptor) (cmd int))
+ (define-call-internally ioctl-with-int-arg "ioctl" int minusp
+ (fd file-descriptor) (cmd int) (arg int))
+ (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
+ (fd file-descriptor) (cmd int)
+ (arg alien-pointer-to-anything-or-nil))
+ (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
+ (if argp
+ (etypecase arg
+ ((alien int) (ioctl-with-int-arg fd cmd arg))
+ ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
+ (ioctl-without-arg fd cmd)))
+ (define-call-internally fcntl-without-arg "fcntl" int minusp
+ (fd file-descriptor) (cmd int))
+ (define-call-internally fcntl-with-int-arg "fcntl" int minusp
+ (fd file-descriptor) (cmd int) (arg int))
+ (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
+ (fd file-descriptor) (cmd int)
+ (arg alien-pointer-to-anything-or-nil))
+ (define-protocol-class flock alien-flock ()
+ ((type :initarg :type :accessor flock-type
+ :documentation "Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.")
+ (whence :initarg :whence :accessor flock-whence
+ :documentation "Flag for starting offset.")
+ (start :initarg :start :accessor flock-start
+ :documentation "Relative offset in bytes.")
+ (len :initarg :len :accessor flock-len
+ :documentation "Size; if 0 then until EOF.")
+ ;; Note: PID isn't initable, and is read-only. But other stuff in
+ ;; SB-POSIX right now loses when a protocol-class slot is unbound,
+ ;; so we initialize it to 0.
+ (pid :initform 0 :reader flock-pid
+ :documentation
+ "Process ID of the process holding the lock; returned with F_GETLK."))
+ (:documentation "Class representing locks used in fcntl(2)."))
+ (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
+ (if argp
+ (etypecase arg
+ ((alien int) (fcntl-with-int-arg fd cmd arg))
+ ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg))
+ (flock (with-alien-flock a-flock ()
+ (flock-to-alien arg a-flock)
+ (let ((r (fcntl-with-pointer-arg fd cmd a-flock)))
+ (alien-to-flock a-flock arg)
+ r))))
+ (fcntl-without-arg fd cmd)))
+
+ ;; uid, gid
+ (define-call "geteuid" uid-t never-fails) ; "always successful", it says
+#-sunos (define-call "getresuid" uid-t never-fails)
+ (define-call "getuid" uid-t never-fails)
+ (define-call "seteuid" int minusp (uid uid-t))
+#-sunos (define-call "setfsuid" int minusp (uid uid-t))
+ (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
+#-sunos (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
+ (define-call "setuid" int minusp (uid uid-t))
+ (define-call "getegid" gid-t never-fails)
+ (define-call "getgid" gid-t never-fails)
+#-sunos (define-call "getresgid" gid-t never-fails)
+ (define-call "setegid" int minusp (gid gid-t))
+#-sunos (define-call "setfsgid" int minusp (gid gid-t))
+ (define-call "setgid" int minusp (gid gid-t))
+ (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
+#-sunos (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
+
+ ;; processes, signals
+ (define-call "alarm" int never-fails (seconds unsigned))
+
+
+
+ ;; FIXME this is a lie, of course this can fail, but there's no
+ ;; error handling here yet!
+ #+mach-exception-handler
+ (define-call "setup_mach_exceptions" void never-fails)
+ (define-call ("posix_fork" :c-name "fork") pid-t minusp)
+ (defun fork ()
+ "Forks the current process, returning 0 in the new process and the PID of
+the child process in the parent. Forking while multiple threads are running is
+not supported."
+ (tagbody
+ (sb-thread::with-all-threads-lock
+ (when (cdr sb-thread::*all-threads*)
+ (go :error))
+ (let ((pid (posix-fork)))
+ #+mach-exception-handler
+ (when (= pid 0)
+ (setup-mach-exceptions))
+ (return-from fork pid)))
+ :error
+ (error "Cannot fork with multiple threads running.")))
+ (export 'fork :sb-posix)
+
+ (define-call "getpgid" pid-t minusp (pid pid-t))
+ (define-call "getppid" pid-t never-fails)
+ (define-call "getpgrp" pid-t never-fails)
+ (define-call "getsid" pid-t minusp (pid pid-t))
+ (define-call "kill" int minusp (pid pid-t) (signal int))
+ (define-call "killpg" int minusp (pgrp int) (signal int))
+ (define-call "pause" int minusp)
+ (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
+ (define-call "setpgrp" int minusp)
+ (define-call "setsid" pid-t minusp))
+
+(defmacro with-growing-c-string ((buffer size) &body body)
+ (sb-int:with-unique-names (c-string-block)
+ `(block ,c-string-block
+ (let (,buffer)
+ (flet ((,buffer (&optional (size-incl-null))
+ (when size-incl-null
+ (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap ,buffer) size-incl-null)
+ 0))
+ (return-from ,c-string-block
+ (sb-alien::c-string-to-string
+ (sb-alien:alien-sap ,buffer)
+ (sb-impl::default-external-format)
+ 'character))))
+ (loop for ,size = 128 then (* 2 ,size)
+ do (unwind-protect
+ (progn
+ (setf ,buffer (make-alien c-string ,size))
+ ,@body)
+ (when ,buffer
+ (free-alien ,buffer)))))))))
+
+#-win32
+(progn
+ (export 'readlink :sb-posix)
+ (defun readlink (pathspec)
+ "Returns the resolved target of a symbolic link as a string."
+ (flet ((%readlink (path buf length)
+ (alien-funcall
+ (extern-alien "readlink" (function int (c-string :not-null t) (* t) int))
+ path buf length)))
+ (with-growing-c-string (buf size)
+ (let ((count (%readlink (filename pathspec) buf size)))
+ (cond ((minusp count)
+ (syscall-error 'readlink))
+ ((< 0 count size)
+ (buf count))))))))
+
+(progn
+ (export 'getcwd :sb-posix)
+ (defun getcwd ()
+ "Returns the process's current working directory as a string."
+ (flet ((%getcwd (buffer size)
+ (alien-funcall
+ (extern-alien #-win32 "getcwd"
+ #+win32 "_getcwd" (function c-string (* t) int))
+ buffer size)))
+ (with-growing-c-string (buf size)
+ (let ((result (%getcwd buf size)))
+ (cond (result
+ (buf))
+ ((/= (get-errno) sb-posix:erange)
+ (syscall-error 'getcwd))))))))
+
+#-win32
+(progn
+ (export 'wait :sb-posix)
+ (declaim (inline wait))
+ (defun wait (&optional statusptr)
+ (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
+ (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
+ (pid (sb-sys:with-pinned-objects (ptr)
+ (alien-funcall
+ (extern-alien "wait" (function pid-t (* int)))
+ (sb-sys:vector-sap ptr)))))
+ (if (minusp pid)
+ (syscall-error 'wait)
+ (values pid (aref ptr 0))))))
+
+#-win32
+(progn
+ (export 'waitpid :sb-posix)
+ (declaim (inline waitpid))
+ (defun waitpid (pid options &optional statusptr)
+ (declare (type (sb-alien:alien pid-t) pid)
+ (type (sb-alien:alien int) options)
+ (type (or null (simple-array (signed-byte 32) (1))) statusptr))
+ (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
+ (pid (sb-sys:with-pinned-objects (ptr)
+ (alien-funcall
+ (extern-alien "waitpid" (function pid-t
+ pid-t (* int) int))
+ pid (sb-sys:vector-sap ptr) options))))
+ (if (minusp pid)
+ (syscall-error 'waitpid)
+ (values pid (aref ptr 0)))))
+ ;; waitpid macros
+ (define-call "wifexited" boolean never-fails (status int))
+ (define-call "wexitstatus" int never-fails (status int))
+ (define-call "wifsignaled" boolean never-fails (status int))
+ (define-call "wtermsig" int never-fails (status int))
+ (define-call "wifstopped" boolean never-fails (status int))
+ (define-call "wstopsig" int never-fails (status int))
+ #+nil ; see alien/waitpid-macros.c
+ (define-call "wifcontinued" boolean never-fails (status int)))