BRL-CAD
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1/* P R O C E S S . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2026 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21#ifndef BU_PROCESS_H
22#define BU_PROCESS_H
23
24#include "common.h"
25
26#include <stdio.h> /* FILE */
27#include "bu/defines.h"
28
30
31/** @addtogroup bu_process
32 *
33 * @brief
34 * Routines for process and sub-process management.
35 */
36/** @{ */
37/** @file bu/process.h */
38
39/* Wrappers for using subprocess execution */
40struct bu_process;
41struct bu_vls;
42
48
49typedef enum {
50 BU_PROCESS_DEFAULT = 0x0, // default process options: equiv to (bu_process_opts)0
51 BU_PROCESS_OUT_EQ_ERR = 0x1, // stdout reads from stderr instead
52 BU_PROCESS_HIDE_WINDOW = 0x2, // (Windows only)hide creation window if process would normally spawn one
54
55#ifndef ERROR_PROCESS_ABORTED
56// have a consistent 'aborted' return code on cross-platforms
57#define ERROR_PROCESS_ABORTED 1067L
58#endif
59
60/**
61 * Callback type used by bu_process_func().
62 *
63 * The callback runs in a subprocess. On POSIX platforms this is
64 * implemented using fork(), so the callback should restrict itself to
65 * fork-safe operations.
66 *
67 * @param[in] data - opaque pointer supplied to bu_process_func()
68 *
69 * @return
70 * integer exit status for the subprocess
71 */
72typedef int (*bu_process_func_t)(void *data);
73
74/**
75 * Status and optional capture buffers for bu_process_func().
76 *
77 * If out and/or err are non-NULL, they must point to initialized
78 * bu_vls containers. bu_process_func() truncates any supplied capture
79 * buffers before appending child process output.
80 */
89
90/**
91 * @brief Wrapper for executing a sub-process
92 *
93 * @deprecated use bu_process_create() instead.
94 *
95 * @note FIXME: eliminate the last two options so all callers are not
96 * exposed to parameters not relevant to them.
97 */
98DEPRECATED BU_EXPORT extern void bu_process_exec(struct bu_process **info, const char *cmd, int argc, const char **argv, int out_eql_err, int hide_window);
99/**
100 * @brief Wrapper for creating a sub-process. Allocates bu_process and starts process
101 *
102 * @param[out] pinfo - bu_process struct to be created
103 * @param[in] argv - array of command line arguments to executed. Last element MUST be NULL
104 * @param[in] process_creation_opts - bit field for bu_process_opts
105 *
106 * @note
107 * does not guarantee child process started successfully. use bu_process_wait() to check exit status
108 */
109BU_EXPORT extern void bu_process_create(struct bu_process **pinfo, const char **argv, int process_creation_opts);
111/**
112 * @brief Run a callback in a subprocess and wait for it to complete.
113 *
114 * The callback runs in an isolated child process. On POSIX
115 * platforms this is implemented using fork(), so the callback should
116 * limit itself to fork-safe operations and communicate results back to
117 * the parent via its return code and any captured output.
118 *
119 * If info is non-NULL, the status fields are populated on return. If
120 * info->out and/or info->err are non-NULL, those initialized bu_vls
121 * buffers are truncated and filled with captured child output. When
122 * BU_PROCESS_OUT_EQ_ERR is supplied, stderr is merged into the stdout
123 * capture buffer; if no stdout buffer is supplied, the merged stream is
124 * captured in info->err when available.
125 *
126 * @param[in,out] info - status record and optional capture buffers. May be NULL
127 * @param[in] func - callback to run in the subprocess
128 * @param[in] data - opaque pointer supplied to func
129 * @param[in] timeout_ms - maximum wait time in milliseconds before forcibly
130 * stopping the subprocess. A value of 0 waits indefinitely
131 * @param[in] flags - subprocess behavior flags. BU_PROCESS_OUT_EQ_ERR is
132 * currently supported; on non-POSIX platforms this API is not yet supported
133 *
134 * @return
135 * callback return code if the subprocess exits normally;
136 * ERROR_PROCESS_ABORTED if it is terminated by a signal or times out;
137 * -1 on setup failure or unsupported platforms
138 */
139BU_EXPORT extern int bu_process_func(struct bu_process_func_info *info, bu_process_func_t func, void *data, int timeout_ms, int flags);
141
142/**
143 * @brief wait for a sub-process to complete, release all process
144 * allocations, and release the process itself.
145 *
146 * @deprecated use bu_process_wait_n instead.
147 *
148 * @note FIXME: 'aborted' argument may be unnecessary (could make function
149 * provide return value of the process waited for). wtime
150 * undocumented.
151 */
152 DEPRECATED BU_EXPORT extern int bu_process_wait(int *aborted, struct bu_process *pinfo, int wtime);
153/**
154 * @brief wait for a sub-process to complete, release all process
155 * allocations, and release the process itself.
156 *
157 * @param[in] pinfo - bu_process structure of interest
158 * @param[in] wtime - maximum wait time (in ms) before forcibly stopping process. NOTE: 0 is treated as INFINITE
159 *
160 * @return
161 * 0 on success; ERROR_PROCESS_ABORTED for aborted process; Otherwise, platform specific exit status
162 */
163 BU_EXPORT extern int bu_process_wait_n(struct bu_process **pinfo, int wtime);
164
166/**
167 * @brief determine whether process is still running
168 *
169 * @param[in] pinfo - bu_process structure of interest
170 *
171 * @return
172 * 1 if alive, else 0
173 */
174BU_EXPORT extern int bu_process_alive(struct bu_process* pinfo);
175
177/**
178 * @brief determine whether there is data pending on fd
179 *
180 * @param[in] fd - file descriptor of interest
181 *
182 * @return
183 * 1 if there is data on fd, else 0
184 */
185BU_EXPORT extern int bu_process_pending(int fd);
186
188/**
189 * Read up to n bytes into buff from a process's specified output
190 * channel (fd == 1 for output, fd == 2 for err).
191 *
192 * @deprecated use bu_process_read_n instead
193 *
194 * @note FIXME: arg ordering and input/output grouping is wrong. partially
195 * redundant with bu_process_fd() and/or bu_process_open().
196 */
197DEPRECATED BU_EXPORT extern int bu_process_read(char *buff, int *count, struct bu_process *pinfo, bu_process_io_t d, int n);
198/**
199 * @brief Read from a process's specified output channel
201 * @param[in] pinfo - bu_process structure of interest
202 * @param[in] d - channel (BU_PROCESS_STDOUT, BU_PROCESS_STDERR)
203 * @param[in] n - max number of bytes to be read
204 * @param[out] buff - data read from channel
205 *
206 * @return
207 * returns the number of bytes read into buff; 0 if read is at EOF; -1 on error
208 *
209 * @note the returned number of bytes read may be less than 'n'
210 * in a successful read.
211 */
212BU_EXPORT extern int bu_process_read_n(struct bu_process *pinfo, bu_process_io_t d, int n, char *buff);
213
214
215/**
216 * Open and return a FILE pointer associated with the specified file
217 * descriptor for input (0), output (1), or error (2) respectively.
218 *
219 * Input will be opened write, output and error will be opened
220 * read.
221 *
222 * Caller should not close these FILE pointers directly. Call
223 * bu_process_close() instead.
224 *
225 * FIXME: misnomer, this does not open a process. Probably doesn't
226 * need to exist; just call fdopen().
227 *
228 * @deprecated use bu_process_file_open instead.
229 */
230DEPRECATED BU_EXPORT extern FILE *bu_process_open(struct bu_process *pinfo, bu_process_io_t d);
231/**
232 * @brief Open and return a FILE pointer associated with the specified channel.
233 *
234 * Input will be opened write, output and error will be opened
235 * read.
236 *
237 * Caller should not close these FILE pointers directly. Call
238 * bu_process_close() instead.
239 *
240 * @param[in] pinfo - bu_process structure of interest
241 * @param[in] d - channel (BU_PROCESS_STDIN, BU_PROCESS_STDOUT, BU_PROCESS_STDERR)
242 *
243 * @return
244 * FILE pointer for specified channel
245 */
246BU_EXPORT extern FILE *bu_process_file_open(struct bu_process *pinfo, bu_process_io_t d);
247
248
249/**
250 * Close any FILE pointers internally opened via bu_process_open().
251 *
252 * FIXME: misnomer, this does not close a process. Probably doesn't
253 * need to exist; just call fclose().
254 *
255 * @deprecated use bu_process_file_close instead.
256 */
257DEPRECATED BU_EXPORT extern void bu_process_close(struct bu_process *pinfo, bu_process_io_t d);
258/**
259 * @brief Close any FILE pointers internally opened via bu_process_open().
260 *
261 * @param[in] pinfo - bu_process structure of interest
262 * @param[in] d - channel (BU_PROCESS_STDIN, BU_PROCESS_STDOUT, BU_PROCESS_STDERR)
263 */
264BU_EXPORT extern void bu_process_file_close(struct bu_process *pinfo, bu_process_io_t d);
265
266
267/**
268 * @brief Retrieve the file descriptor to the I/O channel associated with the process.
270 * @param[in] pinfo - bu_process structure of interest
271 * @param[in] d - channel (BU_PROCESS_STDIN, BU_PROCESS_STDOUT, BU_PROCESS_STDERR)
272 *
273 * @return
274 * file descriptor
275 *
276 * @note For Windows cases where HANDLE is needed, use _get_osfhandle
277 */
278BU_EXPORT int bu_process_fileno(struct bu_process *pinfo, bu_process_io_t d);
279
280
281/**
282 * @brief Return the pid of the subprocess.
284 * @param[in] pinfo - bu_process structure of interest
285 *
286 * @return
287 * process ID
288 */
289BU_EXPORT int bu_process_pid(struct bu_process *pinfo);
290
291
292/**
293 * Reports one or both of the command string and the argv array
294 * used to execute the process.
295 *
296 * The bu_process container owns all strings for both cmd and argv -
297 * for the caller they are read-only.
298 *
299 * If either cmd or argv are NULL they will be skipped - if the
300 * caller only wants one of these outputs the other argument can
301 * be set to NULL.
302 *
303 * @param[out] cmd - pointer to the cmd string used to launch pinfo
304 * @param[out] argv - pointer to the argv array used to launch pinfo
305 * @param[in] pinfo - the bu_process structure of interest
306 *
307 * @return
308 * the corresponding argc count for pinfo's argv array.
309 *
310 * @deprecated use bu_process_args_n instead
311 */
312DEPRECATED BU_EXPORT int bu_process_args(const char **cmd, const char * const **argv, struct bu_process *pinfo);
313/**
314 * Reports one or both of the command string and the argv array
315 * used to execute the process.
316 *
317 * The bu_process container owns all strings for both cmd and argv -
318 * for the caller they are read-only.
319 *
320 * If either cmd or argv are NULL they will be skipped - if the
321 * caller only wants one of these outputs the other argument can
322 * be set to NULL.
323 *
324 * @param[in] pinfo - the bu_process structure of interest
325 * @param[out] cmd - pointer to the cmd string used to launch pinfo
326 * @param[out] argv - pointer to the argv array used to launch pinfo
327 *
328 * @return
329 * the corresponding argc count for pinfo's argv array.
330 */
331BU_EXPORT int bu_process_args_n(struct bu_process *pinfo, const char **cmd, const char * const **argv);
332
333
334/**
335 * @brief Return the process ID of the calling process
337 * @return
338 * process ID
339 *
340 * @deprecated use bu_pid instead
341 */
342DEPRECATED BU_EXPORT extern int bu_process_id(void);
343/**
344 * @brief Return the process ID of the calling process
345 *
346 * @return
347 * process ID
348 */
349BU_EXPORT extern int bu_pid(void);
350
351
352/**
353 * @brief determine whether process is still running using its ID
355 * @param[in] pid - process ID of interest
356 *
357 * @return
358 * 1 if alive, else 0
359 */
360BU_EXPORT extern int bu_pid_alive(int pid);
361
362
363/**
364 * @brief terminate a given process and any children.
366 * returns truthfully whether the process could be killed.
367 *
368 * @deprecated use bu_pid_terminate instead
369 */
371/**
372 * @brief terminate a given process and any children.
373 *
374 * @param[in] pid - process ID of interest
376 * @return
377 * returns truthfully whether the process could be killed
378 */
379BU_EXPORT extern int bu_pid_terminate(int pid);
380
381
382/**
383 * @brief detect whether or not a program is being run in interactive mode
385 * @return
386 * 1 if interactive, else 0
387 */
388BU_EXPORT extern int bu_interactive(void);
389
390/** @} */
391
394#endif /* BU_PROCESS_H */
395
396/*
397 * Local Variables:
398 * mode: C
399 * tab-width: 8
400 * indent-tabs-mode: t
401 * c-file-style: "stroustrup"
402 * End:
403 * ex: shiftwidth=4 tabstop=8
404 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
int bu_process_fileno(struct bu_process *pinfo, bu_process_io_t d)
Retrieve the file descriptor to the I/O channel associated with the process.
int bu_process_pending(int fd)
determine whether there is data pending on fd
int bu_interactive(void)
detect whether or not a program is being run in interactive mode
int bu_process_func(struct bu_process_func_info *info, bu_process_func_t func, void *data, int timeout_ms, int flags)
Run a callback in a subprocess and wait for it to complete.
void bu_process_create(struct bu_process **pinfo, const char **argv, int process_creation_opts)
Wrapper for creating a sub-process. Allocates bu_process and starts process.
DEPRECATED int bu_terminate(int process)
terminate a given process and any children.
DEPRECATED int bu_process_args(const char **cmd, const char *const **argv, struct bu_process *pinfo)
FILE * bu_process_file_open(struct bu_process *pinfo, bu_process_io_t d)
Open and return a FILE pointer associated with the specified channel.
bu_process_opts
Definition process.h:49
int bu_pid_alive(int pid)
determine whether process is still running using its ID
DEPRECATED int bu_process_id(void)
Return the process ID of the calling process.
int bu_pid(void)
Return the process ID of the calling process.
void bu_process_file_close(struct bu_process *pinfo, bu_process_io_t d)
Close any FILE pointers internally opened via bu_process_open().
bu_process_io_t
Definition process.h:43
int bu_process_read_n(struct bu_process *pinfo, bu_process_io_t d, int n, char *buff)
Read from a process's specified output channel.
DEPRECATED int bu_process_read(char *buff, int *count, struct bu_process *pinfo, bu_process_io_t d, int n)
DEPRECATED FILE * bu_process_open(struct bu_process *pinfo, bu_process_io_t d)
int bu_process_wait_n(struct bu_process **pinfo, int wtime)
wait for a sub-process to complete, release all process allocations, and release the process itself.
int bu_process_alive(struct bu_process *pinfo)
determine whether process is still running
DEPRECATED void bu_process_close(struct bu_process *pinfo, bu_process_io_t d)
DEPRECATED int bu_process_wait(int *aborted, struct bu_process *pinfo, int wtime)
wait for a sub-process to complete, release all process allocations, and release the process itself.
int bu_process_args_n(struct bu_process *pinfo, const char **cmd, const char *const **argv)
DEPRECATED void bu_process_exec(struct bu_process **info, const char *cmd, int argc, const char **argv, int out_eql_err, int hide_window)
Wrapper for executing a sub-process.
int bu_pid_terminate(int pid)
terminate a given process and any children.
int bu_process_pid(struct bu_process *pinfo)
Return the pid of the subprocess.
int(* bu_process_func_t)(void *data)
Definition process.h:72
@ BU_PROCESS_OUT_EQ_ERR
Definition process.h:51
@ BU_PROCESS_DEFAULT
Definition process.h:50
@ BU_PROCESS_HIDE_WINDOW
Definition process.h:52
@ BU_PROCESS_STDERR
Definition process.h:46
@ BU_PROCESS_STDIN
Definition process.h:44
@ BU_PROCESS_STDOUT
Definition process.h:45
#define DEPRECATED
Definition common.h:433
struct bu_vls * err
Definition process.h:87
struct bu_vls * out
Definition process.h:86
Definition vls.h:53