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