Derive that make-array with a :fill-pointer produces a vector.
[sbcl.git] / src / code / unix.lisp
blob3bbcd213f63f16fcd0119524da44ec2a26ac4bb1
1 ;;;; This file contains Unix support that SBCL needs to implement
2 ;;;; itself. It's derived from Peter Van Eynde's unix-glibc2.lisp for
3 ;;;; CMU CL, which was derived from CMU CL unix.lisp 1.56. But those
4 ;;;; files aspired to be complete Unix interfaces exported to the end
5 ;;;; user, while this file aims to be as simple as possible and is not
6 ;;;; intended for the end user.
7 ;;;;
8 ;;;; FIXME: The old CMU CL unix.lisp code was implemented as hand
9 ;;;; transcriptions from Unix headers into Lisp. It appears that this was as
10 ;;;; unmaintainable in practice as you'd expect in theory, so I really really
11 ;;;; don't want to do that. It'd be good to implement the various system calls
12 ;;;; as C code implemented using the Unix header files, and have their
13 ;;;; interface back to SBCL code be characterized by things like "32-bit-wide
14 ;;;; int" which are already in the interface between the runtime
15 ;;;; executable and the SBCL lisp code.
17 ;;;; This software is part of the SBCL system. See the README file for
18 ;;;; more information.
19 ;;;;
20 ;;;; This software is derived from the CMU CL system, which was
21 ;;;; written at Carnegie Mellon University and released into the
22 ;;;; public domain. The software is in the public domain and is
23 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
24 ;;;; files for more information.
26 (in-package "SB-UNIX")
28 (/show0 "unix.lisp 21")
30 ;;; Given a C-level zero-terminated array of C strings, return a
31 ;;; corresponding Lisp-level list of SIMPLE-STRINGs.
32 (defun c-strings->string-list (c-strings)
33 (declare (type (alien (* c-string)) c-strings))
34 (let ((reversed-result nil))
35 (dotimes (i most-positive-fixnum)
36 (declare (type index i))
37 (let ((c-string (deref c-strings i)))
38 (if c-string
39 (push c-string reversed-result)
40 (return (nreverse reversed-result)))))))
42 ;;;; Lisp types used by syscalls
44 (deftype unix-pathname () 'simple-string)
45 (deftype unix-fd () `(integer 0 ,most-positive-fixnum))
47 (deftype unix-file-mode () '(unsigned-byte 32))
48 (deftype unix-pid () '(unsigned-byte 32))
49 (deftype unix-uid () '(unsigned-byte 32))
50 (deftype unix-gid () '(unsigned-byte 32))
52 ;;;; system calls
54 (/show0 "unix.lisp 74")
56 (eval-when (:compile-toplevel :load-toplevel :execute)
57 (defun libc-name-for (x)
58 (assert (stringp x))
59 ;; This function takes a possibly-wrapped C name and strips off "sb_"
60 ;; if it doesn't need a wrapper. The list of functions that can be
61 ;; called directly is listed explicitly, because there are also others
62 ;; that might want to be wrapped even if they don't need to be,
63 ;; like sb_opendir and sb_closedir. Why are those wrapped in fact?
64 #+netbsd x
65 #-netbsd (if (member x '("sb_getrusage" ; syscall*
66 "sb_gettimeofday" ;syscall*
67 "sb_select" ; int-syscall
68 "sb_getitimer" ; syscall*
69 "sb_setitimer" ; syscall*
70 "sb_clock_gettime" ; alien-funcall
71 "sb_utimes") ; posix
72 :test #'string=)
73 (subseq x 3)
74 x)))
76 (defmacro syscall ((name &rest arg-types) success-form &rest args)
77 (when (eql 3 (mismatch "[_]" name))
78 (setf name
79 (concatenate 'string #+win32 "_" (subseq name 3))))
80 `(locally
81 (declare (optimize (sb-c::float-accuracy 0)))
82 (let ((result (alien-funcall (extern-alien ,(libc-name-for name)
83 (function int ,@arg-types))
84 ,@args)))
85 (if (minusp result)
86 (values nil (get-errno))
87 ,success-form))))
89 ;;; This is like SYSCALL, but if it fails, signal an error instead of
90 ;;; returning error codes. Should only be used for syscalls that will
91 ;;; never really get an error.
92 (defmacro syscall* ((name &rest arg-types) success-form &rest args)
93 `(locally
94 (declare (optimize (sb-c::float-accuracy 0)))
95 (let ((result (alien-funcall (extern-alien ,(libc-name-for name)
96 (function int ,@arg-types))
97 ,@args)))
98 (if (minusp result)
99 (error "Syscall ~A failed: ~A" ,name (strerror))
100 ,success-form))))
102 (defmacro int-syscall ((name &rest arg-types) &rest args)
103 `(syscall (,(libc-name-for name) ,@arg-types) (values result 0) ,@args))
105 (defmacro with-restarted-syscall ((&optional (value (gensym))
106 (errno (gensym)))
107 syscall-form &rest body)
108 "Evaluate BODY with VALUE and ERRNO bound to the return values of
109 SYSCALL-FORM. Repeat evaluation of SYSCALL-FORM if it is interrupted."
110 `(let (,value ,errno)
111 (loop (multiple-value-setq (,value ,errno)
112 ,syscall-form)
113 (unless #-win32 (eql ,errno eintr) #+win32 nil
114 (return (values ,value ,errno))))
115 ,@body))
117 (defmacro void-syscall ((name &rest arg-types) &rest args)
118 `(syscall (,name ,@arg-types) (values t 0) ,@args))
120 #+win32
121 (progn
122 (defconstant espipe 29))
124 ;;;; hacking the Unix environment
126 #-win32
127 (define-alien-routine ("getenv" posix-getenv) c-string
128 "Return the \"value\" part of the environment string \"name=value\" which
129 corresponds to NAME, or NIL if there is none."
130 (name (c-string :not-null t)))
132 ;;; from stdio.h
134 ;;; Rename the file with string NAME1 to the string NAME2. NIL and an
135 ;;; error code is returned if an error occurs.
136 #-win32
137 (defun unix-rename (name1 name2)
138 (declare (type unix-pathname name1 name2))
139 (void-syscall ("rename" (c-string :not-null t)
140 (c-string :not-null t))
141 name1 name2))
143 ;;; from sys/types.h and gnu/types.h
145 (/show0 "unix.lisp 220")
147 ;;; FIXME: We shouldn't hand-copy types from header files into Lisp
148 ;;; like this unless we have extreme provocation. Reading directories
149 ;;; is not extreme enough, since it doesn't need to be blindingly
150 ;;; fast: we can just implement those functions in C as a wrapper
151 ;;; layer.
152 (define-alien-type fd-mask unsigned)
154 (define-alien-type nil
155 (struct fd-set
156 (fds-bits (array fd-mask #.(/ fd-setsize
157 sb-vm:n-machine-word-bits)))))
159 (/show0 "unix.lisp 304")
162 ;;;; fcntl.h
163 ;;;;
164 ;;;; POSIX Standard: 6.5 File Control Operations <fcntl.h>
166 ;;; Open the file whose pathname is specified by PATH for reading
167 ;;; and/or writing as specified by the FLAGS argument. Various FLAGS
168 ;;; masks (O_RDONLY etc.) are defined in fcntlbits.h.
170 ;;; If the O_CREAT flag is specified, then the file is created with a
171 ;;; permission of argument MODE if the file doesn't exist. An integer
172 ;;; file descriptor is returned by UNIX-OPEN.
173 (defun unix-open (path flags mode &key #+win32 overlapped)
174 (declare (type unix-pathname path)
175 (type fixnum flags)
176 (type unix-file-mode mode)
177 #+win32
178 (ignore mode))
179 #+win32 (sb-win32:unixlike-open path flags :overlapped overlapped)
180 #-win32
181 (with-restarted-syscall (value errno)
182 (locally
183 (declare (optimize (sb-c::float-accuracy 0)))
184 (let ((result (alien-funcall (extern-alien "open" (function int c-string int &optional int))
185 path (logior flags
186 #+largefile o_largefile)
187 mode)))
188 (if (minusp result)
189 (values nil (get-errno))
190 (values result 0))))))
192 ;;; UNIX-CLOSE accepts a file descriptor and attempts to close the file
193 ;;; associated with it.
194 (/show0 "unix.lisp 391")
195 (defun unix-close (fd)
196 #+win32 (sb-win32:unixlike-close fd)
197 #-win32 (declare (type unix-fd fd))
198 #-win32 (void-syscall ("close" int) fd))
200 ;;;; stdlib.h
202 ;;; There are good reasons to implement some OPEN options with an
203 ;;; mkstemp(3)-like routine, but we don't do that yet. Instead, this
204 ;;; function is used only to make a temporary file for RUN-PROGRAM.
205 ;;; sb_mkstemp() is a wrapper that lives in src/runtime/wrap.c. Since
206 ;;; SUSv3 mkstemp() doesn't specify the mode of the created file and
207 ;;; since we have to implement most of this ourselves for Windows
208 ;;; anyway, it seems worthwhile to depart from the mkstemp()
209 ;;; specification by taking a mode to use when creating the new file.
210 (defun sb-mkstemp (template-string mode)
211 (declare (type string template-string)
212 (type unix-file-mode mode))
213 (let ((template-buffer (string-to-octets template-string :null-terminate t)))
214 (with-pinned-objects (template-buffer)
215 (let ((fd (alien-funcall (extern-alien "sb_mkstemp"
216 (function int (* char) int))
217 (vector-sap template-buffer)
218 mode)))
219 (if (minusp fd)
220 (values nil (get-errno))
221 (values #-win32 fd #+win32 (sb-win32::duplicate-and-unwrap-fd fd)
222 (octets-to-string template-buffer)))))))
224 ;;;; resourcebits.h
226 (defconstant rusage_self 0) ; the calling process
227 (defconstant rusage_children -1) ; terminated child processes
228 (defconstant rusage_both -2)
230 (define-alien-type nil
231 (struct rusage
232 (ru-utime (struct timeval)) ; user time used
233 (ru-stime (struct timeval)) ; system time used.
234 (ru-maxrss long) ; maximum resident set size (in kilobytes)
235 (ru-ixrss long) ; integral shared memory size
236 (ru-idrss long) ; integral unshared data size
237 (ru-isrss long) ; integral unshared stack size
238 (ru-minflt long) ; page reclaims
239 (ru-majflt long) ; page faults
240 (ru-nswap long) ; swaps
241 (ru-inblock long) ; block input operations
242 (ru-oublock long) ; block output operations
243 (ru-msgsnd long) ; messages sent
244 (ru-msgrcv long) ; messages received
245 (ru-nsignals long) ; signals received
246 (ru-nvcsw long) ; voluntary context switches
247 (ru-nivcsw long))) ; involuntary context switches
249 ;;;; unistd.h
251 ;;; Given a file path (a string) and one of four constant modes,
252 ;;; return T if the file is accessible with that mode and NIL if not.
253 ;;; When NIL, also return an errno value with NIL which tells why the
254 ;;; file was not accessible.
256 ;;; The access modes are:
257 ;;; r_ok Read permission.
258 ;;; w_ok Write permission.
259 ;;; x_ok Execute permission.
260 ;;; f_ok Presence of file.
262 ;;; In Windows, the MODE argument to access is defined in terms of
263 ;;; literal magic numbers---there are no constants to grovel. X_OK
264 ;;; is not defined.
265 #+win32
266 (progn
267 (defconstant f_ok 0)
268 (defconstant w_ok 2)
269 (defconstant r_ok 4))
271 (defun unix-access (path mode)
272 (declare (type unix-pathname path)
273 (type (mod 8) mode))
274 (void-syscall ("[_]access" c-string int) path mode))
276 ;;; values for the second argument to UNIX-LSEEK
277 ;;; Note that nowadays these are called SEEK_SET, SEEK_CUR, and SEEK_END
278 (defconstant l_set 0) ; to set the file pointer
279 (defconstant l_incr 1) ; to increment the file pointer
280 (defconstant l_xtnd 2) ; to extend the file size
282 ;; off_t is 32 bit on Windows, yet our functions support 64 bit seeks.
283 (define-alien-type unix-offset
284 #-win32 off-t
285 #+win32 (signed 64))
287 ;;; Is a stream interactive?
288 (defun unix-isatty (fd)
289 (declare (type unix-fd fd))
290 #-win32 (int-syscall ("isatty" int) fd)
291 #+win32 (sb-win32::windows-isatty fd))
293 (defun unix-lseek (fd offset whence)
294 "Unix-lseek accepts a file descriptor and moves the file pointer by
295 OFFSET octets. Whence can be any of the following:
297 L_SET Set the file pointer.
298 L_INCR Increment the file pointer.
299 L_XTND Extend the file size.
301 (declare (type unix-fd fd)
302 (type (integer 0 2) whence))
303 (let ((result
304 #-win32
305 (alien-funcall (extern-alien #-largefile "lseek"
306 #+largefile "lseek_largefile"
307 (function off-t int off-t int))
308 fd offset whence)
309 #+win32 (sb-win32:lseeki64 fd offset whence)))
310 (if (minusp result)
311 (values nil (get-errno))
312 (values result 0))))
314 ;;; UNIX-READ accepts a file descriptor, a buffer, and the length to read.
315 ;;; It attempts to read len bytes from the device associated with fd
316 ;;; and store them into the buffer. It returns the actual number of
317 ;;; bytes read.
319 (declaim (maybe-inline unix-read))
321 (defun unix-read (fd buf len)
322 (declare (type unix-fd fd)
323 (type (unsigned-byte 32) len))
324 (int-syscall (#-win32 "read" #+win32 "win32_unix_read"
325 int (* char) int) fd buf len))
327 ;;; UNIX-WRITE accepts a file descriptor, a buffer, an offset, and the
328 ;;; length to write. It attempts to write len bytes to the device
329 ;;; associated with fd from the buffer starting at offset. It returns
330 ;;; the actual number of bytes written.
331 (defun unix-write (fd buf offset len)
332 ;; KLUDGE: change 60fa88b187e438cc made this function unusable in cold-init
333 ;; if compiled with #+sb-show (which increases DEBUG to 2) because of
334 ;; full calls to SB-ALIEN-INTERNALS:DEPORT-ALLOC and DEPORT.
335 (declare (optimize (debug 1)))
336 (declare (type unix-fd fd)
337 (type (unsigned-byte 32) offset len))
338 (flet ((%write (sap)
339 (declare (system-area-pointer sap))
340 (int-syscall (#-win32 "write" #+win32 "win32_unix_write"
341 int (* char) int)
343 (with-alien ((ptr (* char) sap))
344 (addr (deref ptr offset)))
345 len)))
346 (etypecase buf
347 ((simple-array * (*))
348 (with-pinned-objects (buf)
349 (%write (vector-sap buf))))
350 (system-area-pointer
351 (%write buf)))))
353 ;;; Set up a unix-piping mechanism consisting of an input pipe and an
354 ;;; output pipe. Return two values: if no error occurred the first
355 ;;; value is the pipe to be read from and the second is can be written
356 ;;; to. If an error occurred the first value is NIL and the second the
357 ;;; unix error code.
358 #-win32
359 (defun unix-pipe ()
360 (with-alien ((fds (array int 2)))
361 (syscall ("pipe" (* int))
362 (values (deref fds 0) (deref fds 1))
363 (cast fds (* int)))))
365 #+win32
366 (defun unix-pipe ()
367 (sb-win32::windows-pipe))
369 ;; Windows mkdir() doesn't take the mode argument. It's cdecl, so we could
370 ;; actually call it passing the mode argument, but some sharp-eyed reader
371 ;; would put five and twenty-seven together and ask us about it, so...
372 ;; -- AB, 2005-12-27
373 #-win32
374 (defun unix-mkdir (name mode)
375 (declare (type unix-pathname name)
376 (type unix-file-mode mode))
377 (void-syscall ("mkdir" c-string int) name mode))
379 ;;; Given a C char* pointer allocated by malloc(), free it and return a
380 ;;; corresponding Lisp string (or return NIL if the pointer is a C NULL).
381 (defun newcharstar-string (newcharstar)
382 (declare (type (alien (* char)) newcharstar))
383 (if (null-alien newcharstar)
385 (prog1
386 (cast newcharstar c-string)
387 (free-alien newcharstar))))
389 ;;; Return the Unix current directory as a SIMPLE-STRING, in the
390 ;;; style returned by getcwd() (no trailing slash character).
391 #-win32
392 (defun posix-getcwd ()
393 ;; This implementation relies on a BSD/Linux extension to getcwd()
394 ;; behavior, automatically allocating memory when a null buffer
395 ;; pointer is used. On a system which doesn't support that
396 ;; extension, it'll have to be rewritten somehow.
398 ;; SunOS and OSF/1 provide almost as useful an extension: if given a null
399 ;; buffer pointer, it will automatically allocate size space. The
400 ;; KLUDGE in this solution arises because we have just read off
401 ;; PATH_MAX+1 from the Solaris header files and stuck it in here as
402 ;; a constant. Going the grovel_headers route doesn't seem to be
403 ;; helpful, either, as Solaris doesn't export PATH_MAX from
404 ;; unistd.h.
406 ;; Signal an error at compile-time, since it's needed for the
407 ;; runtime to start up
408 #-(or android linux openbsd freebsd netbsd sunos darwin dragonfly haiku)
409 #.(error "POSIX-GETCWD is not implemented.")
411 #+(or linux openbsd freebsd netbsd sunos darwin dragonfly haiku)
412 (newcharstar-string (alien-funcall (extern-alien "getcwd"
413 (function (* char)
414 (* char)
415 size-t))
417 #+(or linux openbsd freebsd netbsd darwin dragonfly haiku) 0
418 #+(or sunos) 1025))
419 #+android
420 (with-alien ((ptr (array char #.path-max)))
421 ;; Older bionic versions do not have the above feature.
422 (alien-funcall
423 (extern-alien "getcwd"
424 (function c-string (array char #.path-max) int))
425 ptr path-max))
426 (simple-perror "getcwd")))
428 ;;; Return the Unix current directory as a SIMPLE-STRING terminated
429 ;;; by a slash character.
430 (defun posix-getcwd/ ()
431 (concatenate 'string (posix-getcwd) "/"))
433 ;;; Duplicate an existing file descriptor (given as the argument) and
434 ;;; return it. If FD is not a valid file descriptor, NIL and an error
435 ;;; number are returned.
436 #-win32
437 (defun unix-dup (fd)
438 (declare (type unix-fd fd))
439 (int-syscall ("dup" int) fd))
441 ;;; Terminate the current process with an optional error code. If
442 ;;; successful, the call doesn't return. If unsuccessful, the call
443 ;;; returns NIL and an error number.
444 (deftype exit-code ()
445 `(signed-byte 32))
446 (defun os-exit (code &key abort)
447 "Exit the process with CODE. If ABORT is true, exit is performed using _exit(2),
448 avoiding atexit(3) hooks, etc. Otherwise exit(2) is called."
449 (unless (typep code 'exit-code)
450 (setf code (if abort 1 0)))
451 (if abort
452 (void-syscall ("_exit" int) code)
453 (void-syscall ("exit" int) code)))
455 (define-deprecated-function :early "1.0.56.55" unix-exit os-exit (code)
456 (os-exit code))
458 ;;; Return the process id of the current process.
459 (define-alien-routine (#+win32 "_getpid" #-win32 "getpid" unix-getpid) int)
461 ;;; Return the real user id associated with the current process.
462 #-win32
463 (define-alien-routine ("getuid" unix-getuid) int)
465 ;;; Translate a user id into a login name.
466 #-win32
467 (defun uid-username (uid)
468 (or (newcharstar-string (alien-funcall (extern-alien "uid_username"
469 (function (* char) int))
470 uid))
471 (error "found no match for Unix uid=~S" uid)))
473 ;;; Return the namestring of the home directory, being careful to
474 ;;; include a trailing #\/
475 #-win32
476 (progn
477 (defun uid-homedir (uid)
478 (or (newcharstar-string (alien-funcall (extern-alien "uid_homedir"
479 (function (* char) int))
480 uid))
481 (error "failed to resolve home directory for Unix uid=~S" uid)))
483 (defun user-homedir (uid)
484 (or (newcharstar-string (alien-funcall (extern-alien "user_homedir"
485 (function (* char) c-string))
486 uid))
487 (error "failed to resolve home directory for Unix uid=~S" uid))))
489 ;;; Invoke readlink(2) on the file name specified by PATH. Return
490 ;;; (VALUES LINKSTRING NIL) on success, or (VALUES NIL ERRNO) on
491 ;;; failure.
492 #-win32
493 (defun unix-readlink (path)
494 (declare (type unix-pathname path))
495 (with-alien ((ptr (* char)
496 (alien-funcall (extern-alien
497 "wrapped_readlink"
498 (function (* char) c-string))
499 path)))
500 (if (null-alien ptr)
501 (values nil (get-errno))
502 (multiple-value-prog1
503 (values (with-alien ((c-string c-string ptr)) c-string)
504 nil)
505 (free-alien ptr)))))
506 #+win32
507 ;; Win32 doesn't do links, but something likes to call this anyway.
508 ;; Something in this file, no less. But it only takes one result, so...
509 (defun unix-readlink (path)
510 (declare (ignore path))
511 nil)
513 (defun unix-realpath (path)
514 (declare (type unix-pathname path))
515 (with-alien ((ptr (* char)
516 (alien-funcall (extern-alien
517 "sb_realpath"
518 (function (* char) c-string))
519 path)))
520 (if (null-alien ptr)
521 (values nil (get-errno))
522 (multiple-value-prog1
523 (values (with-alien ((c-string c-string ptr)) c-string)
524 nil)
525 (free-alien ptr)))))
527 ;;; UNIX-UNLINK accepts a name and deletes the directory entry for that
528 ;;; name and the file if this is the last link.
529 (defun unix-unlink (name)
530 (declare (type unix-pathname name))
531 (void-syscall ("[_]unlink" c-string) name))
533 ;;; Return the name of the host machine as a string.
534 #-win32
535 (defun unix-gethostname ()
536 (with-alien ((buf (array char 256)))
537 (syscall ("gethostname" (* char) int)
538 (cast buf c-string)
539 (cast buf (* char)) 256)))
541 #-win32
542 (defun unix-setsid ()
543 (int-syscall ("setsid")))
545 ;;;; sys/ioctl.h
547 ;;; UNIX-IOCTL performs a variety of operations on open i/o
548 ;;; descriptors. See the UNIX Programmer's Manual for more
549 ;;; information.
550 #-win32
551 (defun unix-ioctl (fd cmd arg)
552 (declare (type unix-fd fd)
553 (type word cmd))
554 (void-syscall ("ioctl" int unsigned-long &optional (* char)) fd cmd arg))
556 ;;;; sys/resource.h
558 ;;; Return information about the resource usage of the process
559 ;;; specified by WHO. WHO can be either the current process
560 ;;; (rusage_self) or all of the terminated child processes
561 ;;; (rusage_children). NIL and an error number is returned if the call
562 ;;; fails.
563 #-win32
564 (defun unix-getrusage (who)
565 (with-alien ((usage (struct rusage)))
566 (syscall ("sb_getrusage" int (* (struct rusage)))
567 (values t
568 (+ (* (slot (slot usage 'ru-utime) 'tv-sec) 1000000)
569 (slot (slot usage 'ru-utime) 'tv-usec))
570 (+ (* (slot (slot usage 'ru-stime) 'tv-sec) 1000000)
571 (slot (slot usage 'ru-stime) 'tv-usec))
572 (slot usage 'ru-maxrss)
573 (slot usage 'ru-ixrss)
574 (slot usage 'ru-idrss)
575 (slot usage 'ru-isrss)
576 (slot usage 'ru-minflt)
577 (slot usage 'ru-majflt)
578 (slot usage 'ru-nswap)
579 (slot usage 'ru-inblock)
580 (slot usage 'ru-oublock)
581 (slot usage 'ru-msgsnd)
582 (slot usage 'ru-msgrcv)
583 (slot usage 'ru-nsignals)
584 (slot usage 'ru-nvcsw)
585 (slot usage 'ru-nivcsw))
586 who (addr usage))))
588 (defvar *on-dangerous-wait* :warn)
590 ;;; Calling select in a bad place can hang in a nasty manner, so it's better
591 ;;; to have some way to detect these.
592 (defun note-dangerous-wait (type)
593 (let ((action *on-dangerous-wait*)
594 (*on-dangerous-wait* nil))
595 (case action
596 (:warn
597 (warn "Starting a ~A without a timeout while interrupts are ~
598 disabled."
599 type))
600 (:error
601 (error "Starting a ~A without a timeout while interrupts are ~
602 disabled."
603 type))
604 (:backtrace
605 (format *debug-io*
606 "~&=== Starting a ~A without a timeout while interrupts are disabled. ===~%"
607 type)
608 (sb-debug:print-backtrace)))
609 nil))
611 ;;;; poll.h
612 #+os-provides-poll
613 (progn
614 (define-alien-type nil
615 (struct pollfd
616 (fd int)
617 (events short) ; requested events
618 (revents short))) ; returned events
620 (declaim (inline unix-poll))
621 (defun unix-poll (pollfds nfds to-msec)
622 (declare (fixnum nfds to-msec))
623 (when (and (minusp to-msec) (not *interrupts-enabled*))
624 (note-dangerous-wait "poll(2)"))
625 ;; FAST-SELECT doesn't use WITH-RESTARTED-SYSCALL so this doesn't either
626 (int-syscall ("poll" (* (struct pollfd)) int int)
627 (alien-sap pollfds) nfds to-msec))
629 ;; "simple" poll operates on a single descriptor only
630 (defun unix-simple-poll (fd direction to-msec)
631 (declare (fixnum fd to-msec))
632 (when (and (minusp to-msec) (not *interrupts-enabled*))
633 (note-dangerous-wait "poll(2)"))
634 (let ((events (ecase direction
635 (:input (logior pollin pollpri))
636 (:output pollout))))
637 (with-alien ((fds (struct pollfd)))
638 (with-restarted-syscall (count errno)
639 (progn
640 (setf (slot fds 'fd) fd
641 (slot fds 'events) events
642 (slot fds 'revents) 0)
643 (int-syscall ("poll" (* (struct pollfd)) int int)
644 (addr fds) 1 to-msec))
645 (if (zerop errno)
646 (let ((revents (slot fds 'revents)))
647 (or (and (eql 1 count) (logtest events revents))
648 (logtest pollhup revents)))
649 (error "Syscall poll(2) failed: ~A" (strerror))))))))
651 ;;;; sys/select.h
653 (defmacro with-fd-setsize ((n) &body body)
654 `(let ((,n (if (< 0 ,n fd-setsize)
656 (error "Cannot select(2) on ~D: above FD_SETSIZE limit."
657 (1- ,n)))))
658 (declare (type (integer 0 #.fd-setsize) ,n))
659 ,@body))
661 ;;; Perform the UNIX select(2) system call.
662 (declaim (inline unix-fast-select))
663 (defun unix-fast-select (num-descriptors
664 read-fds write-fds exception-fds
665 timeout-secs timeout-usecs)
666 (declare (type integer num-descriptors)
667 (type (or (alien (* (struct fd-set))) null)
668 read-fds write-fds exception-fds)
669 (type (or null (unsigned-byte 31)) timeout-secs timeout-usecs))
670 (with-fd-setsize (num-descriptors)
671 (flet ((select (tv-sap)
672 (int-syscall ("sb_select" int (* (struct fd-set)) (* (struct fd-set))
673 (* (struct fd-set)) (* (struct timeval)))
674 num-descriptors read-fds write-fds exception-fds
675 tv-sap)))
676 (cond ((or timeout-secs timeout-usecs)
677 (with-alien ((tv (struct timeval)))
678 (setf (slot tv 'tv-sec) (or timeout-secs 0))
679 (setf (slot tv 'tv-usec) (or timeout-usecs 0))
680 (select (alien-sap (addr tv)))))
682 (unless *interrupts-enabled*
683 (note-dangerous-wait "select(2)"))
684 (select (int-sap 0)))))))
686 ;;; Lisp-side implementations of FD_FOO macros.
687 (declaim (inline fd-set fd-clr fd-isset fd-zero))
688 (defun fd-set (offset fd-set)
689 (multiple-value-bind (word bit) (floor offset
690 sb-vm:n-machine-word-bits)
691 (setf (deref (slot fd-set 'fds-bits) word)
692 (logior (truly-the (unsigned-byte #.sb-vm:n-machine-word-bits)
693 (ash 1 bit))
694 (deref (slot fd-set 'fds-bits) word)))))
696 (defun fd-clr (offset fd-set)
697 (multiple-value-bind (word bit) (floor offset
698 sb-vm:n-machine-word-bits)
699 (setf (deref (slot fd-set 'fds-bits) word)
700 (logand (deref (slot fd-set 'fds-bits) word)
701 (sb-kernel:word-logical-not
702 (truly-the (unsigned-byte #.sb-vm:n-machine-word-bits)
703 (ash 1 bit)))))))
705 (defun fd-isset (offset fd-set)
706 (multiple-value-bind (word bit) (floor offset
707 sb-vm:n-machine-word-bits)
708 (logbitp bit (deref (slot fd-set 'fds-bits) word))))
710 (defun fd-zero (fd-set)
711 (loop for index below (/ fd-setsize sb-vm:n-machine-word-bits)
712 do (setf (deref (slot fd-set 'fds-bits) index) 0)))
714 #-os-provides-poll
715 (defun unix-simple-poll (fd direction to-msec)
716 (multiple-value-bind (to-sec to-usec)
717 (if (minusp to-msec)
718 (values nil nil)
719 (multiple-value-bind (to-sec to-msec2) (truncate to-msec 1000)
720 (values to-sec (* to-msec2 1000))))
721 (with-restarted-syscall (count errno)
722 (with-alien ((fds (struct fd-set)))
723 (fd-zero fds)
724 (fd-set fd fds)
725 (multiple-value-bind (read-fds write-fds)
726 (ecase direction
727 (:input
728 (values (addr fds) nil))
729 (:output
730 (values nil (addr fds))))
731 (unix-fast-select (1+ fd)
732 read-fds write-fds nil
733 to-sec to-usec)))
734 (case count
735 ((1) t)
736 ((0) nil)
737 (otherwise
738 (error "Syscall select(2) failed on fd ~D: ~A" fd (strerror)))))))
740 ;;;; sys/stat.h
742 ;;; This is a structure defined in src/runtime/wrap.c, to look
743 ;;; basically like "struct stat" according to stat(2). It may not
744 ;;; actually correspond to the real in-memory stat structure that the
745 ;;; syscall uses, and that's OK. Linux in particular is packed full of
746 ;;; stat macros, and trying to keep Lisp code in correspondence with
747 ;;; it is more pain than it's worth, so we just let our C runtime
748 ;;; synthesize a nice consistent structure for us.
750 ;;; Note that st-dev is a long, not a dev-t. This is because dev-t on
751 ;;; linux 32 bit archs is a 64 bit quantity, but alien doesn't support
752 ;;; those. We don't actually access that field anywhere, though, so
753 ;;; until we can get 64 bit alien support it'll do. Also note that
754 ;;; st_size is a long, not an off-t, because off-t is a 64-bit
755 ;;; quantity on Alpha. And FIXME: "No one would want a file length
756 ;;; longer than 32 bits anyway, right?":-|
758 ;;; The comment about alien and 64-bit quantities has not been kept in
759 ;;; sync with the comment now in wrap.h (formerly wrap.c), but it's
760 ;;; not clear whether either comment is correct. -- RMK 2007-11-14.
761 (define-alien-type nil
762 (struct wrapped_stat
763 (st-dev wst-dev-t)
764 (st-ino wst-ino-t)
765 (st-mode mode-t)
766 (st-nlink wst-nlink-t)
767 (st-uid wst-uid-t)
768 (st-gid wst-gid-t)
769 (st-rdev wst-dev-t)
770 (st-size wst-off-t)
771 (st-blksize wst-blksize-t)
772 (st-blocks wst-blkcnt-t)
773 (st-atime time-t)
774 (st-mtime time-t)
775 (st-ctime time-t)))
777 ;;; shared C-struct-to-multiple-VALUES conversion for the stat(2)
778 ;;; family of Unix system calls
780 ;;; FIXME: I think this should probably not be INLINE. However, when
781 ;;; this was not inline, it seemed to cause memory corruption
782 ;;; problems. My first guess is that it's a bug in the FFI code, where
783 ;;; the WITH-ALIEN expansion doesn't deal well with being wrapped
784 ;;; around a call to a function returning >10 values. But I didn't try
785 ;;; to figure it out, just inlined it as a quick fix. Perhaps someone
786 ;;; who's motivated to debug the FFI code can go over the DISASSEMBLE
787 ;;; output in the not-inlined case and see whether there's a problem,
788 ;;; and maybe even find a fix..
789 (declaim (inline %extract-stat-results))
790 (defun %extract-stat-results (wrapped-stat)
791 (declare (type (alien (* (struct wrapped_stat))) wrapped-stat))
792 (values t
793 (slot wrapped-stat 'st-dev)
794 (slot wrapped-stat 'st-ino)
795 (slot wrapped-stat 'st-mode)
796 (slot wrapped-stat 'st-nlink)
797 (slot wrapped-stat 'st-uid)
798 (slot wrapped-stat 'st-gid)
799 (slot wrapped-stat 'st-rdev)
800 (slot wrapped-stat 'st-size)
801 (slot wrapped-stat 'st-atime)
802 (slot wrapped-stat 'st-mtime)
803 (slot wrapped-stat 'st-ctime)
804 (slot wrapped-stat 'st-blksize)
805 (slot wrapped-stat 'st-blocks)))
807 ;;; Unix system calls in the stat(2) family are handled by calls to
808 ;;; C-level wrapper functions which copy all the raw "struct stat"
809 ;;; slots into the system-independent wrapped_stat format.
810 ;;; stat(2) <-> stat_wrapper()
811 ;;; fstat(2) <-> fstat_wrapper()
812 ;;; lstat(2) <-> lstat_wrapper()
813 (defun unix-stat (name)
814 (declare (type unix-pathname name))
815 (with-alien ((buf (struct wrapped_stat)))
816 (syscall ("stat_wrapper" c-string (* (struct wrapped_stat)))
817 (%extract-stat-results (addr buf))
818 name (addr buf))))
819 (defun unix-lstat (name)
820 (declare (type unix-pathname name))
821 (with-alien ((buf (struct wrapped_stat)))
822 (syscall ("lstat_wrapper" c-string (* (struct wrapped_stat)))
823 (%extract-stat-results (addr buf))
824 name (addr buf))))
825 (defun unix-fstat (fd)
826 #-win32
827 (declare (type unix-fd fd))
828 (#-win32 funcall #+win32 sb-win32::call-with-crt-fd
829 (lambda (fd)
830 (with-alien ((buf (struct wrapped_stat)))
831 (syscall ("fstat_wrapper" int (* (struct wrapped_stat)))
832 (%extract-stat-results (addr buf))
833 fd (addr buf))))
834 fd))
836 #-win32
837 (defun fd-type (fd)
838 (declare (type unix-fd fd))
839 (let ((mode (or (with-alien ((buf (struct wrapped_stat)))
840 (syscall ("fstat_wrapper" int (* (struct wrapped_stat)))
841 (slot buf 'st-mode)
842 fd (addr buf)))
843 0)))
844 (case (logand mode s-ifmt)
845 (#.s-ifchr :character)
846 (#.s-ifdir :directory)
847 (#.s-ifblk :block)
848 (#.s-ifreg :regular)
849 (#.s-ifsock :socket)
850 (#.s-iflnk :link)
851 (#.s-ififo :fifo)
852 (t :unknown))))
854 ;;;; time.h
856 ;; used by other time functions
857 (define-alien-type nil
858 (struct tm
859 (tm-sec int) ; Seconds. [0-60] (1 leap second)
860 (tm-min int) ; Minutes. [0-59]
861 (tm-hour int) ; Hours. [0-23]
862 (tm-mday int) ; Day. [1-31]
863 (tm-mon int) ; Month. [0-11]
864 (tm-year int) ; Year - 1900.
865 (tm-wday int) ; Day of week. [0-6]
866 (tm-yday int) ; Days in year. [0-365]
867 (tm-isdst int) ; DST. [-1/0/1]
868 (tm-gmtoff long) ; Seconds east of UTC.
869 (tm-zone c-string))) ; Timezone abbreviation.
871 (define-alien-routine get-timezone int
872 (when time-t)
873 ;; BOOLEAN is N-WORD-BITS normally. Reduce it to an unsigned int in size.
874 ;; But we can't just put UNSIGNED-INT here because the clients of this function
875 ;; want to receive a T or NIL, not a 1 or 0.
876 (daylight-savings-p (boolean 32) :out))
877 #-win32
878 (defun nanosleep (secs nsecs)
879 (alien-funcall (extern-alien "sb_nanosleep" (function int time-t int))
880 secs nsecs)
881 nil)
883 #-win32
884 (defun nanosleep-double (seconds)
885 (alien-funcall (extern-alien "sb_nanosleep_double" (function (values) double))
886 seconds)
887 nil)
889 #-win32
890 (defun nanosleep-float (seconds)
891 (alien-funcall (extern-alien "sb_nanosleep_float" (function (values) float))
892 seconds)
893 nil)
895 ;;;; sys/time.h
897 ;;; Structure crudely representing a timezone. KLUDGE: This is
898 ;;; obsolete and should never be used.
899 (define-alien-type nil
900 (struct timezone
901 (tz-minuteswest int) ; minutes west of Greenwich
902 (tz-dsttime int))) ; type of dst correction
905 ;; Type of the second argument to `getitimer' and
906 ;; the second and third arguments `setitimer'.
907 (define-alien-type nil
908 (struct itimerval
909 (it-interval (struct timeval)) ; timer interval
910 (it-value (struct timeval)))) ; current value
912 (defconstant itimer-real 0)
913 (defconstant itimer-virtual 1)
914 (defconstant itimer-prof 2)
916 #-win32
917 (defun unix-getitimer (which)
918 "UNIX-GETITIMER returns the INTERVAL and VALUE slots of one of
919 three system timers (:real :virtual or :profile). On success,
920 unix-getitimer returns 5 values,
921 T, it-interval-secs, it-interval-usec, it-value-secs, it-value-usec."
922 (declare (type (member :real :virtual :profile) which)
923 (values t
924 unsigned-byte (mod 1000000)
925 unsigned-byte (mod 1000000)))
926 (let ((which (ecase which
927 (:real itimer-real)
928 (:virtual itimer-virtual)
929 (:profile itimer-prof))))
930 (with-alien ((itv (struct itimerval)))
931 (syscall* ("sb_getitimer" int (* (struct itimerval)))
932 (values t
933 (slot (slot itv 'it-interval) 'tv-sec)
934 (slot (slot itv 'it-interval) 'tv-usec)
935 (slot (slot itv 'it-value) 'tv-sec)
936 (slot (slot itv 'it-value) 'tv-usec))
937 which (alien-sap (addr itv))))))
939 #-win32
940 (defun unix-setitimer (which int-secs int-usec val-secs val-usec)
941 "UNIX-SETITIMER sets the INTERVAL and VALUE slots of one of three system
942 timers (:real :virtual or :profile). A SIGALRM, SIGVTALRM, or SIGPROF
943 respectively will be delivered in VALUE <seconds+microseconds> from now.
944 INTERVAL, when non-zero, is reloaded into the timer on each expiration.
945 Setting VALUE to zero disables the timer. See the Unix man page for more
946 details. On success, unix-setitimer returns the
947 old contents of the INTERVAL and VALUE slots as in unix-getitimer."
948 (declare (type (member :real :virtual :profile) which)
949 (type unsigned-byte int-secs val-secs)
950 (type (integer 0 (1000000)) int-usec val-usec)
951 (values t
952 unsigned-byte (mod 1000000)
953 unsigned-byte (mod 1000000)))
954 (let ((which (ecase which
955 (:real itimer-real)
956 (:virtual itimer-virtual)
957 (:profile itimer-prof))))
958 (with-alien ((itvn (struct itimerval))
959 (itvo (struct itimerval)))
960 (setf (slot (slot itvn 'it-interval) 'tv-sec ) int-secs
961 (slot (slot itvn 'it-interval) 'tv-usec) int-usec
962 (slot (slot itvn 'it-value ) 'tv-sec ) val-secs
963 (slot (slot itvn 'it-value ) 'tv-usec) val-usec)
964 (syscall* ("sb_setitimer" int (* (struct timeval)) (* (struct timeval)))
965 (values t
966 (slot (slot itvo 'it-interval) 'tv-sec)
967 (slot (slot itvo 'it-interval) 'tv-usec)
968 (slot (slot itvo 'it-value) 'tv-sec)
969 (slot (slot itvo 'it-value) 'tv-usec))
970 which (alien-sap (addr itvn)) (alien-sap (addr itvo))))))
973 ;;; FIXME: Many Unix error code definitions were deleted from the old
974 ;;; CMU CL source code here, but not in the exports of SB-UNIX. I
975 ;;; (WHN) hope that someday I'll figure out an automatic way to detect
976 ;;; unused symbols in package exports, but if I don't, there are
977 ;;; enough of them all in one place here that they should probably be
978 ;;; removed by hand.
980 (defconstant microseconds-per-internal-time-unit
981 (/ 1000000 internal-time-units-per-second))
982 (defconstant nanoseconds-per-internal-time-unit
983 (* microseconds-per-internal-time-unit 1000))
985 ;;; UNIX specific code, that has been cleanly separated from the
986 ;;; Windows build.
987 #-win32
988 (progn
990 #-avoid-clock-gettime
991 (declaim (inline clock-gettime))
992 #-avoid-clock-gettime
993 (defun clock-gettime (clockid)
994 (declare (type (signed-byte 32) clockid))
995 (with-alien ((ts (struct timespec)))
996 (alien-funcall (extern-alien #.(libc-name-for "sb_clock_gettime")
997 (function int int (* (struct timespec))))
998 clockid (addr ts))
999 ;; 'seconds' is definitely a fixnum for 64-bit, because most-positive-fixnum
1000 ;; can express 1E11 years in seconds.
1001 (values #+64-bit (truly-the fixnum (slot ts 'tv-sec))
1002 #-64-bit (slot ts 'tv-sec)
1003 (truly-the (integer 0 #.(expt 10 9)) (slot ts 'tv-nsec)))))
1005 (declaim (inline get-time-of-day))
1006 (defun get-time-of-day ()
1007 "Return the number of seconds and microseconds since the beginning of
1008 the UNIX epoch (January 1st 1970.)"
1009 (with-alien ((tv (struct timeval)))
1010 (syscall* ("sb_gettimeofday" (* (struct timeval)) system-area-pointer)
1011 (values (slot tv 'tv-sec)
1012 (slot tv 'tv-usec))
1013 (addr tv) (int-sap 0))))
1015 ;; The "optimizations that actually matter" don't actually matter for 64-bit.
1016 ;; Microseconds can express at least 1E5 years of uptime:
1017 ;; (float (/ most-positive-fixnum (* 1000000 60 60 24 (+ 365 1/4))))
1018 ;; = microseconds-per-second * seconds-per-minute * minutes-per-hour
1019 ;; * hours-per-day * days-per-year
1021 (defun get-internal-real-time ()
1022 (with-alien ((base (struct timespec) :extern "lisp_init_time"))
1023 (multiple-value-bind (c-sec c-nsec)
1024 ;; By scaling down we end up with far less resolution than clock-realtime
1025 ;; offers, and COARSE is about twice as fast, so use that, but only for linux.
1026 ;; BSD has something similar.
1027 #-avoid-clock-gettime
1028 (clock-gettime #+linux clock-monotonic-coarse #-linux clock-monotonic)
1029 #+avoid-clock-gettime
1030 (multiple-value-bind (c-sec c-usec) (get-time-of-day) (values c-sec (* c-usec 1000)))
1032 #+64-bit ;; I know that my math is valid for 64-bit.
1033 (declare (optimize (sb-c::type-check 0)))
1034 #+64-bit
1035 (let ((delta-sec (the fixnum (- c-sec (the fixnum (slot base 'tv-sec)))))
1036 (delta-nsec (the fixnum (- c-nsec (the fixnum (slot base 'tv-nsec))))))
1037 (the sb-kernel:internal-time
1038 (+ (the fixnum (* delta-sec internal-time-units-per-second))
1039 (truncate delta-nsec nanoseconds-per-internal-time-unit))))
1041 ;; There are two optimizations here that actually matter on 32-bit systems:
1042 ;; (1) subtract the epoch from seconds and milliseconds separately,
1043 ;; (2) avoid consing a new bignum if the result is unchanged.
1045 ;; Thanks to James Anderson for the optimization hint.
1047 ;; Yes, it is possible to a computation to be GET-INTERNAL-REAL-TIME
1048 ;; bound.
1050 ;; --NS 2007-04-05
1051 #-64-bit
1052 (symbol-macrolet ((observed-sec
1053 (sb-thread::thread-observed-internal-real-time-delta-sec thr))
1054 (observed-msec
1055 (sb-thread::thread-observed-internal-real-time-delta-millisec thr))
1056 (time (sb-thread::thread-internal-real-time thr)))
1057 (let* ((delta-sec (- c-sec (slot base 'tv-sec)))
1058 ;; I inadvertently had too many THE casts in here, so I'd prefer
1059 ;; to err on the side of caution rather than cause GC lossage
1060 ;; (which I accidentally did). So assert that nanoseconds are <= 10^9
1061 ;; and the compiler will do as best it can with that information.
1062 (delta-nsec (- c-nsec (the (integer 0 #.(expt 10 9))
1063 (slot base 'tv-nsec))))
1064 ;; ROUND, FLOOR? Who cares, it's a number that's going to change.
1065 ;; More math = more self-induced jitter.
1066 (delta-millisec (floor delta-nsec 1000000))
1067 (thr sb-thread:*current-thread*))
1068 (if (and (= delta-sec observed-sec) (= delta-millisec observed-msec))
1069 time
1070 (let ((current (+ (* delta-sec internal-time-units-per-second)
1071 ;; ASSUMPTION: delta-millisec = delta-itu
1072 delta-millisec)))
1073 (setf time current
1074 observed-msec delta-millisec
1075 observed-sec delta-sec)
1076 current)))))))
1078 (declaim (inline system-internal-run-time))
1080 ;; SunOS defines CLOCK_PROCESS_CPUTIME_ID but you get EINVAL if you try to use it,
1081 ;; also use the same trick when clock_gettime should be avoided.
1082 #-(or sunos avoid-clock-gettime)
1083 (defun system-internal-run-time ()
1084 (multiple-value-bind (sec nsec) (clock-gettime clock-process-cputime-id)
1085 (+ (* sec internal-time-units-per-second)
1086 (floor (+ nsec (floor nanoseconds-per-internal-time-unit 2))
1087 nanoseconds-per-internal-time-unit))))
1088 #+(or sunos avoid-clock-gettime)
1089 (defun system-internal-run-time ()
1090 (multiple-value-bind (utime-sec utime-usec stime-sec stime-usec)
1091 (with-alien ((usage (struct sb-unix::rusage)))
1092 (syscall* ("sb_getrusage" int (* (struct sb-unix::rusage)))
1093 (values (slot (slot usage 'sb-unix::ru-utime) 'sb-unix::tv-sec)
1094 (slot (slot usage 'sb-unix::ru-utime) 'sb-unix::tv-usec)
1095 (slot (slot usage 'sb-unix::ru-stime) 'sb-unix::tv-sec)
1096 (slot (slot usage 'sb-unix::ru-stime) 'sb-unix::tv-usec))
1097 rusage_self (addr usage)))
1098 (+ (* (+ utime-sec stime-sec) internal-time-units-per-second)
1099 (floor (+ utime-usec stime-usec
1100 (floor microseconds-per-internal-time-unit 2))
1101 microseconds-per-internal-time-unit)))))
1103 ;;; FIXME, KLUDGE: GET-TIME-OF-DAY used to be UNIX-GETTIMEOFDAY, and had a
1104 ;;; primary return value indicating sucess, and also returned timezone
1105 ;;; information -- though the timezone data was not there on Darwin.
1106 ;;; Now we have GET-TIME-OF-DAY, but it turns out that despite SB-UNIX being
1107 ;;; an implementation package UNIX-GETTIMEOFDAY has users in the wild.
1108 ;;; So we're stuck with it for a while -- maybe delete it towards the end
1109 ;;; of 2009.
1110 (defun unix-gettimeofday ()
1111 #+win32 (declare (notinline get-time-of-day)) ; forward ref
1112 (multiple-value-bind (sec usec) (get-time-of-day)
1113 (values t sec usec nil nil)))
1115 ;;;; opendir, readdir, closedir, and dirent-name
1117 (declaim (inline unix-opendir))
1118 (defun unix-opendir (namestring &optional (errorp t))
1119 (let ((dir (alien-funcall
1120 (extern-alien "sb_opendir"
1121 (function system-area-pointer c-string))
1122 namestring)))
1123 (if (zerop (sap-int dir))
1124 (when errorp (simple-perror
1125 (format nil "Error opening directory ~S"
1126 namestring)))
1127 dir)))
1129 (declaim (inline unix-readdir))
1130 (defun unix-readdir (dir &optional (errorp t) namestring)
1131 (let ((ent (alien-funcall
1132 (extern-alien "sb_readdir"
1133 (function system-area-pointer system-area-pointer))
1134 dir))
1135 errno)
1136 (if (zerop (sap-int ent))
1137 (when (and errorp
1138 (not (zerop (setf errno (get-errno)))))
1139 (simple-perror
1140 (format nil "Error reading directory entry~@[ from ~S~]"
1141 namestring)
1142 :errno errno))
1143 ent)))
1145 (declaim (inline unix-closedir))
1146 (defun unix-closedir (dir &optional (errorp t) namestring)
1147 (let ((r (alien-funcall
1148 (extern-alien "sb_closedir" (function int system-area-pointer))
1149 dir)))
1150 (if (minusp r)
1151 (when errorp (simple-perror
1152 (format nil "Error closing directory~@[ ~S~]"
1153 namestring)))
1154 r)))
1156 (declaim (inline unix-dirent-name))
1157 (defun unix-dirent-name (ent)
1158 (alien-funcall
1159 (extern-alien "sb_dirent_name" (function c-string system-area-pointer))
1160 ent))