11 #define STDOUT_FILENO 1
14 #define STDERR_FILENO 2
17 extern char **environ;
18 static void close_fd (int *fd) { if ((*fd) >= 0) close (*fd); *fd = -1; }
20 int main(int argc, char *argv[]) {
22 // TODO: if argv > 3, assume pok[] is given, notifify parent.
23 // usage() and a man-page (help2man) would not be bad, either :)
31 pok[0] = atoi(argv[1]);
32 pok[1] = atoi(argv[2]);
33 pin[0] = atoi(argv[3]);
34 pin[1] = atoi(argv[4]);
35 pout[0] = atoi(argv[5]);
36 pout[1] = atoi(argv[6]);
38 int stderr_mode = atoi(argv[7]);
39 int nicelevel = atoi(argv[8]);
41 /* vfork()ed child process - exec external process */
43 fcntl(pok[1], F_SETFD, FD_CLOEXEC);
46 if (pin[0] != STDIN_FILENO) {
47 dup2(pin[0], STDIN_FILENO);
51 if (pout[1] != STDOUT_FILENO) {
52 dup2(pout[1], STDOUT_FILENO);
55 if (stderr_mode == 2) {
56 /* merge STDERR into output */
57 if (pout[1] != STDERR_FILENO) {
58 dup2(pout[1], STDERR_FILENO);
60 } else if (stderr_mode == 1) {
67 if (pout[1] != STDOUT_FILENO && pout[1] != STDERR_FILENO) {
75 /* copy current environment */
78 envp = (char **) calloc(1, sizeof(char*));
79 for (i=0;environ[i];++i) {
80 envp[i] = strdup(environ[i]);
81 envp = (char **) realloc(envp, (i+2) * sizeof(char*));
86 execve(argv[9], &argv[9], envp);
88 /* if we reach here something went wrong.. */
90 (void) write(pok[1], &buf, 1 );