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