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