00001 00002 /* 00003 * server.c Set up and handle communications with a server process. 00004 * 00005 * Server Handling copyright 1992-1999 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 * 59 Temple Place - Suite 330, 00021 * Boston, MA 02111-1307, 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 #include "auto-host.h" 00049 #include "ansidecl.h" 00050 #include "system.h" 00051 00052 #include "server.h" 00053 #include "fixlib.h" 00054 00055 STATIC const char* def_args[] = 00056 { (char *) NULL, (char *) NULL }; 00057 00058 /* 00059 * chain_open 00060 * 00061 * Given an FD for an inferior process to use as stdin, 00062 * start that process and return a NEW FD that that process 00063 * will use for its stdout. Requires the argument vector 00064 * for the new process and, optionally, a pointer to a place 00065 * to store the child's process id. 00066 */ 00067 int 00068 chain_open (stdin_fd, pp_args, p_child) 00069 int stdin_fd; 00070 tCC **pp_args; 00071 pid_t *p_child; 00072 { 00073 t_fd_pair stdout_pair; 00074 pid_t ch_id; 00075 tCC *pz_cmd; 00076 00077 stdout_pair.read_fd = stdout_pair.write_fd = -1; 00078 00079 /* 00080 * Create a pipe it will be the child process' stdout, 00081 * and the parent will read from it. 00082 */ 00083 if (pipe ((int *) &stdout_pair) < 0) 00084 { 00085 if (p_child != (pid_t *) NULL) 00086 *p_child = NOPROCESS; 00087 return -1; 00088 } 00089 00090 /* 00091 * If we did not get an arg list, use the default 00092 */ 00093 if (pp_args == (tCC **) NULL) 00094 pp_args = def_args; 00095 00096 /* 00097 * If the arg list does not have a program, 00098 * assume the "SHELL" from the environment, or, failing 00099 * that, then sh. Set argv[0] to whatever we decided on. 00100 */ 00101 if (pz_cmd = *pp_args, 00102 (pz_cmd == (char *) NULL) || (*pz_cmd == '\0')) 00103 { 00104 00105 pz_cmd = getenv ("SHELL"); 00106 if (pz_cmd == (char *) NULL) 00107 pz_cmd = "sh"; 00108 } 00109 00110 #ifdef DEBUG_PRINT 00111 printf ("START: %s\n", pz_cmd); 00112 { 00113 int idx = 0; 00114 00115 while (pp_args[++idx] != (char *) NULL) 00116 printf (" ARG %2d: %s\n", idx, pp_args[idx]); 00117 } 00118 #endif 00119 00120 /* 00121 * Call fork() and see which process we become 00122 */ 00123 ch_id = fork (); 00124 switch (ch_id) 00125 { 00126 case NOPROCESS: /* parent - error in call */ 00127 close (stdout_pair.read_fd); 00128 close (stdout_pair.write_fd); 00129 if (p_child != (pid_t *) NULL) 00130 *p_child = NOPROCESS; 00131 return -1; 00132 00133 default: /* parent - return opposite FD's */ 00134 if (p_child != (pid_t *) NULL) 00135 *p_child = ch_id; 00136 #ifdef DEBUG_PRINT 00137 printf ("for pid %d: stdin from %d, stdout to %d\n" 00138 "for parent: read from %d\n", 00139 ch_id, stdin_fd, stdout_pair.write_fd, stdout_pair.read_fd); 00140 #endif 00141 close (stdin_fd); 00142 close (stdout_pair.write_fd); 00143 return stdout_pair.read_fd; 00144 00145 case NULLPROCESS: /* child - continue processing */ 00146 break; 00147 } 00148 00149 /* 00150 * Close the pipe end handed back to the parent process 00151 */ 00152 close (stdout_pair.read_fd); 00153 00154 /* 00155 * Close our current stdin and stdout 00156 */ 00157 close (STDIN_FILENO); 00158 close (STDOUT_FILENO); 00159 00160 /* 00161 * Make the fd passed in the stdin, and the write end of 00162 * the new pipe become the stdout. 00163 */ 00164 fcntl (stdout_pair.write_fd, F_DUPFD, STDOUT_FILENO); 00165 fcntl (stdin_fd, F_DUPFD, STDIN_FILENO); 00166 00167 if (*pp_args == (char *) NULL) 00168 *pp_args = pz_cmd; 00169 00170 execvp (pz_cmd, (char**)pp_args); 00171 fprintf (stderr, "Error %d: Could not execvp( '%s', ... ): %s\n", 00172 errno, pz_cmd, xstrerror (errno)); 00173 exit (EXIT_PANIC); 00174 } 00175 00176 00177 /* 00178 * proc2_open 00179 * 00180 * Given a pointer to an argument vector, start a process and 00181 * place its stdin and stdout file descriptors into an fd pair 00182 * structure. The "write_fd" connects to the inferior process 00183 * stdin, and the "read_fd" connects to its stdout. The calling 00184 * process should write to "write_fd" and read from "read_fd". 00185 * The return value is the process id of the created process. 00186 */ 00187 pid_t 00188 proc2_open (p_pair, pp_args) 00189 t_fd_pair *p_pair; 00190 tCC **pp_args; 00191 { 00192 pid_t ch_id; 00193 00194 /* Create a bi-directional pipe. Writes on 0 arrive on 1 and vice 00195 versa, so the parent and child processes will read and write to 00196 opposite FD's. */ 00197 if (pipe ((int *) p_pair) < 0) 00198 return NOPROCESS; 00199 00200 p_pair->read_fd = chain_open (p_pair->read_fd, pp_args, &ch_id); 00201 if (ch_id == NOPROCESS) 00202 close (p_pair->write_fd); 00203 00204 return ch_id; 00205 } 00206 00207 00208 /* 00209 * proc2_fopen 00210 * 00211 * Identical to "proc2_open()", except that the "fd"'s are 00212 * "fdopen(3)"-ed into file pointers instead. 00213 */ 00214 pid_t 00215 proc2_fopen (pf_pair, pp_args) 00216 t_pf_pair *pf_pair; 00217 tCC **pp_args; 00218 { 00219 t_fd_pair fd_pair; 00220 pid_t ch_id = proc2_open (&fd_pair, pp_args); 00221 00222 if (ch_id == NOPROCESS) 00223 return ch_id; 00224 00225 pf_pair->pf_read = fdopen (fd_pair.read_fd, "r"); 00226 pf_pair->pf_write = fdopen (fd_pair.write_fd, "w"); 00227 return ch_id; 00228 }
1.5.6