comparison src/child.c @ 391:0ca270ca11fa

Refactoring and code layouting.
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 17:53:04 +0100
parents b27f66555ba8
children 885e3d886199
comparison
equal deleted inserted replaced
390:68ae9182059c 391:0ca270ca11fa
37 37
38 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0) { 38 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0) {
39 pid_t pid; 39 pid_t pid;
40 40
41 pid = fork(); 41 pid = fork();
42 if (pid == 0) { 42 if (pid == -1) {
43 return -1;
44 } else if (pid == 0) {
45 /* child */
43 int i, max_fd = sysconf(_SC_OPEN_MAX); 46 int i, max_fd = sysconf(_SC_OPEN_MAX);
44 /* child */
45 dup2(pipe[0], 0); 47 dup2(pipe[0], 0);
46 dup2(pipe[0], 1); 48 dup2(pipe[0], 1);
47 dup2(pipe[0], 2); 49 dup2(pipe[0], 2);
48 50
49 if (max_fd <= 0) 51 if (max_fd <= 0)
50 max_fd = 64; 52 max_fd = 64;
51 for (i = 3; i < max_fd; i++) 53 for (i = 3; i < max_fd; i++)
52 close(i); 54 close(i);
53 55
54 { 56 {
55 char *argv[] = { "/bin/sh", "-c", (char *) command, NULL }; 57 char *argv[] = { "/bin/sh", "-c",
58 (char *) command, NULL };
56 execve(*argv, argv, NULL); 59 execve(*argv, argv, NULL);
57 } 60 }
58 logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno)); 61 logwrite(LOG_ALERT, "execve failed: %s\n",
62 strerror(errno));
59 _exit(1); 63 _exit(1);
60 } else if (pid == -1) {
61 return -1;
62 } else { 64 } else {
65 /* parent */
63 close(pipe[0]); 66 close(pipe[0]);
64 return pipe[1]; 67 return pipe[1];
65 } 68 }
66 } 69 }
67 return -2; 70 return -2;