| 1 |
;;;; process.lsp -- External processes. |
| 2 |
|
| 3 |
;;;; Copyright (c) 2003, Juan Jose Garcia-Ripoll |
| 4 |
;;;; Copyright (c) 2017, Daniel Kochmański |
| 5 |
;;;; |
| 6 |
;;;; See file 'LICENSE' for the copyright details. |
| 7 |
|
| 8 |
(in-package "EXT") |
| 9 |
|
| 10 |
(defmacro with-process-lock ((process &optional (wait t)) &body body) |
| 11 |
#+threads |
| 12 |
(ext:with-unique-names (lock wait-p) |
| 13 |
`(let ((,lock (external-process-%lock ,process)) |
| 14 |
(,wait-p ,wait)) |
| 15 |
(when (mp:get-lock ,lock ,wait-p) |
| 16 |
(mp:without-interrupts |
| 17 |
(unwind-protect (mp::with-restored-interrupts |
| 18 |
(locally ,@body)) |
| 19 |
(mp:giveup-lock ,lock)))))) |
| 20 |
#-threads `(progn ,@body)) |
| 21 |
|
| 22 |
(defstruct (external-process (:constructor make-external-process ())) |
| 23 |
pid |
| 24 |
input |
| 25 |
output |
| 26 |
error-stream |
| 27 |
(%status :running) |
| 28 |
(%code nil) |
| 29 |
#+threads (%lock (mp:make-lock)) |
| 30 |
#+threads (%pipe (mp:make-process))) |
| 31 |
|
| 32 |
(defun external-process-status (external-process) |
| 33 |
(let ((status (external-process-%status external-process))) |
| 34 |
(if (member status '(:stopped :resumed :running)) |
| 35 |
(ext:external-process-wait external-process nil) |
| 36 |
(values status (external-process-%code external-process))))) |
| 37 |
|
| 38 |
;;; --------------------------------------------------------------------- |
| 39 |
;;; si:waitpid -> (values status code pid) |
| 40 |
;;; --------------------------------------------------------------------- |
| 41 |
;;; no change :: (values nil nil nil) |
| 42 |
;;; error :: (values (member :abort :error) nil nil) |
| 43 |
;;; finished :: (values (member :exited :signaled) code pid) |
| 44 |
;;; running :: (values (member :stopped :resumed :running) code pid) |
| 45 |
;;; --------------------------------------------------------------------- |
| 46 |
(defun external-process-wait (process &optional wait) |
| 47 |
(with-process-lock (process wait) |
| 48 |
(let ((pid (external-process-pid process))) |
| 49 |
(when pid |
| 50 |
(multiple-value-bind (status code pid) (si:waitpid pid wait) |
| 51 |
(declare (ignore pid)) |
| 52 |
(ecase status |
| 53 |
((:exited :signaled :abort :error) |
| 54 |
(setf (external-process-pid process) nil |
| 55 |
(external-process-%status process) status |
| 56 |
(external-process-%code process) code) |
| 57 |
#+threads (mp:process-join (external-process-%pipe process))) |
| 58 |
((:stopped :resumed :running) |
| 59 |
(setf (external-process-%status process) status |
| 60 |
(external-process-%code process) code)) |
| 61 |
((nil) #| wait was nil and process didn't change |#)))))) |
| 62 |
(values (external-process-%status process) |
| 63 |
(external-process-%code process))) |
| 64 |
|
| 65 |
(defun terminate-process (process &optional force) |
| 66 |
(with-process-lock (process) |
| 67 |
(let ((pid (external-process-pid process))) |
| 68 |
(when pid |
| 69 |
#+windows |
| 70 |
(ffi:c-inline |
| 71 |
(process pid) (:object :object) :void |
| 72 |
"HANDLE *ph = (HANDLE*)ecl_foreign_data_pointer_safe(#1); |
| 73 |
int ret = TerminateProcess(*ph, -1); |
| 74 |
if (ret == 0) FEwin32_error(\"Cannot terminate the process ~A\", 1, #0);") |
| 75 |
#-windows |
| 76 |
(unless (zerop (si:killpid pid (if force +sigkill+ +sigterm+))) |
| 77 |
(error "Cannot terminate the process ~A" process)))))) |
| 78 |
|
| 79 |
;;; |
| 80 |
;;; Backwards compatible SI:SYSTEM call. We avoid ANSI C system() |
| 81 |
;;; because we are consuming the process wait status using a SIGCHLD |
| 82 |
;;; handler -- this breaks some C libraries out there (OS X 32 bit). |
| 83 |
;;; |
| 84 |
#+ (or) |
| 85 |
(defun system (cmd-string) |
| 86 |
(let ((shell "/bin/sh") |
| 87 |
(option "-c")) |
| 88 |
#+windows |
| 89 |
(let ((comspec (getenv "ComSpec"))) |
| 90 |
(when comspec |
| 91 |
(setf shell comspec |
| 92 |
option "/c"))) |
| 93 |
(nth-value 1 (run-program shell (list option cmd-string) |
| 94 |
:wait t :output nil :input nil :error nil |
| 95 |
#+windows :escape-arguments #+windows nil)))) |
| 96 |
|
| 97 |
;;; We don't handle `sigchld' because we don't want races with |
| 98 |
;;; `external-process-wait'. Take care of forgotten processes. |
| 99 |
(defun finalize-external-process (process) |
| 100 |
;; INV: this finalizer also closes the process handle on windows |
| 101 |
;; since external-process-wait calls si:waitpid which closes the |
| 102 |
;; handle once the process has exited. |
| 103 |
(unless (member (ext:external-process-wait process nil) |
| 104 |
'(:exited :signaled :abort :error)) |
| 105 |
(ext:set-finalizer process #'finalize-external-process))) |
| 106 |
|
| 107 |
;;; |
| 108 |
;;; Almighty EXT:RUN-PROGRAM. Built on top of SI:SPAWN-SUBPROCESS. For simpler |
| 109 |
;;; alternative see SI:RUN-PROGRAM-INNER. |
| 110 |
;;; |
| 111 |
(defun run-program (command argv |
| 112 |
&key |
| 113 |
(input :stream) |
| 114 |
(output :stream) |
| 115 |
(error :output) |
| 116 |
(wait t) |
| 117 |
(environ :default) |
| 118 |
(if-input-does-not-exist nil) |
| 119 |
(if-output-exists :error) |
| 120 |
(if-error-exists :error) |
| 121 |
(external-format :default) |
| 122 |
#+windows (escape-arguments t)) |
| 123 |
|
| 124 |
(when (eql input t) (setf input *standard-input*)) |
| 125 |
(when (eql output t) (setf output *standard-output*)) |
| 126 |
(when (eql error t) (setf error *error-output*)) |
| 127 |
|
| 128 |
(labels ((process-stream (which &rest args) |
| 129 |
(si:while (typep which 'synonym-stream) |
| 130 |
(setf which (symbol-value (synonym-stream-symbol which)))) |
| 131 |
(cond ((null which) |
| 132 |
(null-stream (getf args :direction))) |
| 133 |
((or (stringp which) |
| 134 |
(pathnamep which)) |
| 135 |
(apply #'open which :external-format external-format args)) |
| 136 |
#+clos-streams |
| 137 |
((typep which 'virtual-stream) |
| 138 |
:virtual-stream) |
| 139 |
((or (eql which :stream) |
| 140 |
(streamp which)) |
| 141 |
which) |
| 142 |
;; signal error as early as possible |
| 143 |
(T (error "Invalid ~S argument to EXT:RUN-PROGRAM" which)))) |
| 144 |
(prepare-args (args) |
| 145 |
#-windows |
| 146 |
args |
| 147 |
#+windows |
| 148 |
(with-output-to-string (str) |
| 149 |
(loop for (arg . rest) on args |
| 150 |
do (if (and escape-arguments |
| 151 |
(find-if (lambda (c) |
| 152 |
(find c '(#\Space #\Tab #\"))) |
| 153 |
arg)) |
| 154 |
(escape-arg arg str) |
| 155 |
(princ arg str)) |
| 156 |
(when rest |
| 157 |
(write-char #\Space str))))) |
| 158 |
(null-stream (direction) |
| 159 |
(open #-windows "/dev/null" |
| 160 |
#+windows "nul" |
| 161 |
:direction direction |
| 162 |
:if-exists :overwrite)) |
| 163 |
(verify-stream (stream stream-type) |
| 164 |
(case stream |
| 165 |
((nil) |
| 166 |
(when (null (case stream-type |
| 167 |
(:input if-input-does-not-exist) |
| 168 |
(:output if-output-exists) |
| 169 |
(:error if-error-exists))) |
| 170 |
(return-from run-program nil)) |
| 171 |
(null-stream (if (eql stream-type :input) |
| 172 |
:output |
| 173 |
:input))) |
| 174 |
(:virtual-stream :stream) |
| 175 |
(otherwise stream)))) |
| 176 |
(let ((args (prepare-args (cons command argv))) |
| 177 |
(process (make-external-process)) |
| 178 |
(process-input (process-stream input |
| 179 |
:direction :input |
| 180 |
:if-does-not-exist if-input-does-not-exist)) |
| 181 |
(process-output (process-stream output |
| 182 |
:direction :output |
| 183 |
:if-exists if-output-exists)) |
| 184 |
(process-error (if (eql error :output) |
| 185 |
:output |
| 186 |
(process-stream error |
| 187 |
:direction :output |
| 188 |
:if-exists if-error-exists))) |
| 189 |
pid parent-write parent-read parent-error) |
| 190 |
|
| 191 |
(multiple-value-setq (pid parent-write parent-read parent-error) |
| 192 |
(si:spawn-subprocess command args environ |
| 193 |
(verify-stream process-input :input) |
| 194 |
(verify-stream process-output :output) |
| 195 |
(verify-stream process-error :error))) |
| 196 |
|
| 197 |
(let ((stream-write |
| 198 |
(when (plusp parent-write) |
| 199 |
(ext:make-stream-from-fd parent-write :output |
| 200 |
:element-type 'base-char |
| 201 |
:external-format external-format))) |
| 202 |
(stream-read |
| 203 |
(when (plusp parent-read) |
| 204 |
(ext:make-stream-from-fd parent-read :input |
| 205 |
:element-type 'base-char |
| 206 |
:external-format external-format))) |
| 207 |
(stream-error |
| 208 |
(when (plusp parent-error) |
| 209 |
(ext:make-stream-from-fd parent-error :input |
| 210 |
:element-type 'base-char |
| 211 |
:external-format external-format))) |
| 212 |
(pipes nil)) |
| 213 |
|
| 214 |
(when (eql process-input :virtual-stream) |
| 215 |
(push (list input stream-write :input) pipes)) |
| 216 |
(when (eql process-output :virtual-stream) |
| 217 |
(push (list stream-read output :output) pipes)) |
| 218 |
(when (eql process-error :virtual-stream) |
| 219 |
(push (list stream-error error :error) pipes)) |
| 220 |
|
| 221 |
(setf (external-process-pid process) pid |
| 222 |
(external-process-input process) stream-write |
| 223 |
(external-process-output process) stream-read |
| 224 |
(external-process-error-stream process) stream-error) |
| 225 |
|
| 226 |
(when pipes |
| 227 |
#+threads |
| 228 |
(let ((thread (external-process-%pipe process))) |
| 229 |
(mp:process-preset thread #'pipe-streams process pipes) |
| 230 |
(mp:process-enable thread)) |
| 231 |
#-threads |
| 232 |
(if wait |
| 233 |
(pipe-streams process pipes) |
| 234 |
(warn "EXT:RUN-PROGRAM: Ignoring virtual stream I/O argument."))) |
| 235 |
|
| 236 |
(if wait |
| 237 |
(si:external-process-wait process t) |
| 238 |
(ext:set-finalizer process #'finalize-external-process)) |
| 239 |
|
| 240 |
(values (if (and stream-read stream-write) |
| 241 |
(make-two-way-stream stream-read stream-write) |
| 242 |
(or stream-read stream-write)) |
| 243 |
(external-process-%code process) |
| 244 |
process))))) |
| 245 |
|
| 246 |
#+windows |
| 247 |
(defun escape-arg (arg stream) |
| 248 |
;; Normally, #\\ doesn't have to be escaped But if #\" follows #\\, then they |
| 249 |
;; have to be escaped. Do that by counting the number of consequent |
| 250 |
;; backslashes, and upon encoutering #\" immediately after them, output the |
| 251 |
;; same number of backslashes, plus one for #\" |
| 252 |
(write-char #\" stream) |
| 253 |
(loop with slashes = 0 |
| 254 |
for i below (length arg) |
| 255 |
for previous-char = #\a then char |
| 256 |
for char = (char arg i) |
| 257 |
do |
| 258 |
(case char |
| 259 |
(#\" |
| 260 |
(loop repeat slashes |
| 261 |
do (write-char #\\ stream)) |
| 262 |
(write-string "\\\"" stream)) |
| 263 |
(t |
| 264 |
(write-char char stream))) |
| 265 |
(case char |
| 266 |
(#\\ |
| 267 |
(incf slashes)) |
| 268 |
(t |
| 269 |
(setf slashes 0))) |
| 270 |
finally |
| 271 |
;; The final #\" counts too, but doesn't need to be escaped itself |
| 272 |
(loop repeat slashes |
| 273 |
do (write-char #\\ stream))) |
| 274 |
(write-char #\" stream)) |
| 275 |
|
| 276 |
|
| 277 |
(defun pipe-streams (process pipes &aux to-remove) |
| 278 |
;; note we don't use serve-event here because process input may be a virtual |
| 279 |
;; stream and `select' won't catch this stream change. |
| 280 |
(flet ((thunk () |
| 281 |
(loop for pipe in pipes |
| 282 |
for (input output type) = pipe |
| 283 |
do (when (or (null (open-stream-p output)) |
| 284 |
(null (open-stream-p input)) |
| 285 |
(let ((next-char (read-char-no-hang input nil :eof))) |
| 286 |
(cond |
| 287 |
((eq next-char :eof) |
| 288 |
t) |
| 289 |
(next-char |
| 290 |
(unread-char next-char input) |
| 291 |
(si:copy-stream input output nil))))) |
| 292 |
(when (eq type :input) |
| 293 |
(close output)) |
| 294 |
(push pipe to-remove))))) |
| 295 |
(si:until (or (null pipes) |
| 296 |
(member (external-process-wait process nil) |
| 297 |
'(:exited :signaled :abort :error))) |
| 298 |
(thunk) |
| 299 |
;; remove from the list exhausted streams |
| 300 |
(when to-remove |
| 301 |
(setf pipes (set-difference pipes to-remove))) |
| 302 |
(sleep 0.001)) |
| 303 |
;; something may still be in pipes after child termination |
| 304 |
(thunk))) |