Mercurial > masqmail
comparison src/child.c @ 10:26e34ae9a3e3
changed indention and line wrapping to a more consistent style
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:23:10 +0100 |
parents | 08114f7dcc23 |
children | 98cda87105a7 |
comparison
equal
deleted
inserted
replaced
9:31cc8a89cb74 | 10:26e34ae9a3e3 |
---|---|
2 * | 2 * |
3 * This program is free software; you can redistribute it and/or modify | 3 * This program is free software; you can redistribute it and/or modify |
4 * it under the terms of the GNU General Public License as published by | 4 * it under the terms of the GNU General Public License as published by |
5 * the Free Software Foundation; either version 2 of the License, or | 5 * the Free Software Foundation; either version 2 of the License, or |
6 * (at your option) any later version. | 6 * (at your option) any later version. |
7 * | 7 * |
8 * This program is distributed in the hope that it will be useful, | 8 * This program is distributed in the hope that it will be useful, |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 * GNU General Public License for more details. | 11 * GNU General Public License for more details. |
12 * | 12 * |
27 | 27 |
28 #include "masqmail.h" | 28 #include "masqmail.h" |
29 | 29 |
30 int volatile sigchild_seen = 0; | 30 int volatile sigchild_seen = 0; |
31 | 31 |
32 static | 32 static void |
33 void sigchild_handler(int sig) | 33 sigchild_handler(int sig) |
34 { | 34 { |
35 sigchild_seen = 1; | 35 sigchild_seen = 1; |
36 signal(SIGHUP, sigchild_handler); | 36 signal(SIGHUP, sigchild_handler); |
37 } | 37 } |
38 | 38 |
39 int child(const char *command) | 39 int |
40 child(const char *command) | |
40 { | 41 { |
41 int pipe[2]; | 42 int pipe[2]; |
42 | 43 |
43 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0){ | 44 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0) { |
44 pid_t pid; | 45 pid_t pid; |
45 | |
46 /* | |
47 sigchild_seen = 0; | |
48 signal(SIGCHLD, sigchild_handler); | |
49 */ | |
50 | 46 |
51 pid = fork(); | 47 /* |
52 if(pid == 0){ | 48 sigchild_seen = 0; |
53 int i, max_fd = sysconf(_SC_OPEN_MAX); | 49 signal(SIGCHLD, sigchild_handler); |
54 /* child */ | 50 */ |
55 dup2(pipe[0], 0); | |
56 dup2(pipe[0], 1); | |
57 dup2(pipe[0], 2); | |
58 | 51 |
59 if(max_fd <= 0) max_fd = 64; | 52 pid = fork(); |
60 for(i = 3; i < max_fd; i++) | 53 if (pid == 0) { |
61 close(i); | 54 int i, max_fd = sysconf(_SC_OPEN_MAX); |
55 /* child */ | |
56 dup2(pipe[0], 0); | |
57 dup2(pipe[0], 1); | |
58 dup2(pipe[0], 2); | |
62 | 59 |
63 { | 60 if (max_fd <= 0) |
64 char *argv [] = { "/bin/sh", "-c", (char*) command, NULL }; | 61 max_fd = 64; |
65 execve (*argv, argv, NULL); | 62 for (i = 3; i < max_fd; i++) |
66 } | 63 close(i); |
67 logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno)); | 64 |
68 _exit(EXIT_FAILURE); | 65 { |
69 }else if(pid == -1){ | 66 char *argv[] = { "/bin/sh", "-c", (char *) command, NULL }; |
70 return -1; | 67 execve(*argv, argv, NULL); |
71 }else{ | 68 } |
72 close(pipe[0]); | 69 logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno)); |
73 return pipe[1]; | 70 _exit(EXIT_FAILURE); |
74 } | 71 } else if (pid == -1) { |
75 } | 72 return -1; |
76 return -2; | 73 } else { |
74 close(pipe[0]); | |
75 return pipe[1]; | |
76 } | |
77 } | |
78 return -2; | |
77 } | 79 } |
78 | |
79 |