| 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"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 19 | #include "runprog.h"cxx . . $ `<file>/src/bnetd/runprog.h`/ |
| 20 | |
| 21 | #include <cerrno> |
| 22 | #include <cstdlib> |
| 23 | #include <cstring> |
| 24 | |
| 25 | #ifdef HAVE_SYS_TYPES_Hcxx . . $ `build/config.h:16:9`! |
| 26 | # include <sys/types.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_UNISTD_Hcxx . . $ `build/config.h:7:9`! |
| 29 | # include <unistd.h> |
| 30 | #endif |
| 31 | #ifdef HAVE_SYS_WAIT_Hcxx . . $ `build/config.h:17:9`! |
| 32 | # include <sys/wait.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "compat/stdfileno.h"cxx . . $ `<file>/src/compat/stdfileno.h`/ |
| 36 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 37 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 38 | |
| 39 | namespace pvpgncxx . . $ pvpgn/ |
| 40 | { |
| 41 | |
| 42 | namespace bnetdcxx . . $ pvpgn/bnetd/ |
| 43 | { |
| 44 | |
| 45 | |
| 46 | #ifdef DO_SUBPROCcxx . . $ `src/common/setup_before.h:349:10`! |
| 47 | static pid_t currpidcxx . . $ pvpgn/bnetd/currpid. = 0; |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | extern stdcxx . . $ std/::FILEcxx . . $ std/FILE# * runprog_opencxx . . $ pvpgn/bnetd/runprog_open(270b3c3705632523).(char const * command) |
| 52 | { |
| 53 | #ifndef DO_SUBPROCcxx . . $ `src/common/setup_before.h:349:10`! |
| 54 | return NULL; /* always fail */ |
| 55 | #else |
| 56 | int fds[2]; |
| 57 | stdcxx . . $ std/::FILEcxx . . $ std/FILE# * pp; |
| 58 | |
| 59 | if (!command) |
| 60 | { |
| 61 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL command"); |
| 62 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 63 | } |
| 64 | |
| 65 | if (pipe(fds) < 0) |
| 66 | { |
| 67 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not create pipe (pipe: {})", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 68 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 69 | } |
| 70 | |
| 71 | switch ((currpidcxx . . $ pvpgn/bnetd/currpid. = fork())) |
| 72 | { |
| 73 | case 0: |
| 74 | close(fds[0]); |
| 75 | |
| 76 | close(STDINFDcxx . . $ `src/compat/stdfileno.h:26:10`!); |
| 77 | close(STDOUTFDcxx . . $ `src/compat/stdfileno.h:32:10`!); |
| 78 | close(STDERRFDcxx . . $ `src/compat/stdfileno.h:38:10`!); |
| 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] != STDOUTFDcxx . . $ `src/compat/stdfileno.h:32:10`!) |
| 85 | dup2(fds[1], STDOUTFDcxx . . $ `src/compat/stdfileno.h:32:10`!); |
| 86 | if (fds[1] != STDERRFDcxx . . $ `src/compat/stdfileno.h:38:10`!) |
| 87 | dup2(fds[1], STDERRFDcxx . . $ `src/compat/stdfileno.h:38:10`!); |
| 88 | if (fds[1] != STDOUTFDcxx . . $ `src/compat/stdfileno.h:32:10`! && fds[1] != STDERRFDcxx . . $ `src/compat/stdfileno.h:38:10`!) |
| 89 | close(fds[1]); |
| 90 | |
| 91 | if (execlp(command, command, (char *)NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) < 0) |
| 92 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not execute \"{}\" (execlp: {})", command, stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 93 | |
| 94 | stdcxx . . $ std/::exit(127); /* popen exec failure code */ |
| 95 | |
| 96 | case -1: |
| 97 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not fork (fork: {})", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 98 | close(fds[0]); |
| 99 | close(fds[1]); |
| 100 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 101 | |
| 102 | default: |
| 103 | close(fds[1]); |
| 104 | |
| 105 | if (!(pp = fdopen(fds[0], "r"))) |
| 106 | { |
| 107 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not streamify output (fdopen: {})", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 108 | close(fds[0]); |
| 109 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return pp; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | |
| 118 | extern int runprog_closecxx . . $ pvpgn/bnetd/runprog_close(89bc1efcdac0ca22).(stdcxx . . $ std/::FILEcxx . . $ std/FILE# * pp) |
| 119 | { |
| 120 | #ifndef DO_SUBPROCcxx . . $ `src/common/setup_before.h:349:10`! |
| 121 | return -1; /* always fail */ |
| 122 | #else |
| 123 | int status; |
| 124 | pid_t pid; |
| 125 | |
| 126 | if (!pp) |
| 127 | { |
| 128 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "got NULL pp"); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | if (stdcxx . . $ std/::fclosecxx . . $ std/fclose(89bc1efcdac0ca22).(pp) < 0) |
| 133 | { |
| 134 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_errorcxx . . $ pvpgn/eventlog_level_error., __FUNCTION__, "could not close process (std::fclose: {})", stdcxx . . $ std/::strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | for (;;) |
| 139 | { |
| 140 | # ifdef HAVE_WAITPIDcxx . . $ `build/config.h:85:9`! |
| 141 | pid = waitpid(currpidcxx . . $ pvpgn/bnetd/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 (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) |
| 154 | return 0; |
| 155 | } |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | } |