1*a9fa9459Szrj /* Utilities to execute a program in a subprocess (possibly linked by pipes
2*a9fa9459Szrj    with other subprocesses), and wait for it.  Shared logic.
3*a9fa9459Szrj    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004
4*a9fa9459Szrj    Free Software Foundation, Inc.
5*a9fa9459Szrj 
6*a9fa9459Szrj This file is part of the libiberty library.
7*a9fa9459Szrj Libiberty is free software; you can redistribute it and/or
8*a9fa9459Szrj modify it under the terms of the GNU Library General Public
9*a9fa9459Szrj License as published by the Free Software Foundation; either
10*a9fa9459Szrj version 2 of the License, or (at your option) any later version.
11*a9fa9459Szrj 
12*a9fa9459Szrj Libiberty is distributed in the hope that it will be useful,
13*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
14*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*a9fa9459Szrj Library General Public License for more details.
16*a9fa9459Szrj 
17*a9fa9459Szrj You should have received a copy of the GNU Library General Public
18*a9fa9459Szrj License along with libiberty; see the file COPYING.LIB.  If not,
19*a9fa9459Szrj write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20*a9fa9459Szrj Boston, MA 02110-1301, USA.  */
21*a9fa9459Szrj 
22*a9fa9459Szrj #ifndef PEX_COMMON_H
23*a9fa9459Szrj #define PEX_COMMON_H
24*a9fa9459Szrj 
25*a9fa9459Szrj #include "config.h"
26*a9fa9459Szrj #include "libiberty.h"
27*a9fa9459Szrj #include <stdio.h>
28*a9fa9459Szrj 
29*a9fa9459Szrj /* pid_t is may defined by config.h or sys/types.h needs to be
30*a9fa9459Szrj    included.  */
31*a9fa9459Szrj #if !defined(pid_t) && defined(HAVE_SYS_TYPES_H)
32*a9fa9459Szrj #include <sys/types.h>
33*a9fa9459Szrj #endif
34*a9fa9459Szrj 
35*a9fa9459Szrj #define install_error_msg "installation problem, cannot exec `%s'"
36*a9fa9459Szrj 
37*a9fa9459Szrj /* stdin file number.  */
38*a9fa9459Szrj #define STDIN_FILE_NO 0
39*a9fa9459Szrj 
40*a9fa9459Szrj /* stdout file number.  */
41*a9fa9459Szrj #define STDOUT_FILE_NO 1
42*a9fa9459Szrj 
43*a9fa9459Szrj /* stderr file number.  */
44*a9fa9459Szrj #define STDERR_FILE_NO 2
45*a9fa9459Szrj 
46*a9fa9459Szrj /* value of `pipe': port index for reading.  */
47*a9fa9459Szrj #define READ_PORT 0
48*a9fa9459Szrj 
49*a9fa9459Szrj /* value of `pipe': port index for writing.  */
50*a9fa9459Szrj #define WRITE_PORT 1
51*a9fa9459Szrj 
52*a9fa9459Szrj /* The structure used by pex_init and friends.  */
53*a9fa9459Szrj 
54*a9fa9459Szrj struct pex_obj
55*a9fa9459Szrj {
56*a9fa9459Szrj   /* Flags.  */
57*a9fa9459Szrj   int flags;
58*a9fa9459Szrj   /* Name of calling program, for error messages.  */
59*a9fa9459Szrj   const char *pname;
60*a9fa9459Szrj   /* Base name to use for temporary files.  */
61*a9fa9459Szrj   const char *tempbase;
62*a9fa9459Szrj   /* Pipe to use as stdin for next process.  */
63*a9fa9459Szrj   int next_input;
64*a9fa9459Szrj   /* File name to use as stdin for next process.  */
65*a9fa9459Szrj   char *next_input_name;
66*a9fa9459Szrj   /* Whether next_input_name was allocated using malloc.  */
67*a9fa9459Szrj   int next_input_name_allocated;
68*a9fa9459Szrj   /* If not -1, stderr pipe from the last process.  */
69*a9fa9459Szrj   int stderr_pipe;
70*a9fa9459Szrj   /* Number of child processes.  */
71*a9fa9459Szrj   int count;
72*a9fa9459Szrj   /* PIDs of child processes; array allocated using malloc.  */
73*a9fa9459Szrj   pid_t *children;
74*a9fa9459Szrj   /* Exit statuses of child processes; array allocated using malloc.  */
75*a9fa9459Szrj   int *status;
76*a9fa9459Szrj   /* Time used by child processes; array allocated using malloc.  */
77*a9fa9459Szrj   struct pex_time *time;
78*a9fa9459Szrj   /* Number of children we have already waited for.  */
79*a9fa9459Szrj   int number_waited;
80*a9fa9459Szrj   /* FILE created by pex_input_file.  */
81*a9fa9459Szrj   FILE *input_file;
82*a9fa9459Szrj   /* FILE created by pex_read_output.  */
83*a9fa9459Szrj   FILE *read_output;
84*a9fa9459Szrj   /* FILE created by pex_read_err.  */
85*a9fa9459Szrj   FILE *read_err;
86*a9fa9459Szrj   /* Number of temporary files to remove.  */
87*a9fa9459Szrj   int remove_count;
88*a9fa9459Szrj   /* List of temporary files to remove; array allocated using malloc
89*a9fa9459Szrj      of strings allocated using malloc.  */
90*a9fa9459Szrj   char **remove;
91*a9fa9459Szrj   /* Pointers to system dependent functions.  */
92*a9fa9459Szrj   const struct pex_funcs *funcs;
93*a9fa9459Szrj   /* For use by system dependent code.  */
94*a9fa9459Szrj   void *sysdep;
95*a9fa9459Szrj };
96*a9fa9459Szrj 
97*a9fa9459Szrj /* Functions passed to pex_run_common.  */
98*a9fa9459Szrj 
99*a9fa9459Szrj struct pex_funcs
100*a9fa9459Szrj {
101*a9fa9459Szrj   /* Open file NAME for reading.  If BINARY is non-zero, open in
102*a9fa9459Szrj      binary mode.  Return >= 0 on success, -1 on error.  */
103*a9fa9459Szrj   int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */);
104*a9fa9459Szrj   /* Open file NAME for writing.  If BINARY is non-zero, open in
105*a9fa9459Szrj      binary mode.  Return >= 0 on success, -1 on error.  */
106*a9fa9459Szrj   int (*open_write) (struct pex_obj *, const char */* name */,
107*a9fa9459Szrj                      int /* binary */, int /* append */);
108*a9fa9459Szrj   /* Execute a child process.  FLAGS, EXECUTABLE, ARGV, ERR are from
109*a9fa9459Szrj      pex_run.  IN, OUT, ERRDES, TOCLOSE are all descriptors, from
110*a9fa9459Szrj      open_read, open_write, or pipe, or they are one of STDIN_FILE_NO,
111*a9fa9459Szrj      STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not
112*a9fa9459Szrj      STD*_FILE_NO, they should be closed.  If the descriptor TOCLOSE
113*a9fa9459Szrj      is not -1, and the system supports pipes, TOCLOSE should be
114*a9fa9459Szrj      closed in the child process.  The function should handle the
115*a9fa9459Szrj      PEX_STDERR_TO_STDOUT flag.  Return >= 0 on success, or -1 on
116*a9fa9459Szrj      error and set *ERRMSG and *ERR.  */
117*a9fa9459Szrj   pid_t (*exec_child) (struct pex_obj *, int /* flags */,
118*a9fa9459Szrj                       const char */* executable */, char * const * /* argv */,
119*a9fa9459Szrj                       char * const * /* env */,
120*a9fa9459Szrj                       int /* in */, int /* out */, int /* errdes */,
121*a9fa9459Szrj 		      int /* toclose */, const char **/* errmsg */,
122*a9fa9459Szrj 		      int */* err */);
123*a9fa9459Szrj   /* Close a descriptor.  Return 0 on success, -1 on error.  */
124*a9fa9459Szrj   int (*close) (struct pex_obj *, int);
125*a9fa9459Szrj   /* Wait for a child to complete, returning exit status in *STATUS
126*a9fa9459Szrj      and time in *TIME (if it is not null).  CHILD is from fork.  DONE
127*a9fa9459Szrj      is 1 if this is called via pex_free.  ERRMSG and ERR are as in
128*a9fa9459Szrj      fork.  Return 0 on success, -1 on error.  */
129*a9fa9459Szrj   pid_t (*wait) (struct pex_obj *, pid_t /* child */, int * /* status */,
130*a9fa9459Szrj                struct pex_time * /* time */, int /* done */,
131*a9fa9459Szrj                const char ** /* errmsg */, int * /* err */);
132*a9fa9459Szrj   /* Create a pipe (only called if PEX_USE_PIPES is set) storing two
133*a9fa9459Szrj      descriptors in P[0] and P[1].  If BINARY is non-zero, open in
134*a9fa9459Szrj      binary mode.  Return 0 on success, -1 on error.  */
135*a9fa9459Szrj   int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */);
136*a9fa9459Szrj   /* Get a FILE pointer to read from a file descriptor (only called if
137*a9fa9459Szrj      PEX_USE_PIPES is set).  If BINARY is non-zero, open in binary
138*a9fa9459Szrj      mode.  Return pointer on success, NULL on error.  */
139*a9fa9459Szrj   FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */);
140*a9fa9459Szrj   /* Get a FILE pointer to write to the file descriptor FD (only
141*a9fa9459Szrj      called if PEX_USE_PIPES is set).  If BINARY is non-zero, open in
142*a9fa9459Szrj      binary mode.  Arrange for FD not to be inherited by the child
143*a9fa9459Szrj      processes.  Return pointer on success, NULL on error.  */
144*a9fa9459Szrj   FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */);
145*a9fa9459Szrj   /* Free any system dependent data associated with OBJ.  May be
146*a9fa9459Szrj      NULL if there is nothing to do.  */
147*a9fa9459Szrj   void (*cleanup) (struct pex_obj *);
148*a9fa9459Szrj };
149*a9fa9459Szrj 
150*a9fa9459Szrj extern struct pex_obj *pex_init_common (int, const char *, const char *,
151*a9fa9459Szrj 					const struct pex_funcs *);
152*a9fa9459Szrj 
153*a9fa9459Szrj #endif
154