masqmail

annotate src/child.c @ 64:0379789a847b

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