/[cmucl]/src/code/run-program.lisp
ViewVC logotype

Contents of /src/code/run-program.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (show annotations)
Wed Sep 24 09:42:25 2008 UTC (5 years, 6 months ago) by cshapiro
Branch: MAIN
CVS Tags: sse2-packed-2008-11-12, sse2-base, sse2-packed-base, release-19f-pre1, snapshot-2008-12, snapshot-2008-11, label-2009-03-16, release-19f-base, merge-sse2-packed, merge-with-19f, RELEASE_19f, snapshot-2008-10, label-2009-03-25, sse2-checkpoint-2008-10-01, sse2-merge-with-2008-11, sse2-merge-with-2008-10, snapshot-2009-02, snapshot-2009-01, snapshot-2009-05, snapshot-2009-04
Branch point for: RELEASE-19F-BRANCH, sse2-packed-branch, sse2-branch
Changes since 1.26: +20 -31 lines
Use openpty to obtain a pseudo-terminal pair instead of searching for
an available devices on our own.  This resolves an issue with Linux
systems that do not populate /dev with BSD pseudo-terminal devices.
1 ;;; -*- Package: Extensions; Log: code.log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/run-program.lisp,v 1.27 2008/09/24 09:42:25 cshapiro Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; RUN-PROGRAM and friends. Facility for running unix programs from inside
13 ;;; a lisp.
14 ;;;
15 ;;; Written by Jim Healy and Bill Chiles, November 1987, using an earlier
16 ;;; version written by David McDonald.
17 ;;;
18 ;;; Completely re-written by William Lott, July 1989 - January 1990.
19 ;;;
20
21 (in-package "EXTENSIONS")
22
23 (export '(run-program process-status process-exit-code process-core-dumped
24 process-wait process-kill process-input process-output process-plist
25 process-pty process-error process-status-hook process-alive-p
26 process-close process-pid process-p))
27
28
29 ;;;; Import WAIT3 from unix.
30
31 (alien:def-alien-routine ("wait3" c-wait3) c-call:int
32 (status c-call:int :out)
33 (options c-call:int)
34 (rusage c-call:int))
35
36 (eval-when (load eval compile)
37 (defconstant wait-wnohang #-svr4 1 #+svr4 #o100)
38 (defconstant wait-wuntraced #-svr4 2 #+svr4 4)
39 (defconstant wait-wstopped #-svr4 #o177 #+svr4 wait-wuntraced))
40
41 (defun wait3 (&optional do-not-hang check-for-stopped)
42 "Return any available status information on child processed. "
43 (multiple-value-bind (pid status)
44 (c-wait3 (logior (if do-not-hang
45 wait-wnohang
46 0)
47 (if check-for-stopped
48 wait-wuntraced
49 0))
50 0)
51 (cond ((or (minusp pid)
52 (zerop pid))
53 nil)
54 ((eql (ldb (byte 8 0) status)
55 wait-wstopped)
56 (values pid
57 :stopped
58 (ldb (byte 8 8) status)))
59 ((zerop (ldb (byte 7 0) status))
60 (values pid
61 :exited
62 (ldb (byte 8 8) status)))
63 (t
64 (let ((signal (ldb (byte 7 0) status)))
65 (values pid
66 (if (or (eql signal unix:sigstop)
67 (eql signal unix:sigtstp)
68 (eql signal unix:sigttin)
69 (eql signal unix:sigttou))
70 :stopped
71 :signaled)
72 signal
73 (not (zerop (ldb (byte 1 7) status)))))))))
74
75
76
77 ;;;; Process control stuff.
78
79 (defvar *active-processes* nil
80 "List of process structures for all active processes.")
81
82 (defstruct (process (:print-function %print-process))
83 pid ; PID of child process.
84 %status ; Either :RUNNING, :STOPPED, :EXITED, or :SIGNALED.
85 exit-code ; Either exit code or signal
86 core-dumped ; T if a core image was dumped.
87 pty ; Stream to child's pty or nil.
88 input ; Stream to child's input or nil.
89 output ; Stream from child's output or nil.
90 error ; Stream from child's error output or nil.
91 status-hook ; Closure to call when PROC changes status.
92 plist ; Place for clients to stash tings.
93 cookie ; List of the number of pipes from the subproc.
94 )
95
96 (defun %print-process (proc stream depth)
97 (declare (ignore depth))
98 (format stream "#<process ~D ~S>"
99 (process-pid proc)
100 (process-status proc)))
101
102 ;;; PROCESS-STATUS -- Public.
103 ;;;
104 (defun process-status (proc)
105 "Return the current status of process. The result is one of :running,
106 :stopped, :exited, :signaled."
107 (declare (type process proc))
108 (get-processes-status-changes)
109 (process-%status proc))
110
111
112 ;;; PROCESS-WAIT -- Public.
113 ;;;
114 (defun process-wait (proc &optional check-for-stopped)
115 "Wait for PROC to quit running for some reason. Returns PROC."
116 (declare (type process proc))
117 (loop
118 (case (process-status proc)
119 (:running)
120 (:stopped
121 (when check-for-stopped
122 (return)))
123 (t
124 (when (zerop (car (process-cookie proc)))
125 (return))))
126 (system:serve-all-events 1))
127 proc)
128
129
130 #-hpux
131 ;;; FIND-CURRENT-FOREGROUND-PROCESS -- internal
132 ;;;
133 ;;; Finds the current foreground process group id.
134 ;;;
135 (defun find-current-foreground-process (proc)
136 (alien:with-alien ((result c-call:int))
137 (multiple-value-bind
138 (wonp error)
139 (unix:unix-ioctl (system:fd-stream-fd (ext:process-pty proc))
140 unix:TIOCGPGRP
141 (alien:alien-sap (alien:addr result)))
142 (unless wonp
143 (error "TIOCPGRP ioctl failed: ~S"
144 (unix:get-unix-error-msg error)))
145 result))
146 (process-pid proc))
147
148
149 ;;; PROCESS-KILL -- public
150 ;;;
151 ;;; Hand a process a signal.
152 ;;;
153 (defun process-kill (proc signal &optional (whom :pid))
154 "Hand SIGNAL to PROC. If whom is :pid, use the kill Unix system call. If
155 whom is :process-group, use the killpg Unix system call. If whom is
156 :pty-process-group deliver the signal to whichever process group is currently
157 in the foreground."
158 (declare (type process proc))
159 (let ((pid (ecase whom
160 ((:pid :process-group)
161 (process-pid proc))
162 (:pty-process-group
163 #-hpux
164 (find-current-foreground-process proc)))))
165 (multiple-value-bind
166 (okay errno)
167 (case whom
168 #+hpux
169 (:pty-process-group
170 (unix:unix-ioctl (system:fd-stream-fd (process-pty proc))
171 unix:TIOCSIGSEND
172 (system:int-sap
173 (unix:unix-signal-number signal))))
174 ((:process-group #-hpux :pty-process-group)
175 (unix:unix-killpg pid signal))
176 (t
177 (unix:unix-kill pid signal)))
178 (cond ((not okay)
179 (values nil errno))
180 ((and (eql pid (process-pid proc))
181 (= (unix:unix-signal-number signal) unix:sigcont))
182 (setf (process-%status proc) :running)
183 (setf (process-exit-code proc) nil)
184 (when (process-status-hook proc)
185 (funcall (process-status-hook proc) proc))
186 t)
187 (t
188 t)))))
189
190
191 ;;; PROCESS-ALIVE-P -- public
192 ;;;
193 ;;; Returns T if the process is still alive, NIL otherwise.
194 ;;;
195 (defun process-alive-p (proc)
196 "Returns T if the process is still alive, NIL otherwise."
197 (declare (type process proc))
198 (let ((status (process-status proc)))
199 (if (or (eq status :running)
200 (eq status :stopped))
201 t
202 nil)))
203
204 ;;; PROCESS-CLOSE -- public
205 ;;;
206 ;;; Close all the streams held open by PROC.
207 ;;;
208 (defun process-close (proc)
209 "Close all streams connected to PROC and stop maintaining the status slot."
210 (declare (type process proc))
211 (macrolet ((frob (stream abort)
212 `(when ,stream (close ,stream :abort ,abort))))
213 (frob (process-pty proc) t) ; Don't FLUSH-OUTPUT to dead process.
214 (frob (process-input proc) t) ; 'cause it will generate SIGPIPE.
215 (frob (process-output proc) nil)
216 (frob (process-error proc) nil))
217 (system:without-interrupts
218 (setf *active-processes* (delete proc *active-processes*)))
219 proc)
220
221 ;;; SIGCHLD-HANDLER -- Internal.
222 ;;;
223 ;;; This is the handler for sigchld signals that RUN-PROGRAM establishes.
224 ;;;
225 (defun sigchld-handler (ignore1 ignore2 ignore3)
226 (declare (ignore ignore1 ignore2 ignore3))
227 (get-processes-status-changes))
228
229 ;;; GET-PROCESSES-STATUS-CHANGES -- Internal.
230 ;;;
231 (defun get-processes-status-changes ()
232 (loop
233 (multiple-value-bind (pid what code core)
234 (wait3 t t)
235 (unless pid
236 (return))
237 (let ((proc (find pid *active-processes* :key #'process-pid)))
238 (when proc
239 (setf (process-%status proc) what)
240 (setf (process-exit-code proc) code)
241 (setf (process-core-dumped proc) core)
242 (when (process-status-hook proc)
243 (funcall (process-status-hook proc) proc))
244 (when (or (eq what :exited)
245 (eq what :signaled))
246 (system:without-interrupts
247 (setf *active-processes*
248 (delete proc *active-processes*)))))))))
249
250
251
252 ;;;; RUN-PROGRAM and close friends.
253
254 (defvar *close-on-error* nil
255 "List of file descriptors to close when RUN-PROGRAM exits due to an error.")
256 (defvar *close-in-parent* nil
257 "List of file descriptors to close when RUN-PROGRAM returns in the parent.")
258 (defvar *handlers-installed* nil
259 "List of handlers installed by RUN-PROGRAM.")
260
261
262 ;;; FIND-A-PTY -- internal
263 ;;;
264 ;;; Finds a pty that is not in use. Returns three values: the file descriptor
265 ;;; for the master side of the pty, the file descriptor for the slave side of
266 ;;; the pty, and the name of the tty device for the slave side.
267 ;;;
268 #-irix
269 (defun find-a-pty ()
270 "Returns the master fd, the slave fd, and the name of the tty"
271 (multiple-value-bind (error master-fd slave-fd)
272 (unix:unix-openpty nil nil nil)
273 (when (zerop error)
274 #-glibc2
275 (alien:with-alien ((stuff (alien:struct unix:sgttyb)))
276 (let ((sap (alien:alien-sap stuff)))
277 (unix:unix-ioctl slave-fd unix:TIOCGETP sap)
278 (setf (alien:slot stuff 'unix:sg-flags) #o300) ; EVENP|ODDP
279 (unix:unix-ioctl slave-fd unix:TIOCSETP sap)
280 (unix:unix-ioctl master-fd unix:TIOCGETP sap)
281 (setf (alien:slot stuff 'unix:sg-flags)
282 (logand (alien:slot stuff 'unix:sg-flags)
283 (lognot 8))) ; ~ECHO
284 (unix:unix-ioctl master-fd unix:TIOCSETP sap)))
285 (return-from find-a-pty
286 (values master-fd
287 slave-fd
288 (unix:unix-ttyname slave-fd))))
289 (error "Could not find a pty.")))
290
291 #+irix
292 (alien:def-alien-routine ("_getpty" c-getpty) c-call:c-string
293 (fildes c-call:int :out)
294 (oflag c-call:int)
295 (mode c-call:int)
296 (nofork c-call:int))
297
298 #+irix
299 (defun find-a-pty ()
300 "Returns the master fd, the slave fd, and the name of the tty"
301 (multiple-value-bind (line master-fd)
302 (c-getpty (logior unix:o_rdwr unix:o_ndelay) #o600 0)
303 (let* ((slave-name line)
304 (slave-fd (unix:unix-open slave-name unix:o_rdwr #o666)))
305 (when slave-fd
306 ; Maybe put a vhangup here?
307 #-glibc2
308 (alien:with-alien ((stuff (alien:struct unix:sgttyb)))
309 (let ((sap (alien:alien-sap stuff)))
310 (unix:unix-ioctl slave-fd unix:TIOCGETP sap)
311 (setf (alien:slot stuff 'unix:sg-flags) #o300) ; EVENP|ODDP
312 (unix:unix-ioctl slave-fd unix:TIOCSETP sap)
313 (unix:unix-ioctl master-fd unix:TIOCGETP sap)
314 (setf (alien:slot stuff 'unix:sg-flags)
315 (logand (alien:slot stuff 'unix:sg-flags)
316 (lognot 8))) ; ~ECHO
317 (unix:unix-ioctl master-fd unix:TIOCSETP sap)))
318 (return-from find-a-pty
319 (values master-fd
320 slave-fd
321 slave-name))))
322 (unix:unix-close master-fd))
323 (error "Could not find a pty."))
324
325 ;;; OPEN-PTY -- internal
326 ;;;
327 (defun open-pty (pty cookie)
328 (when pty
329 (multiple-value-bind
330 (master slave name)
331 (find-a-pty)
332 (push master *close-on-error*)
333 (push slave *close-in-parent*)
334 (when (streamp pty)
335 (multiple-value-bind (new-fd errno) (unix:unix-dup master)
336 (unless new-fd
337 (error "Could not UNIX:UNIX-DUP ~D: ~A"
338 master (unix:get-unix-error-msg errno)))
339 (push new-fd *close-on-error*)
340 (copy-descriptor-to-stream new-fd pty cookie)))
341 (values name
342 (system:make-fd-stream master :input t :output t)))))
343
344
345 (defmacro round-bytes-to-words (n)
346 `(logand (the fixnum (+ (the fixnum ,n) 3)) (lognot 3)))
347
348 (defun string-list-to-c-strvec (string-list)
349 ;;
350 ;; Make a pass over string-list to calculate the amount of memory
351 ;; needed to hold the strvec.
352 (let ((string-bytes 0)
353 ;; We need an extra for the null, and an extra 'cause exect clobbers
354 ;; argv[-1].
355 (vec-bytes (* #-alpha 4 #+alpha 8 (+ (length string-list) 2))))
356 (declare (fixnum string-bytes vec-bytes))
357 (dolist (s string-list)
358 (check-type s simple-string)
359 (incf string-bytes (round-bytes-to-words (1+ (length s)))))
360 ;;
361 ;; Now allocate the memory and fill it in.
362 (let* ((total-bytes (+ string-bytes vec-bytes))
363 (vec-sap (system:allocate-system-memory total-bytes))
364 (string-sap (sap+ vec-sap vec-bytes))
365 (i #-alpha 4 #+alpha 8))
366 (declare (type (and unsigned-byte fixnum) total-bytes i)
367 (type system:system-area-pointer vec-sap string-sap))
368 (dolist (s string-list)
369 (declare (simple-string s))
370 (let ((n (length s)))
371 ;;
372 ;; Blast the string into place
373 (kernel:copy-to-system-area (the simple-string s)
374 (* vm:vector-data-offset vm:word-bits)
375 string-sap 0
376 (* (1+ n) vm:byte-bits))
377 ;;
378 ;; Blast the pointer to the string into place
379 (setf (sap-ref-sap vec-sap i) string-sap)
380 (setf string-sap (sap+ string-sap (round-bytes-to-words (1+ n))))
381 (incf i #-alpha 4 #+alpha 8)))
382 ;; Blast in last null pointer
383 (setf (sap-ref-sap vec-sap i) (int-sap 0))
384 (values vec-sap (sap+ vec-sap #-alpha 4 #+alpha 8) total-bytes))))
385
386
387 (defmacro with-c-strvec ((var str-list) &body body)
388 (let ((sap (gensym "SAP-"))
389 (size (gensym "SIZE-")))
390 `(multiple-value-bind
391 (,sap ,var ,size)
392 (string-list-to-c-strvec ,str-list)
393 (unwind-protect
394 (progn
395 ,@body)
396 (system:deallocate-system-memory ,sap ,size)))))
397
398 (alien:def-alien-routine spawn c-call:int
399 (program c-call:c-string)
400 (argv (* c-call:c-string))
401 (envp (* c-call:c-string))
402 (pty-name c-call:c-string)
403 (stdin c-call:int)
404 (stdout c-call:int)
405 (stderr c-call:int))
406
407
408 ;;; RUN-PROGRAM -- public
409 ;;;
410 ;;; RUN-PROGRAM uses fork and execve to run a different program. Strange
411 ;;; stuff happens to keep the unix state of the world coherent.
412 ;;;
413 ;;; The child process needs to get its input from somewhere, and send its
414 ;;; output (both standard and error) to somewhere. We have to do different
415 ;;; things depending on where these somewheres really are.
416 ;;;
417 ;;; For input, there are five options:
418 ;;; - T: Just leave fd 0 alone. Pretty simple.
419 ;;; - "file": Read from the file. We need to open the file and pull the
420 ;;; descriptor out of the stream. The parent should close this stream after
421 ;;; the child is up and running to free any storage used in the parent.
422 ;;; - NIL: Same as "file", but use "/dev/null" as the file.
423 ;;; - :STREAM: Use unix-pipe to create two descriptors. Use system:make-fd-stream
424 ;;; to create the output stream on the writeable descriptor, and pass the
425 ;;; readable descriptor to the child. The parent must close the readable
426 ;;; descriptor for EOF to be passed up correctly.
427 ;;; - a stream: If it's a fd-stream, just pull the descriptor out of it.
428 ;;; Otherwise make a pipe as in :STREAM, and copy everything across.
429 ;;;
430 ;;; For output, there are n options:
431 ;;; - T: Leave descriptor 1 alone.
432 ;;; - "file": dump output to the file.
433 ;;; - NIL: dump output to /dev/null.
434 ;;; - :STREAM: return a stream that can be read from.
435 ;;; - a stream: if it's a fd-stream, use the descriptor in it. Otherwise, copy
436 ;;; stuff from output to stream.
437 ;;;
438 ;;; For error, there are all the same options as output plus:
439 ;;; - :OUTPUT: redirect to the same place as output.
440 ;;;
441 ;;; RUN-PROGRAM returns a process struct for the process if the fork worked,
442 ;;; and NIL if it did not.
443 ;;;
444 (defun run-program (program args
445 &key (env *environment-list*) (wait t) pty input
446 if-input-does-not-exist output (if-output-exists :error)
447 (error :output) (if-error-exists :error) status-hook)
448 "RUN-PROGRAM creates a new process and runs the unix program in the
449 file specified by the simple-string PROGRAM. ARGS are the standard
450 arguments that can be passed to a Unix program, for no arguments
451 use NIL (which means just the name of the program is passed as arg 0).
452
453 RUN-PROGRAM will either return NIL or a PROCESS structure. See the CMU
454 Common Lisp Users Manual for details about the PROCESS structure.
455
456 The keyword arguments have the following meanings:
457 :env -
458 An A-LIST mapping keyword environment variables to simple-string
459 values.
460 :wait -
461 If non-NIL (default), wait until the created process finishes. If
462 NIL, continue running Lisp until the program finishes.
463 :pty -
464 Either T, NIL, or a stream. Unless NIL, the subprocess is established
465 under a PTY. If :pty is a stream, all output to this pty is sent to
466 this stream, otherwise the PROCESS-PTY slot is filled in with a stream
467 connected to pty that can read output and write input.
468 :input -
469 Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
470 input for the current process is inherited. If NIL, /dev/null
471 is used. If a pathname, the file so specified is used. If a stream,
472 all the input is read from that stream and send to the subprocess. If
473 :STREAM, the PROCESS-INPUT slot is filled in with a stream that sends
474 its output to the process. Defaults to NIL.
475 :if-input-does-not-exist (when :input is the name of a file) -
476 can be one of:
477 :error - generate an error.
478 :create - create an empty file.
479 nil (default) - return nil from run-program.
480 :output -
481 Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
482 output for the current process is inherited. If NIL, /dev/null
483 is used. If a pathname, the file so specified is used. If a stream,
484 all the output from the process is written to this stream. If
485 :STREAM, the PROCESS-OUTPUT slot is filled in with a stream that can
486 be read to get the output. Defaults to NIL.
487 :if-output-exists (when :output is the name of a file) -
488 can be one of:
489 :error (default) - generates an error if the file already exists.
490 :supersede - output from the program supersedes the file.
491 :append - output from the program is appended to the file.
492 nil - run-program returns nil without doing anything.
493 :error and :if-error-exists -
494 Same as :output and :if-output-exists, except that :error can also be
495 specified as :output in which case all error output is routed to the
496 same place as normal output.
497 :status-hook -
498 This is a function the system calls whenever the status of the
499 process changes. The function takes the process as an argument."
500
501 ;; Make sure the interrupt handler is installed.
502 (system:enable-interrupt unix:sigchld #'sigchld-handler)
503 ;; Make sure all the args are okay.
504 (unless (every #'simple-string-p args)
505 (error "All args to program must be simple strings -- ~S." args))
506 ;; Pre-pend the program to the argument list.
507 (push (namestring program) args)
508 ;; Clear random specials used by GET-DESCRIPTOR-FOR to communicate cleanup
509 ;; info. Also, establish proc at this level so we can return it.
510 (let (*close-on-error* *close-in-parent* *handlers-installed* proc)
511 (unwind-protect
512 (let ((pfile (unix-namestring (merge-pathnames program "path:") t t))
513 (cookie (list 0)))
514 (unless pfile
515 (error "No such program: ~S" program))
516 (multiple-value-bind
517 (stdin input-stream)
518 (get-descriptor-for input cookie :direction :input
519 :if-does-not-exist if-input-does-not-exist)
520 (multiple-value-bind
521 (stdout output-stream)
522 (get-descriptor-for output cookie :direction :output
523 :if-does-not-exist :create
524 :if-exists if-output-exists)
525 (multiple-value-bind
526 (stderr error-stream)
527 (if (eq error :output)
528 (values stdout output-stream)
529 (get-descriptor-for error cookie :direction :output
530 :if-does-not-exist :create
531 :if-exists if-error-exists))
532 (multiple-value-bind (pty-name pty-stream)
533 (open-pty pty cookie)
534 ;; Make sure we are not notified about the child death before
535 ;; we have installed the process struct in *active-processes*
536 (system:without-interrupts
537 (with-c-strvec (argv args)
538 (with-c-strvec
539 (envp (mapcar #'(lambda (entry)
540 (concatenate
541 'string
542 (symbol-name (car entry))
543 "="
544 (cdr entry)))
545 env))
546 (let ((child-pid
547 (without-gcing
548 (spawn pfile argv envp pty-name
549 stdin stdout stderr))))
550 (when (< child-pid 0)
551 (error "Could not fork child process: ~A"
552 (unix:get-unix-error-msg)))
553 (setf proc (make-process :pid child-pid
554 :%status :running
555 :pty pty-stream
556 :input input-stream
557 :output output-stream
558 :error error-stream
559 :status-hook status-hook
560 :cookie cookie))
561 (push proc *active-processes*))))))))))
562 (dolist (fd *close-in-parent*)
563 (unix:unix-close fd))
564 (unless proc
565 (dolist (fd *close-on-error*)
566 (unix:unix-close fd))
567 (dolist (handler *handlers-installed*)
568 (system:remove-fd-handler handler))))
569 (when (and wait proc)
570 (process-wait proc))
571 proc))
572
573 ;;; COPY-DESCRIPTOR-TO-STREAM -- internal
574 ;;;
575 ;;; Installs a handler for any input that shows up on the file descriptor.
576 ;;; The handler reads the data and writes it to the stream.
577 ;;;
578 (defun copy-descriptor-to-stream (descriptor stream cookie)
579 (incf (car cookie))
580 (let ((string (make-string 256))
581 handler)
582 (setf handler
583 (system:add-fd-handler descriptor :input
584 #'(lambda (fd)
585 (declare (ignore fd))
586 (loop
587 (unless handler
588 (return))
589 (multiple-value-bind
590 (result readable/errno)
591 (unix:unix-select (1+ descriptor) (ash 1 descriptor)
592 0 0 0)
593 (cond ((null result)
594 (error "Could not select on sub-process: ~A"
595 (unix:get-unix-error-msg readable/errno)))
596 ((zerop result)
597 (return))))
598 (alien:with-alien ((buf (alien:array c-call:char 256)))
599 (multiple-value-bind
600 (count errno)
601 (unix:unix-read descriptor (alien-sap buf) 256)
602 (cond ((or (and (null count)
603 (eql errno unix:eio))
604 (eql count 0))
605 (system:remove-fd-handler handler)
606 (setf handler nil)
607 (decf (car cookie))
608 (unix:unix-close descriptor)
609 (return))
610 ((null count)
611 (system:remove-fd-handler handler)
612 (setf handler nil)
613 (decf (car cookie))
614 (error "Could not read input from sub-process: ~A"
615 (unix:get-unix-error-msg errno)))
616 (t
617 (kernel:copy-from-system-area
618 (alien-sap buf) 0
619 string (* vm:vector-data-offset vm:word-bits)
620 (* count vm:byte-bits))
621 (write-string string stream
622 :end count)))))))))))
623
624 ;;; GET-DESCRIPTOR-FOR -- internal
625 ;;;
626 ;;; Find a file descriptor to use for object given the direction. Returns
627 ;;; the descriptor. If object is :STREAM, returns the created stream as the
628 ;;; second value.
629 ;;;
630 (defun get-descriptor-for (object cookie &rest keys &key direction
631 &allow-other-keys)
632 (cond ((eq object t)
633 ;; No new descriptor is needed.
634 (values -1 nil))
635 ((eq object nil)
636 ;; Use /dev/null.
637 (multiple-value-bind
638 (fd errno)
639 (unix:unix-open "/dev/null"
640 (case direction
641 (:input unix:o_rdonly)
642 (:output unix:o_wronly)
643 (t unix:o_rdwr))
644 #o666)
645 (unless fd
646 (error "Could not open \"/dev/null\": ~A"
647 (unix:get-unix-error-msg errno)))
648 (push fd *close-in-parent*)
649 (values fd nil)))
650 ((eq object :stream)
651 (multiple-value-bind
652 (read-fd write-fd)
653 (unix:unix-pipe)
654 (unless read-fd
655 (error "Could not create pipe: ~A"
656 (unix:get-unix-error-msg write-fd)))
657 (case direction
658 (:input
659 (push read-fd *close-in-parent*)
660 (push write-fd *close-on-error*)
661 (let ((stream (system:make-fd-stream write-fd :output t)))
662 (values read-fd stream)))
663 (:output
664 (push read-fd *close-on-error*)
665 (push write-fd *close-in-parent*)
666 (let ((stream (system:make-fd-stream read-fd :input t)))
667 (values write-fd stream)))
668 (t
669 (unix:unix-close read-fd)
670 (unix:unix-close write-fd)
671 (error "Direction must be either :INPUT or :OUTPUT, not ~S"
672 direction)))))
673 ((or (pathnamep object) (stringp object))
674 (with-open-stream (file (apply #'open object keys))
675 (multiple-value-bind
676 (fd errno)
677 (unix:unix-dup (system:fd-stream-fd file))
678 (cond (fd
679 (push fd *close-in-parent*)
680 (values fd nil))
681 (t
682 (error "Could not duplicate file descriptor: ~A"
683 (unix:get-unix-error-msg errno)))))))
684 ((system:fd-stream-p object)
685 (values (system:fd-stream-fd object) nil))
686 ((streamp object)
687 (ecase direction
688 (:input
689 (dotimes (count
690 256
691 (error "Could not open a temporary file in /tmp"))
692 (let* ((name (format nil "/tmp/.run-program-~D" count))
693 (fd (unix:unix-open name
694 (logior unix:o_rdwr
695 unix:o_creat
696 unix:o_excl)
697 #o666)))
698 (unix:unix-unlink name)
699 (when fd
700 (let ((newline (string #\Newline)))
701 (loop
702 (multiple-value-bind
703 (line no-cr)
704 (read-line object nil nil)
705 (unless line
706 (return))
707 (unix:unix-write fd line 0 (length line))
708 (if no-cr
709 (return)
710 (unix:unix-write fd newline 0 1)))))
711 (unix:unix-lseek fd 0 unix:l_set)
712 (push fd *close-in-parent*)
713 (return (values fd nil))))))
714 (:output
715 (multiple-value-bind (read-fd write-fd)
716 (unix:unix-pipe)
717 (unless read-fd
718 (error "Cound not create pipe: ~A"
719 (unix:get-unix-error-msg write-fd)))
720 (copy-descriptor-to-stream read-fd object cookie)
721 (push read-fd *close-on-error*)
722 (push write-fd *close-in-parent*)
723 (values write-fd nil)))))
724 (t
725 (error "Invalid option to run-program: ~S" object))))
726

  ViewVC Help
Powered by ViewVC 1.1.5