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@10: * 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@10: static void meillo@10: sigchild_handler(int sig) meillo@0: { meillo@10: sigchild_seen = 1; meillo@10: signal(SIGHUP, sigchild_handler); meillo@0: } meillo@0: meillo@10: int meillo@10: child(const char *command) meillo@0: { meillo@10: int pipe[2]; meillo@0: meillo@10: if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe) == 0) { meillo@10: pid_t pid; meillo@0: meillo@10: /* meillo@10: sigchild_seen = 0; meillo@10: signal(SIGCHLD, sigchild_handler); meillo@10: */ meillo@0: meillo@10: pid = fork(); meillo@10: if (pid == 0) { meillo@10: int i, max_fd = sysconf(_SC_OPEN_MAX); meillo@10: /* child */ meillo@10: dup2(pipe[0], 0); meillo@10: dup2(pipe[0], 1); meillo@10: dup2(pipe[0], 2); meillo@0: meillo@10: if (max_fd <= 0) meillo@10: max_fd = 64; meillo@10: for (i = 3; i < max_fd; i++) meillo@10: close(i); meillo@10: meillo@10: { meillo@10: char *argv[] = { "/bin/sh", "-c", (char *) command, NULL }; meillo@10: execve(*argv, argv, NULL); meillo@10: } meillo@10: logwrite(LOG_ALERT, "execve failed: %s\n", strerror(errno)); meillo@10: _exit(EXIT_FAILURE); meillo@10: } else if (pid == -1) { meillo@10: return -1; meillo@10: } else { meillo@10: close(pipe[0]); meillo@10: return pipe[1]; meillo@10: } meillo@10: } meillo@10: return -2; meillo@0: }