xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/pex-common.h (revision 7e120ff03ede3fe64e2c8620c01465d528502ddb)
198b9484cSchristos /* Utilities to execute a program in a subprocess (possibly linked by pipes
298b9484cSchristos    with other subprocesses), and wait for it.  Shared logic.
3*7e120ff0Schristos    Copyright (C) 1996-2024 Free Software Foundation, Inc.
498b9484cSchristos 
598b9484cSchristos This file is part of the libiberty library.
698b9484cSchristos Libiberty is free software; you can redistribute it and/or
798b9484cSchristos modify it under the terms of the GNU Library General Public
898b9484cSchristos License as published by the Free Software Foundation; either
998b9484cSchristos version 2 of the License, or (at your option) any later version.
1098b9484cSchristos 
1198b9484cSchristos Libiberty is distributed in the hope that it will be useful,
1298b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1398b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1498b9484cSchristos Library General Public License for more details.
1598b9484cSchristos 
1698b9484cSchristos You should have received a copy of the GNU Library General Public
1798b9484cSchristos License along with libiberty; see the file COPYING.LIB.  If not,
1898b9484cSchristos write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1998b9484cSchristos Boston, MA 02110-1301, USA.  */
2098b9484cSchristos 
2198b9484cSchristos #ifndef PEX_COMMON_H
2298b9484cSchristos #define PEX_COMMON_H
2398b9484cSchristos 
2498b9484cSchristos #include "config.h"
2598b9484cSchristos #include "libiberty.h"
2698b9484cSchristos #include <stdio.h>
2798b9484cSchristos 
2898b9484cSchristos /* pid_t is may defined by config.h or sys/types.h needs to be
2998b9484cSchristos    included.  */
3098b9484cSchristos #if !defined(pid_t) && defined(HAVE_SYS_TYPES_H)
3198b9484cSchristos #include <sys/types.h>
3298b9484cSchristos #endif
3398b9484cSchristos 
3498b9484cSchristos #define install_error_msg "installation problem, cannot exec `%s'"
3598b9484cSchristos 
3698b9484cSchristos /* stdin file number.  */
3798b9484cSchristos #define STDIN_FILE_NO 0
3898b9484cSchristos 
3998b9484cSchristos /* stdout file number.  */
4098b9484cSchristos #define STDOUT_FILE_NO 1
4198b9484cSchristos 
4298b9484cSchristos /* stderr file number.  */
4398b9484cSchristos #define STDERR_FILE_NO 2
4498b9484cSchristos 
4598b9484cSchristos /* value of `pipe': port index for reading.  */
4698b9484cSchristos #define READ_PORT 0
4798b9484cSchristos 
4898b9484cSchristos /* value of `pipe': port index for writing.  */
4998b9484cSchristos #define WRITE_PORT 1
5098b9484cSchristos 
5198b9484cSchristos /* The structure used by pex_init and friends.  */
5298b9484cSchristos 
5398b9484cSchristos struct pex_obj
5498b9484cSchristos {
5598b9484cSchristos   /* Flags.  */
5698b9484cSchristos   int flags;
5798b9484cSchristos   /* Name of calling program, for error messages.  */
5898b9484cSchristos   const char *pname;
5998b9484cSchristos   /* Base name to use for temporary files.  */
6098b9484cSchristos   const char *tempbase;
6198b9484cSchristos   /* Pipe to use as stdin for next process.  */
6298b9484cSchristos   int next_input;
6398b9484cSchristos   /* File name to use as stdin for next process.  */
6498b9484cSchristos   char *next_input_name;
6598b9484cSchristos   /* Whether next_input_name was allocated using malloc.  */
6698b9484cSchristos   int next_input_name_allocated;
6798b9484cSchristos   /* If not -1, stderr pipe from the last process.  */
6898b9484cSchristos   int stderr_pipe;
6998b9484cSchristos   /* Number of child processes.  */
7098b9484cSchristos   int count;
7198b9484cSchristos   /* PIDs of child processes; array allocated using malloc.  */
7298b9484cSchristos   pid_t *children;
7398b9484cSchristos   /* Exit statuses of child processes; array allocated using malloc.  */
7498b9484cSchristos   int *status;
7598b9484cSchristos   /* Time used by child processes; array allocated using malloc.  */
7698b9484cSchristos   struct pex_time *time;
7798b9484cSchristos   /* Number of children we have already waited for.  */
7898b9484cSchristos   int number_waited;
7998b9484cSchristos   /* FILE created by pex_input_file.  */
8098b9484cSchristos   FILE *input_file;
8198b9484cSchristos   /* FILE created by pex_read_output.  */
8298b9484cSchristos   FILE *read_output;
8398b9484cSchristos   /* FILE created by pex_read_err.  */
8498b9484cSchristos   FILE *read_err;
8598b9484cSchristos   /* Number of temporary files to remove.  */
8698b9484cSchristos   int remove_count;
8798b9484cSchristos   /* List of temporary files to remove; array allocated using malloc
8898b9484cSchristos      of strings allocated using malloc.  */
8998b9484cSchristos   char **remove;
9098b9484cSchristos   /* Pointers to system dependent functions.  */
9198b9484cSchristos   const struct pex_funcs *funcs;
9298b9484cSchristos   /* For use by system dependent code.  */
9398b9484cSchristos   void *sysdep;
9498b9484cSchristos };
9598b9484cSchristos 
9698b9484cSchristos /* Functions passed to pex_run_common.  */
9798b9484cSchristos 
9898b9484cSchristos struct pex_funcs
9998b9484cSchristos {
10098b9484cSchristos   /* Open file NAME for reading.  If BINARY is non-zero, open in
10198b9484cSchristos      binary mode.  Return >= 0 on success, -1 on error.  */
10298b9484cSchristos   int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */);
10398b9484cSchristos   /* Open file NAME for writing.  If BINARY is non-zero, open in
10498b9484cSchristos      binary mode.  Return >= 0 on success, -1 on error.  */
10598b9484cSchristos   int (*open_write) (struct pex_obj *, const char */* name */,
106837edd6bSchristos                      int /* binary */, int /* append */);
10798b9484cSchristos   /* Execute a child process.  FLAGS, EXECUTABLE, ARGV, ERR are from
10898b9484cSchristos      pex_run.  IN, OUT, ERRDES, TOCLOSE are all descriptors, from
10998b9484cSchristos      open_read, open_write, or pipe, or they are one of STDIN_FILE_NO,
11098b9484cSchristos      STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not
11198b9484cSchristos      STD*_FILE_NO, they should be closed.  If the descriptor TOCLOSE
11298b9484cSchristos      is not -1, and the system supports pipes, TOCLOSE should be
11398b9484cSchristos      closed in the child process.  The function should handle the
11498b9484cSchristos      PEX_STDERR_TO_STDOUT flag.  Return >= 0 on success, or -1 on
11598b9484cSchristos      error and set *ERRMSG and *ERR.  */
11698b9484cSchristos   pid_t (*exec_child) (struct pex_obj *, int /* flags */,
11798b9484cSchristos                       const char */* executable */, char * const * /* argv */,
11898b9484cSchristos                       char * const * /* env */,
11998b9484cSchristos                       int /* in */, int /* out */, int /* errdes */,
12098b9484cSchristos 		      int /* toclose */, const char **/* errmsg */,
12198b9484cSchristos 		      int */* err */);
12298b9484cSchristos   /* Close a descriptor.  Return 0 on success, -1 on error.  */
12398b9484cSchristos   int (*close) (struct pex_obj *, int);
12498b9484cSchristos   /* Wait for a child to complete, returning exit status in *STATUS
12598b9484cSchristos      and time in *TIME (if it is not null).  CHILD is from fork.  DONE
12698b9484cSchristos      is 1 if this is called via pex_free.  ERRMSG and ERR are as in
12798b9484cSchristos      fork.  Return 0 on success, -1 on error.  */
12898b9484cSchristos   pid_t (*wait) (struct pex_obj *, pid_t /* child */, int * /* status */,
12998b9484cSchristos                struct pex_time * /* time */, int /* done */,
13098b9484cSchristos                const char ** /* errmsg */, int * /* err */);
13198b9484cSchristos   /* Create a pipe (only called if PEX_USE_PIPES is set) storing two
13298b9484cSchristos      descriptors in P[0] and P[1].  If BINARY is non-zero, open in
13398b9484cSchristos      binary mode.  Return 0 on success, -1 on error.  */
13498b9484cSchristos   int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */);
13598b9484cSchristos   /* Get a FILE pointer to read from a file descriptor (only called if
13698b9484cSchristos      PEX_USE_PIPES is set).  If BINARY is non-zero, open in binary
13798b9484cSchristos      mode.  Return pointer on success, NULL on error.  */
13898b9484cSchristos   FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */);
13998b9484cSchristos   /* Get a FILE pointer to write to the file descriptor FD (only
14098b9484cSchristos      called if PEX_USE_PIPES is set).  If BINARY is non-zero, open in
14198b9484cSchristos      binary mode.  Arrange for FD not to be inherited by the child
14298b9484cSchristos      processes.  Return pointer on success, NULL on error.  */
14398b9484cSchristos   FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */);
14498b9484cSchristos   /* Free any system dependent data associated with OBJ.  May be
14598b9484cSchristos      NULL if there is nothing to do.  */
14698b9484cSchristos   void (*cleanup) (struct pex_obj *);
14798b9484cSchristos };
14898b9484cSchristos 
14998b9484cSchristos extern struct pex_obj *pex_init_common (int, const char *, const char *,
15098b9484cSchristos 					const struct pex_funcs *);
15198b9484cSchristos 
15298b9484cSchristos #endif
153