1*404b540aSrobert
2*404b540aSrobert /*
3*404b540aSrobert * server.c Set up and handle communications with a server process.
4*404b540aSrobert *
5*404b540aSrobert * Server Handling copyright 1992-1999, 2004 The Free Software Foundation
6*404b540aSrobert *
7*404b540aSrobert * Server Handling is free software.
8*404b540aSrobert * You may redistribute it and/or modify it under the terms of the
9*404b540aSrobert * GNU General Public License, as published by the Free Software
10*404b540aSrobert * Foundation; either version 2, or (at your option) any later version.
11*404b540aSrobert *
12*404b540aSrobert * Server Handling is distributed in the hope that it will be useful,
13*404b540aSrobert * but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert * GNU General Public License for more details.
16*404b540aSrobert *
17*404b540aSrobert * You should have received a copy of the GNU General Public License
18*404b540aSrobert * along with Server Handling. See the file "COPYING". If not,
19*404b540aSrobert * write to: The Free Software Foundation, Inc.,
20*404b540aSrobert * 51 Franklin Street, Fifth Floor,
21*404b540aSrobert * Boston, MA 02110-1301, USA.
22*404b540aSrobert *
23*404b540aSrobert * As a special exception, The Free Software Foundation gives
24*404b540aSrobert * permission for additional uses of the text contained in his release
25*404b540aSrobert * of ServerHandler.
26*404b540aSrobert *
27*404b540aSrobert * The exception is that, if you link the ServerHandler library with other
28*404b540aSrobert * files to produce an executable, this does not by itself cause the
29*404b540aSrobert * resulting executable to be covered by the GNU General Public License.
30*404b540aSrobert * Your use of that executable is in no way restricted on account of
31*404b540aSrobert * linking the ServerHandler library code into it.
32*404b540aSrobert *
33*404b540aSrobert * This exception does not however invalidate any other reasons why
34*404b540aSrobert * the executable file might be covered by the GNU General Public License.
35*404b540aSrobert *
36*404b540aSrobert * This exception applies only to the code released by The Free
37*404b540aSrobert * Software Foundation under the name ServerHandler. If you copy code
38*404b540aSrobert * from other sources under the General Public License into a copy of
39*404b540aSrobert * ServerHandler, as the General Public License permits, the exception
40*404b540aSrobert * does not apply to the code that you add in this way. To avoid
41*404b540aSrobert * misleading anyone as to the status of such modified files, you must
42*404b540aSrobert * delete this exception notice from them.
43*404b540aSrobert *
44*404b540aSrobert * If you write modifications of your own for ServerHandler, it is your
45*404b540aSrobert * choice whether to permit this exception to apply to your modifications.
46*404b540aSrobert * If you do not wish that, delete this exception notice.
47*404b540aSrobert */
48*404b540aSrobert
49*404b540aSrobert #include "fixlib.h"
50*404b540aSrobert #include "server.h"
51*404b540aSrobert
52*404b540aSrobert STATIC const char* def_args[] =
53*404b540aSrobert { (char *) NULL, (char *) NULL };
54*404b540aSrobert
55*404b540aSrobert /*
56*404b540aSrobert * chain_open
57*404b540aSrobert *
58*404b540aSrobert * Given an FD for an inferior process to use as stdin,
59*404b540aSrobert * start that process and return a NEW FD that that process
60*404b540aSrobert * will use for its stdout. Requires the argument vector
61*404b540aSrobert * for the new process and, optionally, a pointer to a place
62*404b540aSrobert * to store the child's process id.
63*404b540aSrobert */
64*404b540aSrobert int
chain_open(int stdin_fd,tCC ** pp_args,pid_t * p_child)65*404b540aSrobert chain_open (int stdin_fd, tCC** pp_args, pid_t* p_child)
66*404b540aSrobert {
67*404b540aSrobert t_fd_pair stdout_pair;
68*404b540aSrobert pid_t ch_id;
69*404b540aSrobert tCC *pz_cmd;
70*404b540aSrobert
71*404b540aSrobert stdout_pair.read_fd = stdout_pair.write_fd = -1;
72*404b540aSrobert
73*404b540aSrobert /*
74*404b540aSrobert * Create a pipe it will be the child process' stdout,
75*404b540aSrobert * and the parent will read from it.
76*404b540aSrobert */
77*404b540aSrobert if (pipe ((int *) &stdout_pair) < 0)
78*404b540aSrobert {
79*404b540aSrobert if (p_child != (pid_t *) NULL)
80*404b540aSrobert *p_child = NOPROCESS;
81*404b540aSrobert return -1;
82*404b540aSrobert }
83*404b540aSrobert
84*404b540aSrobert /*
85*404b540aSrobert * If we did not get an arg list, use the default
86*404b540aSrobert */
87*404b540aSrobert if (pp_args == (tCC **) NULL)
88*404b540aSrobert pp_args = def_args;
89*404b540aSrobert
90*404b540aSrobert /*
91*404b540aSrobert * If the arg list does not have a program,
92*404b540aSrobert * assume the "SHELL" from the environment, or, failing
93*404b540aSrobert * that, then sh. Set argv[0] to whatever we decided on.
94*404b540aSrobert */
95*404b540aSrobert if (pz_cmd = *pp_args,
96*404b540aSrobert (pz_cmd == (char *) NULL) || (*pz_cmd == '\0'))
97*404b540aSrobert {
98*404b540aSrobert
99*404b540aSrobert pz_cmd = getenv ("SHELL");
100*404b540aSrobert if (pz_cmd == (char *) NULL)
101*404b540aSrobert pz_cmd = "sh";
102*404b540aSrobert }
103*404b540aSrobert
104*404b540aSrobert #ifdef DEBUG_PRINT
105*404b540aSrobert printf ("START: %s\n", pz_cmd);
106*404b540aSrobert {
107*404b540aSrobert int idx = 0;
108*404b540aSrobert
109*404b540aSrobert while (pp_args[++idx] != (char *) NULL)
110*404b540aSrobert printf (" ARG %2d: %s\n", idx, pp_args[idx]);
111*404b540aSrobert }
112*404b540aSrobert #endif
113*404b540aSrobert
114*404b540aSrobert /*
115*404b540aSrobert * Call fork() and see which process we become
116*404b540aSrobert */
117*404b540aSrobert ch_id = fork ();
118*404b540aSrobert switch (ch_id)
119*404b540aSrobert {
120*404b540aSrobert case NOPROCESS: /* parent - error in call */
121*404b540aSrobert close (stdout_pair.read_fd);
122*404b540aSrobert close (stdout_pair.write_fd);
123*404b540aSrobert if (p_child != (pid_t *) NULL)
124*404b540aSrobert *p_child = NOPROCESS;
125*404b540aSrobert return -1;
126*404b540aSrobert
127*404b540aSrobert default: /* parent - return opposite FD's */
128*404b540aSrobert if (p_child != (pid_t *) NULL)
129*404b540aSrobert *p_child = ch_id;
130*404b540aSrobert #ifdef DEBUG_PRINT
131*404b540aSrobert printf ("for pid %d: stdin from %d, stdout to %d\n"
132*404b540aSrobert "for parent: read from %d\n",
133*404b540aSrobert ch_id, stdin_fd, stdout_pair.write_fd, stdout_pair.read_fd);
134*404b540aSrobert #endif
135*404b540aSrobert close (stdin_fd);
136*404b540aSrobert close (stdout_pair.write_fd);
137*404b540aSrobert return stdout_pair.read_fd;
138*404b540aSrobert
139*404b540aSrobert case NULLPROCESS: /* child - continue processing */
140*404b540aSrobert break;
141*404b540aSrobert }
142*404b540aSrobert
143*404b540aSrobert /*
144*404b540aSrobert * Close the pipe end handed back to the parent process
145*404b540aSrobert */
146*404b540aSrobert close (stdout_pair.read_fd);
147*404b540aSrobert
148*404b540aSrobert /*
149*404b540aSrobert * Close our current stdin and stdout
150*404b540aSrobert */
151*404b540aSrobert close (STDIN_FILENO);
152*404b540aSrobert close (STDOUT_FILENO);
153*404b540aSrobert
154*404b540aSrobert /*
155*404b540aSrobert * Make the fd passed in the stdin, and the write end of
156*404b540aSrobert * the new pipe become the stdout.
157*404b540aSrobert */
158*404b540aSrobert dup2 (stdout_pair.write_fd, STDOUT_FILENO);
159*404b540aSrobert dup2 (stdin_fd, STDIN_FILENO);
160*404b540aSrobert
161*404b540aSrobert if (*pp_args == (char *) NULL)
162*404b540aSrobert *pp_args = pz_cmd;
163*404b540aSrobert
164*404b540aSrobert execvp (pz_cmd, (char**)pp_args);
165*404b540aSrobert fprintf (stderr, "Error %d: Could not execvp( '%s', ... ): %s\n",
166*404b540aSrobert errno, pz_cmd, xstrerror (errno));
167*404b540aSrobert exit (EXIT_PANIC);
168*404b540aSrobert }
169*404b540aSrobert
170*404b540aSrobert
171*404b540aSrobert /*
172*404b540aSrobert * proc2_open
173*404b540aSrobert *
174*404b540aSrobert * Given a pointer to an argument vector, start a process and
175*404b540aSrobert * place its stdin and stdout file descriptors into an fd pair
176*404b540aSrobert * structure. The "write_fd" connects to the inferior process
177*404b540aSrobert * stdin, and the "read_fd" connects to its stdout. The calling
178*404b540aSrobert * process should write to "write_fd" and read from "read_fd".
179*404b540aSrobert * The return value is the process id of the created process.
180*404b540aSrobert */
181*404b540aSrobert pid_t
proc2_open(t_fd_pair * p_pair,tCC ** pp_args)182*404b540aSrobert proc2_open (t_fd_pair* p_pair, tCC** pp_args)
183*404b540aSrobert {
184*404b540aSrobert pid_t ch_id;
185*404b540aSrobert
186*404b540aSrobert /* Create a bi-directional pipe. Writes on 0 arrive on 1 and vice
187*404b540aSrobert versa, so the parent and child processes will read and write to
188*404b540aSrobert opposite FD's. */
189*404b540aSrobert if (pipe ((int *) p_pair) < 0)
190*404b540aSrobert return NOPROCESS;
191*404b540aSrobert
192*404b540aSrobert p_pair->read_fd = chain_open (p_pair->read_fd, pp_args, &ch_id);
193*404b540aSrobert if (ch_id == NOPROCESS)
194*404b540aSrobert close (p_pair->write_fd);
195*404b540aSrobert
196*404b540aSrobert return ch_id;
197*404b540aSrobert }
198*404b540aSrobert
199*404b540aSrobert
200*404b540aSrobert /*
201*404b540aSrobert * proc2_fopen
202*404b540aSrobert *
203*404b540aSrobert * Identical to "proc2_open()", except that the "fd"'s are
204*404b540aSrobert * "fdopen(3)"-ed into file pointers instead.
205*404b540aSrobert */
206*404b540aSrobert pid_t
proc2_fopen(t_pf_pair * pf_pair,tCC ** pp_args)207*404b540aSrobert proc2_fopen (t_pf_pair* pf_pair, tCC** pp_args)
208*404b540aSrobert {
209*404b540aSrobert t_fd_pair fd_pair;
210*404b540aSrobert pid_t ch_id = proc2_open (&fd_pair, pp_args);
211*404b540aSrobert
212*404b540aSrobert if (ch_id == NOPROCESS)
213*404b540aSrobert return ch_id;
214*404b540aSrobert
215*404b540aSrobert pf_pair->pf_read = fdopen (fd_pair.read_fd, "r");
216*404b540aSrobert pf_pair->pf_write = fdopen (fd_pair.write_fd, "w");
217*404b540aSrobert return ch_id;
218*404b540aSrobert }
219