/[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.26.32.2 - (show annotations)
Sun May 25 13:57:00 2008 UTC (5 years, 10 months ago) by rtoy
Branch: unicode-utf16-branch
Changes since 1.26.32.1: +8 -4 lines
run-program.lisp
o Add comment
o Forgot to terminate the C strings with nul.

unix.lisp:
unix-glibc2.lisp:
o Add comment
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.26.32.2 2008/05/25 13:57:00 rtoy 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 (dolist (char '(#\p #\q))
272 (dotimes (digit 16)
273 (let* ((master-name (format nil "/dev/pty~C~X" char digit))
274 (master-fd (unix:unix-open master-name
275 unix:o_rdwr
276 #o666)))
277 (when master-fd
278 (let* ((slave-name (format nil "/dev/tty~C~X" char digit))
279 (slave-fd (unix:unix-open slave-name
280 unix:o_rdwr
281 #o666)))
282 (when slave-fd
283 ; Maybe put a vhangup here?
284 #-glibc2
285 (alien:with-alien ((stuff (alien:struct unix:sgttyb)))
286 (let ((sap (alien:alien-sap stuff)))
287 (unix:unix-ioctl slave-fd unix:TIOCGETP sap)
288 (setf (alien:slot stuff 'unix:sg-flags) #o300) ; EVENP|ODDP
289 (unix:unix-ioctl slave-fd unix:TIOCSETP sap)
290 (unix:unix-ioctl master-fd unix:TIOCGETP sap)
291 (setf (alien:slot stuff 'unix:sg-flags)
292 (logand (alien:slot stuff 'unix:sg-flags)
293 (lognot 8))) ; ~ECHO
294 (unix:unix-ioctl master-fd unix:TIOCSETP sap)))
295 (return-from find-a-pty
296 (values master-fd
297 slave-fd
298 slave-name)))
299 (unix:unix-close master-fd))))))
300 (error "Could not find a pty."))
301
302 #+irix
303 (alien:def-alien-routine ("_getpty" c-getpty) c-call:c-string
304 (fildes c-call:int :out)
305 (oflag c-call:int)
306 (mode c-call:int)
307 (nofork c-call:int))
308
309 #+irix
310 (defun find-a-pty ()
311 "Returns the master fd, the slave fd, and the name of the tty"
312 (multiple-value-bind (line master-fd)
313 (c-getpty (logior unix:o_rdwr unix:o_ndelay) #o600 0)
314 (let* ((slave-name line)
315 (slave-fd (unix:unix-open slave-name unix:o_rdwr #o666)))
316 (when slave-fd
317 ; Maybe put a vhangup here?
318 #-glibc2
319 (alien:with-alien ((stuff (alien:struct unix:sgttyb)))
320 (let ((sap (alien:alien-sap stuff)))
321 (unix:unix-ioctl slave-fd unix:TIOCGETP sap)
322 (setf (alien:slot stuff 'unix:sg-flags) #o300) ; EVENP|ODDP
323 (unix:unix-ioctl slave-fd unix:TIOCSETP sap)
324 (unix:unix-ioctl master-fd unix:TIOCGETP sap)
325 (setf (alien:slot stuff 'unix:sg-flags)
326 (logand (alien:slot stuff 'unix:sg-flags)
327 (lognot 8))) ; ~ECHO
328 (unix:unix-ioctl master-fd unix:TIOCSETP sap)))
329 (return-from find-a-pty
330 (values master-fd
331 slave-fd
332 slave-name))))
333 (unix:unix-close master-fd))
334 (error "Could not find a pty."))
335
336 ;;; OPEN-PTY -- internal
337 ;;;
338 (defun open-pty (pty cookie)
339 (when pty
340 (multiple-value-bind
341 (master slave name)
342 (find-a-pty)
343 (push master *close-on-error*)
344 (push slave *close-in-parent*)
345 (when (streamp pty)
346 (multiple-value-bind (new-fd errno) (unix:unix-dup master)
347 (unless new-fd
348 (error "Could not UNIX:UNIX-DUP ~D: ~A"
349 master (unix:get-unix-error-msg errno)))
350 (push new-fd *close-on-error*)
351 (copy-descriptor-to-stream new-fd pty cookie)))
352 (values name
353 (system:make-fd-stream master :input t :output t)))))
354
355
356 (defmacro round-bytes-to-words (n)
357 `(logand (the fixnum (+ (the fixnum ,n) 3)) (lognot 3)))
358
359 (defun string-list-to-c-strvec (string-list)
360 ;;
361 ;; Make a pass over string-list to calculate the amount of memory
362 ;; needed to hold the strvec.
363 (let ((string-bytes 0)
364 ;; We need an extra for the null, and an extra 'cause exect clobbers
365 ;; argv[-1].
366 (vec-bytes (* #-alpha 4 #+alpha 8 (+ (length string-list) 2))))
367 (declare (fixnum string-bytes vec-bytes))
368 (dolist (s string-list)
369 (check-type s simple-string)
370 (incf string-bytes (round-bytes-to-words (1+ (length s)))))
371 ;;
372 ;; Now allocate the memory and fill it in.
373 (let* ((total-bytes (+ string-bytes vec-bytes))
374 (vec-sap (system:allocate-system-memory total-bytes))
375 (string-sap (sap+ vec-sap vec-bytes))
376 (i #-alpha 4 #+alpha 8))
377 (declare (type (and unsigned-byte fixnum) total-bytes i)
378 (type system:system-area-pointer vec-sap string-sap))
379 (dolist (s string-list)
380 (declare (simple-string s))
381 (let ((n (length s)))
382 ;;
383 ;; Blast the string into place
384 #-unicode
385 (kernel:copy-to-system-area (the simple-string s)
386 (* vm:vector-data-offset vm:word-bits)
387 string-sap 0
388 (* (1+ n) vm:byte-bits))
389 #+unicode
390 (progn
391 ;; FIXME: Do we need to apply some kind of transformation
392 ;; to convert Lisp unicode strings to C strings? Utf-8?
393 (dotimes (k n)
394 (setf (sap-ref-8 string-sap k)
395 (logand #xff (char-code (aref s k)))))
396 (setf (sap-ref-8 string-sap n) 0))
397 ;;
398 ;; Blast the pointer to the string into place
399 (setf (sap-ref-sap vec-sap i) string-sap)
400 (setf string-sap (sap+ string-sap (round-bytes-to-words (1+ n))))
401 (incf i #-alpha 4 #+alpha 8)))
402 ;; Blast in last null pointer
403 (setf (sap-ref-sap vec-sap i) (int-sap 0))
404 (values vec-sap (sap+ vec-sap #-alpha 4 #+alpha 8) total-bytes))))
405
406
407 (defmacro with-c-strvec ((var str-list) &body body)
408 (let ((sap (gensym "SAP-"))
409 (size (gensym "SIZE-")))
410 `(multiple-value-bind
411 (,sap ,var ,size)
412 (string-list-to-c-strvec ,str-list)
413 (unwind-protect
414 (progn
415 ,@body)
416 (system:deallocate-system-memory ,sap ,size)))))
417
418 (alien:def-alien-routine spawn c-call:int
419 (program c-call:c-string)
420 (argv (* c-call:c-string))
421 (envp (* c-call:c-string))
422 (pty-name c-call:c-string)
423 (stdin c-call:int)
424 (stdout c-call:int)
425 (stderr c-call:int))
426
427
428 ;;; RUN-PROGRAM -- public
429 ;;;
430 ;;; RUN-PROGRAM uses fork and execve to run a different program. Strange
431 ;;; stuff happens to keep the unix state of the world coherent.
432 ;;;
433 ;;; The child process needs to get its input from somewhere, and send its
434 ;;; output (both standard and error) to somewhere. We have to do different
435 ;;; things depending on where these somewheres really are.
436 ;;;
437 ;;; For input, there are five options:
438 ;;; - T: Just leave fd 0 alone. Pretty simple.
439 ;;; - "file": Read from the file. We need to open the file and pull the
440 ;;; descriptor out of the stream. The parent should close this stream after
441 ;;; the child is up and running to free any storage used in the parent.
442 ;;; - NIL: Same as "file", but use "/dev/null" as the file.
443 ;;; - :STREAM: Use unix-pipe to create two descriptors. Use system:make-fd-stream
444 ;;; to create the output stream on the writeable descriptor, and pass the
445 ;;; readable descriptor to the child. The parent must close the readable
446 ;;; descriptor for EOF to be passed up correctly.
447 ;;; - a stream: If it's a fd-stream, just pull the descriptor out of it.
448 ;;; Otherwise make a pipe as in :STREAM, and copy everything across.
449 ;;;
450 ;;; For output, there are n options:
451 ;;; - T: Leave descriptor 1 alone.
452 ;;; - "file": dump output to the file.
453 ;;; - NIL: dump output to /dev/null.
454 ;;; - :STREAM: return a stream that can be read from.
455 ;;; - a stream: if it's a fd-stream, use the descriptor in it. Otherwise, copy
456 ;;; stuff from output to stream.
457 ;;;
458 ;;; For error, there are all the same options as output plus:
459 ;;; - :OUTPUT: redirect to the same place as output.
460 ;;;
461 ;;; RUN-PROGRAM returns a process struct for the process if the fork worked,
462 ;;; and NIL if it did not.
463 ;;;
464 (defun run-program (program args
465 &key (env *environment-list*) (wait t) pty input
466 if-input-does-not-exist output (if-output-exists :error)
467 (error :output) (if-error-exists :error) status-hook)
468 "RUN-PROGRAM creates a new process and runs the unix program in the
469 file specified by the simple-string PROGRAM. ARGS are the standard
470 arguments that can be passed to a Unix program, for no arguments
471 use NIL (which means just the name of the program is passed as arg 0).
472
473 RUN-PROGRAM will either return NIL or a PROCESS structure. See the CMU
474 Common Lisp Users Manual for details about the PROCESS structure.
475
476 The keyword arguments have the following meanings:
477 :env -
478 An A-LIST mapping keyword environment variables to simple-string
479 values.
480 :wait -
481 If non-NIL (default), wait until the created process finishes. If
482 NIL, continue running Lisp until the program finishes.
483 :pty -
484 Either T, NIL, or a stream. Unless NIL, the subprocess is established
485 under a PTY. If :pty is a stream, all output to this pty is sent to
486 this stream, otherwise the PROCESS-PTY slot is filled in with a stream
487 connected to pty that can read output and write input.
488 :input -
489 Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
490 input for the current process is inherited. If NIL, /dev/null
491 is used. If a pathname, the file so specified is used. If a stream,
492 all the input is read from that stream and send to the subprocess. If
493 :STREAM, the PROCESS-INPUT slot is filled in with a stream that sends
494 its output to the process. Defaults to NIL.
495 :if-input-does-not-exist (when :input is the name of a file) -
496 can be one of:
497 :error - generate an error.
498 :create - create an empty file.
499 nil (default) - return nil from run-program.
500 :output -
501 Either T, NIL, a pathname, a stream, or :STREAM. If T, the standard
502 output for the current process is inherited. If NIL, /dev/null
503 is used. If a pathname, the file so specified is used. If a stream,
504 all the output from the process is written to this stream. If
505 :STREAM, the PROCESS-OUTPUT slot is filled in with a stream that can
506 be read to get the output. Defaults to NIL.
507 :if-output-exists (when :output is the name of a file) -
508 can be one of:
509 :error (default) - generates an error if the file already exists.
510 :supersede - output from the program supersedes the file.
511 :append - output from the program is appended to the file.
512 nil - run-program returns nil without doing anything.
513 :error and :if-error-exists -
514 Same as :output and :if-output-exists, except that :error can also be
515 specified as :output in which case all error output is routed to the
516 same place as normal output.
517 :status-hook -
518 This is a function the system calls whenever the status of the
519 process changes. The function takes the process as an argument."
520
521 ;; Make sure the interrupt handler is installed.
522 (system:enable-interrupt unix:sigchld #'sigchld-handler)
523 ;; Make sure all the args are okay.
524 (unless (every #'simple-string-p args)
525 (error "All args to program must be simple strings -- ~S." args))
526 ;; Pre-pend the program to the argument list.
527 (push (namestring program) args)
528 ;; Clear random specials used by GET-DESCRIPTOR-FOR to communicate cleanup
529 ;; info. Also, establish proc at this level so we can return it.
530 (let (*close-on-error* *close-in-parent* *handlers-installed* proc)
531 (unwind-protect
532 (let ((pfile (unix-namestring (merge-pathnames program "path:") t t))
533 (cookie (list 0)))
534 (unless pfile
535 (error "No such program: ~S" program))
536 (multiple-value-bind
537 (stdin input-stream)
538 (get-descriptor-for input cookie :direction :input
539 :if-does-not-exist if-input-does-not-exist)
540 (multiple-value-bind
541 (stdout output-stream)
542 (get-descriptor-for output cookie :direction :output
543 :if-does-not-exist :create
544 :if-exists if-output-exists)
545 (multiple-value-bind
546 (stderr error-stream)
547 (if (eq error :output)
548 (values stdout output-stream)
549 (get-descriptor-for error cookie :direction :output
550 :if-does-not-exist :create
551 :if-exists if-error-exists))
552 (multiple-value-bind (pty-name pty-stream)
553 (open-pty pty cookie)
554 ;; Make sure we are not notified about the child death before
555 ;; we have installed the process struct in *active-processes*
556 (system:without-interrupts
557 (with-c-strvec (argv args)
558 (with-c-strvec
559 (envp (mapcar #'(lambda (entry)
560 (concatenate
561 'string
562 (symbol-name (car entry))
563 "="
564 (cdr entry)))
565 env))
566 (let ((child-pid
567 (without-gcing
568 (spawn pfile argv envp pty-name
569 stdin stdout stderr))))
570 (when (< child-pid 0)
571 (error "Could not fork child process: ~A"
572 (unix:get-unix-error-msg)))
573 (setf proc (make-process :pid child-pid
574 :%status :running
575 :pty pty-stream
576 :input input-stream
577 :output output-stream
578 :error error-stream
579 :status-hook status-hook
580 :cookie cookie))
581 (push proc *active-processes*))))))))))
582 (dolist (fd *close-in-parent*)
583 (unix:unix-close fd))
584 (unless proc
585 (dolist (fd *close-on-error*)
586 (unix:unix-close fd))
587 (dolist (handler *handlers-installed*)
588 (system:remove-fd-handler handler))))
589 (when (and wait proc)
590 (process-wait proc))
591 proc))
592
593 ;;; COPY-DESCRIPTOR-TO-STREAM -- internal
594 ;;;
595 ;;; Installs a handler for any input that shows up on the file descriptor.
596 ;;; The handler reads the data and writes it to the stream.
597 ;;;
598 (defun copy-descriptor-to-stream (descriptor stream cookie)
599 (incf (car cookie))
600 (let ((string (make-string 256))
601 handler)
602 (setf handler
603 (system:add-fd-handler descriptor :input
604 #'(lambda (fd)
605 (declare (ignore fd))
606 (loop
607 (unless handler
608 (return))
609 (multiple-value-bind
610 (result readable/errno)
611 (unix:unix-select (1+ descriptor) (ash 1 descriptor)
612 0 0 0)
613 (cond ((null result)
614 (error "Could not select on sub-process: ~A"
615 (unix:get-unix-error-msg readable/errno)))
616 ((zerop result)
617 (return))))
618 (alien:with-alien ((buf (alien:array c-call:char 256)))
619 (multiple-value-bind
620 (count errno)
621 (unix:unix-read descriptor (alien-sap buf) 256)
622 (cond ((or (and (null count)
623 (eql errno unix:eio))
624 (eql count 0))
625 (system:remove-fd-handler handler)
626 (setf handler nil)
627 (decf (car cookie))
628 (unix:unix-close descriptor)
629 (return))
630 ((null count)
631 (system:remove-fd-handler handler)
632 (setf handler nil)
633 (decf (car cookie))
634 (error "Could not read input from sub-process: ~A"
635 (unix:get-unix-error-msg errno)))
636 (t
637 (kernel:copy-from-system-area
638 (alien-sap buf) 0
639 string (* vm:vector-data-offset vm:word-bits)
640 (* count vm:byte-bits))
641 (write-string string stream
642 :end count)))))))))))
643
644 ;;; GET-DESCRIPTOR-FOR -- internal
645 ;;;
646 ;;; Find a file descriptor to use for object given the direction. Returns
647 ;;; the descriptor. If object is :STREAM, returns the created stream as the
648 ;;; second value.
649 ;;;
650 (defun get-descriptor-for (object cookie &rest keys &key direction
651 &allow-other-keys)
652 (cond ((eq object t)
653 ;; No new descriptor is needed.
654 (values -1 nil))
655 ((eq object nil)
656 ;; Use /dev/null.
657 (multiple-value-bind
658 (fd errno)
659 (unix:unix-open "/dev/null"
660 (case direction
661 (:input unix:o_rdonly)
662 (:output unix:o_wronly)
663 (t unix:o_rdwr))
664 #o666)
665 (unless fd
666 (error "Could not open \"/dev/null\": ~A"
667 (unix:get-unix-error-msg errno)))
668 (push fd *close-in-parent*)
669 (values fd nil)))
670 ((eq object :stream)
671 (multiple-value-bind
672 (read-fd write-fd)
673 (unix:unix-pipe)
674 (unless read-fd
675 (error "Could not create pipe: ~A"
676 (unix:get-unix-error-msg write-fd)))
677 (case direction
678 (:input
679 (push read-fd *close-in-parent*)
680 (push write-fd *close-on-error*)
681 (let ((stream (system:make-fd-stream write-fd :output t)))
682 (values read-fd stream)))
683 (:output
684 (push read-fd *close-on-error*)
685 (push write-fd *close-in-parent*)
686 (let ((stream (system:make-fd-stream read-fd :input t)))
687 (values write-fd stream)))
688 (t
689 (unix:unix-close read-fd)
690 (unix:unix-close write-fd)
691 (error "Direction must be either :INPUT or :OUTPUT, not ~S"
692 direction)))))
693 ((or (pathnamep object) (stringp object))
694 (with-open-stream (file (apply #'open object keys))
695 (multiple-value-bind
696 (fd errno)
697 (unix:unix-dup (system:fd-stream-fd file))
698 (cond (fd
699 (push fd *close-in-parent*)
700 (values fd nil))
701 (t
702 (error "Could not duplicate file descriptor: ~A"
703 (unix:get-unix-error-msg errno)))))))
704 ((system:fd-stream-p object)
705 (values (system:fd-stream-fd object) nil))
706 ((streamp object)
707 (ecase direction
708 (:input
709 (dotimes (count
710 256
711 (error "Could not open a temporary file in /tmp"))
712 (let* ((name (format nil "/tmp/.run-program-~D" count))
713 (fd (unix:unix-open name
714 (logior unix:o_rdwr
715 unix:o_creat
716 unix:o_excl)
717 #o666)))
718 (unix:unix-unlink name)
719 (when fd
720 (let ((newline (string #\Newline)))
721 (loop
722 (multiple-value-bind
723 (line no-cr)
724 (read-line object nil nil)
725 (unless line
726 (return))
727 (unix:unix-write fd line 0 (length line))
728 (if no-cr
729 (return)
730 (unix:unix-write fd newline 0 1)))))
731 (unix:unix-lseek fd 0 unix:l_set)
732 (push fd *close-in-parent*)
733 (return (values fd nil))))))
734 (:output
735 (multiple-value-bind (read-fd write-fd)
736 (unix:unix-pipe)
737 (unless read-fd
738 (error "Cound not create pipe: ~A"
739 (unix:get-unix-error-msg write-fd)))
740 (copy-descriptor-to-stream read-fd object cookie)
741 (push read-fd *close-on-error*)
742 (push write-fd *close-in-parent*)
743 (values write-fd nil)))))
744 (t
745 (error "Invalid option to run-program: ~S" object))))
746

  ViewVC Help
Powered by ViewVC 1.1.5