masqmail-0.2

diff src/child.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/child.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,79 @@
     1.4 +/* child.c, Copyright (C) 2000 by Oliver Kurth,
     1.5 + *
     1.6 + * This program is free software; you can redistribute it and/or modify
     1.7 + * it under the terms of the GNU General Public License as published by
     1.8 + * the Free Software Foundation; either version 2 of the License, or
     1.9 + * (at your option) any later version.
    1.10 + * 
    1.11 + * This program is distributed in the hope that it will be useful,
    1.12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.14 + * GNU General Public License for more details.
    1.15 + *
    1.16 + * You should have received a copy of the GNU General Public License
    1.17 + * along with this program; if not, write to the Free Software
    1.18 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    1.19 + */
    1.20 +
    1.21 +#include <errno.h>
    1.22 +#include <stdio.h>
    1.23 +#include <stdlib.h>
    1.24 +#include <unistd.h>
    1.25 +#include <signal.h>
    1.26 +#include <sys/types.h>
    1.27 +#include <sys/socket.h>
    1.28 +#include <syslog.h>
    1.29 +#include <string.h>
    1.30 +
    1.31 +#include "masqmail.h"
    1.32 +
    1.33 +int volatile sigchild_seen = 0;
    1.34 +
    1.35 +static
    1.36 +void sigchild_handler(int sig)
    1.37 +{
    1.38 +  sigchild_seen = 1;
    1.39 +  signal(SIGHUP, sigchild_handler);
    1.40 +}
    1.41 +
    1.42 +int child(const char *command)
    1.43 +{
    1.44 +  int pipe[2];
    1.45 +
    1.46 +  if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0){
    1.47 +    pid_t pid;
    1.48 +      
    1.49 +    /*
    1.50 +    sigchild_seen = 0;
    1.51 +    signal(SIGCHLD, sigchild_handler);
    1.52 +    */
    1.53 +
    1.54 +    pid = fork();
    1.55 +    if(pid == 0){
    1.56 +      int i, max_fd = sysconf(_SC_OPEN_MAX);
    1.57 +      /* child */
    1.58 +      dup2(pipe[0], 0);
    1.59 +      dup2(pipe[0], 1);
    1.60 +      dup2(pipe[0], 2);
    1.61 +
    1.62 +      if(max_fd <= 0) max_fd = 64;
    1.63 +      for(i = 3; i < max_fd; i++)
    1.64 +	close(i);
    1.65 +
    1.66 +      {
    1.67 +	char *argv [] = { "/bin/sh", "-c", (char*) command, NULL };
    1.68 +	execve (*argv, argv, NULL);
    1.69 +      }
    1.70 +      logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno));
    1.71 +      _exit(EXIT_FAILURE);
    1.72 +    }else if(pid == -1){
    1.73 +      return -1;
    1.74 +    }else{
    1.75 +      close(pipe[0]);
    1.76 +      return pipe[1];
    1.77 +    }
    1.78 +  }
    1.79 +  return -2;
    1.80 +}
    1.81 +
    1.82 +