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

  ViewVC Help
Powered by ViewVC 1.1.5