Skip to content
ffi-functions-unix.lisp 32.6 KiB
Newer Older
;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
;;;
;;; --- *UNIX foreign function definitions.
;;;

(in-package :iolib.syscalls)

(eval-when (:compile-toplevel)
  (declaim (optimize (speed 3) (safety 1) (debug 1))))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (define-foreign-library libfixposix
    (t (:default "libfixposix")))
  (use-foreign-library libfixposix))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
;;; ERRNO-related functions
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defcfun (errno "lfp_errno") :int)

(defun (setf errno) (value)
  (foreign-funcall "lfp_set_errno" :int value :int))

Stelian Ionescu's avatar
Stelian Ionescu committed
(defsyscall (%strerror "lfp_strerror")
(defentrypoint strerror (&optional (err (errno)))
  "Look up the error message string for ERRNO (reentrant)."
  (let ((errno
         (if (keywordp err)
             (foreign-enum-value 'errno-values err)
             err)))
    (with-foreign-pointer-as-string ((buf bufsiz) 1024)
Stelian Ionescu's avatar
Stelian Ionescu committed
      (%strerror errno buf bufsiz))))
(defmethod print-object ((e syscall-error) s)
  (with-slots (syscall code identifier message handle handle2) e
    (print-unreadable-object (e s :type nil :identity nil)
      (cond
        (message
         (format s "~A" message))
        (t
         (format s "Syscall ~S signalled error ~A(~S) ~S"
                 syscall identifier (or code "[No code]")
                 (or (strerror code) "[Can't get error string.]"))
         (when handle (format s " FD=~A" handle))
         (when handle2 (format s " FD2=~A" handle2)))))))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defcfun (memset "memset") :pointer
  "Fill the first COUNT bytes of BUFFER with the constant VALUE."
  (buffer :pointer)
  (value  :int)
  (count  size-t))

Stelian Ionescu's avatar
Stelian Ionescu committed
(defentrypoint bzero (buffer count)
  "Fill the first COUNT bytes of BUFFER with zeros."
  (memset buffer 0 count))

(defcfun (memcpy "memcpy") :pointer
  "Copy COUNT octets from SRC to DEST.
The two memory areas must not overlap."
  (dest :pointer)
  (src  :pointer)
  (count size-t))

(defcfun (memmove "memmove") :pointer
  "Copy COUNT octets from SRC to DEST.
The two memory areas may overlap."
  (dest :pointer)
  (src  :pointer)
  (count size-t))


Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
;;; Files
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (%open "lfp_open")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :restart t)
  (mode  mode-t))
(defentrypoint open (path flags &optional (mode #o666))
  "Open a file descriptor for PATH using FLAGS and permissions MODE(#o666 by default)."
  (%open path flags mode))
(defsyscall (creat "lfp_creat")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :restart t)
  "Create file PATH with permissions MODE and return the new FD."
  (mode mode-t))
(defsyscall (%pipe "pipe") :int
  (fds :pointer))
(defentrypoint pipe ()
  "Create pipe, returns two values with the new FDs."
  (with-foreign-object (fds :int 2)
    (%pipe fds)
    (values (mem-aref fds :int 0)
            (mem-aref fds :int 1))))
(defsyscall (mkfifo "mkfifo") :int
  "Create a FIFO (named pipe) with name PATH and permissions MODE."
  (mode mode-t))

(defsyscall (umask "umask") mode-t
  "Sets the umask to NEW-MODE and returns the old one."
  (new-mode mode-t))

(defsyscall (lseek "lfp_lseek")
    (off-t :handle fd)
  "Reposition the offset of the open file associated with the file descriptor FD
to the argument OFFSET according to the directive WHENCE."
  (fd     :int)
(defsyscall (access "access") :int
  "Check whether the file PATH can be accessed using mode MODE."
  (mode :int))
(defsyscall (truncate "lfp_truncate")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :restart t)
  "Truncate the file PATH to a size of precisely LENGTH octets."
(defsyscall (ftruncate "lfp_ftruncate")
    (:int :restart t :handle fd)
  "Truncate the file referenced by FD to a size of precisely LENGTH octets."
(defsyscall (rename "rename") :int
  "Rename file named by OLDPATH to NEWPATH."
  (oldpath sstring)
  (newpath sstring))
(defsyscall (link "link") :int
  "Create a hard link from file OLDPATH to NEWPATH."
  (oldpath sstring)
  (newpath sstring))
(defsyscall (symlink "symlink") :int
  "Create a symbolic link from file OLDPATH to NEWPATH."
  (oldpath sstring)
  (newpath sstring))
(defsyscall (%readlink "readlink") ssize-t
  (buf     :pointer)
  (bufsize size-t))

(defentrypoint readlink (path)
  "Read the file name pointed by the symbolic link PATH."
  (with-foreign-pointer (buf +cstring-path-max+ bufsize)
    (let ((count (%readlink path buf bufsize)))
      (cstring-to-sstring buf count))))
(defsyscall (%realpath "realpath") sstring
Stelian Ionescu's avatar
Stelian Ionescu committed
  (resolved-path :pointer))

(defentrypoint realpath (path)
Stelian Ionescu's avatar
Stelian Ionescu committed
  "Read the file name pointed by the symbolic link PATH."
  (with-foreign-pointer (buf +cstring-path-max+)
    (%realpath path buf)))
(defsyscall (unlink "unlink") :int
  "Delete the file PATH from the file system."
(defsyscall (chown "chown")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :restart t)
  "Change ownership of file PATH to uid OWNER and gid GROUP(dereferences symlinks)."
  (owner uid-t)
  (group uid-t))

(defsyscall (fchown "fchown")
    (:int :restart t :handle fd)
  "Change ownership of an open file referenced by FD to uid OWNER and gid GROUP."
  (fd    :int)
  (owner uid-t)
  (group uid-t))

(defsyscall (lchown "lchown")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :restart t)
  "Change ownership of a file PATH to uid OWNER and gid GROUP(does not dereference symlinks)."
  (owner uid-t)
  (group uid-t))

(defsyscall (chmod "chmod")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :restart t)
  "Change permissions of file PATH to mode MODE."
  (mode mode-t))

(defsyscall (fchmod "fchmod")
    (:int :restart t :handle fd)
  "Change permissions of open file referenced by FD to mode MODE."
  (fd   :int)
  (mode mode-t))
;;;-------------------------------------------------------------------------
;;; I/O
;;;-------------------------------------------------------------------------

(defsyscall (read "read")
    (ssize-t :restart t :handle fd)
  "Read at most COUNT bytes from FD into the foreign area BUF."
  (fd    :int)
  (buf   :pointer)
  (count size-t))

(defsyscall (write "write")
    (ssize-t :restart t :handle fd)
  "Write at most COUNT bytes to FD from the foreign area BUF."
  (fd    :int)
  (buf   :pointer)
  (count size-t))

(defsyscall (readv "readv")
    (ssize-t :restart t :handle fd)
  "Read from FD into the first IOVCNT buffers of the IOV array."
  (fd     :int)
  (iov    :pointer)
  (iovcnt :int))

(defsyscall (writev "writev")
    (ssize-t :restart t :handle fd)
  "Writes to FD the first IOVCNT buffers of the IOV array."
  (fd     :int)
  (iov    :pointer)
  (iovcnt :int))

(defsyscall (pread "lfp_pread")
    (ssize-t :restart t :handle fd)
  "Read at most COUNT bytes from FD at offset OFFSET into the foreign area BUF."
  (fd     :int)
  (buf    :pointer)
  (count  size-t)
  (offset off-t))

(defsyscall (pwrite "lfp_pwrite")
    (ssize-t :restart t :handle fd)
  "Write at most COUNT bytes to FD at offset OFFSET from the foreign area BUF."
  (fd     :int)
  (buf    :pointer)
  (count  size-t)
  (offset off-t))

(defsyscall (sendfile "lfp_sendfile")
    (ssize-t :restart t :handle infd :handle2 outfd)
  (infd   :int)
  (outfd  :int)
  (offset off-t)
  (nbytes size-t))
;;;-------------------------------------------------------------------------
;;; Stat()
;;;-------------------------------------------------------------------------

(define-c-struct-wrapper stat ())

(defsyscall (%stat "lfp_stat")
  (file-name sstring)
(defsyscall (%fstat "lfp_fstat")
    (:int :handle fd)
  (fd      :int)
(defsyscall (%lstat "lfp_lstat")
  (file-name sstring)

;;; If necessary for performance reasons, we can add an optional
;;; argument to this function and use that to reuse a wrapper object.
(defentrypoint funcall-stat (fn arg)
  (with-foreign-object (buf 'stat)
    (funcall fn arg buf)
    (make-instance 'stat :pointer buf)))

(defentrypoint stat (path)
  "Get information about file PATH(dereferences symlinks)."
  (funcall-stat #'%stat path))
(defentrypoint fstat (fd)
  "Get information about file descriptor FD."
  (funcall-stat #'%fstat fd))
(defentrypoint lstat (path)
  "Get information about file PATH(does not dereference symlinks)."
  (funcall-stat #'%lstat path))
(defsyscall (sync "sync") :void
  "Schedule all file system buffers to be written to disk.")

(defsyscall (fsync "fsync")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :restart t)
  "Schedule a file's buffers to be written to disk."
  (fd :int))
(defsyscall (%mkstemp "lfp_mkstemp") :int
  (template :pointer))
(defentrypoint mkstemp (&optional (template ""))
  "Generate a unique temporary filename from TEMPLATE.
Return two values: the file descriptor and the path of the temporary file."
  (let ((template (concatenate 'string template "XXXXXX")))
    (with-sstring-to-cstring (ptr template)
      (values (%mkstemp ptr) (cstring-to-sstring ptr)))))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
;;; Directories
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (mkdir "mkdir") :int
  "Create directory PATH with permissions MODE."
  (mode mode-t))

(defsyscall (rmdir "rmdir") :int
  "Delete directory PATH."
(defsyscall (chdir "chdir") :int
  "Change the current working directory to PATH."
(defsyscall (fchdir "fchdir")
    (:int :restart t :handle fd)
  "Change the current working directory to the directory referenced by FD."
  (fd :int))
(defsyscall (%getcwd "getcwd") :pointer
  (buf :pointer)
  (size size-t))

(defentrypoint getcwd ()
  "Return the current working directory as a string."
  (with-cstring-to-sstring (buf +cstring-path-max+ bufsize)
    (%getcwd buf bufsize)))
(defsyscall (%mkdtemp "mkdtemp") sstring
  (template sstring))
(defentrypoint mkdtemp (&optional (template ""))
  "Generate a unique temporary filename from TEMPLATE."
  (let ((template (concatenate 'string template "XXXXXX")))
    (%mkdtemp template)))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
;;; File Descriptors
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (close "close")
    (:int :handle fd)
  "Close open file descriptor FD."
  (fd :int))

(defsyscall (dup "dup")
    (:int :handle fd)
  "Duplicate file descriptor FD."
  (fd :int))
(defsyscall (dup2 "dup2")
    (:int :restart t :handle oldfd :handle2 newfd)
  "Make NEWFD be the copy of OLDFD, closing NEWFD first if necessary."
  (oldfd :int)
  (newfd :int))
(defsyscall (%fcntl/noarg "fcntl")
    (:int :handle fd)
  (fd  :int)
  (cmd :int))

;;; FIXME: Linux/glibc says ARG's type is long, POSIX says it's int.
;;; Is this an issue?
(defsyscall (%fcntl/int "fcntl")
    (:int :handle fd)
  (fd  :int)
  (cmd :int)
  (arg :int))

(defsyscall (%fcntl/pointer "fcntl")
    (:int :handle fd)
  (fd  :int)
  (cmd :int)
  (arg :pointer))

(defentrypoint fcntl (fd cmd &optional (arg nil argp))
    ((not argp)     (%fcntl/noarg   fd cmd))
    ((integerp arg) (%fcntl/int     fd cmd arg))
    ((pointerp arg) (%fcntl/pointer fd cmd arg))
    (t (error 'type-error :datum arg
              :expected-type '(or null integer foreign-pointer)))))
(defsyscall (%ioctl/noarg "ioctl")
    (:int :handle fd)
  "Send request REQUEST to file referenced by FD."
  (fd      :int)
(defsyscall (%ioctl/pointer "ioctl")
    (:int :handle fd)
  "Send request REQUEST to file referenced by FD using argument ARG."
 (fd      :int)
 (arg     :pointer))

(defentrypoint ioctl (fd request &optional (arg nil argp))
  "Control an I/O device."
  (cond
    ((not argp)     (%ioctl/noarg   fd request))
    ((pointerp arg) (%ioctl/pointer fd request arg))
    (t (error 'type-error :datum arg
              :expected-type '(or null foreign-pointer)))))
(defsyscall (fd-cloexec-p "lfp_is_fd_cloexec") bool-designator
  (fd :int))

(defsyscall (%set-fd-cloexec "lfp_set_fd_cloexec") :int
  (fd      :int)
  (enabled bool-designator))

(defentrypoint (setf fd-cloexec-p) (enabled fd)
  (%set-fd-cloexec fd enabled))

(defsyscall (fd-nonblock-p "lfp_is_fd_nonblock") bool-designator
  (fd :int))

(defsyscall (%set-fd-nonblock "lfp_set_fd_nonblock") :int
  (fd      :int)
  (enabled bool-designator))

(defentrypoint (setf fd-nonblock-p) (enabled fd)
  (%set-fd-nonblock fd enabled))

(defsyscall (fd-open-p "lfp_is_fd_open") bool-designator
  (fd :int))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
;;; TTYs
;;;-------------------------------------------------------------------------

(defsyscall (openpt "lfp_openpt") :int
  (flags :uint64))
Stelian Ionescu's avatar
Stelian Ionescu committed

(defsyscall (grantpt "grantpt")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :handle fd)
  (fd :int))

(defsyscall (unlockpt "unlockpt")
Stelian Ionescu's avatar
Stelian Ionescu committed
    (:int :handle fd)
  (fd :int))

(defsyscall (%ptsname "lfp_ptsname")
    (:pointer :handle fd)
  (fd     :int)
  (buf    :pointer)
  (buflen size-t))
  (with-foreign-pointer (buf +cstring-path-max+ bufsize)
    (%ptsname fd buf bufsize)
    (nth-value 0 (foreign-string-to-lisp buf))))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (select "lfp_select") :int
  "Scan for I/O activity on multiple file descriptors."
  (nfds      :int)
  (readfds   :pointer)
  (writefds  :pointer)
  (exceptfds :pointer)
  (memcpy to from (sizeof 'fd-set))
  to)

(defcfun (fd-clr "lfp_fd_clr") :void
  (fd     :int)
  (fd-set :pointer))

(defcfun (fd-isset "lfp_fd_isset") bool
  (fd     :int)
  (fd-set :pointer))

(defcfun (fd-set "lfp_fd_set") :void
  (fd     :int)
  (fd-set :pointer))

(defcfun (fd-zero "lfp_fd_zero") :void
  (fd-set :pointer))

;;; FIXME: Until a way to autodetect platform features is implemented
Stelian Ionescu's avatar
Stelian Ionescu committed
(eval-when (:compile-toplevel :load-toplevel :execute)
  (unless (boundp 'pollrdhup)
    (defconstant pollrdhup 0)))
(defsyscall (poll "poll") :int
  "Scan for I/O activity on multiple file descriptors."
  (fds     :pointer)
  (nfds    nfds-t)
  (timeout :int))
  (defsyscall (epoll-create "epoll_create") :int
    "Open an epoll file descriptor."
    (size :int))

  (defsyscall (epoll-ctl "epoll_ctl")
      (:int :handle epfd :handle2 fd)
    "Control interface for an epoll descriptor."
    (epfd  :int)
    (op    :int)
    (fd    :int)
    (event :pointer))

  (defsyscall (epoll-wait "epoll_wait")
      (:int :handle epfd)
    "Wait for an I/O event on an epoll file descriptor."
    (epfd      :int)
    (events    :pointer)
    (maxevents :int)
    (timeout   :int)))

#+bsd
(progn
  (defsyscall (kqueue "kqueue") :int
    "Open a kernel event queue.")

  (defsyscall (kevent "kevent")
      (:int :handle fd)
    "Control interface for a kernel event queue."
    (fd         :int)
    (changelist :pointer)               ; const struct kevent *
    (nchanges   :int)
    (eventlist  :pointer)               ; struct kevent *
    (nevents    :int)
    (timeout    :pointer))              ; const struct timespec *

  (defentrypoint ev-set (%kev %ident %filter %flags %fflags %data %udata)
    (with-foreign-slots ((ident filter flags fflags data udata) %kev kevent)
      (setf ident %ident filter %filter flags %flags
            fflags %fflags data %data udata %udata))))
;;;-------------------------------------------------------------------------
;;; Socket message readers
;;;-------------------------------------------------------------------------

(defcfun (cmsg.firsthdr "lfp_cmsg_firsthdr") :pointer
  (msgh :pointer))

(defcfun (cmsg.nxthdr "lfp_cmsg_nxthdr") :pointer
  (msgh :pointer)
  (cmsg :pointer))

(defcfun (cmsg.space "lfp_cmsg_space") size-t
  (length size-t))

(defcfun (cmsg.len "lfp_cmsg_len") size-t
  (length size-t))

(defcfun (cmsg.data "lfp_cmsg_data") :pointer
  (cmsg :pointer))


;;;-------------------------------------------------------------------------
;;; Directory walking
;;;-------------------------------------------------------------------------

(defsyscall (opendir "opendir") :pointer
  "Open directory PATH for listing of its contents."
(defsyscall (closedir "closedir") :int
  "Close directory DIR when done listing its contents."
  (dirp :pointer))
(defentrypoint readdir (dir)
  "Reads an item from the listing of directory DIR (reentrant)."
  (with-foreign-objects ((entry 'dirent) (result :pointer))
    (%readdir dir entry result)
    (if (null-pointer-p (mem-ref result :pointer))
        nil
        (with-foreign-slots ((name type fileno) entry dirent)
          (values (cstring-to-sstring name) type fileno)))))
(defsyscall (rewinddir "rewinddir") :void
  "Rewind directory DIR."
  (dirp :pointer))
(defsyscall (seekdir "seekdir") :void
  "Seek into directory DIR to position POS(as returned by TELLDIR)."
  (dirp :pointer)
  (pos  :long))

;;; FIXME: According to POSIX docs "no errors are defined" for
;;; telldir() but Linux manpages specify a possible EBADF.
(defsyscall (telldir "telldir") off-t
  "Return the current location in directory DIR."
  (dirp :pointer))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
;;; Memory mapping
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (mmap "lfp_mmap")
    (:pointer :handle fd)
  "Map file referenced by FD at offset OFFSET into address space of the
calling process at address ADDR and length LENGTH.
PROT describes the desired memory protection of the mapping.
FLAGS determines whether updates to the mapping are visible to other
processes mapping the same region."
  (addr   :pointer)
  (length size-t)
  (prot   :int)
  (flags  :int)
  (fd     :int)
  (offset off-t))

(defsyscall (munmap "munmap") :int
  "Unmap pages of memory starting at address ADDR with length LENGTH."
  (addr   :pointer)
  (length size-t))
;;;-------------------------------------------------------------------------
;;; Process creation and info
;;;-------------------------------------------------------------------------

(defsyscall (fork "fork") pid-t)

(defsyscall (execv "execv") :int
  (path sstring)
  (argv :pointer))

Stelian Ionescu's avatar
Stelian Ionescu committed
(defsyscall (%waitpid "waitpid") pid-t
  (pid     pid-t)
  (status  :pointer)
  (options :int))

Stelian Ionescu's avatar
Stelian Ionescu committed
(defentrypoint waitpid (pid options)
  (with-foreign-pointer (status (sizeof :int))
Stelian Ionescu's avatar
Stelian Ionescu committed
    (let ((ret (%waitpid pid status options)))
      (values ret (mem-ref status :int)))))

(defsyscall (getpid "getpid") pid-t
  "Returns the process id of the current process")

(defsyscall (getppid "getppid") pid-t
  "Returns the process id of the current process's parent")

#+linux
(defentrypoint gettid ()
Stelian Ionescu's avatar
Stelian Ionescu committed
  (foreign-funcall "syscall" :int sys-gettid :int))
(defsyscall (getuid "getuid") uid-t
  "Get real user id of the current process.")

(defsyscall (setuid "setuid") :int
  "Set real user id of the current process to UID."
(defsyscall (geteuid "geteuid") uid-t
  "Get effective user id of the current process.")

(defsyscall (seteuid "seteuid") :int
  "Set effective user id of the current process to UID."
(defsyscall (getgid "getgid") gid-t
  "Get real group id of the current process.")

(defsyscall (setgid "setgid") :int
  "Set real group id of the current process to GID."
(defsyscall (getegid "getegid") gid-t
  "Get effective group id of the current process.")

(defsyscall (setegid "setegid") :int
  "Set effective group id of the current process to GID."
(defsyscall (setreuid "setreuid") :int
  "Set real and effective user id of the current process to RUID and EUID."
(defsyscall (setregid "setregid") :int
  "Set real and effective group id of the current process to RGID and EGID."
(defsyscall (getpgid "getpgid") pid-t
  "Get process group id of process PID."
(defsyscall (setpgid "setpgid") :int
  "Set process group id of process PID to value PGID."
(defsyscall (getpgrp "getpgrp") pid-t
  "Get process group id of the current process.")

(defsyscall (setpgrp "setpgrp") pid-t
  "Set process group id of the current process.")

(defsyscall (setsid "setsid") pid-t
  "Create session and set process group id of the current process.")

(defsyscall (%getrlimit "lfp_getrlimit")
(defentrypoint getrlimit (resource)
  "Return soft and hard limit of system resource RESOURCE."
  (with-foreign-object (rl 'rlimit)
    (with-foreign-slots ((cur max) rl rlimit)
      (%getrlimit resource rl)
(defsyscall (%setrlimit "lfp_setrlimit")
(defentrypoint setrlimit (resource soft-limit hard-limit)
  "Set SOFT-LIMIT and HARD-LIMIT of system resource RESOURCE."
  (with-foreign-object (rl 'rlimit)
    (with-foreign-slots ((cur max) rl rlimit)
      (setf cur soft-limit
            max hard-limit)
      (%setrlimit resource rl))))
(defsyscall (%getrusage "getrusage") :int
  (who   :int)
  (usage :pointer))

;;; TODO: it might be more convenient to return a wrapper object here
;;; instead like we do in STAT.
(defentrypoint getrusage (who)
  "Return resource usage measures of WHO."
  (with-foreign-object (ru 'rusage)
    (%getrusage who ru)
    (with-foreign-slots ((maxrss ixrss idrss isrss minflt majflt nswap inblock
                          oublock msgsnd msgrcv nsignals nvcsw nivcsw)
                         ru rusage)
      (values (foreign-slot-value (foreign-slot-pointer ru 'rusage 'utime)
                                  'timeval 'sec)
              (foreign-slot-value (foreign-slot-pointer ru 'rusage 'utime)
                                  'timeval 'usec)
              (foreign-slot-value (foreign-slot-pointer ru 'rusage 'stime)
                                  'timeval 'sec)
              (foreign-slot-value (foreign-slot-pointer ru 'rusage 'stime)
                                  'timeval 'usec)
              maxrss ixrss idrss isrss minflt majflt
              nswap inblock oublock msgsnd
              msgrcv nsignals nvcsw nivcsw))))

(defsyscall (getpriority "getpriority") :int
  "Get the scheduling priority of a process, process group, or user,
as indicated by WHICH and WHO."
(defsyscall (setpriority "setpriority") :int
  "Set the scheduling priority of a process, process group, or user,
as indicated by WHICH and WHO to VALUE."
(defentrypoint nice (&optional (increment 0))
  "Get or set process priority."
  ;; FIXME: race condition. might need WITHOUT-INTERRUPTS on some impl.s
  (setf (errno) 0)
  (let ((retval (foreign-funcall "nice" :int increment :int))
        (errno (errno)))
    (if (and (= retval -1) (/= errno 0))
        (signal-syscall-error errno "nice")
        retval)))
(defsyscall (exit "_exit") :void
  "terminate the calling process"
  (status :int))

Stelian Ionescu's avatar
Stelian Ionescu committed


;;;-------------------------------------------------------------------------
;;; Signals
;;;-------------------------------------------------------------------------
Stelian Ionescu's avatar
Stelian Ionescu committed

(defsyscall (kill "kill") :int
Stelian Ionescu's avatar
Stelian Ionescu committed
  "Send signal SIG to process PID."
Stelian Ionescu's avatar
Stelian Ionescu committed
  (pid    pid-t)
  (signum signal))
(defsyscall (sigaction "sigaction") :int
Stelian Ionescu's avatar
Stelian Ionescu committed
  (signum :int)
  (act    :pointer)
  (oldact :pointer))

(defentrypoint wifexited (status)
  (plusp (foreign-funcall "lfp_wifexited" :int status :int)))

(defentrypoint wexitstatus (status)
  (foreign-funcall "lfp_wexitstatus" :int status :int))

(defentrypoint wifsignaled (status)
  (plusp (foreign-funcall "lfp_wifsignaled" :int status :int)))

(defentrypoint wtermsig (status)
  (foreign-funcall "lfp_wtermsig" :int status :int))

(defentrypoint wtermsig* (status)
  (foreign-enum-keyword 'signal (wtermsig status)))

(defentrypoint wcoredump (status)
  (plusp (foreign-funcall "lfp_wcoredump" :int status :int)))

(defentrypoint wifstopped (status)
  (plusp (foreign-funcall "lfp_wifstopped" :int status :int)))

(defentrypoint wstopsig (status)
  (foreign-funcall "lfp_wstopsig" :int status :int))

(defentrypoint wifcontinued (status)
  (plusp (foreign-funcall "lfp_wifcontinued" :int status :int)))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
;;; Time
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (usleep "usleep") :int
  "Suspend execution for USECONDS microseconds."
  (useconds useconds-t))

(defsyscall (%clock-getres "lfp_clock_getres") :int
  "Returns the resolution of the clock CLOCKID."
  (clockid clockid-t)
  (res     :pointer))

(defentrypoint clock-getres (clock-id)
  (with-foreign-object (ts 'timespec)
    (with-foreign-slots ((sec nsec) ts timespec)
      (%clock-getres clock-id ts)
      (values sec nsec))))

(defsyscall (%clock-gettime "lfp_clock_gettime") :int
  (clockid clockid-t)
  (tp      :pointer))

(defentrypoint clock-gettime (clock-id)
  "Returns the time of the clock CLOCKID."
  (with-foreign-object (ts 'timespec)
    (with-foreign-slots ((sec nsec) ts timespec)
      (%clock-gettime clock-id ts)
      (values sec nsec))))

(defsyscall (%clock-settime "lfp_clock_settime") :int
  (clockid clockid-t)
  (tp      :pointer))

(defentrypoint clock-settime (clock-id)
  "Sets the time of the clock CLOCKID."
  (with-foreign-object (ts 'timespec)
    (with-foreign-slots ((sec nsec) ts timespec)
      (%clock-settime clock-id ts)
      (values sec nsec))))

;; FIXME: replace it with clock_gettime(CLOCK_MONOTONIC, ...)
(defentrypoint get-monotonic-time ()
  "Gets current time in seconds from a system's monotonic clock."
  (multiple-value-bind (seconds nanoseconds)
    (+ seconds (/ nanoseconds 1d9))))


Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
Stelian Ionescu's avatar
Stelian Ionescu committed
;;; Environment
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (os-environ "lfp_get_environ") :pointer
  "Return a pointer to the current process environment.")

(defmacro %obsolete-*environ* ()
  (iolib.base::signal-obsolete '*environ* "use function OS-ENVIRON instead"
                               "symbol macro" :WARN)
  `(os-environ))

(define-symbol-macro *environ* (%obsolete-*environ*))
(defentrypoint getenv (name)
  "Returns the value of environment variable NAME."
Stelian Ionescu's avatar
Stelian Ionescu committed
  (when (and (pointerp name) (null-pointer-p name))
    (setf (errno) einval)
    (signal-syscall-error einval "getenv"))
Stelian Ionescu's avatar
Stelian Ionescu committed
  (foreign-funcall "getenv" :string name :string))
(defsyscall (setenv "setenv") :int
  "Changes the value of environment variable NAME to VALUE.
Stelian Ionescu's avatar
Stelian Ionescu committed
The environment variable is overwritten only if overwrite is not NIL."
  (name      :string)
  (value     :string)
  (overwrite bool-designator))

(defsyscall (unsetenv "unsetenv") :int
  "Removes the binding of environment variable NAME."
  (name :string))
(defentrypoint clearenv ()
  "Remove all name-value pairs from the environment set the
OS environment to NULL."
  (let ((envptr (os-environ)))
Stelian Ionescu's avatar
Stelian Ionescu committed
    (unless (null-pointer-p envptr)
      (loop :for i :from 0 :by 1
            :for string := (mem-aref envptr :string i)
            :for name := (subseq string 0 (position #\= string))
            :while name :do (unsetenv name))
Stelian Ionescu's avatar
Stelian Ionescu committed
      (setf (mem-ref envptr :pointer) (null-pointer)))
    (values)))
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
Stelian Ionescu's avatar
Stelian Ionescu committed
;;;-------------------------------------------------------------------------
(defsyscall (%gethostname "gethostname") :int
  (name    :pointer)
  (namelen size-t))

(defentrypoint gethostname ()
  "Return the host name of the current machine."
  (with-foreign-pointer-as-string ((cstr size) 256)
    (%gethostname cstr size)))
(defsyscall (%getdomainname "getdomainname") :int
  (name    :pointer)
  (namelen size-t))

(defentrypoint getdomainname ()
  "Return the domain name of the current machine."
  (with-foreign-pointer-as-string ((cstr size) 256)
    (%getdomainname cstr size)))
Stelian Ionescu's avatar
Stelian Ionescu committed

(defsyscall (%uname "uname") :int
Stelian Ionescu's avatar
Stelian Ionescu committed
  (buf :pointer))

(defentrypoint uname ()
Stelian Ionescu's avatar
Stelian Ionescu committed
  "Get name and information about current kernel."
  (with-foreign-object (buf 'utsname)
    (bzero buf (sizeof 'utsname))
    (%uname buf)