masqmail
view 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 |
line source
1 /*
2 ** child.c
3 ** Copyright (C) 2000 by Oliver Kurth,
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <syslog.h>
28 #include <string.h>
30 #include "masqmail.h"
33 int
34 child(const char *command)
35 {
36 int pipe[2];
38 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0) {
39 pid_t pid;
41 pid = fork();
42 if (pid == -1) {
43 return -1;
44 } else if (pid == 0) {
45 /* child */
46 int i, max_fd = sysconf(_SC_OPEN_MAX);
47 dup2(pipe[0], 0);
48 dup2(pipe[0], 1);
49 dup2(pipe[0], 2);
51 if (max_fd <= 0)
52 max_fd = 64;
53 for (i = 3; i < max_fd; i++)
54 close(i);
56 {
57 char *argv[] = { "/bin/sh", "-c",
58 (char *) command, NULL };
59 execve(*argv, argv, NULL);
60 }
61 logwrite(LOG_ALERT, "execve failed: %s\n",
62 strerror(errno));
63 _exit(1);
64 } else {
65 /* parent */
66 close(pipe[0]);
67 return pipe[1];
68 }
69 }
70 return -2;
71 }