Debian Codegraph @

comlib/process.lisp

raw
1 ;;;-*- Mode:Lisp; Syntax:Common-Lisp; Package:CHAOS -*-
2 ;;;
3 ;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;
29 (in-package :CHAOS)
30 #|==============================================================================
31 System: Chaos
32 Module: comlib
33 File: process.lisp
34 ==============================================================================|#
35 #-:chaos-debug
36 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
37 #+:chaos-debug
38 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
39
40 ;;; *NOTE* GCL Specific
41
42 ;;; ************************************
43 ;;; SPAWNING PROCESS WITH TOW-WAY STREAM
44 ;;; ************************************
45
46 ;;;
47 ;;; The following C codes are borrowed from GCL run_process.c
48 ;;; with some minor bug fixes including enhancements by [email protected].
49 ;;;
50
51 (Clines "
52 #undef PAGESIZE
53 #include <errno.h> /* errno global, error codes for UNIX IO */
54 #include <sys/types.h> /* Data types definitions */
55 #include <sys/socket.h> /* Socket definitions with out this forget it */
56 #include <netinet/in.h> /* Internet address definition AF_INET etc... */
57 #include <signal.h> /* UNIX Signal codes */
58 #include <sys/ioctl.h> /* IO control standard UNIx fair */
59 #include <sys/file.h>
60 #include <fcntl.h> /* Function to set socket aync/interrupt */
61 #include <sys/time.h> /* Time for select time out */
62 #include <netdb.h> /* Data Base interface for network files */
63 /* patch by [email protected] */
64 #include <sys/wait.h> /* Wait system call options */
65 /* patch end */
66 #include <stdio.h>
67
68 static char *lisp_to_string(string)
69 object string;
70 {
71 int i, len;
72 char *sself;
73 char *cstr;
74
75 len = string->st.st_fillp;
76
77 cstr = (char *) malloc (len+1);
78 sself = &(string->st.st_self[0]);
79 for (i=0; i<len; i++)
80 {
81 cstr[i] = sself[i];
82 }
83 cstr[i] = 0;
84 return (cstr);
85 }
86
87 /*
88 * make 2 two-way streams
89 */
90
91 static object
92 make_pipe()
93 {
94 int sockets_in[2];
95 int sockets_out[2];
96 FILE *fp1, *fp2;
97 int pid;
98 object stream_in, stream_out, stream;
99
100 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets_in) < 0)
101 FEerror(\"Failure to open socket stream pair\", 0);
102 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets_out) < 0)
103 FEerror(\"Failure to open socket stream pair\", 0);
104 fp1 = fdopen(sockets_in[0], \"r\");
105 fp2 = fdopen(sockets_out[0], \"w\");
106
107 stream_in = (object) alloc_object(t_stream);
108 stream_in->sm.sm_mode = smm_input;
109 stream_in->sm.sm_fp = fp1;
110 stream_in->sm.sm_int0 = sockets_in[1];
111 stream_in->sm.sm_int1 = 0;
112 /* patch by [email protected] -- repairing printing problem */
113 stream_in->sm.sm_object0 = sLstring_char;
114 stream_in->sm.sm_object1 = Cnil;
115 /* end patch */
116 stream_out = (object) alloc_object(t_stream);
117 stream_out->sm.sm_mode = smm_output;
118 stream_out->sm.sm_fp = fp2;
119 setup_stream_buffer(stream_in);
120 setup_stream_buffer(stream_out);
121 stream_out->sm.sm_int0 = sockets_out[1];
122 stream_out->sm.sm_int1 = 0;
123 /* patch by [email protected] -- ditto */
124 stream_out->sm.sm_object0 = sLstring_char;
125 stream_out->sm.sm_object1 = Cnil;
126 /* patch end */
127 stream = make_two_way_stream(stream_in, stream_out);
128 return(stream);
129 }
130
131 /* the routines for spawning off a process with streams
132 *
133 * Assumes that istream and ostream are both associated
134 * with \"C\" type streams.
135 */
136
137 static spawn_child(istream, ostream, pname, argv)
138 object istream;
139 object ostream;
140 char *pname;
141 char **argv;
142 {
143
144 int fdin;
145 int fdout;
146 if (istream->sm.sm_fp == NULL || ostream->sm.sm_fp == NULL)
147 FEerror(\"Cannot spawn process with given stream\", 0);
148 fdin = istream->sm.sm_int0;
149 fdout = ostream->sm.sm_int0;
150 if (fork() == 0)
151 { /* the child --- replace standard in and out with descriptors given */
152 /* patch by [email protected] */
153 setsid(); /* in order to get rid of job control */
154 fclose(istream->sm.sm_fp); /* close parent-side file desc. */
155 fclose(ostream->sm.sm_fp); /* ditto */
156 /* end patch */
157 close(0);
158 dup(fdin);
159 close(1);
160 dup(fdout);
161 if (execvp(pname, argv) == -1)
162 {
163 fprintf(stderr, \"\\n***** Error in process spawning *******\");
164 fflush(stderr);
165 exit(1);
166 }
167 }
168 /* patch by [email protected] */
169 else
170 { /* the parent */
171 close(fdin); /* close child-side file descriptor */
172 close(fdout); /* ditto */
173 }
174 /* end patch */
175 }
176
177 /*
178 #if defined(NeXT)
179 # define WAITPID(pid, statusp, options) \
180 wait3((union wait*)statusp, options, (struct rusage*)0)
181 #else
182 # define WAITPID(pid, statusp, options) \
183 waitpid(pid, statusp, options)
184 #endif
185 */
186
187 /* patch by [email protected] */
188 static reap_child()
189 {
190 /*
191 int dummy;
192 while (WAITPID(-1, &dummy, WNOHANG) > 0)
193 ;
194 */
195 }
196 /* end patch */
197
198 static run_child(file, arglist)
199 object file;
200 object arglist;
201 {
202 char *filename = object_to_string(file);
203 char *argv[100];
204 object stream;
205 int i;
206
207 /* patch by [email protected] */
208 signal(SIGCHLD, reap_child);
209 /* end patch */
210
211 argv[0] = \"\";
212 for(i = 1; arglist != Cnil; i++) {
213 argv[i] = lisp_to_string(arglist->c.c_car);
214 arglist = arglist->c.c_cdr;
215 }
216 argv[i] = (char *)0;
217
218 stream = make_pipe();
219 spawn_child(stream->sm.sm_object1,
220 stream->sm.sm_object0,
221 filename, argv);
222 return(stream);
223
224 }
225 "
226 )
227
228 (defentry run-child (object object) (object run_child))
229
230 (defentry make-pipe () (object make_pipe))
231
232 ;;;
233 ;;;
234 (defstruct process
235 (name "" :type string :read-only t)
236 (in-stream)
237 (out-stream))
238
239 ;;;
240 (defun run-process (program &optional args)
241 (let ((stream (run-child program args)))
242 (make-process :name program
243 :in-stream (si::fp-input-stream stream)
244 :out-stream (si::fp-output-stream stream))))
245
246 (defmacro with-write-to-process ((process) &body body)
247 ` (let ((*standard-output* (process-out-stream ,process)))
248 ,@body
249 (finish-output)))
250
251 (defmacro with-read-from-process ((process) &body body)
252 ` (let ((*standard-input* (process-in-stream ,process)))
253 ,@body
254 ))