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

source code of libiberty/pex-common.h