comparison src/child.c @ 401:885e3d886199

Various minor refactoring.
author markus schnalke <meillo@marmaro.de>
date Tue, 21 Feb 2012 16:11:00 +0100
parents 0ca270ca11fa
children
comparison
equal deleted inserted replaced
400:6500db550a03 401:885e3d886199
39 pid_t pid; 39 pid_t pid;
40 40
41 pid = fork(); 41 pid = fork();
42 if (pid == -1) { 42 if (pid == -1) {
43 return -1; 43 return -1;
44
44 } else if (pid == 0) { 45 } else if (pid == 0) {
45 /* child */ 46 /* child */
47 char *argv[] = { "/bin/sh", "-c", (char *)command,
48 NULL };
46 int i, max_fd = sysconf(_SC_OPEN_MAX); 49 int i, max_fd = sysconf(_SC_OPEN_MAX);
50
47 dup2(pipe[0], 0); 51 dup2(pipe[0], 0);
48 dup2(pipe[0], 1); 52 dup2(pipe[0], 1);
49 dup2(pipe[0], 2); 53 dup2(pipe[0], 2);
50 54
51 if (max_fd <= 0) 55 if (max_fd <= 0) {
52 max_fd = 64; 56 max_fd = 64;
53 for (i = 3; i < max_fd; i++) 57 }
58 for (i = 3; i < max_fd; i++) {
54 close(i); 59 close(i);
55
56 {
57 char *argv[] = { "/bin/sh", "-c",
58 (char *) command, NULL };
59 execve(*argv, argv, NULL);
60 } 60 }
61 execve(*argv, argv, NULL);
61 logwrite(LOG_ALERT, "execve failed: %s\n", 62 logwrite(LOG_ALERT, "execve failed: %s\n",
62 strerror(errno)); 63 strerror(errno));
63 _exit(1); 64 _exit(1);
65
64 } else { 66 } else {
65 /* parent */ 67 /* parent */
66 close(pipe[0]); 68 close(pipe[0]);
67 return pipe[1]; 69 return pipe[1];
68 } 70 }