masqmail

annotate 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
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@391 44 } else if (pid == 0) {
meillo@391 45 /* child */
meillo@10 46 int i, max_fd = sysconf(_SC_OPEN_MAX);
meillo@10 47 dup2(pipe[0], 0);
meillo@10 48 dup2(pipe[0], 1);
meillo@10 49 dup2(pipe[0], 2);
meillo@0 50
meillo@10 51 if (max_fd <= 0)
meillo@10 52 max_fd = 64;
meillo@10 53 for (i = 3; i < max_fd; i++)
meillo@10 54 close(i);
meillo@10 55
meillo@10 56 {
meillo@391 57 char *argv[] = { "/bin/sh", "-c",
meillo@391 58 (char *) command, NULL };
meillo@10 59 execve(*argv, argv, NULL);
meillo@10 60 }
meillo@391 61 logwrite(LOG_ALERT, "execve failed: %s\n",
meillo@391 62 strerror(errno));
meillo@262 63 _exit(1);
meillo@10 64 } else {
meillo@391 65 /* parent */
meillo@10 66 close(pipe[0]);
meillo@10 67 return pipe[1];
meillo@10 68 }
meillo@10 69 }
meillo@10 70 return -2;
meillo@0 71 }