00001 00002 /* 00003 * server.c Set up and handle communications with a server process. 00004 * 00005 * Server Handling copyright 1992-1999, 2004 The Free Software Foundation 00006 * 00007 * Server Handling is free software. 00008 * You may redistribute it and/or modify it under the terms of the 00009 * GNU General Public License, as published by the Free Software 00010 * Foundation; either version 2, or (at your option) any later version. 00011 * 00012 * Server Handling is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with Server Handling. See the file "COPYING". If not, 00019 * write to: The Free Software Foundation, Inc., 00020 * 51 Franklin Street, Fifth Floor, 00021 * Boston, MA 02110-1301, USA. 00022 * 00023 * As a special exception, The Free Software Foundation gives 00024 * permission for additional uses of the text contained in his release 00025 * of ServerHandler. 00026 * 00027 * The exception is that, if you link the ServerHandler library with other 00028 * files to produce an executable, this does not by itself cause the 00029 * resulting executable to be covered by the GNU General Public License. 00030 * Your use of that executable is in no way restricted on account of 00031 * linking the ServerHandler library code into it. 00032 * 00033 * This exception does not however invalidate any other reasons why 00034 * the executable file might be covered by the GNU General Public License. 00035 * 00036 * This exception applies only to the code released by The Free 00037 * Software Foundation under the name ServerHandler. If you copy code 00038 * from other sources under the General Public License into a copy of 00039 * ServerHandler, as the General Public License permits, the exception 00040 * does not apply to the code that you add in this way. To avoid 00041 * misleading anyone as to the status of such modified files, you must 00042 * delete this exception notice from them. 00043 * 00044 * If you write modifications of your own for ServerHandler, it is your 00045 * choice whether to permit this exception to apply to your modifications. 00046 * If you do not wish that, delete this exception notice. 00047 */ 00048 00049 #include "fixlib.h" 00050 #include "server.h" 00051 00052 STATIC const char* def_args[] = 00053 { (char *) NULL, (char *) NULL }; 00054 00055 /* 00056 * chain_open 00057 * 00058 * Given an FD for an inferior process to use as stdin, 00059 * start that process and return a NEW FD that that process 00060 * will use for its stdout. Requires the argument vector 00061 * for the new process and, optionally, a pointer to a place 00062 * to store the child's process id. 00063 */ 00064 int 00065 chain_open (int stdin_fd, tCC** pp_args, pid_t* p_child) 00066 { 00067 t_fd_pair stdout_pair; 00068 pid_t ch_id; 00069 tCC *pz_cmd; 00070 00071 stdout_pair.read_fd = stdout_pair.write_fd = -1; 00072 00073 /* 00074 * Create a pipe it will be the child process' stdout, 00075 * and the parent will read from it. 00076 */ 00077 if (pipe ((int *) &stdout_pair) < 0) 00078 { 00079 if (p_child != (pid_t *) NULL) 00080 *p_child = NOPROCESS; 00081 return -1; 00082 } 00083 00084 /* 00085 * If we did not get an arg list, use the default 00086 */ 00087 if (pp_args == (tCC **) NULL) 00088 pp_args = def_args; 00089 00090 /* 00091 * If the arg list does not have a program, 00092 * assume the "SHELL" from the environment, or, failing 00093 * that, then sh. Set argv[0] to whatever we decided on. 00094 */ 00095 if (pz_cmd = *pp_args, 00096 (pz_cmd == (char *) NULL) || (*pz_cmd == '\0')) 00097 { 00098 00099 pz_cmd = getenv ("SHELL"); 00100 if (pz_cmd == (char *) NULL) 00101 pz_cmd = "sh"; 00102 } 00103 00104 #ifdef DEBUG_PRINT 00105 printf ("START: %s\n", pz_cmd); 00106 { 00107 int idx = 0; 00108 00109 while (pp_args[++idx] != (char *) NULL) 00110 printf (" ARG %2d: %s\n", idx, pp_args[idx]); 00111 } 00112 #endif 00113 00114 /* 00115 * Call fork() and see which process we become 00116 */ 00117 ch_id = fork (); 00118 switch (ch_id) 00119 { 00120 case NOPROCESS: /* parent - error in call */ 00121 close (stdout_pair.read_fd); 00122 close (stdout_pair.write_fd); 00123 if (p_child != (pid_t *) NULL) 00124 *p_child = NOPROCESS; 00125 return -1; 00126 00127 default: /* parent - return opposite FD's */ 00128 if (p_child != (pid_t *) NULL) 00129 *p_child = ch_id; 00130 #ifdef DEBUG_PRINT 00131 printf ("for pid %d: stdin from %d, stdout to %d\n" 00132 "for parent: read from %d\n", 00133 ch_id, stdin_fd, stdout_pair.write_fd, stdout_pair.read_fd); 00134 #endif 00135 close (stdin_fd); 00136 close (stdout_pair.write_fd); 00137 return stdout_pair.read_fd; 00138 00139 case NULLPROCESS: /* child - continue processing */ 00140 break; 00141 } 00142 00143 /* 00144 * Close the pipe end handed back to the parent process 00145 */ 00146 close (stdout_pair.read_fd); 00147 00148 /* 00149 * Close our current stdin and stdout 00150 */ 00151 close (STDIN_FILENO); 00152 close (STDOUT_FILENO); 00153 00154 /* 00155 * Make the fd passed in the stdin, and the write end of 00156 * the new pipe become the stdout. 00157 */ 00158 dup2 (stdout_pair.write_fd, STDOUT_FILENO); 00159 dup2 (stdin_fd, STDIN_FILENO); 00160 00161 if (*pp_args == (char *) NULL) 00162 *pp_args = pz_cmd; 00163 00164 execvp (pz_cmd, (char**)pp_args); 00165 fprintf (stderr, "Error %d: Could not execvp( '%s', ... ): %s\n", 00166 errno, pz_cmd, xstrerror (errno)); 00167 exit (EXIT_PANIC); 00168 } 00169 00170 00171 /* 00172 * proc2_open 00173 * 00174 * Given a pointer to an argument vector, start a process and 00175 * place its stdin and stdout file descriptors into an fd pair 00176 * structure. The "write_fd" connects to the inferior process 00177 * stdin, and the "read_fd" connects to its stdout. The calling 00178 * process should write to "write_fd" and read from "read_fd". 00179 * The return value is the process id of the created process. 00180 */ 00181 pid_t 00182 proc2_open (t_fd_pair* p_pair, tCC** pp_args) 00183 { 00184 pid_t ch_id; 00185 00186 /* Create a bi-directional pipe. Writes on 0 arrive on 1 and vice 00187 versa, so the parent and child processes will read and write to 00188 opposite FD's. */ 00189 if (pipe ((int *) p_pair) < 0) 00190 return NOPROCESS; 00191 00192 p_pair->read_fd = chain_open (p_pair->read_fd, pp_args, &ch_id); 00193 if (ch_id == NOPROCESS) 00194 close (p_pair->write_fd); 00195 00196 return ch_id; 00197 } 00198 00199 00200 /* 00201 * proc2_fopen 00202 * 00203 * Identical to "proc2_open()", except that the "fd"'s are 00204 * "fdopen(3)"-ed into file pointers instead. 00205 */ 00206 pid_t 00207 proc2_fopen (t_pf_pair* pf_pair, tCC** pp_args) 00208 { 00209 t_fd_pair fd_pair; 00210 pid_t ch_id = proc2_open (&fd_pair, pp_args); 00211 00212 if (ch_id == NOPROCESS) 00213 return ch_id; 00214 00215 pf_pair->pf_read = fdopen (fd_pair.read_fd, "r"); 00216 pf_pair->pf_write = fdopen (fd_pair.write_fd, "w"); 00217 return ch_id; 00218 }
1.5.6