1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
3 ;;; --- Wrapper over lfp_spawn(3)
8 (defun tty-read-fn (fd buf nbytes
)
10 (isys:read fd buf nbytes
)
13 (defun tty-write-fn (fd buf nbytes
)
15 (isys:write fd buf nbytes
)
21 (defclass tty-stream
(iolib.streams
:dual-channel-gray-stream
)
23 (:default-initargs
:read-fn
#'tty-read-fn
24 :write-fn
#'tty-write-fn
))
27 ((pid :initarg
:pid
:reader process-pid
)
28 (status :initform
:running
)
29 (closed :initform nil
)
30 (stdin :reader process-stdin
)
31 (stdout :reader process-stdout
)
32 (stderr :reader process-stderr
)))
34 (defmethod initialize-instance :after
((process process
) &key
35 stdin stdout stderr external-format
)
36 (with-slots ((in stdin
) (out stdout
) (err stderr
))
39 (setf in
(make-instance 'tty-stream
:fd stdin
40 :external-format external-format
)))
42 (setf out
(make-instance 'tty-stream
:fd stdout
43 :external-format external-format
)))
45 (setf err
(make-instance 'tty-stream
:fd stderr
46 :external-format external-format
)))))
48 (defmethod close ((process process
) &key abort
)
49 (if (slot-value process
'closed
)
51 (macrolet ((close-process-stream (slot)
52 `(when (slot-boundp process
',slot
)
53 (close (slot-value process
',slot
) :abort abort
)
54 (slot-makunbound process
',slot
))))
55 (close-process-stream stdin
)
56 (close-process-stream stdout
)
57 (close-process-stream stderr
)
58 (process-status process
:wait
(not abort
))
59 (setf (slot-value process
'closed
) t
)
62 (defmethod print-object ((o process
) s
)
63 (print-unreadable-object (o s
:type t
:identity t
)
64 (format s
"~S ~S ~S ~S"
66 :status
(process-status o
))))
68 (defun exit-status (status)
70 ((isys:wifexited status
)
71 (isys:wexitstatus status
))
72 ((isys:wifsignaled status
)
73 (values (isys:wtermsig
* status
)
74 (isys:wcoredump status
)))))
76 (defmethod process-status ((process process
) &key wait
)
77 (if (integerp (slot-value process
'status
))
78 (exit-status (slot-value process
'status
))
79 (multiple-value-bind (pid status
)
80 (isys:waitpid
(process-pid process
)
81 (if wait
0 isys
:wnohang
))
86 (setf (slot-value process
'status
) status
)
87 (exit-status status
))))))
89 (defmethod process-activep ((process process
))
90 (eql :running
(process-status process
)))
92 (defmethod process-kill ((process process
) &optional
(signum :sigterm
))
93 (isys:kill
(process-pid process
) signum
)
97 (defmacro with-lfp-spawn-arguments
((attributes file-actions pid
) &body body
)
98 (with-gensyms (spawnattr-initialized-p file-actions-initialized-p
)
99 `(with-foreign-objects ((,attributes
'lfp-spawnattr-t
)
100 (,file-actions
'lfp-spawn-file-actions-t
)
102 (let ((,spawnattr-initialized-p nil
)
103 (,file-actions-initialized-p nil
))
106 (setf ,spawnattr-initialized-p
107 (lfp-spawnattr-init ,attributes
))
108 (setf ,file-actions-initialized-p
109 (lfp-spawn-file-actions-init ,file-actions
))
111 (when ,spawnattr-initialized-p
112 (lfp-spawnattr-destroy ,attributes
))
113 (when ,file-actions-initialized-p
114 (lfp-spawn-file-actions-destroy ,file-actions
)))))))
116 (defun allocate-argv (argv program arglist
)
118 (setf (mem-aref argv
:pointer
0)
119 (foreign-string-alloc program
))
120 ;; copy program arguments
122 :for arg
:in arglist
:do
123 (setf (mem-aref argv
:pointer i
)
124 (foreign-string-alloc arg
))))
126 (defun find-program (program)
128 ((eql :shell program
)
131 (file-path-namestring program
))))
133 (defmacro with-argv
(((arg0 argv
) program arguments
) &body body
)
135 `(let ((,program
(find-program ,program
))
136 (,argc
(+ 2 (length ,arguments
))))
137 (with-foreign-object (,argv
:pointer
,argc
)
138 (isys:bzero
,argv
(* ,argc
(isys:sizeof
:pointer
)))
141 (allocate-argv ,argv
,program
,arguments
)
142 (let ((,arg0
(mem-ref ,argv
:pointer
)))
144 (deallocate-null-ended-list ,argv
))))))
146 (defun redirect-one-stream (file-actions fd stream
&optional flags
(mode #o644
) close-old-fd
)
147 (flet ((dup-from-path (path)
148 (lfp-spawn-file-actions-addopen file-actions fd path flags mode
))
150 (lfp-spawn-file-actions-adddup2 file-actions oldfd fd
)
152 (lfp-spawn-file-actions-addclose file-actions oldfd
))))
155 ((or string file-path pathname
)
156 (dup-from-path (file-path-namestring stream
)))
158 (dup-from-path "/dev/null"))
160 (dup-from-fd stream
))
161 (iolib.streams
:dual-channel-fd-mixin
162 (dup-from-fd (iolib.streams
:fd-of stream
)))
164 (lfp-spawn-file-actions-addclose file-actions fd
)))))
166 (defun redirect-to-pipes (file-actions fd keep-write-fd
)
167 (multiple-value-bind (pipe-parent pipe-child
)
169 (when keep-write-fd
(rotatef pipe-parent pipe-child
))
170 (lfp-spawn-file-actions-adddup2 file-actions pipe-child fd
)
171 (lfp-spawn-file-actions-addclose file-actions pipe-parent
)
172 (lfp-spawn-file-actions-addclose file-actions pipe-child
)
173 (values pipe-parent pipe-child
)))
175 (defun setup-redirections (file-actions stdin stdout stderr ptmfd pts
)
176 (let (infd infd-child outfd outfd-child errfd errfd-child
)
180 (setf (values infd infd-child
)
181 (redirect-to-pipes file-actions
+stdin
+ t
)))
183 (setf infd
(isys:dup ptmfd
))
184 (redirect-one-stream file-actions
+stdin
+ pts isys
:o-rdonly
))
185 (t (redirect-one-stream file-actions
+stdin
+ stdin isys
:o-rdonly
)))
189 (setf (values outfd outfd-child
)
190 (redirect-to-pipes file-actions
+stdout
+ nil
)))
192 (setf outfd
(isys:dup ptmfd
))
193 (redirect-one-stream file-actions
+stdout
+ pts
(logior isys
:o-wronly
195 (t (redirect-one-stream file-actions
+stdout
+ stdout
(logior isys
:o-wronly
200 (setf (values errfd errfd-child
)
201 (redirect-to-pipes file-actions
+stderr
+ nil
)))
203 (setf errfd
(isys:dup ptmfd
))
204 (redirect-one-stream file-actions
+stderr
+ pts
(logior isys
:o-wronly
206 (t (redirect-one-stream file-actions
+stderr
+ stderr
(logior isys
:o-wronly
208 (values infd infd-child outfd outfd-child errfd errfd-child
)))
210 (defun close-fds (&rest fds
)
212 (when fd
(isys:close fd
))))
214 (defun setup-slave-pty ()
215 (let ((ptmfd (isys:openpt
(logior isys
:o-rdwr isys
:o-noctty isys
:o-cloexec
))))
217 (isys:unlockpt ptmfd
)
218 (values ptmfd
(isys:ptsname ptmfd
))))
220 (defmacro with-pty
((ptmfd pts
) &body body
)
221 `(multiple-value-bind (,ptmfd
,pts
)
225 (close-fds ,ptmfd
))))
227 (defmacro with-redirections
(((infd outfd errfd
)
228 (file-actions stdin stdout stderr
))
230 (with-gensyms (infd-child outfd-child errfd-child ptmfd pts
)
231 `(with-pty (,ptmfd
,pts
)
232 (multiple-value-bind (,infd
,infd-child
,outfd
,outfd-child
,errfd
,errfd-child
)
233 (setup-redirections ,file-actions
,stdin
,stdout
,stderr
,ptmfd
,pts
)
234 (unwind-protect-case ()
237 (close-fds ,infd-child
,outfd-child
,errfd-child
))
239 (close-fds ,infd
,outfd
,errfd
)))))))
241 (defun process-other-spawn-args (attributes new-session current-directory
244 (lfp-spawnattr-setsid attributes
))
245 (when current-directory
246 (lfp-spawnattr-setcwd attributes current-directory
))
248 (lfp-spawnattr-setuid attributes uid
))
250 (lfp-spawnattr-setgid attributes gid
))
252 (lfp-spawnattr-setflags attributes lfp-spawn-resetids
)))
254 ;; program: :shell - the system shell
255 ;; file-path designator - a path
257 ;; environment: t - inherit environment
258 ;; nil - NULL environment
259 ;; alist - the environment to use
260 ;; stdin, stdout, stderr:
261 ;; file-path designator - open file, redirect to it
262 ;; :null - redirect to /dev/null - useful because /dev/null doesn't exist on Windows
263 ;; file-descriptor designator(integer or stream) - file descriptor, redirecto to it
264 ;; :pipe - create pipe, redirect the child descriptor to one end and wrap the other end
265 ;; into a stream which goes into PROCESS slot
268 ;; new-session: boolean - create a new session using setsid()
269 ;; current-directory: path - a directory to switch to before executing
270 ;; uid: user id - unsigned-byte or string
271 ;; gid: group id - unsigned-byte or string
272 ;; resetids: boolean - reset effective UID and GID to saved IDs
274 (defun create-process (program-and-args &key
(environment t
)
275 (stdin :pipe
) (stdout :pipe
) (stderr :pipe
)
276 new-session current-directory uid gid resetids
277 (external-format :utf-8
))
278 (flet ((new-ctty-p (stdin stdout stderr
)
282 (destructuring-bind (program &rest arguments
)
283 (ensure-list program-and-args
)
284 (when (new-ctty-p stdin stdout stderr
)
285 (setf new-session t
))
286 (with-argv ((arg0 argv
) program arguments
)
287 (with-c-environment (envp environment
)
288 (with-lfp-spawn-arguments (attributes file-actions pid
)
289 (with-redirections ((infd outfd errfd
)
290 (file-actions stdin stdout stderr
))
291 (process-other-spawn-args attributes new-session current-directory
293 (lfp-spawnp pid arg0 argv envp file-actions attributes
)
294 (make-instance 'process
:pid
(mem-ref pid
'pid-t
)
295 :stdin infd
:stdout outfd
:stderr errfd
296 :external-format external-format
))))))))
298 (defun run-program (program-and-args &key
(environment t
) (stderr :pipe
)
299 (external-format :utf-8
))
300 (flet ((slurp (stream)
301 (with-output-to-string (s)
302 (loop :for c
:= (read-char stream nil nil
)
303 :while c
:do
(write-char c s
)))))
304 (let ((process (create-process program-and-args
305 :environment environment
309 :external-format external-format
)))
311 (values (process-status process
:wait t
)
312 (slurp (process-stdout process
))
313 (if (eql :pipe stderr
)
314 (slurp (process-stderr process
))