1 ;;;; various RUN-PROGRAM tests with side effects
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (cl:in-package :cl-user)
16 ;; In addition to definitions lower down the impurity we're avoiding
17 ;; is the sigchld handler that RUN-PROGRAM sets up, which interfers
18 ;; with the manual unix process control done by the test framework
19 ;; (sometimes the handler will manage to WAIT3 a process before
20 ;; run-tests WAITPIDs it).
22 (with-test (:name :run-program-cat-1)
23 (let* ((process (sb-ext:run-program "/bin/cat" '() :wait nil
24 :output :stream :input :stream))
25 (out (process-input process))
26 (in (process-output process)))
28 (loop for i from 0 to 255 do
31 (assert (= (read-byte in) i)))
32 (process-close process))))
35 (with-test (:name :run-program-cat-2)
36 ;; Tests that reading from a FIFO is interruptible.
37 (let* ((process (sb-ext:run-program "/bin/cat" '()
39 :output :stream :input :stream))
40 (in (process-input process))
41 (out (process-output process))
42 (sem (sb-thread:make-semaphore))
44 (writer (sb-thread:make-thread (lambda ()
45 (sb-thread:wait-on-semaphore sem)
54 (sb-thread:signal-semaphore sem)
58 (setf got (read-line out))
64 (assert (eq unwind :sleep))
65 (sb-thread:join-thread writer)
66 (assert (equal "OK" (read-line out)))))
68 (defclass buffer-stream (sb-gray:fundamental-binary-input-stream sb-gray:fundamental-binary-output-stream)
69 ((buffer :initform (make-array 128
70 :element-type '(unsigned-byte 8)
75 (defmethod stream-element-type ((stream buffer-stream))
78 (defmethod sb-gray:stream-read-sequence ((stream buffer-stream) seq &optional (start 0) end)
79 (let* ((buffer (slot-value stream 'buffer))
80 (end (or end (length seq)))
81 (mark (slot-value stream 'mark))
82 (fill-pointer (fill-pointer buffer))
83 (new-mark (+ mark (min fill-pointer (- end start)))))
84 (setf (slot-value stream 'mark) new-mark)
86 :start1 start :end1 end
87 :start2 mark :end2 fill-pointer)
88 (min end (+ start (- fill-pointer mark)))))
90 (defmethod sb-gray:stream-write-sequence ((stream buffer-stream) seq &optional (start 0) end)
91 (let* ((buffer (slot-value stream 'buffer))
92 (end (or end (length seq)))
93 (fill-pointer (fill-pointer buffer))
94 (new-fill (min (array-total-size buffer) (+ fill-pointer (- end start)))))
95 (setf (fill-pointer buffer) new-fill)
98 :start2 start :end2 end)
101 (with-test (:name :run-program-cat-3)
102 ;; User-defined binary input and output streams.
103 (let ((in (make-instance 'buffer-stream))
104 (out (make-instance 'buffer-stream))
105 (data #(0 1 2 3 4 5 6 7 8 9 10 11 12)))
106 (write-sequence data in)
107 (let ((process (sb-ext:run-program "/bin/cat" '() :wait t :output out :input in))
108 (buf (make-array (length data))))
109 (assert (= 13 (read-sequence buf out)))
110 (assert (= 0 (read-sequence (make-array 8) out)))
111 (assert (equalp buf data)))))
113 ;;; Test driving an external program (cat) through pipes wrapped in
114 ;;; composite streams.
119 (multiple-value-bind (in out) (sb-posix:pipe)
120 (let ((input (sb-sys:make-fd-stream in
122 :external-format :ascii
123 :buffering :none :name "in"))
124 (output (sb-sys:make-fd-stream out
126 :external-format :ascii
127 :buffering :none :name "out")))
128 (make-two-way-stream input output))))
130 (defparameter *cat-in-pipe* (make-pipe))
131 (defparameter *cat-in* (make-synonym-stream '*cat-in-pipe*))
132 (defparameter *cat-out-pipe* (make-pipe))
133 (defparameter *cat-out* (make-synonym-stream '*cat-out-pipe*))
135 (with-test (:name :run-program-cat-2)
136 (let ((cat (run-program "/bin/cat" nil :input *cat-in* :output *cat-out*
138 (dolist (test '("This is a test!"
139 "This is another test!"
140 "This is the last test...."))
141 (write-line test *cat-in*)
142 (assert (equal test (read-line *cat-out*))))
143 (process-close cat)))
145 ;;; The above test used to use ed, but there were buffering issues: on some platforms
146 ;;; buffering of stdin and stdout depends on their TTYness, and ed isn't sufficiently
147 ;;; agressive about flushing them. So, here's another test using :PTY.
149 (defparameter *tmpfile* "run-program-ed-test.tmp")
151 (with-open-file (f *tmpfile*
153 :if-exists :supersede)
154 (write-line "bar" f))
157 (run-program "/bin/ed" (list *tmpfile*) :wait nil :pty t))
159 (defparameter *ed-pipe* (make-two-way-stream (process-pty *ed*) (process-pty *ed*)))
160 (defparameter *ed-in* (make-synonym-stream '*ed-pipe*))
161 (defparameter *ed-out* (make-synonym-stream '*ed-pipe*))
163 (defun read-linish (stream)
164 (with-output-to-string (s)
165 (loop for c = (read-char stream)
166 while (and c (not (eq #\newline c)))
167 ;; Some eds like to send \r\n
168 do (unless (eq #\return c)
171 (defun assert-ed (command response)
173 (write-line command *ed-in*)
174 (force-output *ed-in*))
176 (let ((got (read-linish *ed-out*)))
177 (unless (equal response got)
178 (error "wanted '~A' from ed, got '~A'" response got))))
182 (with-test (:name :run-program-ed)
184 (assert-ed ".s/bar/baz/g" nil)
188 (with-open-file (f *tmpfile*)
189 (assert (equal "baz" (read-line f)))))
190 (delete-file *tmpfile*))
192 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
193 ;; pathname designator. Since these use the same code, it should
194 ;; suffice to test just :INPUT.
197 (progn (with-open-file (f "run-program-test.tmp" :direction :output)
198 (setf file (truename f))
199 (write-line "Foo" f))
200 (assert (run-program "cat" ()
201 :input file :output t
204 (delete-file file))))
206 ;;; This used to crash on Darwin and trigger recursive lock errors on
208 (with-test (:name (:run-program :stress))
209 ;; Do it a hundred times in batches of 10 so that with a low limit
210 ;; of the number of processes the test can have a chance to pass.
214 #'sb-ext:process-wait
217 (sb-ext:run-program "/bin/echo" '
218 ("It would be nice if this didn't crash.")
219 :wait nil :output nil)))))
221 (with-test (:name (:run-program :pty-stream))
224 (with-output-to-string (s)
225 (assert (= 42 (process-exit-code
226 (run-program "/bin/sh" '("-c" "echo OK; exit 42") :wait t
232 ;; Check whether RUN-PROGRAM puts its child process into the foreground
233 ;; when stdin is inherited. If it fails to do so we will receive a SIGTTIN.
235 ;; We can't check for the signal itself since run-program.c resets the
236 ;; forked process' signal mask to defaults. But the default is `stop'
237 ;; of which we can be notified asynchronously by providing a status hook.
238 (with-test (:name (:run-program :inherit-stdin))
240 (flet ((status-hook (proc)
241 (case (sb-ext:process-status proc)
242 (:stopped (setf stopped t)))))
243 (let ((proc (sb-ext:run-program "/bin/ed" nil :search nil :wait nil
245 :status-hook #'status-hook)))
246 ;; Give the program a generous time to generate the SIGTTIN.
247 ;; If it hasn't done so after that time we can consider it
248 ;; to be working (i.e. waiting for input without generating SIGTTIN).
250 ;; either way we have to signal it to terminate
251 (process-kill proc sb-posix:sigterm)
253 (assert (not stopped))))))