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