Debian Codegraph @

src/ikarus-process.c

raw
1 /*
2 * Ikarus Scheme -- A compiler for R6RS Scheme.
3 * Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include "ikarus-data.h"
28
29 extern ikptr ik_errno_to_code();
30
31 static int
32 list_length(ikptr x){
33 int n = 0;
34 while(tagof(x) == pair_tag){
35 n++;
36 x = ref(x, off_cdr);
37 }
38 return n;
39 }
40
41 static char**
42 list_to_vec(ikptr x){
43 int n = list_length(x);
44 char** vec = malloc((n+1) * sizeof(char*));
45 if (vec == NULL) exit(-1);
46 int i;
47 for(i=0; i<n; i++){
48 vec[i] = (char*)(long)ref(x, off_car) + off_bytevector_data;
49 x = ref(x, off_cdr);
50 }
51 vec[n] = 0;
52 return vec;
53 }
54
55 static int
56 execvpe_(const char *cmd, char *const *argv, char *const *envp){
57 char *path = NULL;
58 const char *searchpath;
59 const char *sep;
60 size_t cmd_len;
61
62 if (cmd[0] == '/')
63 execve(cmd, argv, envp);
64
65 searchpath = getenv("PATH");
66 if (searchpath == NULL)
67 searchpath = "/bin:/usr/bin";
68
69 cmd_len = strlen(cmd);
70
71 sep = NULL;
72 do {
73 size_t prefix_len, path_len;
74
75 sep = strchr(searchpath, ':');
76 if (sep == NULL) {
77 sep = searchpath + strlen(searchpath);
78 }
79
80 prefix_len = (sep - searchpath);
81 path_len = prefix_len + cmd_len + 2;
82 path = realloc(path, path_len);
83 if (path == NULL) {
84 errno = ENOMEM;
85 return -1;
86 }
87 memcpy(path, searchpath, prefix_len);
88 if (prefix_len == 0 || searchpath[prefix_len - 1] == '/') {
89 memcpy(path + prefix_len, cmd, cmd_len + 1);
90 } else {
91 path[prefix_len] = '/';
92 memcpy(path + prefix_len + 1, cmd, cmd_len + 1);
93 }
94
95 execve(path, argv, envp);
96 switch (errno) {
97 case E2BIG:
98 case ENOEXEC:
99 case ENOMEM:
100 case ETXTBSY:
101 break; /* these are treated as error, abort search */
102 }
103
104 searchpath = sep + 1;
105 } while (sep[0] != '\0');
106
107 if (path) free(path);
108
109 return -1;
110 }
111
112 ikptr
113 ikrt_process(ikptr rvec, ikptr env, ikptr cmd, ikptr argv /*, ikpcb* pcb */){
114 int infds[2];
115 int outfds[2];
116 int errfds[2];
117 int search_p = ref(rvec, off_vector_data+0*wordsize) != false_object;
118 int stdin_fd = unfix(ref(rvec, off_vector_data+1*wordsize));
119 int stdout_fd = unfix(ref(rvec, off_vector_data+2*wordsize));
120 int stderr_fd = unfix(ref(rvec, off_vector_data+3*wordsize));
121
122 if(stdin_fd < 0 && pipe(infds)) return ik_errno_to_code();
123 if(stdout_fd < 0 && pipe(outfds)) return ik_errno_to_code();
124 if(stderr_fd < 0 && pipe(errfds)) return ik_errno_to_code();
125 pid_t pid = fork();
126 if(pid == 0){
127 /* child */
128 if (stdin_fd < 0){
129 if(close(infds[1])) exit(1);
130 stdin_fd = infds[0];
131 }
132 if (stdout_fd < 0){
133 if(close(outfds[0])) exit(1);
134 stdout_fd = outfds[1];
135 }
136 if (stderr_fd < 0){
137 if(close(errfds[0])) exit(1);
138 stderr_fd = errfds[1];
139 }
140 if (stdin_fd != 0){
141 if(close(0)) exit(1);
142 if(dup(stdin_fd) == -1) exit(1);
143 }
144 if (stdout_fd != 1){
145 if(close(1)) exit(1);
146 if(dup(stdout_fd) == -1) exit(1);
147 }
148 if (stderr_fd != 2){
149 if(close(2)) exit(2);
150 if(dup(stderr_fd) == -1) exit(1);
151 }
152 char *cmd_str = (char*)(long)(cmd+off_bytevector_data);
153 char **env_strs = env == false_object ? 0 : list_to_vec(env);
154 char **argv_strs = list_to_vec(argv);
155 if (env_strs && search_p)
156 execvpe_(cmd_str, argv_strs, env_strs);
157 else if (env_strs)
158 execve(cmd_str, argv_strs, env_strs);
159 else if (search_p)
160 execvp(cmd_str, argv_strs);
161 else
162 execv(cmd_str, argv_strs);
163 fprintf(stderr, "failed to exec %s: %s\n",
164 (char*)(long)(cmd+off_bytevector_data),
165 strerror(errno));
166 exit(-1);
167 } else if(pid > 0){
168 /* parent */
169 ref(rvec,off_vector_data+0*wordsize) = fix(pid);
170
171 if (stdin_fd < 0) {
172 close(infds[0]); /* ignore errors */
173 ref(rvec,off_vector_data+1*wordsize) = fix(infds[1]);
174 }
175 if (stdout_fd < 0) {
176 close(outfds[1]);
177 ref(rvec,off_vector_data+2*wordsize) = fix(outfds[0]);
178 }
179 if (stderr_fd < 0) {
180 close(errfds[1]);
181 ref(rvec,off_vector_data+3*wordsize) = fix(errfds[0]);
182 }
183 return rvec;
184 } else {
185 return ik_errno_to_code();
186 }
187 }
188
189 typedef struct signal_info {
190 int n;
191 ikptr c;
192 } signal_info;
193
194 #define signal_info_table_len 28
195
196 static signal_info signal_info_table[signal_info_table_len] = {
197 /* Signals from POSIX */
198 {SIGABRT, fix(1)},
199 {SIGALRM, fix(2)},
200 {SIGBUS, fix(3)},
201 {SIGCHLD, fix(4)},
202 {SIGCONT, fix(5)},
203 {SIGFPE, fix(6)},
204 {SIGHUP, fix(7)},
205 {SIGILL, fix(8)},
206 {SIGINT, fix(9)},
207 {SIGKILL, fix(10)},
208 {SIGPIPE, fix(11)},
209 {SIGQUIT, fix(12)},
210 {SIGSEGV, fix(13)},
211 {SIGSTOP, fix(14)},
212 {SIGTERM, fix(15)},
213 {SIGTSTP, fix(16)},
214 {SIGTTIN, fix(17)},
215 {SIGTTOU, fix(18)},
216 {SIGUSR1, fix(19)},
217 {SIGUSR2, fix(20)},
218 #ifdef SIGPOLL
219 {SIGPOLL, fix(21)},
220 #else
221 {SIGEMT, fix(21)},
222 #endif
223 {SIGPROF, fix(22)},
224 {SIGSYS, fix(23)},
225 {SIGTRAP, fix(24)},
226 {SIGURG, fix(25)},
227 {SIGVTALRM, fix(26)},
228 {SIGXCPU, fix(27)},
229 {SIGXFSZ, fix(28)}
230 };
231
232
233 ikptr
234 ik_signal_num_to_code(int signum){
235 signal_info* si;
236 int i;
237 for(i=0; i < signal_info_table_len; i++){
238 si = &signal_info_table[i];
239 if(si->n == signum){
240 return si->c;
241 }
242 }
243 fprintf(stderr, "\n*** ik_signal_num_to_code: Don't know signal %d ***\n\n",
244 signum);
245 return fix(99999);
246 }
247
248 int
249 ik_signal_code_to_num(ikptr sigcode){
250 signal_info* si;
251 int i;
252 for(i=0; i < signal_info_table_len; i++){
253 si = &signal_info_table[i];
254 if(si->c == sigcode){
255 return si->n;
256 }
257 }
258 fprintf(stderr, "ik_signal_code_to_num: Don't know code %ld\n",
259 unfix(sigcode));
260 exit(EXIT_FAILURE);
261 return 0;
262 }
263
264 static
265 void
266 ik_sigset(sigset_t* s, ikptr sigcodes){
267 while(tagof(sigcodes) == pair_tag){
268 sigaddset(s, ik_signal_code_to_num(ref(sigcodes, off_car)));
269 sigcodes = ref(sigcodes, off_cdr);
270 }
271 }
272
273 static
274 ikptr
275 ik_sigmembers(sigset_t* s, ikpcb* pcb){
276 int i;
277 signal_info* p;
278 ikptr accum = null_object;
279 pcb->root0 = &accum;
280 for(i = signal_info_table_len - 1; i >= 0; i--){
281 p = &signal_info_table[i];
282 if (sigismember(s, p->n)) {
283 ikptr x = ik_safe_alloc(pcb, pair_size) + pair_tag;
284 ref(x, off_car) = p->c;
285 ref(x, off_cdr) = accum;
286 accum = x;
287 }
288 }
289 pcb->root0 = NULL;
290 return accum;
291 }
292
293 ikptr
294 ik_sigprocmask(ikptr how, ikptr sigcodes, ikptr get_old, ikpcb* pcb){
295 int h = 0;
296 sigset_t s, o;
297 sigset_t* ps, * po;
298 if (sigcodes != false_object){
299 int u = unfix(how);
300 if (1==u) h = SIG_BLOCK;
301 else if (2==u) h = SIG_SETMASK;
302 else if (3==u) h = SIG_UNBLOCK;
303 ps = &s;
304 sigemptyset(ps);
305 ik_sigset(ps, sigcodes);
306 } else
307 ps = NULL;
308 if (get_old != false_object){
309 po = &o;
310 sigemptyset(po);
311 } else
312 po = NULL;
313 if (sigprocmask(h, ps, po))
314 return ik_errno_to_code();
315 else if (po)
316 return ik_sigmembers(po, pcb);
317 else
318 return true_object;
319 }
320
321 ikptr
322 ik_sigpending(ikpcb* pcb){
323 sigset_t s;
324 sigemptyset(&s);
325 sigpending(&s);
326 return ik_sigmembers(&s, pcb);
327 }
328
329 ikptr
330 ikrt_kill(ikptr pid, ikptr sigcode /*, ikpcb* pcb */){
331 int r = kill((pid_t)unfix(pid), ik_signal_code_to_num(sigcode));
332 if(r == 0){
333 return fix(0);
334 }
335 return ik_errno_to_code();
336 }
337
338 ikptr
339 ikrt_waitpid(ikptr rvec, ikptr pid, ikptr block /*, ikpcb* pcb */){
340 /* rvec is assumed to come in as #(#f #f #f) */
341 int status, options = 0;
342 if(block == false_object){
343 options = WNOHANG;
344 }
345 pid_t r = waitpid(unfix(pid), &status, options);
346 if(r > 0){
347 ref(rvec, off_record_data+0*wordsize) = fix(r);
348 if(WIFEXITED(status)) {
349 ref(rvec, off_record_data+1*wordsize) = fix(WEXITSTATUS(status));
350 }
351 if(WIFSIGNALED(status)) {
352 ref(rvec, off_record_data+2*wordsize) =
353 ik_signal_num_to_code(WTERMSIG(status));
354 }
355 return rvec;
356 } else if(r == 0){ /* would have blocked */
357 return fix(0);
358 } else {
359 return ik_errno_to_code();
360 }
361 }
362
363 ikptr
364 ikrt_getpid(void)
365 {
366 return fix(getpid());
367 }
368
369 ikptr
370 ikrt_getppid(void)
371 {
372 return fix(getppid());
373 }