masqmail-0.2

annotate src/child.c @ 3:8c55886cacd8

man pages will be maintained in troff now
author meillo@marmaro.de
date Fri, 26 Sep 2008 21:40:10 +0200
parents
children 26e34ae9a3e3
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@0 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@0 32 static
meillo@0 33 void sigchild_handler(int sig)
meillo@0 34 {
meillo@0 35 sigchild_seen = 1;
meillo@0 36 signal(SIGHUP, sigchild_handler);
meillo@0 37 }
meillo@0 38
meillo@0 39 int child(const char *command)
meillo@0 40 {
meillo@0 41 int pipe[2];
meillo@0 42
meillo@0 43 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0){
meillo@0 44 pid_t pid;
meillo@0 45
meillo@0 46 /*
meillo@0 47 sigchild_seen = 0;
meillo@0 48 signal(SIGCHLD, sigchild_handler);
meillo@0 49 */
meillo@0 50
meillo@0 51 pid = fork();
meillo@0 52 if(pid == 0){
meillo@0 53 int i, max_fd = sysconf(_SC_OPEN_MAX);
meillo@0 54 /* child */
meillo@0 55 dup2(pipe[0], 0);
meillo@0 56 dup2(pipe[0], 1);
meillo@0 57 dup2(pipe[0], 2);
meillo@0 58
meillo@0 59 if(max_fd <= 0) max_fd = 64;
meillo@0 60 for(i = 3; i < max_fd; i++)
meillo@0 61 close(i);
meillo@0 62
meillo@0 63 {
meillo@0 64 char *argv [] = { "/bin/sh", "-c", (char*) command, NULL };
meillo@0 65 execve (*argv, argv, NULL);
meillo@0 66 }
meillo@0 67 logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno));
meillo@0 68 _exit(EXIT_FAILURE);
meillo@0 69 }else if(pid == -1){
meillo@0 70 return -1;
meillo@0 71 }else{
meillo@0 72 close(pipe[0]);
meillo@0 73 return pipe[1];
meillo@0 74 }
meillo@0 75 }
meillo@0 76 return -2;
meillo@0 77 }
meillo@0 78
meillo@0 79