1 ;;;; RUN-PROGRAM and friends, a facility for running Unix programs
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB-IMPL") ;(SB-IMPL, not SB!IMPL, since we're built in warm load.)
15 ;;;; hacking the Unix environment
17 ;;;; In the original CMU CL code that LOAD-FOREIGN is derived from, the
18 ;;;; Unix environment (as in "man environ") was represented as an
19 ;;;; alist from keywords to strings, so that e.g. the Unix environment
20 ;;;; "SHELL=/bin/bash" "HOME=/root" "PAGER=less"
21 ;;;; was represented as
22 ;;;; ((:SHELL . "/bin/bash") (:HOME . "/root") (:PAGER "less"))
23 ;;;; This had a few problems in principle: the mapping into
24 ;;;; keyword symbols smashed the case of environment
25 ;;;; variables, and the whole mapping depended on the presence of
26 ;;;; #\= characters in the environment strings. In practice these
27 ;;;; problems weren't hugely important, since conventionally environment
28 ;;;; variables are uppercase strings followed by #\= followed by
29 ;;;; arbitrary data. However, since it's so manifestly not The Right
30 ;;;; Thing to make code which breaks unnecessarily on input which
31 ;;;; doesn't follow what is, after all, only a tradition, we've switched
32 ;;;; formats in SBCL, so that the fundamental environment list
33 ;;;; is just a list of strings, with a one-to-one-correspondence
34 ;;;; to the C-level representation. I.e., in the example above,
35 ;;;; the SBCL representation is
36 ;;;; '("SHELL=/bin/bash" "HOME=/root" "PAGER=less")
37 ;;;; CMU CL's implementation is currently supported to help with porting.
39 ;;;; It's not obvious that this code belongs here (instead of e.g. in
40 ;;;; unix.lisp), since it has only a weak logical connection with
41 ;;;; RUN-PROGRAM. However, physically it's convenient to put it here.
42 ;;;; It's not needed at cold init, so we *can* put it in this
43 ;;;; warm-loaded file. And by putting it in this warm-loaded file, we
44 ;;;; make it easy for it to get to the C-level 'environ' variable.
45 ;;;; which (at least in sbcl-0.6.10 on Red Hat Linux 6.2) is not
46 ;;;; visible at GENESIS time.
48 (define-alien-routine wrapped-environ (* c-string))
49 (defun posix-environ ()
50 "Return the Unix environment (\"man environ\") as a list of SIMPLE-STRINGs."
51 (c-strings->string-list (wrapped-environ)))
53 ;;; Convert as best we can from an SBCL representation of a Unix
54 ;;; environment to a CMU CL representation.
56 ;;; * (UNIX-ENVIRONMENT-CMUCL-FROM-SBCL '("Bletch=fub" "Noggin" "YES=No!"))
58 ;;; smashing case of "Bletch=fub" in conversion to CMU-CL-style
61 ;;; no #\= in "Noggin", eliding it in CMU-CL-style environment alist
62 ;;; ((:BLETCH . "fub") (:YES . "No!"))
63 (defun unix-environment-cmucl-from-sbcl (sbcl)
66 (declare (type simple-string string))
67 (let ((=-pos (position #\= string :test #'equal)))
70 (let* ((key-as-string (subseq string 0 =-pos))
71 (key-as-upcase-string (string-upcase key-as-string))
72 (key (keywordicate key-as-upcase-string))
73 (val (subseq string (1+ =-pos))))
74 (unless (string= key-as-string key-as-upcase-string)
75 (warn "smashing case of ~S in conversion to CMU-CL-style ~
79 (warn "no #\\= in ~S, eliding it in CMU-CL-style environment alist"
83 ;;; Convert from a CMU CL representation of a Unix environment to a
84 ;;; SBCL representation.
85 (defun unix-environment-sbcl-from-cmucl (cmucl)
88 (destructuring-bind (key . val) cons
89 (declare (type keyword key) (type simple-string val))
90 (concatenate 'simple-string (symbol-name key) "=" val)))
93 ;;;; Import wait3(2) from Unix.
95 (define-alien-routine ("wait3" c-wait3) sb-alien:int
96 (status sb-alien:int :out)
97 (options sb-alien:int)
98 (rusage sb-alien:int))
100 (defun wait3 (&optional do-not-hang check-for-stopped)
101 "Return any available status information on child process. "
102 (multiple-value-bind (pid status)
103 (c-wait3 (logior (if do-not-hang
106 (if check-for-stopped
110 (cond ((or (minusp pid)
113 ((eql (ldb (byte 8 0) status)
117 (ldb (byte 8 8) status)))
118 ((zerop (ldb (byte 7 0) status))
121 (ldb (byte 8 8) status)))
123 (let ((signal (ldb (byte 7 0) status)))
127 (sb-unix:unix-signal-number :sigstop)
128 (sb-unix:unix-signal-number :sigtstp)
129 (sb-unix:unix-signal-number :sigttin)
130 (sb-unix:unix-signal-number :sigttou)))
134 (not (zerop (ldb (byte 1 7) status)))))))))
136 ;;;; process control stuff
138 (defvar *active-processes* nil
139 "List of process structures for all active processes.")
141 (defstruct (process (:copier nil))
142 pid ; PID of child process
143 %status ; either :RUNNING, :STOPPED, :EXITED, or :SIGNALED
144 exit-code ; either exit code or signal
145 core-dumped ; T if a core image was dumped
146 pty ; stream to child's pty, or NIL
147 input ; stream to child's input, or NIL
148 output ; stream from child's output, or NIL
149 error ; stream from child's error output, or NIL
150 status-hook ; closure to call when PROC changes status
151 plist ; a place for clients to stash things
152 cookie) ; list of the number of pipes from the subproc
154 (defmethod print-object ((process process) stream)
155 (print-unreadable-object (process stream :type t)
158 (process-pid process)
159 (process-status process)))
162 (defun process-status (proc)
163 "Return the current status of process. The result is one of :RUNNING,
164 :STOPPED, :EXITED, or :SIGNALED."
165 (get-processes-status-changes)
166 (process-%status proc))
168 (defun process-wait (proc &optional check-for-stopped)
169 "Wait for PROC to quit running for some reason. Returns PROC."
171 (case (process-status proc)
174 (when check-for-stopped
177 (when (zerop (car (process-cookie proc)))
179 (sb-sys:serve-all-events 1))
183 ;;; Find the current foreground process group id.
184 (defun find-current-foreground-process (proc)
185 (with-alien ((result sb-alien:int))
188 (sb-unix:unix-ioctl (sb-sys:fd-stream-fd (process-pty proc))
190 (alien-sap (sb-alien:addr result)))
192 (error "TIOCPGRP ioctl failed: ~S" (strerror error)))
196 (defun process-kill (proc signal &optional (whom :pid))
197 "Hand SIGNAL to PROC. If WHOM is :PID, use the kill Unix system call. If
198 WHOM is :PROCESS-GROUP, use the killpg Unix system call. If WHOM is
199 :PTY-PROCESS-GROUP deliver the signal to whichever process group is
200 currently in the foreground."
201 (let ((pid (ecase whom
202 ((:pid :process-group)
206 (find-current-foreground-process proc)))))
212 (sb-unix:unix-ioctl (sb-sys:fd-stream-fd (process-pty proc))
215 (sb-unix:unix-signal-number signal))))
216 ((:process-group #-hpux :pty-process-group)
217 (sb-unix:unix-killpg pid signal))
219 (sb-unix:unix-kill pid signal)))
222 ((and (eql pid (process-pid proc))
223 (= (sb-unix:unix-signal-number signal)
224 (sb-unix:unix-signal-number :sigcont)))
225 (setf (process-%status proc) :running)
226 (setf (process-exit-code proc) nil)
227 (when (process-status-hook proc)
228 (funcall (process-status-hook proc) proc))
233 (defun process-alive-p (proc)
234 "Return T if the process is still alive, NIL otherwise."
235 (let ((status (process-status proc)))
236 (if (or (eq status :running)
237 (eq status :stopped))
241 (defun process-close (proc)
242 "Close all streams connected to PROC and stop maintaining the status slot."
243 (macrolet ((frob (stream abort)
244 `(when ,stream (close ,stream :abort ,abort))))
245 (frob (process-pty proc) t) ; Don't FLUSH-OUTPUT to dead process, ..
246 (frob (process-input proc) t) ; .. 'cause it will generate SIGPIPE.
247 (frob (process-output proc) nil)
248 (frob (process-error proc) nil))
249 (sb-sys:without-interrupts
250 (setf *active-processes* (delete proc *active-processes*)))
253 ;;; the handler for SIGCHLD signals that RUN-PROGRAM establishes
254 (defun sigchld-handler (ignore1 ignore2 ignore3)
255 (declare (ignore ignore1 ignore2 ignore3))
256 (get-processes-status-changes))
258 (defun get-processes-status-changes ()
260 (multiple-value-bind (pid what code core)
264 (let ((proc (find pid *active-processes* :key #'process-pid)))
266 (setf (process-%status proc) what)
267 (setf (process-exit-code proc) code)
268 (setf (process-core-dumped proc) core)
269 (when (process-status-hook proc)
270 (funcall (process-status-hook proc) proc))
271 (when (position what #(:exited :signaled))
272 (sb-sys:without-interrupts
273 (setf *active-processes*
274 (delete proc *active-processes*)))))))))
276 ;;;; RUN-PROGRAM and close friends
278 ;;; list of file descriptors to close when RUN-PROGRAM exits due to an error
279 (defvar *close-on-error* nil)
281 ;;; list of file descriptors to close when RUN-PROGRAM returns in the parent
282 (defvar *close-in-parent* nil)
284 ;;; list of handlers installed by RUN-PROGRAM
285 (defvar *handlers-installed* nil)
288 (define-alien-type nil
290 (sg-ispeed sb-alien:char) ; input speed
291 (sg-ospeed sb-alien:char) ; output speed
292 (sg-erase sb-alien:char) ; erase character
293 (sg-kill sb-alien:char) ; kill character
294 (sg-flags sb-alien:short))) ; mode flags
296 (define-alien-type nil
298 (sg-four sb-alien:int)
299 (sg-chars (array sb-alien:char 4))
300 (sg-flags sb-alien:int)))
302 ;;; Find an unused pty. Return three values: the file descriptor for
303 ;;; the master side of the pty, the file descriptor for the slave side
304 ;;; of the pty, and the name of the tty device for the slave side.
306 (dolist (char '(#\p #\q))
308 (let* ((master-name (format nil "/dev/pty~C~X" char digit))
309 (master-fd (sb-unix:unix-open master-name
313 (let* ((slave-name (format nil "/dev/tty~C~X" char digit))
314 (slave-fd (sb-unix:unix-open slave-name
318 ;; comment from classic CMU CL:
319 ;; Maybe put a vhangup here?
321 ;; FIXME: It seems as though this logic should be in
322 ;; OPEN-PTY, not FIND-A-PTY (both from the comments
323 ;; documenting DEFUN FIND-A-PTY, and from the
324 ;; connotations of the function names).
326 ;; FIXME: It would be nice to have a note, and/or a pointer
327 ;; to some reference material somewhere, explaining
328 ;; why we need this on *BSD and not on Linux.
330 (sb-alien:with-alien ((stuff (sb-alien:struct sgttyb)))
331 (let ((sap (sb-alien:alien-sap stuff)))
332 (sb-unix:unix-ioctl slave-fd sb-unix:TIOCGETP sap)
333 (setf (sb-alien:slot stuff 'sg-flags)
334 ;; This is EVENP|ODDP, the same numeric code
335 ;; both on FreeBSD and on OpenBSD. -- WHN 20000929
337 (sb-unix:unix-ioctl slave-fd sb-unix:TIOCSETP sap)
338 (sb-unix:unix-ioctl master-fd sb-unix:TIOCGETP sap)
339 (setf (sb-alien:slot stuff 'sg-flags)
340 (logand (sb-alien:slot stuff 'sg-flags)
341 ;; This is ~ECHO, the same numeric
342 ;; code both on FreeBSD and on OpenBSD.
345 (sb-unix:unix-ioctl master-fd sb-unix:TIOCSETP sap)))
346 (return-from find-a-pty
350 (sb-unix:unix-close master-fd))))))
351 (error "could not find a pty"))
353 (defun open-pty (pty cookie)
358 (push master *close-on-error*)
359 (push slave *close-in-parent*)
361 (multiple-value-bind (new-fd errno) (sb-unix:unix-dup master)
363 (error "couldn't SB-UNIX:UNIX-DUP ~W: ~A" master (strerror errno)))
364 (push new-fd *close-on-error*)
365 (copy-descriptor-to-stream new-fd pty cookie)))
367 (sb-sys:make-fd-stream master :input t :output t)))))
369 (defmacro round-bytes-to-words (n)
370 `(logand (the fixnum (+ (the fixnum ,n) 3)) (lognot 3)))
372 (defun string-list-to-c-strvec (string-list)
373 ;; Make a pass over STRING-LIST to calculate the amount of memory
374 ;; needed to hold the strvec.
375 (let ((string-bytes 0)
376 ;; We need an extra for the null, and an extra 'cause exect
377 ;; clobbers argv[-1].
378 (vec-bytes (* #-alpha 4 #+alpha 8 (+ (length string-list) 2))))
379 (declare (fixnum string-bytes vec-bytes))
380 (dolist (s string-list)
381 (enforce-type s simple-string)
382 (incf string-bytes (round-bytes-to-words (1+ (length s)))))
383 ;; Now allocate the memory and fill it in.
384 (let* ((total-bytes (+ string-bytes vec-bytes))
385 (vec-sap (sb-sys:allocate-system-memory total-bytes))
386 (string-sap (sap+ vec-sap vec-bytes))
387 (i #-alpha 4 #+alpha 8))
388 (declare (type (and unsigned-byte fixnum) total-bytes i)
389 (type sb-sys:system-area-pointer vec-sap string-sap))
390 (dolist (s string-list)
391 (declare (simple-string s))
392 (let ((n (length s)))
393 ;; Blast the string into place.
394 (sb-kernel:copy-to-system-area (the simple-string s)
395 (* sb-vm:vector-data-offset
398 (* (1+ n) sb-vm:n-byte-bits))
399 ;; Blast the pointer to the string into place.
400 (setf (sap-ref-sap vec-sap i) string-sap)
401 (setf string-sap (sap+ string-sap (round-bytes-to-words (1+ n))))
402 (incf i #-alpha 4 #+alpha 8)))
403 ;; Blast in the last null pointer.
404 (setf (sap-ref-sap vec-sap i) (int-sap 0))
405 (values vec-sap (sap+ vec-sap #-alpha 4 #+alpha 8) total-bytes))))
407 (defmacro with-c-strvec ((var str-list) &body body)
408 (let ((sap (gensym "SAP-"))
409 (size (gensym "SIZE-")))
410 `(multiple-value-bind
412 (string-list-to-c-strvec ,str-list)
416 (sb-sys:deallocate-system-memory ,sap ,size)))))
418 (sb-alien:define-alien-routine spawn sb-alien:int
419 (program sb-alien:c-string)
420 (argv (* sb-alien:c-string))
421 (envp (* sb-alien:c-string))
422 (pty-name sb-alien:c-string)
424 (stdout sb-alien:int)
425 (stderr sb-alien:int))
427 ;;; Is UNIX-FILENAME the name of a file that we can execute?
428 (defun unix-filename-is-executable-p (unix-filename)
429 (declare (type simple-string unix-filename))
430 (values (and (eq (sb-unix:unix-file-kind unix-filename) :file)
431 (sb-unix:unix-access unix-filename sb-unix:x_ok))))
433 ;;; FIXME: There shouldn't be two semiredundant versions of the
434 ;;; documentation. Since this is a public extension function, the
435 ;;; documentation should be in the doc string. So all information from
436 ;;; this comment should be merged into the doc string, and then this
437 ;;; comment can go away.
439 ;;; RUN-PROGRAM uses fork() and execve() to run a different program.
440 ;;; Strange stuff happens to keep the Unix state of the world
443 ;;; The child process needs to get its input from somewhere, and send
444 ;;; its output (both standard and error) to somewhere. We have to do
445 ;;; different things depending on where these somewheres really are.
447 ;;; For input, there are five options:
448 ;;; -- T: Just leave fd 0 alone. Pretty simple.
449 ;;; -- "file": Read from the file. We need to open the file and
450 ;;; pull the descriptor out of the stream. The parent should close
451 ;;; this stream after the child is up and running to free any
452 ;;; storage used in the parent.
453 ;;; -- NIL: Same as "file", but use "/dev/null" as the file.
454 ;;; -- :STREAM: Use Unix pipe() to create two descriptors. Use
455 ;;; SB-SYS:MAKE-FD-STREAM to create the output stream on the
456 ;;; writeable descriptor, and pass the readable descriptor to
457 ;;; the child. The parent must close the readable descriptor for
458 ;;; EOF to be passed up correctly.
459 ;;; -- a stream: If it's a fd-stream, just pull the descriptor out
460 ;;; of it. Otherwise make a pipe as in :STREAM, and copy
461 ;;; everything across.
463 ;;; For output, there are five options:
464 ;;; -- T: Leave descriptor 1 alone.
465 ;;; -- "file": dump output to the file.
466 ;;; -- NIL: dump output to /dev/null.
467 ;;; -- :STREAM: return a stream that can be read from.
468 ;;; -- a stream: if it's a fd-stream, use the descriptor in it.
469 ;;; Otherwise, copy stuff from output to stream.
471 ;;; For error, there are all the same options as output plus:
472 ;;; -- :OUTPUT: redirect to the same place as output.
474 ;;; RUN-PROGRAM returns a PROCESS structure for the process if
475 ;;; the fork worked, and NIL if it did not.
476 (defun run-program (program args
479 (environment (if env-p
480 (unix-environment-sbcl-from-cmucl env)
486 if-input-does-not-exist
488 (if-output-exists :error)
490 (if-error-exists :error)
492 "RUN-PROGRAM creates a new Unix process running the Unix program found in
493 the file specified by the PROGRAM argument. ARGS are the standard
494 arguments that can be passed to a Unix program. For no arguments, use NIL
495 (which means that just the name of the program is passed as arg 0).
497 RUN-PROGRAM will either return NIL or a PROCESS structure. See the CMU
498 Common Lisp Users Manual for details about the PROCESS structure.
500 notes about Unix environments (as in the :ENVIRONMENT and :ENV args):
501 1. The SBCL implementation of RUN-PROGRAM, like Perl and many other
502 programs, but unlike the original CMU CL implementation, copies
503 the Unix environment by default.
504 2. Running Unix programs from a setuid process, or in any other
505 situation where the Unix environment is under the control of someone
506 else, is a mother lode of security problems. If you are contemplating
507 doing this, read about it first. (The Perl community has a lot of good
508 documentation about this and other security issues in script-like
511 The &KEY arguments have the following meanings:
513 a list of SIMPLE-STRINGs describing the new Unix environment (as
514 in \"man environ\"). The default is to copy the environment of
517 an alternative lossy representation of the new Unix environment,
518 for compatibility with CMU CL
520 If non-NIL (default), wait until the created process finishes. If
521 NIL, continue running Lisp until the program finishes.
523 Either T, NIL, or a stream. Unless NIL, the subprocess is established
524 under a PTY. If :pty is a stream, all output to this pty is sent to
525 this stream, otherwise the PROCESS-PTY slot is filled in with a stream
526 connected to pty that can read output and write input.
528 Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
529 input for the current process is inherited. If NIL, /dev/null
530 is used. If a pathname, the file so specified is used. If a stream,
531 all the input is read from that stream and send to the subprocess. If
532 :STREAM, the PROCESS-INPUT slot is filled in with a stream that sends
533 its output to the process. Defaults to NIL.
534 :IF-INPUT-DOES-NOT-EXIST (when :INPUT is the name of a file)
536 :ERROR to generate an error
537 :CREATE to create an empty file
538 NIL (the default) to return NIL from RUN-PROGRAM
540 Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
541 output for the current process is inherited. If NIL, /dev/null
542 is used. If a pathname, the file so specified is used. If a stream,
543 all the output from the process is written to this stream. If
544 :STREAM, the PROCESS-OUTPUT slot is filled in with a stream that can
545 be read to get the output. Defaults to NIL.
546 :IF-OUTPUT-EXISTS (when :OUTPUT is the name of a file)
548 :ERROR (the default) to generate an error
549 :SUPERSEDE to supersede the file with output from the program
550 :APPEND to append output from the program to the file
551 NIL to return NIL from RUN-PROGRAM, without doing anything
552 :ERROR and :IF-ERROR-EXISTS
553 Same as :OUTPUT and :IF-OUTPUT-EXISTS, except that :ERROR can also be
554 specified as :OUTPUT in which case all error output is routed to the
555 same place as normal output.
557 This is a function the system calls whenever the status of the
558 process changes. The function takes the process as an argument."
560 (when (and env-p environment-p)
561 (error "can't specify :ENV and :ENVIRONMENT simultaneously"))
562 ;; Make sure that the interrupt handler is installed.
563 (sb-sys:enable-interrupt :sigchld #'sigchld-handler)
564 ;; Prepend the program to the argument list.
565 (push (namestring program) args)
566 (let (;; Clear various specials used by GET-DESCRIPTOR-FOR to
567 ;; communicate cleanup info.
571 ;; Establish PROC at this level so that we can return it.
573 ;; It's friendly to allow the caller to pass any string
574 ;; designator, but internally we'd like SIMPLE-STRINGs.
575 (simple-args (mapcar (lambda (x) (coerce x 'simple-string)) args)))
577 (let (;; FIXME: The old code here used to do
578 ;; (MERGE-PATHNAMES PROGRAM "path:"),
579 ;; which is the right idea (searching through the Unix
580 ;; PATH). Unfortunately, there is no logical pathname
581 ;; "path:" defined in sbcl-0.6.10. It would probably be
582 ;; reasonable to restore Unix PATH searching in SBCL, e.g.
583 ;; with a function FIND-EXECUTABLE-FILE-IN-POSIX-PATH.
584 ;; CMU CL did it with a "PATH:" search list, but CMU CL
585 ;; search lists are a non-ANSI extension that SBCL
586 ;; doesn't support. -- WHN)
587 (pfile (unix-namestring program t))
590 (error "no such program: ~S" program))
591 (unless (unix-filename-is-executable-p pfile)
592 (error "not executable: ~S" program))
593 (multiple-value-bind (stdin input-stream)
594 (get-descriptor-for input cookie
596 :if-does-not-exist if-input-does-not-exist)
597 (multiple-value-bind (stdout output-stream)
598 (get-descriptor-for output cookie
600 :if-exists if-output-exists)
601 (multiple-value-bind (stderr error-stream)
602 (if (eq error :output)
603 (values stdout output-stream)
604 (get-descriptor-for error cookie
606 :if-exists if-error-exists))
607 (multiple-value-bind (pty-name pty-stream)
608 (open-pty pty cookie)
609 ;; Make sure we are not notified about the child
610 ;; death before we have installed the PROCESS
611 ;; structure in *ACTIVE-PROCESSES*.
612 (sb-sys:without-interrupts
613 (with-c-strvec (args-vec simple-args)
614 (with-c-strvec (environment-vec environment)
617 (spawn pfile args-vec environment-vec pty-name
618 stdin stdout stderr))))
619 (when (< child-pid 0)
620 (error "couldn't fork child process: ~A"
622 (setf proc (make-process :pid child-pid
626 :output output-stream
628 :status-hook status-hook
630 (push proc *active-processes*))))))))))
631 (dolist (fd *close-in-parent*)
632 (sb-unix:unix-close fd))
634 (dolist (fd *close-on-error*)
635 (sb-unix:unix-close fd))
636 (dolist (handler *handlers-installed*)
637 (sb-sys:remove-fd-handler handler))))
638 (when (and wait proc)
642 ;;; Install a handler for any input that shows up on the file
643 ;;; descriptor. The handler reads the data and writes it to the
645 (defun copy-descriptor-to-stream (descriptor stream cookie)
647 (let ((string (make-string 256))
650 (sb-sys:add-fd-handler
653 (declare (ignore fd))
658 (result readable/errno)
659 (sb-unix:unix-select (1+ descriptor)
663 (error "~@<couldn't select on sub-process: ~
665 (strerror readable/errno)))
668 (sb-alien:with-alien ((buf (sb-alien:array
673 (sb-unix:unix-read descriptor
676 (cond ((or (and (null count)
677 (eql errno sb-unix:eio))
679 (sb-sys:remove-fd-handler handler)
682 (sb-unix:unix-close descriptor)
685 (sb-sys:remove-fd-handler handler)
689 "~@<couldn't read input from sub-process: ~
693 (sb-kernel:copy-from-system-area
695 string (* sb-vm:vector-data-offset
697 (* count sb-vm:n-byte-bits))
698 (write-string string stream
699 :end count)))))))))))
701 ;;; Find a file descriptor to use for object given the direction.
702 ;;; Returns the descriptor. If object is :STREAM, returns the created
703 ;;; stream as the second value.
704 (defun get-descriptor-for (object
710 ;; No new descriptor is needed.
716 (sb-unix:unix-open "/dev/null"
718 (:input sb-unix:o_rdonly)
719 (:output sb-unix:o_wronly)
723 (error "~@<couldn't open \"/dev/null\": ~2I~_~A~:>"
725 (push fd *close-in-parent*)
728 (multiple-value-bind (read-fd write-fd) (sb-unix:unix-pipe)
730 (error "couldn't create pipe: ~A" (strerror write-fd)))
733 (push read-fd *close-in-parent*)
734 (push write-fd *close-on-error*)
735 (let ((stream (sb-sys:make-fd-stream write-fd :output t)))
736 (values read-fd stream)))
738 (push read-fd *close-on-error*)
739 (push write-fd *close-in-parent*)
740 (let ((stream (sb-sys:make-fd-stream read-fd :input t)))
741 (values write-fd stream)))
743 (sb-unix:unix-close read-fd)
744 (sb-unix:unix-close write-fd)
745 (error "Direction must be either :INPUT or :OUTPUT, not ~S."
747 ((or (pathnamep object) (stringp object))
748 (with-open-stream (file (apply #'open object keys))
751 (sb-unix:unix-dup (sb-sys:fd-stream-fd file))
753 (push fd *close-in-parent*)
756 (error "couldn't duplicate file descriptor: ~A"
757 (strerror errno)))))))
758 ((sb-sys:fd-stream-p object)
759 (values (sb-sys:fd-stream-fd object) nil))
763 ;; FIXME: We could use a better way of setting up
764 ;; temporary files, both here and in LOAD-FOREIGN.
767 (error "could not open a temporary file in /tmp"))
768 (let* ((name (format nil "/tmp/.run-program-~D" count))
769 (fd (sb-unix:unix-open name
770 (logior sb-unix:o_rdwr
774 (sb-unix:unix-unlink name)
776 (let ((newline (string #\Newline)))
780 (read-line object nil nil)
783 (sb-unix:unix-write fd line 0 (length line))
786 (sb-unix:unix-write fd newline 0 1)))))
787 (sb-unix:unix-lseek fd 0 sb-unix:l_set)
788 (push fd *close-in-parent*)
789 (return (values fd nil))))))
791 (multiple-value-bind (read-fd write-fd)
794 (error "couldn't create pipe: ~S" (strerror write-fd)))
795 (copy-descriptor-to-stream read-fd object cookie)
796 (push read-fd *close-on-error*)
797 (push write-fd *close-in-parent*)
798 (values write-fd nil)))))
800 (error "invalid option to RUN-PROGRAM: ~S" object))))