1169695Skan/* Utilities to execute a program in a subprocess (possibly linked by pipes 2169695Skan with other subprocesses), and wait for it. Shared logic. 3169695Skan Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004 4169695Skan Free Software Foundation, Inc. 5169695Skan 6169695SkanThis file is part of the libiberty library. 7169695SkanLibiberty is free software; you can redistribute it and/or 8169695Skanmodify it under the terms of the GNU Library General Public 9169695SkanLicense as published by the Free Software Foundation; either 10169695Skanversion 2 of the License, or (at your option) any later version. 11169695Skan 12169695SkanLibiberty is distributed in the hope that it will be useful, 13169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 14169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15169695SkanLibrary General Public License for more details. 16169695Skan 17169695SkanYou should have received a copy of the GNU Library General Public 18169695SkanLicense along with libiberty; see the file COPYING.LIB. If not, 19169695Skanwrite to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 20169695SkanBoston, MA 02110-1301, USA. */ 21169695Skan 22169695Skan#ifndef PEX_COMMON_H 23169695Skan#define PEX_COMMON_H 24169695Skan 25169695Skan#include "config.h" 26169695Skan#include "libiberty.h" 27169695Skan#include <stdio.h> 28169695Skan 29169695Skan#define install_error_msg "installation problem, cannot exec `%s'" 30169695Skan 31169695Skan/* stdin file number. */ 32169695Skan#define STDIN_FILE_NO 0 33169695Skan 34169695Skan/* stdout file number. */ 35169695Skan#define STDOUT_FILE_NO 1 36169695Skan 37169695Skan/* stderr file number. */ 38169695Skan#define STDERR_FILE_NO 2 39169695Skan 40169695Skan/* value of `pipe': port index for reading. */ 41169695Skan#define READ_PORT 0 42169695Skan 43169695Skan/* value of `pipe': port index for writing. */ 44169695Skan#define WRITE_PORT 1 45169695Skan 46169695Skan/* The structure used by pex_init and friends. */ 47169695Skan 48169695Skanstruct pex_obj 49169695Skan{ 50169695Skan /* Flags. */ 51169695Skan int flags; 52169695Skan /* Name of calling program, for error messages. */ 53169695Skan const char *pname; 54169695Skan /* Base name to use for temporary files. */ 55169695Skan const char *tempbase; 56169695Skan /* Pipe to use as stdin for next process. */ 57169695Skan int next_input; 58169695Skan /* File name to use as stdin for next process. */ 59169695Skan char *next_input_name; 60169695Skan /* Whether next_input_name was allocated using malloc. */ 61169695Skan int next_input_name_allocated; 62169695Skan /* Number of child processes. */ 63169695Skan int count; 64169695Skan /* PIDs of child processes; array allocated using malloc. */ 65169695Skan long *children; 66169695Skan /* Exit statuses of child processes; array allocated using malloc. */ 67169695Skan int *status; 68169695Skan /* Time used by child processes; array allocated using malloc. */ 69169695Skan struct pex_time *time; 70169695Skan /* Number of children we have already waited for. */ 71169695Skan int number_waited; 72169695Skan /* FILE created by pex_input_file. */ 73169695Skan FILE *input_file; 74169695Skan /* FILE created by pex_read_output. */ 75169695Skan FILE *read_output; 76169695Skan /* Number of temporary files to remove. */ 77169695Skan int remove_count; 78169695Skan /* List of temporary files to remove; array allocated using malloc 79169695Skan of strings allocated using malloc. */ 80169695Skan char **remove; 81169695Skan /* Pointers to system dependent functions. */ 82169695Skan const struct pex_funcs *funcs; 83169695Skan /* For use by system dependent code. */ 84169695Skan void *sysdep; 85169695Skan}; 86169695Skan 87169695Skan/* Functions passed to pex_run_common. */ 88169695Skan 89169695Skanstruct pex_funcs 90169695Skan{ 91169695Skan /* Open file NAME for reading. If BINARY is non-zero, open in 92169695Skan binary mode. Return >= 0 on success, -1 on error. */ 93169695Skan int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */); 94169695Skan /* Open file NAME for writing. If BINARY is non-zero, open in 95169695Skan binary mode. Return >= 0 on success, -1 on error. */ 96169695Skan int (*open_write) (struct pex_obj *, const char */* name */, 97169695Skan int /* binary */); 98169695Skan /* Execute a child process. FLAGS, EXECUTABLE, ARGV, ERR are from 99169695Skan pex_run. IN, OUT, ERRDES, TOCLOSE are all descriptors, from 100169695Skan open_read, open_write, or pipe, or they are one of STDIN_FILE_NO, 101169695Skan STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not 102169695Skan STD*_FILE_NO, they should be closed. If the descriptor TOCLOSE 103169695Skan is not -1, and the system supports pipes, TOCLOSE should be 104169695Skan closed in the child process. The function should handle the 105169695Skan PEX_STDERR_TO_STDOUT flag. Return >= 0 on success, or -1 on 106169695Skan error and set *ERRMSG and *ERR. */ 107169695Skan long (*exec_child) (struct pex_obj *, int /* flags */, 108169695Skan const char */* executable */, char * const * /* argv */, 109169695Skan char * const * /* env */, 110169695Skan int /* in */, int /* out */, int /* errdes */, 111169695Skan int /* toclose */, const char **/* errmsg */, 112169695Skan int */* err */); 113169695Skan /* Close a descriptor. Return 0 on success, -1 on error. */ 114169695Skan int (*close) (struct pex_obj *, int); 115169695Skan /* Wait for a child to complete, returning exit status in *STATUS 116169695Skan and time in *TIME (if it is not null). CHILD is from fork. DONE 117169695Skan is 1 if this is called via pex_free. ERRMSG and ERR are as in 118169695Skan fork. Return 0 on success, -1 on error. */ 119169695Skan int (*wait) (struct pex_obj *, long /* child */, int * /* status */, 120169695Skan struct pex_time * /* time */, int /* done */, 121169695Skan const char ** /* errmsg */, int * /* err */); 122169695Skan /* Create a pipe (only called if PEX_USE_PIPES is set) storing two 123169695Skan descriptors in P[0] and P[1]. If BINARY is non-zero, open in 124169695Skan binary mode. Return 0 on success, -1 on error. */ 125169695Skan int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */); 126169695Skan /* Get a FILE pointer to read from a file descriptor (only called if 127169695Skan PEX_USE_PIPES is set). If BINARY is non-zero, open in binary 128169695Skan mode. Return pointer on success, NULL on error. */ 129169695Skan FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */); 130169695Skan /* Get a FILE pointer to write to the file descriptor FD (only 131169695Skan called if PEX_USE_PIPES is set). If BINARY is non-zero, open in 132169695Skan binary mode. Arrange for FD not to be inherited by the child 133169695Skan processes. Return pointer on success, NULL on error. */ 134169695Skan FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */); 135169695Skan /* Free any system dependent data associated with OBJ. May be 136169695Skan NULL if there is nothing to do. */ 137169695Skan void (*cleanup) (struct pex_obj *); 138169695Skan}; 139169695Skan 140169695Skanextern struct pex_obj *pex_init_common (int, const char *, const char *, 141169695Skan const struct pex_funcs *); 142169695Skan 143169695Skan#endif 144