1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
3 ;;; --- *UNIX foreign function definitions.
6 (in-package :iolib.syscalls
)
8 (eval-when (:compile-toplevel
)
9 (declaim (optimize (speed 3) (safety 1) (debug 1))))
11 ;; FIXME: move this into an ASDF operation
12 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
13 (define-foreign-library libfixposix
14 (t (:default
"libfixposix")))
15 (use-foreign-library libfixposix
))
19 ;;;-------------------------------------------------------------------------
20 ;;; ERRNO-related functions
21 ;;;-------------------------------------------------------------------------
23 (defcfun (errno "lfp_errno") :int
)
25 (defun (setf errno
) (value)
26 (foreign-funcall "lfp_set_errno" :int value
:int
))
28 (defsyscall (%strerror
"lfp_strerror")
34 (defentrypoint strerror
(&optional
(err (errno)))
35 "Look up the error message string for ERRNO (reentrant)."
38 (foreign-enum-value 'errno-values err
)
40 (with-foreign-pointer-as-string ((buf bufsiz
) 1024)
41 (%strerror errno buf bufsiz
))))
43 (defmethod print-object ((e syscall-error
) s
)
44 (with-slots (syscall code identifier message handle handle2
) e
45 (print-unreadable-object (e s
:type nil
:identity nil
)
48 (format s
"~A" message
))
50 (format s
"Syscall ~S signalled error ~A(~S) ~S"
51 syscall identifier
(or code
"[No code]")
52 (or (strerror code
) "[Can't get error string.]"))
53 (when handle
(format s
" FD=~A" handle
))
54 (when handle2
(format s
" FD2=~A" handle2
)))))))
57 ;;;-------------------------------------------------------------------------
58 ;;; Memory manipulation
59 ;;;-------------------------------------------------------------------------
61 (defcfun (memset "memset") :pointer
62 "Fill the first COUNT bytes of BUFFER with the constant VALUE."
67 (defentrypoint bzero
(buffer count
)
68 "Fill the first COUNT bytes of BUFFER with zeros."
69 (memset buffer
0 count
))
71 (defcfun (memcpy "memcpy") :pointer
72 "Copy COUNT octets from SRC to DEST.
73 The two memory areas must not overlap."
78 (defcfun (memmove "memmove") :pointer
79 "Copy COUNT octets from SRC to DEST.
80 The two memory areas may overlap."
86 ;;;-------------------------------------------------------------------------
88 ;;;-------------------------------------------------------------------------
90 (defsyscall (%open
"lfp_open")
96 (defentrypoint open
(path flags
&optional
(mode #o666
))
97 "Open a file descriptor for PATH using FLAGS and permissions MODE(#o666 by default)."
98 (%open path flags mode
))
100 (defsyscall (creat "lfp_creat")
102 "Create file PATH with permissions MODE and return the new FD."
106 (defsyscall (%pipe
"pipe") :int
109 (defentrypoint pipe
()
110 "Create pipe, returns two values with the new FDs."
111 (with-foreign-object (fds :int
2)
113 (values (mem-aref fds
:int
0)
114 (mem-aref fds
:int
1))))
116 (defsyscall (mkfifo "mkfifo") :int
117 "Create a FIFO (named pipe) with name PATH and permissions MODE."
121 (defsyscall (umask "umask") mode-t
122 "Sets the umask to NEW-MODE and returns the old one."
125 (defsyscall (lseek "lfp_lseek")
127 "Reposition the offset of the open file associated with the file descriptor FD
128 to the argument OFFSET according to the directive WHENCE."
133 (defsyscall (access "access") :int
134 "Check whether the file PATH can be accessed using mode MODE."
138 (defsyscall (truncate "lfp_truncate")
140 "Truncate the file PATH to a size of precisely LENGTH octets."
144 (defsyscall (ftruncate "lfp_ftruncate")
145 (:int
:restart t
:handle fd
)
146 "Truncate the file referenced by FD to a size of precisely LENGTH octets."
150 (defsyscall (rename "rename") :int
151 "Rename file named by OLDPATH to NEWPATH."
155 (defsyscall (link "link") :int
156 "Create a hard link from file OLDPATH to NEWPATH."
160 (defsyscall (symlink "symlink") :int
161 "Create a symbolic link from file OLDPATH to NEWPATH."
165 (defsyscall (%readlink
"readlink") ssize-t
170 (defentrypoint readlink
(path)
171 "Read the file name pointed by the symbolic link PATH."
172 (with-foreign-pointer (buf +cstring-path-max
+ bufsize
)
173 (let ((count (%readlink path buf bufsize
)))
174 (cstring-to-sstring buf count
))))
176 (defsyscall (%realpath
"realpath") sstring
178 (resolved-path :pointer
))
180 (defentrypoint realpath
(path)
181 "Read the file name pointed by the symbolic link PATH."
182 (with-foreign-pointer (buf +cstring-path-max
+)
183 (%realpath path buf
)))
185 (defsyscall (unlink "unlink") :int
186 "Delete the file PATH from the file system."
189 (defsyscall (chown "chown")
191 "Change ownership of file PATH to uid OWNER and gid GROUP(dereferences symlinks)."
196 (defsyscall (fchown "fchown")
197 (:int
:restart t
:handle fd
)
198 "Change ownership of an open file referenced by FD to uid OWNER and gid GROUP."
203 (defsyscall (lchown "lchown")
205 "Change ownership of a file PATH to uid OWNER and gid GROUP(does not dereference symlinks)."
210 (defsyscall (chmod "chmod")
212 "Change permissions of file PATH to mode MODE."
216 (defsyscall (fchmod "fchmod")
217 (:int
:restart t
:handle fd
)
218 "Change permissions of open file referenced by FD to mode MODE."
223 ;;;-------------------------------------------------------------------------
225 ;;;-------------------------------------------------------------------------
227 (defsyscall (read "read")
228 (ssize-t :restart t
:handle fd
)
229 "Read at most COUNT bytes from FD into the foreign area BUF."
234 (defsyscall (write "write")
235 (ssize-t :restart t
:handle fd
)
236 "Write at most COUNT bytes to FD from the foreign area BUF."
241 (defsyscall (readv "readv")
242 (ssize-t :restart t
:handle fd
)
243 "Read from FD into the first IOVCNT buffers of the IOV array."
248 (defsyscall (writev "writev")
249 (ssize-t :restart t
:handle fd
)
250 "Writes to FD the first IOVCNT buffers of the IOV array."
255 (defsyscall (pread "lfp_pread")
256 (ssize-t :restart t
:handle fd
)
257 "Read at most COUNT bytes from FD at offset OFFSET into the foreign area BUF."
263 (defsyscall (pwrite "lfp_pwrite")
264 (ssize-t :restart t
:handle fd
)
265 "Write at most COUNT bytes to FD at offset OFFSET from the foreign area BUF."
271 (defsyscall (sendfile "lfp_sendfile")
272 (ssize-t :restart t
:handle infd
:handle2 outfd
)
279 ;;;-------------------------------------------------------------------------
281 ;;;-------------------------------------------------------------------------
283 (define-c-struct-wrapper stat
())
285 (defsyscall (%stat
"lfp_stat")
290 (defsyscall (%fstat
"lfp_fstat")
295 (defsyscall (%lstat
"lfp_lstat")
300 ;;; If necessary for performance reasons, we can add an optional
301 ;;; argument to this function and use that to reuse a wrapper object.
302 (defentrypoint funcall-stat
(fn arg
)
303 (with-foreign-object (buf 'stat
)
305 (make-instance 'stat
:pointer buf
)))
307 (defentrypoint stat
(path)
308 "Get information about file PATH(dereferences symlinks)."
309 (funcall-stat #'%stat path
))
311 (defentrypoint fstat
(fd)
312 "Get information about file descriptor FD."
313 (funcall-stat #'%fstat fd
))
315 (defentrypoint lstat
(path)
316 "Get information about file PATH(does not dereference symlinks)."
317 (funcall-stat #'%lstat path
))
319 (defsyscall (sync "sync") :void
320 "Schedule all file system buffers to be written to disk.")
322 (defsyscall (fsync "fsync")
324 "Schedule a file's buffers to be written to disk."
327 (defsyscall (%mkstemp
"lfp_mkstemp") :int
330 (defentrypoint mkstemp
(&optional
(template ""))
331 "Generate a unique temporary filename from TEMPLATE.
332 Return two values: the file descriptor and the path of the temporary file."
333 (let ((template (concatenate 'string template
"XXXXXX")))
334 (with-sstring-to-cstring (ptr template
)
335 (values (%mkstemp ptr
) (cstring-to-sstring ptr
)))))
338 ;;;-------------------------------------------------------------------------
340 ;;;-------------------------------------------------------------------------
342 (defsyscall (mkdir "mkdir") :int
343 "Create directory PATH with permissions MODE."
347 (defsyscall (rmdir "rmdir") :int
348 "Delete directory PATH."
351 (defsyscall (chdir "chdir") :int
352 "Change the current working directory to PATH."
355 (defsyscall (fchdir "fchdir")
356 (:int
:restart t
:handle fd
)
357 "Change the current working directory to the directory referenced by FD."
360 (defsyscall (%getcwd
"getcwd") :pointer
364 (defentrypoint getcwd
()
365 "Return the current working directory as a string."
366 (with-cstring-to-sstring (buf +cstring-path-max
+ bufsize
)
367 (%getcwd buf bufsize
)))
369 (defsyscall (%mkdtemp
"mkdtemp") sstring
372 (defentrypoint mkdtemp
(&optional
(template ""))
373 "Generate a unique temporary filename from TEMPLATE."
374 (let ((template (concatenate 'string template
"XXXXXX")))
375 (%mkdtemp template
)))
378 ;;;-------------------------------------------------------------------------
380 ;;;-------------------------------------------------------------------------
382 (defsyscall (close "close")
384 "Close open file descriptor FD."
387 (defsyscall (dup "dup")
389 "Duplicate file descriptor FD."
392 (defsyscall (dup2 "dup2")
393 (:int
:restart t
:handle oldfd
:handle2 newfd
)
394 "Make NEWFD be the copy of OLDFD, closing NEWFD first if necessary."
398 (defsyscall (%fcntl
/noarg
"fcntl")
403 ;;; FIXME: Linux/glibc says ARG's type is long, POSIX says it's int.
404 ;;; Is this an issue?
405 (defsyscall (%fcntl
/int
"fcntl")
411 (defsyscall (%fcntl
/pointer
"fcntl")
417 (defentrypoint fcntl
(fd cmd
&optional
(arg nil argp
))
419 ((not argp
) (%fcntl
/noarg fd cmd
))
420 ((integerp arg
) (%fcntl
/int fd cmd arg
))
421 ((pointerp arg
) (%fcntl
/pointer fd cmd arg
))
422 (t (error 'type-error
:datum arg
423 :expected-type
'(or null integer foreign-pointer
)))))
425 (defsyscall (%ioctl
/noarg
"ioctl")
427 "Send request REQUEST to file referenced by FD."
429 (request :unsigned-int
))
431 (defsyscall (%ioctl
/pointer
"ioctl")
433 "Send request REQUEST to file referenced by FD using argument ARG."
435 (request :unsigned-int
)
438 (defentrypoint ioctl
(fd request
&optional
(arg nil argp
))
439 "Control an I/O device."
441 ((not argp
) (%ioctl
/noarg fd request
))
442 ((pointerp arg
) (%ioctl
/pointer fd request arg
))
443 (t (error 'type-error
:datum arg
444 :expected-type
'(or null foreign-pointer
)))))
446 (defsyscall (fd-cloexec-p "lfp_is_fd_cloexec") bool-designator
449 (defsyscall (%set-fd-cloexec
"lfp_set_fd_cloexec") :int
451 (enabled bool-designator
))
453 (defentrypoint (setf fd-cloexec-p
) (enabled fd
)
454 (%set-fd-cloexec fd enabled
))
456 (defsyscall (fd-nonblock-p "lfp_is_fd_nonblock") bool-designator
459 (defsyscall (%set-fd-nonblock
"lfp_set_fd_nonblock") :int
461 (enabled bool-designator
))
463 (defentrypoint (setf fd-nonblock-p
) (enabled fd
)
464 (%set-fd-nonblock fd enabled
))
466 (defsyscall (fd-open-p "lfp_is_fd_open") bool-designator
470 ;;;-------------------------------------------------------------------------
472 ;;;-------------------------------------------------------------------------
474 (defsyscall (openpt "lfp_openpt") :int
477 (defsyscall (grantpt "grantpt")
481 (defsyscall (unlockpt "unlockpt")
485 (defsyscall (%ptsname
"lfp_ptsname")
491 (defentrypoint ptsname
(fd)
492 (with-foreign-pointer (buf +cstring-path-max
+ bufsize
)
493 (%ptsname fd buf bufsize
)
494 (nth-value 0 (foreign-string-to-lisp buf
))))
497 ;;;-------------------------------------------------------------------------
499 ;;;-------------------------------------------------------------------------
501 (defsyscall (select "lfp_select") :int
502 "Scan for I/O activity on multiple file descriptors."
509 (defentrypoint copy-fd-set
(from to
)
510 (memcpy to from
(sizeof 'fd-set
))
513 (defcfun (fd-clr "lfp_fd_clr") :void
517 (defcfun (fd-isset "lfp_fd_isset") bool
521 (defcfun (fd-set "lfp_fd_set") :void
525 (defcfun (fd-zero "lfp_fd_zero") :void
528 ;;; FIXME: Until a way to autodetect platform features is implemented
529 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
530 (unless (boundp 'pollrdhup
)
531 (defconstant pollrdhup
0)))
533 (defsyscall (poll "poll") :int
534 "Scan for I/O activity on multiple file descriptors."
541 (defsyscall (epoll-create "epoll_create") :int
542 "Open an epoll file descriptor."
545 (defsyscall (epoll-ctl "epoll_ctl")
546 (:int
:handle epfd
:handle2 fd
)
547 "Control interface for an epoll descriptor."
553 (defsyscall (epoll-wait "epoll_wait")
555 "Wait for an I/O event on an epoll file descriptor."
563 (defsyscall (kqueue "kqueue") :int
564 "Open a kernel event queue.")
566 (defsyscall (kevent "kevent")
568 "Control interface for a kernel event queue."
570 (changelist :pointer
) ; const struct kevent *
572 (eventlist :pointer
) ; struct kevent *
574 (timeout :pointer
)) ; const struct timespec *
576 (defentrypoint ev-set
(%kev %ident %filter %flags %fflags %data %udata
)
577 (with-foreign-slots ((ident filter flags fflags data udata
) %kev kevent
)
578 (setf ident %ident filter %filter flags %flags
579 fflags %fflags data %data udata %udata
))))
582 ;;;-------------------------------------------------------------------------
583 ;;; Socket message readers
584 ;;;-------------------------------------------------------------------------
586 (defcfun (cmsg.firsthdr
"lfp_cmsg_firsthdr") :pointer
589 (defcfun (cmsg.nxthdr
"lfp_cmsg_nxthdr") :pointer
593 (defcfun (cmsg.space
"lfp_cmsg_space") size-t
596 (defcfun (cmsg.len
"lfp_cmsg_len") size-t
599 (defcfun (cmsg.data
"lfp_cmsg_data") :pointer
603 ;;;-------------------------------------------------------------------------
604 ;;; Directory walking
605 ;;;-------------------------------------------------------------------------
607 (defsyscall (opendir "opendir") :pointer
608 "Open directory PATH for listing of its contents."
611 (defsyscall (closedir "closedir") :int
612 "Close directory DIR when done listing its contents."
615 (defsyscall (%readdir
"lfp_readdir") :int
620 (defentrypoint readdir
(dir)
621 "Reads an item from the listing of directory DIR (reentrant)."
622 (with-foreign-objects ((entry 'dirent
) (result :pointer
))
623 (%readdir dir entry result
)
624 (if (null-pointer-p (mem-ref result
:pointer
))
626 (with-foreign-slots ((name type fileno
) entry dirent
)
627 (values (cstring-to-sstring name
) type fileno
)))))
629 (defsyscall (rewinddir "rewinddir") :void
630 "Rewind directory DIR."
633 (defsyscall (seekdir "seekdir") :void
634 "Seek into directory DIR to position POS(as returned by TELLDIR)."
638 ;;; FIXME: According to POSIX docs "no errors are defined" for
639 ;;; telldir() but Linux manpages specify a possible EBADF.
640 (defsyscall (telldir "telldir") off-t
641 "Return the current location in directory DIR."
645 ;;;-------------------------------------------------------------------------
647 ;;;-------------------------------------------------------------------------
649 (defsyscall (mmap "lfp_mmap")
650 (:pointer
:handle fd
)
651 "Map file referenced by FD at offset OFFSET into address space of the
652 calling process at address ADDR and length LENGTH.
653 PROT describes the desired memory protection of the mapping.
654 FLAGS determines whether updates to the mapping are visible to other
655 processes mapping the same region."
663 (defsyscall (munmap "munmap") :int
664 "Unmap pages of memory starting at address ADDR with length LENGTH."
669 ;;;-------------------------------------------------------------------------
670 ;;; Process creation and info
671 ;;;-------------------------------------------------------------------------
673 (defsyscall (fork "fork") pid-t
)
675 (defsyscall (execv "execv") :int
679 (defsyscall (%waitpid
"waitpid") pid-t
684 (defentrypoint waitpid
(pid options
)
685 (with-foreign-pointer (status (sizeof :int
))
686 (let ((ret (%waitpid pid status options
)))
687 (values ret
(mem-ref status
:int
)))))
689 (defsyscall (getpid "getpid") pid-t
690 "Returns the process id of the current process")
692 (defsyscall (getppid "getppid") pid-t
693 "Returns the process id of the current process's parent")
696 (defentrypoint gettid
()
697 (foreign-funcall "syscall" :int sys-gettid
:int
))
699 (defsyscall (getuid "getuid") uid-t
700 "Get real user id of the current process.")
702 (defsyscall (setuid "setuid") :int
703 "Set real user id of the current process to UID."
706 (defsyscall (geteuid "geteuid") uid-t
707 "Get effective user id of the current process.")
709 (defsyscall (seteuid "seteuid") :int
710 "Set effective user id of the current process to UID."
713 (defsyscall (getgid "getgid") gid-t
714 "Get real group id of the current process.")
716 (defsyscall (setgid "setgid") :int
717 "Set real group id of the current process to GID."
720 (defsyscall (getegid "getegid") gid-t
721 "Get effective group id of the current process.")
723 (defsyscall (setegid "setegid") :int
724 "Set effective group id of the current process to GID."
727 (defsyscall (setreuid "setreuid") :int
728 "Set real and effective user id of the current process to RUID and EUID."
732 (defsyscall (setregid "setregid") :int
733 "Set real and effective group id of the current process to RGID and EGID."
737 (defsyscall (getpgid "getpgid") pid-t
738 "Get process group id of process PID."
741 (defsyscall (setpgid "setpgid") :int
742 "Set process group id of process PID to value PGID."
746 (defsyscall (getpgrp "getpgrp") pid-t
747 "Get process group id of the current process.")
749 (defsyscall (setpgrp "setpgrp") pid-t
750 "Set process group id of the current process.")
752 (defsyscall (setsid "setsid") pid-t
753 "Create session and set process group id of the current process.")
755 (defsyscall (%getrlimit
"lfp_getrlimit")
760 (defentrypoint getrlimit
(resource)
761 "Return soft and hard limit of system resource RESOURCE."
762 (with-foreign-object (rl 'rlimit
)
763 (with-foreign-slots ((cur max
) rl rlimit
)
764 (%getrlimit resource rl
)
767 (defsyscall (%setrlimit
"lfp_setrlimit")
772 (defentrypoint setrlimit
(resource soft-limit hard-limit
)
773 "Set SOFT-LIMIT and HARD-LIMIT of system resource RESOURCE."
774 (with-foreign-object (rl 'rlimit
)
775 (with-foreign-slots ((cur max
) rl rlimit
)
778 (%setrlimit resource rl
))))
780 (defsyscall (%getrusage
"getrusage") :int
784 ;;; TODO: it might be more convenient to return a wrapper object here
785 ;;; instead like we do in STAT.
786 (defentrypoint getrusage
(who)
787 "Return resource usage measures of WHO."
788 (with-foreign-object (ru 'rusage
)
790 (with-foreign-slots ((maxrss ixrss idrss isrss minflt majflt nswap inblock
791 oublock msgsnd msgrcv nsignals nvcsw nivcsw
)
793 (values (foreign-slot-value (foreign-slot-pointer ru
'rusage
'utime
)
795 (foreign-slot-value (foreign-slot-pointer ru
'rusage
'utime
)
797 (foreign-slot-value (foreign-slot-pointer ru
'rusage
'stime
)
799 (foreign-slot-value (foreign-slot-pointer ru
'rusage
'stime
)
801 maxrss ixrss idrss isrss minflt majflt
802 nswap inblock oublock msgsnd
803 msgrcv nsignals nvcsw nivcsw
))))
805 (defsyscall (getpriority "getpriority") :int
806 "Get the scheduling priority of a process, process group, or user,
807 as indicated by WHICH and WHO."
811 (defsyscall (setpriority "setpriority") :int
812 "Set the scheduling priority of a process, process group, or user,
813 as indicated by WHICH and WHO to VALUE."
818 (defentrypoint nice
(&optional
(increment 0))
819 "Get or set process priority."
820 ;; FIXME: race condition. might need WITHOUT-INTERRUPTS on some impl.s
822 (let ((retval (foreign-funcall "nice" :int increment
:int
))
824 (if (and (= retval -
1) (/= errno
0))
825 (signal-syscall-error errno
"nice")
828 (defsyscall (exit "_exit") :void
829 "terminate the calling process"
834 ;;;-------------------------------------------------------------------------
836 ;;;-------------------------------------------------------------------------
838 (defsyscall (kill "kill") :int
839 "Send signal SIG to process PID."
843 (defsyscall (sigaction "sigaction") :int
848 (defentrypoint wifexited
(status)
849 (plusp (foreign-funcall "lfp_wifexited" :int status
:int
)))
851 (defentrypoint wexitstatus
(status)
852 (foreign-funcall "lfp_wexitstatus" :int status
:int
))
854 (defentrypoint wifsignaled
(status)
855 (plusp (foreign-funcall "lfp_wifsignaled" :int status
:int
)))
857 (defentrypoint wtermsig
(status)
858 (foreign-funcall "lfp_wtermsig" :int status
:int
))
860 (defentrypoint wtermsig
* (status)
861 (foreign-enum-keyword 'signal
(wtermsig status
)))
863 (defentrypoint wcoredump
(status)
864 (plusp (foreign-funcall "lfp_wcoredump" :int status
:int
)))
866 (defentrypoint wifstopped
(status)
867 (plusp (foreign-funcall "lfp_wifstopped" :int status
:int
)))
869 (defentrypoint wstopsig
(status)
870 (foreign-funcall "lfp_wstopsig" :int status
:int
))
872 (defentrypoint wifcontinued
(status)
873 (plusp (foreign-funcall "lfp_wifcontinued" :int status
:int
)))
876 ;;;-------------------------------------------------------------------------
878 ;;;-------------------------------------------------------------------------
880 (defsyscall (usleep "usleep") :int
881 "Suspend execution for USECONDS microseconds."
882 (useconds useconds-t
))
884 (defsyscall (%clock-getres
"lfp_clock_getres") :int
885 "Returns the resolution of the clock CLOCKID."
889 (defentrypoint clock-getres
(clock-id)
890 (with-foreign-object (ts 'timespec
)
891 (with-foreign-slots ((sec nsec
) ts timespec
)
892 (%clock-getres clock-id ts
)
895 (defsyscall (%clock-gettime
"lfp_clock_gettime") :int
899 (defentrypoint clock-gettime
(clock-id)
900 "Returns the time of the clock CLOCKID."
901 (with-foreign-object (ts 'timespec
)
902 (with-foreign-slots ((sec nsec
) ts timespec
)
903 (%clock-gettime clock-id ts
)
906 (defsyscall (%clock-settime
"lfp_clock_settime") :int
910 (defentrypoint clock-settime
(clock-id)
911 "Sets the time of the clock CLOCKID."
912 (with-foreign-object (ts 'timespec
)
913 (with-foreign-slots ((sec nsec
) ts timespec
)
914 (%clock-settime clock-id ts
)
917 ;; FIXME: replace it with clock_gettime(CLOCK_MONOTONIC, ...)
918 (defentrypoint get-monotonic-time
()
919 "Gets current time in seconds from a system's monotonic clock."
920 (multiple-value-bind (seconds nanoseconds
)
921 (clock-gettime clock-monotonic
)
922 (+ seconds
(/ nanoseconds
1d9
))))
925 ;;;-------------------------------------------------------------------------
927 ;;;-------------------------------------------------------------------------
929 (defsyscall (os-environ "lfp_get_environ") :pointer
930 "Return a pointer to the current process environment.")
932 (defmacro %obsolete-
*environ
* ()
933 (iolib.base
::signal-obsolete
'*environ
* "use function OS-ENVIRON instead"
934 "symbol macro" :WARN
)
937 (define-symbol-macro *environ
* (%obsolete-
*environ
*))
939 (defentrypoint getenv
(name)
940 "Returns the value of environment variable NAME."
941 (when (and (pointerp name
) (null-pointer-p name
))
942 (setf (errno) einval
)
943 (signal-syscall-error einval
"getenv"))
944 (foreign-funcall "getenv" :string name
:string
))
946 (defsyscall (setenv "setenv") :int
947 "Changes the value of environment variable NAME to VALUE.
948 The environment variable is overwritten only if overwrite is not NIL."
951 (overwrite bool-designator
))
953 (defsyscall (unsetenv "unsetenv") :int
954 "Removes the binding of environment variable NAME."
957 ;; FIXME: move into libfixposix
958 (defentrypoint clearenv
()
959 "Remove all name-value pairs from the environment set the
960 OS environment to NULL."
961 (let ((envptr (os-environ)))
962 (unless (null-pointer-p envptr
)
963 (loop :for i
:from
0 :by
1
964 :for string
:= (mem-aref envptr
:string i
)
965 :for name
:= (subseq string
0 (position #\
= string
))
966 :while name
:do
(unsetenv name
))
967 (setf (mem-ref envptr
:pointer
) (null-pointer)))
971 ;;;-------------------------------------------------------------------------
973 ;;;-------------------------------------------------------------------------
975 (defsyscall (%gethostname
"gethostname") :int
979 (defentrypoint gethostname
()
980 "Return the host name of the current machine."
981 (with-foreign-pointer-as-string ((cstr size
) 256)
982 (%gethostname cstr size
)))
984 (defsyscall (%getdomainname
"getdomainname") :int
988 (defentrypoint getdomainname
()
989 "Return the domain name of the current machine."
990 (with-foreign-pointer-as-string ((cstr size
) 256)
991 (%getdomainname cstr size
)))
993 (defsyscall (%uname
"uname") :int
996 (defentrypoint uname
()
997 "Get name and information about current kernel."
998 (with-foreign-object (buf 'utsname
)
999 (bzero buf
(sizeof 'utsname
))
1001 (macrolet ((utsname-slot (name)
1002 `(foreign-string-to-lisp
1003 (foreign-slot-pointer buf
'utsname
',name
))))
1004 (values (utsname-slot sysname
)
1005 (utsname-slot nodename
)
1006 (utsname-slot release
)
1007 (utsname-slot version
)
1008 (utsname-slot machine
)))))
1011 ;;;-------------------------------------------------------------------------
1013 ;;;-------------------------------------------------------------------------
1015 (defsyscall (%getpwuid-r
"getpwuid_r")
1017 :error-predicate plusp
1018 :error-location
:return
)
1025 (defsyscall (%getpwnam-r
"getpwnam_r")
1027 :error-predicate plusp
1028 :error-location
:return
)
1035 (defun funcall-getpw (fn arg
)
1036 (with-foreign-objects ((pw 'passwd
) (pwp :pointer
))
1037 (with-foreign-pointer (buf +cstring-path-max
+ bufsize
)
1038 (with-foreign-slots ((name passwd uid gid gecos dir shell
) pw passwd
)
1039 (funcall fn arg pw buf bufsize pwp
)
1040 (if (null-pointer-p (mem-ref pwp
:pointer
))
1042 (values name passwd uid gid gecos dir shell
))))))
1044 (defentrypoint getpwuid
(uid)
1045 "Gets the passwd info of a user, by user id (reentrant)."
1046 (funcall-getpw #'%getpwuid-r uid
))
1048 (defentrypoint getpwnam
(name)
1049 "Gets the passwd info of a user, by username (reentrant)."
1050 (funcall-getpw #'%getpwnam-r name
))
1053 ;;;-------------------------------------------------------------------------
1055 ;;;-------------------------------------------------------------------------
1057 (defsyscall (%getgrgid-r
"getgrgid_r")
1059 :error-predicate plusp
1060 :error-location
:return
)
1067 (defsyscall (%getgrnam-r
"getgrnam_r")
1069 :error-predicate plusp
1070 :error-location
:return
)
1077 ;; FIXME: return group members too
1078 (defun funcall-getgr (fn arg
)
1079 (with-foreign-objects ((gr 'group
) (grp :pointer
))
1080 (with-foreign-pointer (buf +cstring-path-max
+ bufsize
)
1081 (with-foreign-slots ((name passwd gid
) gr group
)
1082 (funcall fn arg gr buf bufsize grp
)
1083 (if (null-pointer-p (mem-ref grp
:pointer
))
1085 (values name passwd gid
))))))
1087 (defentrypoint getgrgid
(gid)
1088 "Gets a group info, by group id (reentrant)."
1089 (funcall-getgr #'%getgrgid-r gid
))
1091 (defentrypoint getgrnam
(name)
1092 "Gets a group info, by group name (reentrant)."
1093 (funcall-getgr #'%getgrnam-r name
))
1096 ;;;-------------------------------------------------------------------------
1098 ;;;-------------------------------------------------------------------------
1100 (defsyscall (openlog "lfp_openlog") :void
1101 "Opens a connection to the system logger for a program."
1106 (defsyscall (%syslog
"lfp_syslog") :void
1107 "Generates a log message, which will be distributed by syslogd."
1112 (defentrypoint syslog
(priority format
&rest args
)
1113 "Generates a log message, which will be distributed by syslogd.
1114 Using a FORMAT string and ARGS for lisp-side message formating."
1115 (with-foreign-string (c-string (apply #'format nil format args
))
1116 (%syslog priority
"%s" c-string
)))
1118 (defsyscall (closelog "lfp_closelog") :void
1119 "Closes the descriptor being used to write to the system logger (optional).")
1121 (defsyscall (setlogmask "lfp_setlogmask") :int
1122 "Set the log mask level."
1125 (defsyscall (log-mask "lfp_log_mask") :int
1126 "Log mask corresponding to PRIORITY."
1129 (defsyscall (log-upto "lfp_log_upto") :int
1130 "Log mask upto and including PRIORITY."
1133 (defmacro with-syslog
((identity &key
(options log-ndelay
) (facility log-daemon
))
1137 (openlog ,identity
,options
,facility
)