masqmail

view src/child.c @ 49:58e8dd8bc90d

Added tag 0.2.22 for changeset f3bdb5b382ec
author meillo@marmaro.de
date Thu, 20 May 2010 21:23:19 +0200
parents 08114f7dcc23
children 98cda87105a7
line source
1 /* child.c, Copyright (C) 2000 by Oliver Kurth,
2 *
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
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <signal.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <syslog.h>
26 #include <string.h>
28 #include "masqmail.h"
30 int volatile sigchild_seen = 0;
32 static void
33 sigchild_handler(int sig)
34 {
35 sigchild_seen = 1;
36 signal(SIGHUP, sigchild_handler);
37 }
39 int
40 child(const char *command)
41 {
42 int pipe[2];
44 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0) {
45 pid_t pid;
47 /*
48 sigchild_seen = 0;
49 signal(SIGCHLD, sigchild_handler);
50 */
52 pid = fork();
53 if (pid == 0) {
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);
60 if (max_fd <= 0)
61 max_fd = 64;
62 for (i = 3; i < max_fd; i++)
63 close(i);
65 {
66 char *argv[] = { "/bin/sh", "-c", (char *) command, NULL };
67 execve(*argv, argv, NULL);
68 }
69 logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno));
70 _exit(EXIT_FAILURE);
71 } else if (pid == -1) {
72 return -1;
73 } else {
74 close(pipe[0]);
75 return pipe[1];
76 }
77 }
78 return -2;
79 }