1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; --- *UNIX foreign function definitions.
6 (in-package :iolib.syscalls
)
8 ;;; Needed for clock_gettime() and friends.
9 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
10 (define-foreign-library librt
12 (use-foreign-library librt
))
15 ;;;-------------------------------------------------------------------------
16 ;;; ERRNO-related functions
17 ;;;-------------------------------------------------------------------------
19 (defentrypoint (setf errno
) (value)
23 (defsyscall (%strerror-r
(#+linux
"__xpg_strerror_r" "strerror_r"))
29 (defentrypoint strerror
(&optional
(err (errno)))
30 "Look up the error message string for ERRNO (reentrant)."
33 (foreign-enum-value 'errno-values err
)
35 (with-foreign-pointer-as-string ((buf bufsiz
) 1024)
36 (%strerror-r errno buf bufsiz
))))
38 (defmethod print-object ((e syscall-error
) s
)
39 (with-slots (syscall code identifier message handle handle2
) e
41 (format s
"~A" message
)
42 (print-unreadable-object (e s
:type nil
:identity nil
)
43 (format s
"Syscall ~S signalled error ~A(~S) ~S"
44 syscall identifier
(or code
"[No code]")
45 (or (strerror code
) "[Can't get error string.]"))
46 (when handle
(format s
" FD=~A" handle
))
47 (when handle2
(format s
" FD2=~A" handle2
))))))
50 ;;;-------------------------------------------------------------------------
51 ;;; Memory manipulation
52 ;;;-------------------------------------------------------------------------
54 (defcfun* (memset "memset") :pointer
55 "Fill the first COUNT bytes of BUFFER with the constant VALUE."
60 (defentrypoint bzero
(buffer count
)
61 "Fill the first COUNT bytes of BUFFER with zeros."
62 (memset buffer
0 count
))
64 (defcfun* (memcpy "memcpy") :pointer
65 "Copy COUNT octets from SRC to DEST.
66 The two memory areas must not overlap."
71 (defcfun* (memmove "memmove") :pointer
72 "Copy COUNT octets from SRC to DEST.
73 The two memory areas may overlap."
79 ;;;-------------------------------------------------------------------------
81 ;;;-------------------------------------------------------------------------
83 (defsyscall (read "read")
84 (ssize-t :restart t
:handle fd
)
85 "Read at most COUNT bytes from FD into the foreign area BUF."
90 (defsyscall (write "write")
91 (ssize-t :restart t
:handle fd
)
92 "Write at most COUNT bytes to FD from the foreign area BUF."
97 (defsyscall (readv "readv")
98 (ssize-t :restart t
:handle fd
)
99 "Read from FD into the first IOVCNT buffers of the IOV array."
104 (defsyscall (writev "writev")
105 (ssize-t :restart t
:handle fd
)
106 "Writes to FD the first IOVCNT buffers of the IOV array."
111 (defsyscall (pread (#+linux
"pread64" "pread"))
112 (ssize-t :restart t
:handle fd
)
113 "Read at most COUNT bytes from FD at offset OFFSET into the foreign area BUF."
119 (defsyscall (pwrite (#+linux
"pwrite64" "pwrite"))
120 (ssize-t :restart t
:handle fd
)
121 "Write at most COUNT bytes to FD at offset OFFSET from the foreign area BUF."
128 ;;;-------------------------------------------------------------------------
130 ;;;-------------------------------------------------------------------------
132 (defsyscall (%open
(#+linux
"open64" "open"))
138 (defvar *default-open-mode
* #o666
)
140 (defentrypoint open
(path flags
&optional
(mode *default-open-mode
*))
141 "Open a file descriptor for PATH using FLAGS and permissions MODE
142 \(default value is *DEFAULT-OPEN-MODE* - #o666)."
143 (%open path flags mode
))
145 (defsyscall (creat (#+linux
"creat64" "creat"))
147 "Create file PATH with permissions MODE and return the new FD."
151 (defsyscall (%pipe
"pipe") :int
154 (defentrypoint pipe
()
155 "Create pipe, returns two values with the new FDs."
156 (with-foreign-object (fds :int
2)
158 (values (mem-aref fds
:int
0)
159 (mem-aref fds
:int
1))))
161 (defsyscall (mkfifo "mkfifo") :int
162 "Create a FIFO (named pipe) with name PATH and permissions MODE."
166 (defsyscall (umask "umask") mode-t
167 "Sets the umask to NEW-MODE and returns the old one."
170 (defsyscall (lseek (#+linux
"lseek64" "lseek"))
172 "Reposition the offset of the open file associated with the file descriptor FD
173 to the argument OFFSET according to the directive WHENCE."
178 (defsyscall (access "access") :int
179 "Check whether the file PATH can be accessed using mode MODE."
183 (defsyscall (truncate (#+linux
"truncate64" "truncate"))
185 "Truncate the file PATH to a size of precisely LENGTH octets."
189 (defsyscall (ftruncate (#+linux
"ftruncate64" "ftruncate"))
190 (:int
:restart t
:handle fd
)
191 "Truncate the file referenced by FD to a size of precisely LENGTH octets."
195 (defsyscall (rename "rename") :int
196 "Rename file named by OLDPATH to NEWPATH."
200 (defsyscall (link "link") :int
201 "Create a hard link from file OLDPATH to NEWPATH."
205 (defsyscall (symlink "symlink") :int
206 "Create a symbolic link from file OLDPATH to NEWPATH."
210 (defsyscall (%readlink
"readlink") ssize-t
215 (defentrypoint readlink
(path)
216 "Read the file name pointed by the symbolic link PATH."
217 (with-foreign-pointer (buf +cstring-path-max
+ bufsize
)
218 (let ((count (%readlink path buf bufsize
)))
219 (cstring-to-sstring buf count
))))
221 (defsyscall (%realpath
"realpath") sstring
223 (resolved-path :pointer
))
225 (defentrypoint realpath
(path)
226 "Read the file name pointed by the symbolic link PATH."
227 (with-foreign-pointer (buf +cstring-path-max
+)
228 (%realpath path buf
)))
230 (defsyscall (unlink "unlink") :int
231 "Delete the file PATH from the file system."
234 (defsyscall (chown "chown")
236 "Change ownership of file PATH to uid OWNER and gid GROUP(dereferences symlinks)."
241 (defsyscall (fchown "fchown")
242 (:int
:restart t
:handle fd
)
243 "Change ownership of an open file referenced by FD to uid OWNER and gid GROUP."
248 (defsyscall (lchown "lchown")
250 "Change ownership of a file PATH to uid OWNER and gid GROUP(does not dereference symlinks)."
255 (defsyscall (chmod "chmod")
257 "Change permissions of file PATH to mode MODE."
261 (defsyscall (fchmod "fchmod")
262 (:int
:restart t
:handle fd
)
263 "Change permissions of open file referenced by FD to mode MODE."
268 ;;;-------------------------------------------------------------------------
270 ;;;-------------------------------------------------------------------------
272 (define-c-struct-wrapper stat
())
274 (defsyscall (%stat
(#+linux
"__xstat64" "stat"))
281 (defsyscall (%fstat
(#+linux
"__fxstat64" "fstat"))
288 (defsyscall (%lstat
(#+linux
"__lxstat64" "lstat"))
295 ;;; If necessary for performance reasons, we can add an optional
296 ;;; argument to this function and use that to reuse a wrapper object.
297 (defentrypoint funcall-stat
(fn arg
)
298 (with-foreign-object (buf 'stat
)
299 (funcall fn
#+linux
+stat-version
+ arg buf
)
300 (make-instance 'stat
:pointer buf
)))
302 (defentrypoint stat
(path)
303 "Get information about file PATH(dereferences symlinks)."
304 (funcall-stat #'%stat path
))
306 (defentrypoint fstat
(fd)
307 "Get information about file descriptor FD."
308 (funcall-stat #'%fstat fd
))
310 (defentrypoint lstat
(path)
311 "Get information about file PATH(does not dereference symlinks)."
312 (funcall-stat #'%lstat path
))
314 (defsyscall (sync "sync") :void
315 "Schedule all file system buffers to be written to disk.")
317 (defsyscall (fsync "fsync")
319 "Schedule a file's buffers to be written to disk."
322 (defsyscall (%mkstemp
(#+linux
"mkstemp64" "mkstemp")) :int
325 (defentrypoint mkstemp
(&optional
(template ""))
326 "Generate a unique temporary filename from TEMPLATE.
327 Return two values: the file descriptor and the path of the temporary file."
328 (let ((template (concatenate 'string template
"XXXXXX")))
329 (with-sstring-to-cstring (ptr template
)
330 (values (%mkstemp ptr
) (cstring-to-sstring ptr
)))))
333 ;;;-------------------------------------------------------------------------
335 ;;;-------------------------------------------------------------------------
337 (defsyscall (mkdir "mkdir") :int
338 "Create directory PATH with permissions MODE."
342 (defsyscall (rmdir "rmdir") :int
343 "Delete directory PATH."
346 (defsyscall (chdir "chdir") :int
347 "Change the current working directory to PATH."
350 (defsyscall (fchdir "fchdir")
351 (:int
:restart t
:handle fd
)
352 "Change the current working directory to the directory referenced by FD."
355 (defsyscall (%getcwd
"getcwd") :pointer
359 (defentrypoint getcwd
()
360 "Return the current working directory as a string."
361 (with-cstring-to-sstring (buf +cstring-path-max
+ bufsize
)
362 (%getcwd buf bufsize
)))
364 (defsyscall (%mkdtemp
"mkdtemp") sstring
367 (defentrypoint mkdtemp
(&optional
(template ""))
368 "Generate a unique temporary filename from TEMPLATE."
369 (let ((template (concatenate 'string template
"XXXXXX")))
370 (%mkdtemp template
)))
373 ;;;-------------------------------------------------------------------------
375 ;;;-------------------------------------------------------------------------
377 (defsyscall (close "close")
379 "Close open file descriptor FD."
382 (defsyscall (dup "dup")
384 "Duplicate file descriptor FD."
387 (defsyscall (dup2 "dup2")
388 (:int
:restart t
:handle oldfd
:handle2 newfd
)
389 "Make NEWFD be the copy of OLDFD, closing NEWFD first if necessary."
393 (defsyscall (%fcntl
/noarg
"fcntl")
398 ;;; FIXME: Linux/glibc says ARG's type is long, POSIX says it's int.
399 ;;; Is this an issue?
400 (defsyscall (%fcntl
/int
"fcntl")
406 (defsyscall (%fcntl
/pointer
"fcntl")
412 (defentrypoint fcntl
(fd cmd
&optional
(arg nil argp
))
414 ((not argp
) (%fcntl
/noarg fd cmd
))
415 ((integerp arg
) (%fcntl
/int fd cmd arg
))
416 ((pointerp arg
) (%fcntl
/pointer fd cmd arg
))
417 ;; FIXME: signal a type error
418 (t (error "Wrong argument to fcntl: ~S" arg
))))
420 (defentrypoint fd-nonblock
(fd)
421 (let ((current-flags (fcntl fd f-getfl
)))
422 (logtest o-nonblock current-flags
)))
424 (defentrypoint (setf fd-nonblock
) (newmode fd
)
425 (let* ((current-flags (fcntl fd f-getfl
))
426 (new-flags (if newmode
427 (logior current-flags o-nonblock
)
428 (logandc2 current-flags o-nonblock
))))
429 (when (/= new-flags current-flags
)
430 (fcntl fd f-setfl new-flags
))
433 (defsyscall (%ioctl
/noarg
"ioctl")
434 (:int
:restart t
:handle fd
)
435 "Send request REQUEST to file referenced by FD."
439 (defsyscall (%ioctl
/pointer
"ioctl")
440 (:int
:restart t
:handle fd
)
441 "Send request REQUEST to file referenced by FD using argument ARG."
446 (defentrypoint ioctl
(fd request
&optional
(arg nil argp
))
447 "Control an I/O device."
449 ((not argp
) (%ioctl
/noarg fd request
))
450 ((pointerp arg
) (%ioctl
/pointer fd request arg
))
451 ;; FIXME: signal a type error
452 (t (error "Wrong argument to ioctl: ~S" arg
))))
454 (defentrypoint fd-open-p
(fd)
460 ;;;-------------------------------------------------------------------------
462 ;;;-------------------------------------------------------------------------
464 (defsyscall (posix-openpt "posix_openpt") :int
467 (defsyscall (grantpt "grantpt")
471 (defsyscall (unlockpt "unlockpt")
475 (defsyscall (ptsname "ptsname")
480 ;;;-------------------------------------------------------------------------
481 ;;; File descriptor polling
482 ;;;-------------------------------------------------------------------------
484 (defsyscall (select "select") :int
485 "Scan for I/O activity on multiple file descriptors."
492 (defentrypoint fd-zero
(fd-set)
493 (bzero fd-set size-of-fd-set
)
496 (defentrypoint copy-fd-set
(from to
)
497 (memcpy to from size-of-fd-set
)
500 (deftype select-file-descriptor
()
503 (defentrypoint fd-isset
(fd fd-set
)
504 (multiple-value-bind (byte-off bit-off
) (floor fd
8)
505 (let ((oldval (mem-aref fd-set
:uint8 byte-off
)))
506 (logbitp bit-off oldval
))))
508 (defentrypoint fd-clr
(fd fd-set
)
509 (multiple-value-bind (byte-off bit-off
) (floor fd
8)
510 (let ((oldval (mem-aref fd-set
:uint8 byte-off
)))
511 (setf (mem-aref fd-set
:uint8 byte-off
)
512 (logandc2 oldval
(ash 1 bit-off
)))))
515 (defentrypoint fd-set
(fd fd-set
)
516 (multiple-value-bind (byte-off bit-off
) (floor fd
8)
517 (let ((oldval (mem-aref fd-set
:uint8 byte-off
)))
518 (setf (mem-aref fd-set
:uint8 byte-off
)
519 (logior oldval
(ash 1 bit-off
)))))
522 ;;; FIXME: Until a way to autodetect platform features is implemented
523 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
524 (unless (boundp 'pollrdhup
)
525 (defconstant pollrdhup
0)))
527 (defsyscall (poll "poll") :int
528 "Scan for I/O activity on multiple file descriptors."
535 (defsyscall (epoll-create "epoll_create") :int
536 "Open an epoll file descriptor."
539 (defsyscall (epoll-ctl "epoll_ctl")
540 (:int
:handle epfd
:handle2 fd
)
541 "Control interface for an epoll descriptor."
547 (defsyscall (epoll-wait "epoll_wait")
549 "Wait for an I/O event on an epoll file descriptor."
557 (defsyscall (kqueue "kqueue") :int
558 "Open a kernel event queue.")
560 (defsyscall (kevent "kevent")
562 "Control interface for a kernel event queue."
564 (changelist :pointer
) ; const struct kevent *
566 (eventlist :pointer
) ; struct kevent *
568 (timeout :pointer
)) ; const struct timespec *
570 (defentrypoint ev-set
(%kev %ident %filter %flags %fflags %data %udata
)
571 (with-foreign-slots ((ident filter flags fflags data udata
) %kev kevent
)
572 (setf ident %ident filter %filter flags %flags
573 fflags %fflags data %data udata %udata
))))
576 ;;;-------------------------------------------------------------------------
577 ;;; Directory walking
578 ;;;-------------------------------------------------------------------------
580 (defsyscall (opendir "opendir") :pointer
581 "Open directory PATH for listing of its contents."
585 (defsyscall (fdopendir "fdopendir") :pointer
586 "Open directory denoted by descriptor FD for listing of its contents."
589 (defsyscall (closedir "closedir") :int
590 "Close directory DIR when done listing its contents."
593 (defsyscall (%readdir-r
(#+linux
"readdir64_r" "readdir_r"))
595 :error-predicate plusp
596 :error-location
:return
)
601 (defentrypoint readdir
(dir)
602 "Reads an item from the listing of directory DIR (reentrant)."
603 (with-foreign-objects ((entry 'dirent
) (result :pointer
))
604 (%readdir-r dir entry result
)
605 (if (null-pointer-p (mem-ref result
:pointer
))
607 (with-foreign-slots ((name type fileno
) entry dirent
)
608 (values (cstring-to-sstring name
) type fileno
)))))
610 (defsyscall (rewinddir "rewinddir") :void
611 "Rewind directory DIR."
614 (defsyscall (seekdir "seekdir") :void
615 "Seek into directory DIR to position POS(as returned by TELLDIR)."
619 ;;; FIXME: According to POSIX docs "no errors are defined" for
620 ;;; telldir() but Linux manpages specify a possible EBADF.
621 (defsyscall (telldir "telldir") off-t
622 "Return the current location in directory DIR."
626 ;;;-------------------------------------------------------------------------
628 ;;;-------------------------------------------------------------------------
630 (defsyscall (mmap (#+linux
"mmap64" "mmap"))
631 (:pointer
:handle fd
)
632 "Map file referenced by FD at offset OFFSET into address space of the
633 calling process at address ADDR and length LENGTH.
634 PROT describes the desired memory protection of the mapping.
635 FLAGS determines whether updates to the mapping are visible to other
636 processes mapping the same region."
644 (defsyscall (munmap "munmap") :int
645 "Unmap pages of memory starting at address ADDR with length LENGTH."
650 ;;;-------------------------------------------------------------------------
651 ;;; Process creation and info
652 ;;;-------------------------------------------------------------------------
654 (defsyscall (fork "fork") pid-t
655 "Create a child process.")
657 (defsyscall (execv "execv") :int
661 (defsyscall (execvp "execvp") :int
665 (defsyscall (waitpid "waitpid") pid-t
670 (defsyscall (getpid "getpid") pid-t
671 "Returns the process id of the current process")
673 (defsyscall (getppid "getppid") pid-t
674 "Returns the process id of the current process's parent")
677 (defentrypoint gettid
()
678 (foreign-funcall "syscall" :int sys-gettid
:int
))
680 (defsyscall (getuid "getuid") uid-t
681 "Get real user id of the current process.")
683 (defsyscall (setuid "setuid") :int
684 "Set real user id of the current process to UID."
687 (defsyscall (geteuid "geteuid") uid-t
688 "Get effective user id of the current process.")
690 (defsyscall (seteuid "seteuid") :int
691 "Set effective user id of the current process to UID."
694 (defsyscall (getgid "getgid") gid-t
695 "Get real group id of the current process.")
697 (defsyscall (setgid "setgid") :int
698 "Set real group id of the current process to GID."
701 (defsyscall (getegid "getegid") gid-t
702 "Get effective group id of the current process.")
704 (defsyscall (setegid "setegid") :int
705 "Set effective group id of the current process to GID."
708 (defsyscall (setreuid "setreuid") :int
709 "Set real and effective user id of the current process to RUID and EUID."
713 (defsyscall (setregid "setregid") :int
714 "Set real and effective group id of the current process to RGID and EGID."
718 (defsyscall (getpgid "getpgid") pid-t
719 "Get process group id of process PID."
722 (defsyscall (setpgid "setpgid") :int
723 "Set process group id of process PID to value PGID."
727 (defsyscall (getpgrp "getpgrp") pid-t
728 "Get process group id of the current process.")
730 (defsyscall (setpgrp "setpgrp") pid-t
731 "Set process group id of the current process.")
733 (defsyscall (setsid "setsid") pid-t
734 "Create session and set process group id of the current process.")
736 (defsyscall (%getrlimit
(#+linux
"getrlimit64" "getrlimit"))
741 (defentrypoint getrlimit
(resource)
742 "Return soft and hard limit of system resource RESOURCE."
743 (with-foreign-object (rl 'rlimit
)
744 (with-foreign-slots ((cur max
) rl rlimit
)
745 (%getrlimit resource rl
)
748 (defsyscall (%setrlimit
(#+linux
"setrlimit64" "setrlimit"))
753 (defentrypoint setrlimit
(resource soft-limit hard-limit
)
754 "Set SOFT-LIMIT and HARD-LIMIT of system resource RESOURCE."
755 (with-foreign-object (rl 'rlimit
)
756 (with-foreign-slots ((cur max
) rl rlimit
)
759 (%setrlimit resource rl
))))
761 (defsyscall (%getrusage
"getrusage") :int
765 ;;; TODO: it might be more convenient to return a wrapper object here
766 ;;; instead like we do in STAT.
767 (defentrypoint getrusage
(who)
768 "Return resource usage measures of WHO."
769 (with-foreign-object (ru 'rusage
)
771 (with-foreign-slots ((maxrss ixrss idrss isrss minflt majflt nswap inblock
772 oublock msgsnd msgrcv nsignals nvcsw nivcsw
)
774 (values (foreign-slot-value (foreign-slot-pointer ru
'rusage
'utime
)
776 (foreign-slot-value (foreign-slot-pointer ru
'rusage
'utime
)
778 (foreign-slot-value (foreign-slot-pointer ru
'rusage
'stime
)
780 (foreign-slot-value (foreign-slot-pointer ru
'rusage
'stime
)
782 maxrss ixrss idrss isrss minflt majflt
783 nswap inblock oublock msgsnd
784 msgrcv nsignals nvcsw nivcsw
))))
786 (defsyscall (getpriority "getpriority") :int
787 "Get the scheduling priority of a process, process group, or user,
788 as indicated by WHICH and WHO."
792 (defsyscall (setpriority "setpriority") :int
793 "Set the scheduling priority of a process, process group, or user,
794 as indicated by WHICH and WHO to VALUE."
799 (defentrypoint nice
(&optional
(increment 0))
800 "Get or set process priority."
801 ;; FIXME: race condition. might need WITHOUT-INTERRUPTS on some impl.s
803 (let ((retval (foreign-funcall "nice" :int increment
:int
))
805 (if (and (= retval -
1) (/= errno
0))
806 (signal-syscall-error errno
)
809 (defsyscall (exit "_exit") :void
810 "terminate the calling process"
815 ;;;-------------------------------------------------------------------------
817 ;;;-------------------------------------------------------------------------
819 (defsyscall (kill "kill") :int
820 "Send signal SIG to process PID."
824 (defsyscall (sigaction "sigaction") :int
830 ;;;-------------------------------------------------------------------------
832 ;;;-------------------------------------------------------------------------
834 (defsyscall (usleep "usleep") :int
835 "Suspend execution for USECONDS microseconds."
836 (useconds useconds-t
))
838 (defsyscall (%time
"time") time-t
841 (defentrypoint time
()
842 "Get time in seconds."
843 (%time
(null-pointer)))
845 (defsyscall (%gettimeofday
"gettimeofday") :int
849 (defentrypoint gettimeofday
()
850 "Return the time in seconds and microseconds."
851 (with-foreign-object (tv 'timeval
)
852 (with-foreign-slots ((sec usec
) tv timeval
)
853 (%gettimeofday tv
(null-pointer))
858 (defsyscall (%clock-getres
"clock_getres") :int
859 "Returns the resolution of the clock CLOCKID."
863 (defentrypoint clock-getres
(clock-id)
864 (with-foreign-object (ts 'timespec
)
865 (with-foreign-slots ((sec nsec
) ts timespec
)
866 (%clock-getres clock-id ts
)
869 (defsyscall (%clock-gettime
"clock_gettime") :int
873 (defentrypoint clock-gettime
(clock-id)
874 "Returns the time of the clock CLOCKID."
875 (with-foreign-object (ts 'timespec
)
876 (with-foreign-slots ((sec nsec
) ts timespec
)
877 (%clock-gettime clock-id ts
)
880 (defsyscall (%clock-settime
"clock_settime") :int
884 (defentrypoint clock-settime
(clock-id)
885 "Sets the time of the clock CLOCKID."
886 (with-foreign-object (ts 'timespec
)
887 (with-foreign-slots ((sec nsec
) ts timespec
)
888 (%clock-settime clock-id ts
)
889 (values sec nsec
)))))
891 ;;; FIXME: or we can implement this through the MACH functions.
894 (defctype kern-return-t
:int
)
895 (defctype clock-res-t
:int
)
896 (defctype clock-id-t
:int
)
897 (defctype port-t
:unsigned-int
) ; not sure
898 (defctype clock-serv-t port-t
)
900 (defconstant kern-success
0)
902 (defconstant system-clock
0)
903 (defconstant calendar-clock
1)
904 (defconstant realtime-clock
0)
906 (defsyscall (mach-host-self "mach_host_self") port-t
)
908 (defsyscall (%host-get-clock-service
"host_get_clock_service") kern-return-t
911 (clock-name :pointer
))
913 (defentrypoint host-get-clock-service
(id &optional
(host (mach-host-self)))
914 (with-foreign-object (clock 'clock-serv-t
)
915 (%host-get-clock-service host id clock
)
916 (mem-ref clock
:int
)))
918 (defsyscall (%clock-get-time
"clock_get_time") kern-return-t
919 (clock-serv clock-serv-t
)
922 (defentrypoint clock-get-time
(clock-service)
923 (with-foreign-object (time 'timespec
)
924 (%clock-get-time clock-service time
)
925 (with-foreign-slots ((sec nsec
) time timespec
)
926 (values sec nsec
)))))
928 (defentrypoint get-monotonic-time
()
929 "Gets current time in seconds from a system's monotonic clock."
930 (multiple-value-bind (seconds nanoseconds
)
931 #-darwin
(clock-gettime clock-monotonic
)
932 #+darwin
(clock-get-time (host-get-clock-service system-clock
))
933 (+ seconds
(/ nanoseconds
1d9
))))
936 ;;;-------------------------------------------------------------------------
938 ;;;-------------------------------------------------------------------------
940 (defcvar ("environ" :read-only t
) (:pointer
:string
))
942 (defentrypoint getenv
(name)
943 "Returns the value of environment variable NAME."
944 (when (and (pointerp name
) (null-pointer-p name
))
945 (setf (errno) einval
)
946 (signal-syscall-error))
947 (foreign-funcall "getenv" :string name
:string
))
949 (defsyscall (setenv "setenv") :int
950 "Changes the value of environment variable NAME to VALUE.
951 The environment variable is overwritten only if overwrite is not NIL."
954 (overwrite bool-designator
))
956 (defsyscall (unsetenv "unsetenv") :int
957 "Removes the binding of environment variable NAME."
960 (defentrypoint clearenv
()
961 "Remove all name-value pairs from the environment and set the external
962 variable *environ* to NULL."
963 (let ((envptr *environ
*))
964 (unless (null-pointer-p envptr
)
965 (loop :for i
:from
0 :by
1
966 :for string
:= (mem-aref envptr
:string i
)
967 :for name
:= (subseq string
0 (position #\
= string
))
968 :while name
:do
(unsetenv name
))
969 (setf (mem-ref envptr
:pointer
) (null-pointer)))
973 ;;;-------------------------------------------------------------------------
975 ;;;-------------------------------------------------------------------------
977 (defsyscall (%gethostname
"gethostname") :int
981 (defentrypoint gethostname
()
982 "Return the host name of the current machine."
983 (with-foreign-pointer-as-string ((cstr size
) 256)
984 (%gethostname cstr size
)))
986 (defsyscall (%getdomainname
"getdomainname") :int
990 (defentrypoint getdomainname
()
991 "Return the domain name of the current machine."
992 (with-foreign-pointer-as-string ((cstr size
) 256)
993 (%getdomainname cstr size
)))
995 (defsyscall (%uname
"uname") :int
998 (defentrypoint uname
()
999 "Get name and information about current kernel."
1000 (with-foreign-object (buf 'utsname
)
1001 (bzero buf size-of-utsname
)
1003 (macrolet ((utsname-slot (name)
1004 `(foreign-string-to-lisp
1005 (foreign-slot-pointer buf
'utsname
',name
))))
1006 (values (utsname-slot sysname
)
1007 (utsname-slot nodename
)
1008 (utsname-slot release
)
1009 (utsname-slot version
)
1010 (utsname-slot machine
)))))
1013 ;;;-------------------------------------------------------------------------
1015 ;;;-------------------------------------------------------------------------
1017 (defsyscall (%getpwuid-r
"getpwuid_r")
1019 :error-predicate plusp
1020 :error-location
:return
)
1027 (defsyscall (%getpwnam-r
"getpwnam_r")
1029 :error-predicate plusp
1030 :error-location
:return
)
1037 (defun funcall-getpw (fn arg
)
1038 (with-foreign-objects ((pw 'passwd-entry
) (pwp :pointer
))
1039 (with-foreign-pointer (buf +cstring-path-max
+ bufsize
)
1040 (with-foreign-slots ((name passwd uid gid gecos dir shell
) pw passwd-entry
)
1041 (funcall fn arg pw buf bufsize pwp
)
1042 (if (null-pointer-p (mem-ref pwp
:pointer
))
1044 (values name passwd uid gid gecos dir shell
))))))
1046 (defentrypoint getpwuid
(uid)
1047 "Gets the password-entry of a user, by user id (reentrant)."
1048 (funcall-getpw #'%getpwuid-r uid
))
1050 (defentrypoint getpwnam
(name)
1051 "Gets the password-entry of a user, by username (reentrant)."
1052 (funcall-getpw #'%getpwnam-r name
))
1055 ;;;-------------------------------------------------------------------------
1057 ;;;-------------------------------------------------------------------------
1059 (defsyscall (%getgrgid-r
"getgrgid_r")
1061 :error-predicate plusp
1062 :error-location
:return
)
1069 (defsyscall (%getgrnam-r
"getgrnam_r")
1071 :error-predicate plusp
1072 :error-location
:return
)
1079 ;; FIXME: return group members too
1080 (defun funcall-getgr (fn arg
)
1081 (with-foreign-objects ((gr 'group-entry
) (grp :pointer
))
1082 (with-foreign-pointer (buf +cstring-path-max
+ bufsize
)
1083 (with-foreign-slots ((name passwd gid
) gr group-entry
)
1084 (funcall fn arg gr buf bufsize grp
)
1085 (if (null-pointer-p (mem-ref grp
:pointer
))
1087 (values name passwd gid
))))))
1089 (defentrypoint getgrgid
(gid)
1090 "Gets a group-entry, by group id (reentrant)."
1091 (funcall-getgr #'%getgrgid-r gid
))
1093 (defentrypoint getgrnam
(name)
1094 "Gets a group-entry, by group name (reentrant)."
1095 (funcall-getgr #'%getgrnam-r name
))