File: ikarus-process.c

package info (click to toggle)
ikarus 0.0.3+bzr.2010.01.26-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, sid
  • size: 39,868 kB
  • ctags: 9,284
  • sloc: lisp: 47,954; ansic: 13,247; sh: 4,595; java: 641; asm: 366; makefile: 264; awk: 186; perl: 66
file content (297 lines) | stat: -rw-r--r-- 7,445 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 *  Ikarus Scheme -- A compiler for R6RS Scheme.
 *  Copyright (C) 2006,2007,2008  Abdulaziz Ghuloum
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *  
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "ikarus-data.h"

extern ikptr ik_errno_to_code();

static int 
list_length(ikptr x){
  int n = 0;
  while(tagof(x) == pair_tag){
    n++;
    x = ref(x, off_cdr);
  }
  return n;
}

static char**
list_to_vec(ikptr x){
  int n = list_length(x);
  char** vec = malloc((n+1) * sizeof(char*));
  if (vec == NULL) exit(-1);
  int i;
  for(i=0; i<n; i++){
    vec[i] = (char*)(long)ref(x, off_car) + off_bytevector_data;
    x = ref(x, off_cdr);
  }
  vec[n] = 0;
  return vec;
}

static int
execvpe_(const char *cmd, char *const *argv, char *const *envp){
  char *path = NULL;
  const char *searchpath;
  const char *sep;
  size_t cmd_len;
  
  if (cmd[0] == '/')
    execve(cmd, argv, envp);
  
  searchpath = getenv("PATH");
  if (searchpath == NULL)
    searchpath = "/bin:/usr/bin";

  cmd_len = strlen(cmd);
  
  sep = NULL;
  do {
    size_t prefix_len, path_len;
    
    sep = strchr(searchpath, ':');
    if (sep == NULL) {
      sep = searchpath + strlen(searchpath);
    }
    
    prefix_len = (sep - searchpath);
    path_len = prefix_len + cmd_len + 2;
    path = realloc(path, path_len);
    if (path == NULL) {
      errno = ENOMEM;
      return -1;
    }
    memcpy(path, searchpath, prefix_len);
    if (prefix_len == 0 || searchpath[prefix_len - 1] == '/') {
      memcpy(path + prefix_len, cmd, cmd_len + 1);
    } else {
      path[prefix_len] = '/';
      memcpy(path + prefix_len + 1, cmd, cmd_len + 1);
    }
    
    execve(path, argv, envp);
    switch (errno) {
    case E2BIG:
    case ENOEXEC:
    case ENOMEM:
    case ETXTBSY:
      break; /* these are treated as error, abort search */
    }
    
    searchpath = sep + 1;
  } while (sep[0] != '\0');

  if (path) free(path);
  
  return -1;
}

ikptr 
ikrt_process(ikptr rvec, ikptr env, ikptr cmd, ikptr argv /*, ikpcb* pcb */){
  int infds[2];
  int outfds[2];
  int errfds[2];
  int search_p = ref(rvec, off_vector_data+0*wordsize) != false_object;
  int stdin_fd = unfix(ref(rvec, off_vector_data+1*wordsize));
  int stdout_fd = unfix(ref(rvec, off_vector_data+2*wordsize));
  int stderr_fd = unfix(ref(rvec, off_vector_data+3*wordsize));

  if(stdin_fd < 0 && pipe(infds))  return ik_errno_to_code();
  if(stdout_fd < 0 && pipe(outfds)) return ik_errno_to_code();
  if(stderr_fd < 0 && pipe(errfds)) return ik_errno_to_code();
  pid_t pid = fork();
  if(pid == 0){
    /* child */
    if (stdin_fd < 0){
      if(close(infds[1]))  exit(1);
      stdin_fd = infds[0];
    }
    if (stdout_fd < 0){
      if(close(outfds[0])) exit(1);
      stdout_fd = outfds[1];
    }
    if (stderr_fd < 0){
        if(close(errfds[0])) exit(1);
        stderr_fd = errfds[1];
    }
    if (stdin_fd != 0){
      if(close(0))             exit(1);
      if(dup(stdin_fd) == -1)  exit(1);
    }
    if (stdout_fd != 1){
      if(close(1))             exit(1);
      if(dup(stdout_fd) == -1) exit(1);
    }
    if (stderr_fd != 2){
      if(close(2))             exit(2);
      if(dup(stderr_fd) == -1) exit(1);
    }
    char *cmd_str = (char*)(long)(cmd+off_bytevector_data);
    char **env_strs = env == false_object ? 0 : list_to_vec(env);
    char **argv_strs = list_to_vec(argv);
    if (env_strs && search_p)
      execvpe_(cmd_str, argv_strs, env_strs);
    else if (env_strs)
      execve(cmd_str, argv_strs, env_strs);
    else if (search_p)
      execvp(cmd_str, argv_strs);
    else
      execv(cmd_str, argv_strs);
    fprintf(stderr, "failed to exec %s: %s\n", 
        (char*)(long)(cmd+off_bytevector_data),
        strerror(errno));
    exit(-1);
  } else if(pid > 0){
    /* parent */
    ref(rvec,off_vector_data+0*wordsize) = fix(pid);

    if (stdin_fd < 0) {
      close(infds[0]); /* ignore errors */
      ref(rvec,off_vector_data+1*wordsize) = fix(infds[1]);
    }
    if (stdout_fd < 0) {
      close(outfds[1]);
      ref(rvec,off_vector_data+2*wordsize) = fix(outfds[0]);
    }
    if (stderr_fd < 0) {
      close(errfds[1]);
      ref(rvec,off_vector_data+3*wordsize) = fix(errfds[0]);
    }
    return rvec;
  } else {
    return ik_errno_to_code();
  }
}

typedef struct signal_info {
  int n;
  ikptr c;
} signal_info;

#define signal_info_table_len 28

static signal_info signal_info_table[signal_info_table_len] = {
  /* Signals from POSIX */
  {SIGABRT,     fix(1)},
  {SIGALRM,     fix(2)},
  {SIGBUS,      fix(3)},
  {SIGCHLD,     fix(4)},
  {SIGCONT,     fix(5)},
  {SIGFPE,      fix(6)},
  {SIGHUP,      fix(7)},
  {SIGILL,      fix(8)},
  {SIGINT,      fix(9)},
  {SIGKILL,     fix(10)},
  {SIGPIPE,     fix(11)},
  {SIGQUIT,     fix(12)},
  {SIGSEGV,     fix(13)},
  {SIGSTOP,     fix(14)},
  {SIGTERM,     fix(15)},
  {SIGTSTP,     fix(16)},
  {SIGTTIN,     fix(17)},
  {SIGTTOU,     fix(18)},
  {SIGUSR1,     fix(19)},
  {SIGUSR2,     fix(20)},
#ifdef SIGPOLL
  {SIGPOLL,     fix(21)},
#else
  {SIGEMT,      fix(21)},
#endif
  {SIGPROF,     fix(22)},
  {SIGSYS,      fix(23)},
  {SIGTRAP,     fix(24)},
  {SIGURG,      fix(25)},
  {SIGVTALRM,   fix(26)},
  {SIGXCPU,     fix(27)},
  {SIGXFSZ,     fix(28)}
};


ikptr
ik_signal_num_to_code(int signum){
  signal_info* si;
  int i;
  for(i=0; i < signal_info_table_len; i++){
    si = &signal_info_table[i];
    if(si->n == signum){
      return si->c;
    }
  }
  fprintf(stderr, "\n*** ik_signal_num_to_code: Don't know signal %d ***\n\n", 
          signum);
  return fix(99999);
}

int
ik_signal_code_to_num(ikptr sigcode){
  signal_info* si;
  int i;
  for(i=0; i < signal_info_table_len; i++){
    si = &signal_info_table[i];
    if(si->c == sigcode){
      return si->n;
    }
  }
  fprintf(stderr, "ik_signal_code_to_num: Don't know code %ld\n",
          unfix(sigcode));
  exit(EXIT_FAILURE);
  return 0;
}

ikptr
ikrt_kill(ikptr pid, ikptr sigcode /*, ikpcb* pcb */){
  int r = kill((pid_t)unfix(pid), ik_signal_code_to_num(sigcode));
  if(r == 0){
    return fix(0);
  }
  return ik_errno_to_code();
}

ikptr 
ikrt_waitpid(ikptr rvec, ikptr pid, ikptr block /*, ikpcb* pcb */){
  /* rvec is assumed to come in as #(#f #f #f) */
  int status, options = 0;
  if(block == false_object){
    options = WNOHANG;
  }
  pid_t r = waitpid(unfix(pid), &status, options);
  if(r > 0){
    ref(rvec, off_record_data+0*wordsize) = fix(r);
    if(WIFEXITED(status)) {
      ref(rvec, off_record_data+1*wordsize) = fix(WEXITSTATUS(status));
    }
    if(WIFSIGNALED(status)) {
      ref(rvec, off_record_data+2*wordsize) = 
        ik_signal_num_to_code(WTERMSIG(status));
    }
    return rvec;
  } else if(r == 0){  /* would have blocked */
    return fix(0);
  } else {
    return ik_errno_to_code();
  }
}