| 1 | /* |
| 2 | * Copyright (C) 2000 Ross Combs ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include "common/setup_before.h" |
| 19 | #include "runprog.h" |
| 20 | |
| 21 | #include <cerrno> |
| 22 | #include <cstdlib> |
| 23 | #include <cstring> |
| 24 | |
| 25 | #ifdef HAVE_SYS_TYPES_H |
| 26 | # include <sys/types.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_UNISTD_H |
| 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | #ifdef HAVE_SYS_WAIT_H |
| 32 | # include <sys/wait.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "compat/stdfileno.h" |
| 36 | #include "common/eventlog.h" |
| 37 | #include "common/setup_after.h" |
| 38 | |
| 39 | namespace pvpgn |
| 40 | { |
| 41 | |
| 42 | namespace bnetd |
| 43 | { |
| 44 | |
| 45 | |
| 46 | #ifdef DO_SUBPROC |
| 47 | static pid_t currpid = 0; |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | extern std::FILE * runprog_open(char const * command) |
| 52 | { |
| 53 | #ifndef DO_SUBPROC |
| 54 | return NULL; /* always fail */ |
| 55 | #else |
| 56 | int fds[2]; |
| 57 | std::FILE * pp; |
| 58 | |
| 59 | if (!command) |
| 60 | { |
| 61 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL command"); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | if (pipe(fds) < 0) |
| 66 | { |
| 67 | eventlog(eventlog_level_error, __FUNCTION__, "could not create pipe (pipe: {})", std::strerror(errno)); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | switch ((currpid = fork())) |
| 72 | { |
| 73 | case 0: |
| 74 | close(fds[0]); |
| 75 | |
| 76 | close(STDINFD); |
| 77 | close(STDOUTFD); |
| 78 | close(STDERRFD); |
| 79 | /* FIXME: we should close all other fds to make sure the program doesn't use them. |
| 80 | For now, leave it alone because we would either have to keep track of them all |
| 81 | or do a for for (fd=0; fd<BIGNUMBER; fd++) close(fd); loop :( */ |
| 82 | |
| 83 | /* assume prog doesn't use stdin */ |
| 84 | if (fds[1] != STDOUTFD) |
| 85 | dup2(fds[1], STDOUTFD); |
| 86 | if (fds[1] != STDERRFD) |
| 87 | dup2(fds[1], STDERRFD); |
| 88 | if (fds[1] != STDOUTFD && fds[1] != STDERRFD) |
| 89 | close(fds[1]); |
| 90 | |
| 91 | if (execlp(command, command, (char *)NULL) < 0) |
| 92 | eventlog(eventlog_level_error, __FUNCTION__, "could not execute \"{}\" (execlp: {})", command, std::strerror(errno)); |
| 93 | |
| 94 | std::exit(127); /* popen exec failure code */ |
| 95 | |
| 96 | case -1: |
| 97 | eventlog(eventlog_level_error, __FUNCTION__, "could not fork (fork: {})", std::strerror(errno)); |
| 98 | close(fds[0]); |
| 99 | close(fds[1]); |
| 100 | return NULL; |
| 101 | |
| 102 | default: |
| 103 | close(fds[1]); |
| 104 | |
| 105 | if (!(pp = fdopen(fds[0], "r"))) |
| 106 | { |
| 107 | eventlog(eventlog_level_error, __FUNCTION__, "could not streamify output (fdopen: {})", std::strerror(errno)); |
| 108 | close(fds[0]); |
| 109 | return NULL; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return pp; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | |
| 118 | extern int runprog_close(std::FILE * pp) |
| 119 | { |
| 120 | #ifndef DO_SUBPROC |
| 121 | return -1; /* always fail */ |
| 122 | #else |
| 123 | int status; |
| 124 | pid_t pid; |
| 125 | |
| 126 | if (!pp) |
| 127 | { |
| 128 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL pp"); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | if (std::fclose(pp) < 0) |
| 133 | { |
| 134 | eventlog(eventlog_level_error, __FUNCTION__, "could not close process (std::fclose: {})", std::strerror(errno)); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | for (;;) |
| 139 | { |
| 140 | # ifdef HAVE_WAITPID |
| 141 | pid = waitpid(currpid, &status, 0); |
| 142 | # else |
| 143 | # ifdef HAVE_WAIT |
| 144 | pid = wait(&status); |
| 145 | if (pid != currpid) |
| 146 | continue; |
| 147 | # else |
| 148 | return 0; /* assume program succeeded and hope that SIGCHLD handles the zombie */ |
| 149 | # endif |
| 150 | # endif |
| 151 | if (pid != -1) |
| 152 | return status; |
| 153 | if (errno != EINTR) |
| 154 | return 0; |
| 155 | } |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | } |