meillo@0: /* child.c, Copyright (C) 2000 by Oliver Kurth, meillo@0: * meillo@0: * This program is free software; you can redistribute it and/or modify meillo@0: * it under the terms of the GNU General Public License as published by meillo@0: * the Free Software Foundation; either version 2 of the License, or meillo@0: * (at your option) any later version. meillo@0: * meillo@0: * This program is distributed in the hope that it will be useful, meillo@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: * GNU General Public License for more details. meillo@0: * meillo@0: * You should have received a copy of the GNU General Public License meillo@0: * along with this program; if not, write to the Free Software meillo@0: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. meillo@0: */ meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: meillo@0: #include "masqmail.h" meillo@0: meillo@0: int volatile sigchild_seen = 0; meillo@0: meillo@0: static meillo@0: void sigchild_handler(int sig) meillo@0: { meillo@0: sigchild_seen = 1; meillo@0: signal(SIGHUP, sigchild_handler); meillo@0: } meillo@0: meillo@0: int child(const char *command) meillo@0: { meillo@0: int pipe[2]; meillo@0: meillo@0: if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0){ meillo@0: pid_t pid; meillo@0: meillo@0: /* meillo@0: sigchild_seen = 0; meillo@0: signal(SIGCHLD, sigchild_handler); meillo@0: */ meillo@0: meillo@0: pid = fork(); meillo@0: if(pid == 0){ meillo@0: int i, max_fd = sysconf(_SC_OPEN_MAX); meillo@0: /* child */ meillo@0: dup2(pipe[0], 0); meillo@0: dup2(pipe[0], 1); meillo@0: dup2(pipe[0], 2); meillo@0: meillo@0: if(max_fd <= 0) max_fd = 64; meillo@0: for(i = 3; i < max_fd; i++) meillo@0: close(i); meillo@0: meillo@0: { meillo@0: char *argv [] = { "/bin/sh", "-c", (char*) command, NULL }; meillo@0: execve (*argv, argv, NULL); meillo@0: } meillo@0: logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno)); meillo@0: _exit(EXIT_FAILURE); meillo@0: }else if(pid == -1){ meillo@0: return -1; meillo@0: }else{ meillo@0: close(pipe[0]); meillo@0: return pipe[1]; meillo@0: } meillo@0: } meillo@0: return -2; meillo@0: } meillo@0: meillo@0: