applied patch from andreas rottmann for providing "process*" which

is the kitchen sink of subprocess creation.
This commit is contained in:
Abdulaziz Ghuloum
2009-09-02 22:47:57 +03:00
parent f33e304606
commit 4ce666c8d6
4 changed files with 182 additions and 71 deletions
+114 -20
View File
@@ -52,40 +52,134 @@ list_to_vec(ikptr x){
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 cmd, ikptr argv /*, ikpcb* pcb */){
ikrt_process(ikptr rvec, ikptr env, ikptr cmd, ikptr argv /*, ikpcb* pcb */){
int infds[2];
int outfds[2];
int errfds[2];
if(pipe(infds)) return ik_errno_to_code();
if(pipe(outfds)) return ik_errno_to_code();
if(pipe(errfds)) return ik_errno_to_code();
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(close(infds[1])) exit(1);
if(close(outfds[0])) exit(1);
if(close(errfds[0])) exit(1);
if(close(0)) exit(1);
if(dup(infds[0]) == -1) exit(1);
if(close(1)) exit(1);
if(dup(outfds[1]) == -1) exit(1);
if(close(2)) exit(2);
if(dup(errfds[1]) == -1) exit(1);
execvp((char*)(long)(cmd+off_bytevector_data), list_to_vec(argv));
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 */
close(infds[0]); /* ignore errors */
close(outfds[1]);
close(errfds[1]);
ref(rvec,off_vector_data+0*wordsize) = fix(pid);
ref(rvec,off_vector_data+1*wordsize) = fix(infds[1]);
ref(rvec,off_vector_data+2*wordsize) = fix(outfds[0]);
ref(rvec,off_vector_data+3*wordsize) = fix(errfds[0]);
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();