1155131Srwatson/* Utilities to execute a program in a subprocess (possibly linked by pipes
2162621Srwatson   with other subprocesses), and wait for it.  Shared logic.
3155131Srwatson   Copyright (C) 1996-2020 Free Software Foundation, Inc.
4155131Srwatson
5155131SrwatsonThis file is part of the libiberty library.
6155131SrwatsonLibiberty is free software; you can redistribute it and/or
7155131Srwatsonmodify it under the terms of the GNU Library General Public
8155131SrwatsonLicense as published by the Free Software Foundation; either
9155131Srwatsonversion 2 of the License, or (at your option) any later version.
10155131Srwatson
11155131SrwatsonLibiberty is distributed in the hope that it will be useful,
12155131Srwatsonbut WITHOUT ANY WARRANTY; without even the implied warranty of
13168777SrwatsonMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14155131SrwatsonLibrary General Public License for more details.
15155131Srwatson
16155131SrwatsonYou should have received a copy of the GNU Library General Public
17155131SrwatsonLicense along with libiberty; see the file COPYING.LIB.  If not,
18155131Srwatsonwrite to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19155131SrwatsonBoston, MA 02110-1301, USA.  */
20155131Srwatson
21155131Srwatson#ifndef PEX_COMMON_H
22155131Srwatson#define PEX_COMMON_H
23155131Srwatson
24155131Srwatson#include "config.h"
25155131Srwatson#include "libiberty.h"
26244390Srwatson#include <stdio.h>
27155131Srwatson
28155131Srwatson/* pid_t is may defined by config.h or sys/types.h needs to be
29155131Srwatson   included.  */
30155131Srwatson#if !defined(pid_t) && defined(HAVE_SYS_TYPES_H)
31155131Srwatson#include <sys/types.h>
32155131Srwatson#endif
33155131Srwatson
34155131Srwatson#define install_error_msg "installation problem, cannot exec `%s'"
35244390Srwatson
36189279Srwatson/* stdin file number.  */
37162621Srwatson#define STDIN_FILE_NO 0
38155131Srwatson
39244390Srwatson/* stdout file number.  */
40244390Srwatson#define STDOUT_FILE_NO 1
41162503Srwatson
42162503Srwatson/* stderr file number.  */
43168777Srwatson#define STDERR_FILE_NO 2
44162503Srwatson
45168777Srwatson/* value of `pipe': port index for reading.  */
46155131Srwatson#define READ_PORT 0
47155131Srwatson
48155131Srwatson/* value of `pipe': port index for writing.  */
49168777Srwatson#define WRITE_PORT 1
50155131Srwatson
51168777Srwatson/* The structure used by pex_init and friends.  */
52155131Srwatson
53168777Srwatsonstruct pex_obj
54155131Srwatson{
55155131Srwatson  /* Flags.  */
56155131Srwatson  int flags;
57244390Srwatson  /* Name of calling program, for error messages.  */
58155131Srwatson  const char *pname;
59189279Srwatson  /* Base name to use for temporary files.  */
60189279Srwatson  const char *tempbase;
61162621Srwatson  /* Pipe to use as stdin for next process.  */
62162621Srwatson  int next_input;
63155131Srwatson  /* File name to use as stdin for next process.  */
64155131Srwatson  char *next_input_name;
65244390Srwatson  /* Whether next_input_name was allocated using malloc.  */
66244390Srwatson  int next_input_name_allocated;
67244390Srwatson  /* If not -1, stderr pipe from the last process.  */
68244390Srwatson  int stderr_pipe;
69155131Srwatson  /* Number of child processes.  */
70162503Srwatson  int count;
71162503Srwatson  /* PIDs of child processes; array allocated using malloc.  */
72162503Srwatson  pid_t *children;
73191273Srwatson  /* Exit statuses of child processes; array allocated using malloc.  */
74162503Srwatson  int *status;
75191273Srwatson  /* Time used by child processes; array allocated using malloc.  */
76155131Srwatson  struct pex_time *time;
77155131Srwatson  /* Number of children we have already waited for.  */
78155131Srwatson  int number_waited;
79155131Srwatson  /* FILE created by pex_input_file.  */
80155131Srwatson  FILE *input_file;
81168777Srwatson  /* FILE created by pex_read_output.  */
82155131Srwatson  FILE *read_output;
83168777Srwatson  /* FILE created by pex_read_err.  */
84155131Srwatson  FILE *read_err;
85168777Srwatson  /* Number of temporary files to remove.  */
86168777Srwatson  int remove_count;
87155131Srwatson  /* List of temporary files to remove; array allocated using malloc
88168777Srwatson     of strings allocated using malloc.  */
89171537Srwatson  char **remove;
90168777Srwatson  /* Pointers to system dependent functions.  */
91155131Srwatson  const struct pex_funcs *funcs;
92155131Srwatson  /* For use by system dependent code.  */
93155131Srwatson  void *sysdep;
94155131Srwatson};
95168777Srwatson
96155131Srwatson/* Functions passed to pex_run_common.  */
97168777Srwatson
98155364Srwatsonstruct pex_funcs
99155131Srwatson{
100168777Srwatson  /* Open file NAME for reading.  If BINARY is non-zero, open in
101155131Srwatson     binary mode.  Return >= 0 on success, -1 on error.  */
102168777Srwatson  int (*open_read) (struct pex_obj *, const char */* name */, int /* binary */);
103155131Srwatson  /* Open file NAME for writing.  If BINARY is non-zero, open in
104168777Srwatson     binary mode.  Return >= 0 on success, -1 on error.  */
105244390Srwatson  int (*open_write) (struct pex_obj *, const char */* name */,
106244390Srwatson                     int /* binary */, int /* append */);
107244390Srwatson  /* Execute a child process.  FLAGS, EXECUTABLE, ARGV, ERR are from
108155131Srwatson     pex_run.  IN, OUT, ERRDES, TOCLOSE are all descriptors, from
109168777Srwatson     open_read, open_write, or pipe, or they are one of STDIN_FILE_NO,
110189279Srwatson     STDOUT_FILE_NO or STDERR_FILE_NO; if IN, OUT, and ERRDES are not
111244390Srwatson     STD*_FILE_NO, they should be closed.  If the descriptor TOCLOSE
112189279Srwatson     is not -1, and the system supports pipes, TOCLOSE should be
113189279Srwatson     closed in the child process.  The function should handle the
114189279Srwatson     PEX_STDERR_TO_STDOUT flag.  Return >= 0 on success, or -1 on
115189279Srwatson     error and set *ERRMSG and *ERR.  */
116189279Srwatson  pid_t (*exec_child) (struct pex_obj *, int /* flags */,
117189279Srwatson                      const char */* executable */, char * const * /* argv */,
118189279Srwatson                      char * const * /* env */,
119244390Srwatson                      int /* in */, int /* out */, int /* errdes */,
120189279Srwatson		      int /* toclose */, const char **/* errmsg */,
121189279Srwatson		      int */* err */);
122189279Srwatson  /* Close a descriptor.  Return 0 on success, -1 on error.  */
123189279Srwatson  int (*close) (struct pex_obj *, int);
124189279Srwatson  /* Wait for a child to complete, returning exit status in *STATUS
125189279Srwatson     and time in *TIME (if it is not null).  CHILD is from fork.  DONE
126189279Srwatson     is 1 if this is called via pex_free.  ERRMSG and ERR are as in
127189279Srwatson     fork.  Return 0 on success, -1 on error.  */
128162621Srwatson  pid_t (*wait) (struct pex_obj *, pid_t /* child */, int * /* status */,
129168777Srwatson               struct pex_time * /* time */, int /* done */,
130168777Srwatson               const char ** /* errmsg */, int * /* err */);
131168777Srwatson  /* Create a pipe (only called if PEX_USE_PIPES is set) storing two
132168777Srwatson     descriptors in P[0] and P[1].  If BINARY is non-zero, open in
133162621Srwatson     binary mode.  Return 0 on success, -1 on error.  */
134162621Srwatson  int (*pipe) (struct pex_obj *, int * /* p */, int /* binary */);
135168777Srwatson  /* Get a FILE pointer to read from a file descriptor (only called if
136155131Srwatson     PEX_USE_PIPES is set).  If BINARY is non-zero, open in binary
137168777Srwatson     mode.  Return pointer on success, NULL on error.  */
138155131Srwatson  FILE * (*fdopenr) (struct pex_obj *, int /* fd */, int /* binary */);
139168777Srwatson  /* Get a FILE pointer to write to the file descriptor FD (only
140155131Srwatson     called if PEX_USE_PIPES is set).  If BINARY is non-zero, open in
141168777Srwatson     binary mode.  Arrange for FD not to be inherited by the child
142155131Srwatson     processes.  Return pointer on success, NULL on error.  */
143168777Srwatson  FILE * (*fdopenw) (struct pex_obj *, int /* fd */, int /* binary */);
144244390Srwatson  /* Free any system dependent data associated with OBJ.  May be
145244390Srwatson     NULL if there is nothing to do.  */
146244390Srwatson  void (*cleanup) (struct pex_obj *);
147244390Srwatson};
148244390Srwatson
149244390Srwatsonextern struct pex_obj *pex_init_common (int, const char *, const char *,
150244390Srwatson					const struct pex_funcs *);
151244390Srwatson
152244390Srwatson#endif
153244390Srwatson