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

  ViewVC Help
Powered by ViewVC 1.1.5