meillo@367: /* meillo@367: ** child.c meillo@367: ** Copyright (C) 2000 by Oliver Kurth, meillo@367: ** meillo@367: ** This program is free software; you can redistribute it and/or modify meillo@367: ** it under the terms of the GNU General Public License as published by meillo@367: ** the Free Software Foundation; either version 2 of the License, or meillo@367: ** (at your option) any later version. meillo@367: ** meillo@367: ** This program is distributed in the hope that it will be useful, meillo@367: ** but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@367: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@367: ** GNU General Public License for more details. meillo@367: ** meillo@367: ** You should have received a copy of the GNU General Public License meillo@367: ** along with this program; if not, write to the Free Software meillo@367: ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. meillo@367: */ 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: 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: pid = fork(); meillo@391: if (pid == -1) { meillo@391: return -1; meillo@391: } else if (pid == 0) { meillo@391: /* child */ meillo@10: int i, max_fd = sysconf(_SC_OPEN_MAX); 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@391: char *argv[] = { "/bin/sh", "-c", meillo@391: (char *) command, NULL }; meillo@10: execve(*argv, argv, NULL); meillo@10: } meillo@391: logwrite(LOG_ALERT, "execve failed: %s\n", meillo@391: strerror(errno)); meillo@262: _exit(1); meillo@10: } else { meillo@391: /* parent */ meillo@10: close(pipe[0]); meillo@10: return pipe[1]; meillo@10: } meillo@10: } meillo@10: return -2; meillo@0: }