masqmail

annotate src/child.c @ 369:19d57eb1b6a1

Minor refactoring change (else-if).
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:23:47 +0200
parents fc1c6425c024
children 0ca270ca11fa
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@10 42 if (pid == 0) {
meillo@10 43 int i, max_fd = sysconf(_SC_OPEN_MAX);
meillo@10 44 /* child */
meillo@10 45 dup2(pipe[0], 0);
meillo@10 46 dup2(pipe[0], 1);
meillo@10 47 dup2(pipe[0], 2);
meillo@0 48
meillo@10 49 if (max_fd <= 0)
meillo@10 50 max_fd = 64;
meillo@10 51 for (i = 3; i < max_fd; i++)
meillo@10 52 close(i);
meillo@10 53
meillo@10 54 {
meillo@10 55 char *argv[] = { "/bin/sh", "-c", (char *) command, NULL };
meillo@10 56 execve(*argv, argv, NULL);
meillo@10 57 }
meillo@10 58 logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno));
meillo@262 59 _exit(1);
meillo@10 60 } else if (pid == -1) {
meillo@10 61 return -1;
meillo@10 62 } else {
meillo@10 63 close(pipe[0]);
meillo@10 64 return pipe[1];
meillo@10 65 }
meillo@10 66 }
meillo@10 67 return -2;
meillo@0 68 }