BRL-CAD
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-2023 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
29__BEGIN_DECLS
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/**
40 * returns the process ID of the calling process
41 */
42BU_EXPORT extern int bu_process_id(void);
43
44/**
45 * @brief terminate a given process and any children.
46 *
47 * returns truthfully whether the process could be killed.
48 */
49BU_EXPORT extern int bu_terminate(int process);
50
51
52/* Wrappers for using subprocess execution */
53struct bu_process;
54
55typedef enum {
60
61/**
62 * Open and return a FILE pointer associated with the specified file
63 * descriptor for input (0), output (1), or error (2) respectively.
64 *
65 * Input will be opened write, output and error will be opened
66 * read.
67 *
68 * Caller should not close these FILE pointers directly. Call
69 * bu_process_close() instead.
70 *
71 * FIXME: misnomer, this does not open a process. Probably doesn't
72 * need to exist; just call fdopen().
73 */
74BU_EXPORT extern FILE *bu_process_open(struct bu_process *pinfo, bu_process_io_t d);
75
76
77/**
78 * Close any FILE pointers internally opened via bu_process_open().
79 *
80 * FIXME: misnomer, this does not close a process. Probably doesn't
81 * need to exist; just call fclose().
82 */
83BU_EXPORT extern void bu_process_close(struct bu_process *pinfo, bu_process_io_t d);
84
85
86/**
87 * Retrieve the file descriptor to the input (BU_PROCESS_STDIN), output
88 * (BU_PROCESS_STDOUT), or error (BU_PROCESS_STDERR) I/O channel associated
89 * with the process.
90 *
91 * For Windows cases where HANDLE is needed, use _get_osfhandle
92 */
93BU_EXPORT int bu_process_fileno(struct bu_process *pinfo, bu_process_io_t d);
94
95
96/**
97 * Return the pid of the subprocess.
98 *
99 * FIXME: seemingly redundant or combinable with bu_process_id()
100 * (perhaps make NULL be equivalent to the current process).
101 */
102BU_EXPORT int bu_process_pid(struct bu_process *pinfo);
103
104
105/**
106 * Reports one or both of the command string and the argv array
107 * used to execute the process.
108 *
109 * The bu_process container owns all strings for both cmd and argv -
110 * for the caller they are read-only.
111 *
112 * If either cmd or argv are NULL they will be skipped - if the
113 * caller only wants one of these outputs the other argument can
114 * be set to NULL.
115 *
116 * @param[out] cmd - pointer to the cmd string used to launch pinfo
117 * @param[out] argv - pointer to the argv array used to launch pinfo
118 * @param[in] pinfo - the bu_process structure of interest
119 *
120 * @return
121 * the corresponding argc count for pinfo's argv array.
122 */
123BU_EXPORT int bu_process_args(const char **cmd, const char * const **argv, struct bu_process *pinfo);
124
125
126/**
127 * Read up to n bytes into buff from a process's specified output
128 * channel (fd == 1 for output, fd == 2 for err).
129 *
130 * FIXME: arg ordering and input/output grouping is wrong. partially
131 * redundant with bu_process_fd() and/or bu_process_open().
132 */
133BU_EXPORT extern int bu_process_read(char *buff, int *count, struct bu_process *pinfo, bu_process_io_t d, int n);
134
135
136/**
137 * @brief Wrapper for executing a sub-process
138 *
139 * FIXME: eliminate the last two options so all callers are not
140 * exposed to parameters not relevant to them.
141 */
142BU_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);
143
144
145/**
146 * @brief wait for a sub-process to complete, release all process
147 * allocations, and release the process itself.
148 *
149 * FIXME: 'aborted' argument may be unnecessary (could make function
150 * provide return value of the process waited for). wtime
151 * undocumented.
152 */
153 BU_EXPORT extern int bu_process_wait(int *aborted, struct bu_process *pinfo, int wtime);
154
155/**
156 * @brief detect whether or not a program is being run in interactive mode
157 *
158 * Returns 1 if interactive, else 0
159 */
160BU_EXPORT extern int bu_interactive(void);
161
162/** @} */
163
164__END_DECLS
165
166#endif /* BU_PROCESS_H */
167
168/*
169 * Local Variables:
170 * mode: C
171 * tab-width: 8
172 * indent-tabs-mode: t
173 * c-file-style: "stroustrup"
174 * End:
175 * ex: shiftwidth=4 tabstop=8
176 */
Header file for the BRL-CAD common definitions.
int bu_process_fileno(struct bu_process *pinfo, bu_process_io_t d)
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_interactive(void)
detect whether or not a program is being run in interactive mode
int bu_process_read(char *buff, int *count, struct bu_process *pinfo, bu_process_io_t d, int n)
int bu_terminate(int process)
terminate a given process and any children.
bu_process_io_t
Definition: process.h:55
int bu_process_id(void)
void bu_process_close(struct bu_process *pinfo, bu_process_io_t d)
FILE * bu_process_open(struct bu_process *pinfo, bu_process_io_t d)
int bu_process_pid(struct bu_process *pinfo)
int bu_process_args(const char **cmd, const char *const **argv, struct bu_process *pinfo)
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.
@ BU_PROCESS_STDERR
Definition: process.h:58
@ BU_PROCESS_STDIN
Definition: process.h:56
@ BU_PROCESS_STDOUT
Definition: process.h:57
void float float int * n
Definition: tig.h:74