BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BProcess.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #ifndef BADVPN_BPROCESS_H |
||
31 | #define BADVPN_BPROCESS_H |
||
32 | |||
33 | #include <stdint.h> |
||
34 | #include <unistd.h> |
||
35 | |||
36 | #include <misc/debug.h> |
||
37 | #include <misc/debugerror.h> |
||
38 | #include <structure/LinkedList1.h> |
||
39 | #include <base/DebugObject.h> |
||
40 | #include <system/BUnixSignal.h> |
||
41 | #include <base/BPending.h> |
||
42 | |||
43 | /** |
||
44 | * Manages child processes. |
||
45 | * There may be at most one process manager at any given time. This restriction is not |
||
46 | * enforced, however. |
||
47 | */ |
||
48 | typedef struct { |
||
49 | BReactor *reactor; |
||
50 | BUnixSignal signal; |
||
51 | LinkedList1 processes; |
||
52 | BPending wait_job; |
||
53 | DebugObject d_obj; |
||
54 | } BProcessManager; |
||
55 | |||
56 | /** |
||
57 | * Handler called when the process terminates. |
||
58 | * The process object must be freed from the job context of this handler. |
||
59 | * {@link BProcess_Terminate} or {@link BProcess_Kill} must not be called |
||
60 | * after this handler is called. |
||
61 | * |
||
62 | * @param user as in {@link BProcess_InitWithFds} or {@link BProcess_Init} |
||
63 | * @param normally whether the child process terminated normally (0 or 1) |
||
64 | * @param normally_exit_status if the child process terminated normally, its exit |
||
65 | * status; otherwise undefined |
||
66 | */ |
||
67 | typedef void (*BProcess_handler) (void *user, int normally, uint8_t normally_exit_status); |
||
68 | |||
69 | /** |
||
70 | * Represents a child process. |
||
71 | */ |
||
72 | typedef struct { |
||
73 | BProcessManager *m; |
||
74 | BProcess_handler handler; |
||
75 | void *user; |
||
76 | pid_t pid; |
||
77 | LinkedList1Node list_node; // node in BProcessManager.processes |
||
78 | DebugObject d_obj; |
||
79 | DebugError d_err; |
||
80 | } BProcess; |
||
81 | |||
82 | /** |
||
83 | * Initializes the process manager. |
||
84 | * There may be at most one process manager at any given time. This restriction is not |
||
85 | * enforced, however. |
||
86 | * |
||
87 | * @param o the object |
||
88 | * @param reactor reactor we live in |
||
89 | * @return 1 on success, 0 on failure |
||
90 | */ |
||
91 | int BProcessManager_Init (BProcessManager *o, BReactor *reactor) WARN_UNUSED; |
||
92 | |||
93 | /** |
||
94 | * Frees the process manager. |
||
95 | * There must be no {@link BProcess} objects using this process manager. |
||
96 | * |
||
97 | * @param o the object |
||
98 | */ |
||
99 | void BProcessManager_Free (BProcessManager *o); |
||
100 | |||
101 | struct BProcess_params { |
||
102 | const char *username; |
||
103 | const int *fds; |
||
104 | const int *fds_map; |
||
105 | int do_setsid; |
||
106 | }; |
||
107 | |||
108 | /** |
||
109 | * Initializes the process. |
||
110 | * 'file', 'argv', 'username', 'fds' and 'fds_map' arguments are only used during this |
||
111 | * function call. |
||
112 | * If no file descriptor is mapped to a standard stream (file descriptors 0, 1, 2), |
||
113 | * then /dev/null will be opened in the child for that standard stream. |
||
114 | * |
||
115 | * @param o the object |
||
116 | * @param m process manager |
||
117 | * @param handler handler called when the process terminates |
||
118 | * @param user argument to handler |
||
119 | * @param file path to executable file |
||
120 | * @param argv arguments array, including the zeroth argument, terminated with a NULL pointer |
||
121 | * @param params.username user account to run the program as, or NULL to not switch user |
||
122 | * @param params.fds array of file descriptors in the parent to map to file descriptors in the child, |
||
123 | * terminated with -1 |
||
124 | * @param params.fds_map array of file descriptors in the child that file descriptors in 'fds' will |
||
125 | * be mapped to, in the same order. Must contain the same number of file descriptors |
||
126 | * as the 'fds' argument, and does not have to be terminated with -1. |
||
127 | * @param params.do_setsid if set to non-zero, will make the child call setsid() before exec'ing. |
||
128 | * Failure of setsid() will be ignored. |
||
129 | * @return 1 on success, 0 on failure |
||
130 | */ |
||
131 | int BProcess_Init2 (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], struct BProcess_params params) WARN_UNUSED; |
||
132 | |||
133 | /** |
||
134 | * Initializes the process. |
||
135 | * 'file', 'argv', 'username', 'fds' and 'fds_map' arguments are only used during this |
||
136 | * function call. |
||
137 | * If no file descriptor is mapped to a standard stream (file descriptors 0, 1, 2), |
||
138 | * then /dev/null will be opened in the child for that standard stream. |
||
139 | * |
||
140 | * @param o the object |
||
141 | * @param m process manager |
||
142 | * @param handler handler called when the process terminates |
||
143 | * @param user argument to handler |
||
144 | * @param file path to executable file |
||
145 | * @param argv arguments array, including the zeroth argument, terminated with a NULL pointer |
||
146 | * @param username user account to run the program as, or NULL to not switch user |
||
147 | * @param fds array of file descriptors in the parent to map to file descriptors in the child, |
||
148 | * terminated with -1 |
||
149 | * @param fds_map array of file descriptors in the child that file descriptors in 'fds' will |
||
150 | * be mapped to, in the same order. Must contain the same number of file descriptors |
||
151 | * as the 'fds' argument, and does not have to be terminated with -1. |
||
152 | * @return 1 on success, 0 on failure |
||
153 | */ |
||
154 | int BProcess_InitWithFds (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username, const int *fds, const int *fds_map) WARN_UNUSED; |
||
155 | |||
156 | /** |
||
157 | * Initializes the process. |
||
158 | * Like {@link BProcess_InitWithFds}, but without file descriptor mapping. |
||
159 | * 'file', 'argv' and 'username' arguments are only used during this function call. |
||
160 | * |
||
161 | * @param o the object |
||
162 | * @param m process manager |
||
163 | * @param handler handler called when the process terminates |
||
164 | * @param user argument to handler |
||
165 | * @param file path to executable file |
||
166 | * @param argv arguments array, including the zeroth argument, terminated with a NULL pointer |
||
167 | * @param username user account to run the program as, or NULL to not switch user |
||
168 | * @return 1 on success, 0 on failure |
||
169 | */ |
||
170 | int BProcess_Init (BProcess *o, BProcessManager *m, BProcess_handler handler, void *user, const char *file, char *const argv[], const char *username) WARN_UNUSED; |
||
171 | |||
172 | /** |
||
173 | * Frees the process. |
||
174 | * This does not do anything with the actual child process; it only prevents the user to wait |
||
175 | * for its termination. If the process terminates while a process manager is running, it will still |
||
176 | * be waited for (and will not become a zombie). |
||
177 | * |
||
178 | * @param o the object |
||
179 | */ |
||
180 | void BProcess_Free (BProcess *o); |
||
181 | |||
182 | /** |
||
183 | * Sends the process the SIGTERM signal. |
||
184 | * Success of this action does NOT mean that the child has terminated. |
||
185 | * |
||
186 | * @param o the object |
||
187 | * @return 1 on success, 0 on failure |
||
188 | */ |
||
189 | int BProcess_Terminate (BProcess *o); |
||
190 | |||
191 | /** |
||
192 | * Sends the process the SIGKILL signal. |
||
193 | * Success of this action does NOT mean that the child has terminated. |
||
194 | * |
||
195 | * @param o the object |
||
196 | * @return 1 on success, 0 on failure |
||
197 | */ |
||
198 | int BProcess_Kill (BProcess *o); |
||
199 | |||
200 | #endif |