Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | 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 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/processes.h" | ||
30 | |||
31 | #include "gate/platforms.h" | ||
32 | #include "gate/debugging.h" | ||
33 | #include "gate/results.h" | ||
34 | |||
35 | #if defined(GATE_SYS_WIN16) | ||
36 | # define GATE_PROCESSES_WINTASK_IMPL 1 | ||
37 | #elif defined(GATE_SYS_WIN) | ||
38 | # define GATE_PROCESSES_WINAPI_IMPL 1 | ||
39 | #elif defined(GATE_SYS_WASM) | ||
40 | # define GATE_PROCESSES_WASM_IMPL 1 | ||
41 | #elif defined(GATE_SYS_BEOS) | ||
42 | # define GATE_PROCESSES_BEOS_IMPL 1 | ||
43 | #elif defined(GATE_SYS_POSIX) | ||
44 | # define GATE_PROCESSES_POSIX_IMPL 1 | ||
45 | #elif defined(GATE_SYS_EFI) | ||
46 | # define GATE_PROCESSES_EFI_IMPL 1 | ||
47 | #elif defined(GATE_SYS_DOS) | ||
48 | # define GATE_PROCESSES_DOS_IMPL 1 | ||
49 | #else | ||
50 | # define GATE_PROCESSES_NOIMPL 1 | ||
51 | #endif | ||
52 | |||
53 | |||
54 | 1 | gate_size_t gate_process_print_pid(gate_process_id_t pid, char* text, gate_size_t textlen) | |
55 | { | ||
56 | 1 | return gate_str_print_int64(text, textlen, pid); | |
57 | } | ||
58 | 1 | gate_size_t gate_process_parse_pid(char const* text, gate_size_t textlen, gate_process_id_t* pid) | |
59 | { | ||
60 | 1 | return gate_str_parse_int64(text, textlen, pid); | |
61 | } | ||
62 | |||
63 | 1 | gate_result_t gate_process_run( | |
64 | gate_string_t const* executablepath, | ||
65 | gate_string_t const* args, gate_size_t argcount, | ||
66 | gate_string_t const* workdir, gate_string_t const* envvars, gate_size_t envvarcount, gate_enumint_t flags, | ||
67 | gate_stream_t* output, int* exit_code | ||
68 | ) | ||
69 | { | ||
70 | gate_result_t ret; | ||
71 | 1 | gate_process_handle_t handle = NULL; | |
72 | 1 | gate_process_stream_t* proc_stream = NULL; | |
73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | gate_process_stream_t** ptr_stream = (output == NULL) ? NULL : &proc_stream; |
74 | char buffer[GATE_MAX_COPYBUFFER_LENGTH]; | ||
75 | gate_size_t used; | ||
76 | gate_size_t written; | ||
77 | |||
78 | do | ||
79 | { | ||
80 | 1 | ret = gate_process_start(executablepath, args, argcount, workdir, envvars, envvarcount, flags, &handle, NULL, ptr_stream); | |
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_BREAK_IF_FAILED(ret); |
82 | |||
83 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | if ((ptr_stream != NULL) && (*ptr_stream != NULL)) |
84 | { | ||
85 | ✗ | while (GATE_SUCCEEDED(ret)) | |
86 | { | ||
87 | ✗ | ret = gate_stream_read(*ptr_stream, buffer, sizeof(buffer), &used); | |
88 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
89 | ✗ | if (used == 0) | |
90 | { | ||
91 | ✗ | break; | |
92 | } | ||
93 | ✗ | ret = gate_stream_write_block(output, buffer, used, &written); | |
94 | } | ||
95 | |||
96 | ✗ | if (GATE_FAILED(ret)) | |
97 | { | ||
98 | ✗ | ret = gate_process_kill(&handle); | |
99 | } | ||
100 | |||
101 | ✗ | if (GATE_SUCCEEDED(ret) && (exit_code != NULL)) | |
102 | { | ||
103 | ✗ | ret = gate_process_get_exitcode(&handle, exit_code); | |
104 | } | ||
105 | } | ||
106 | } while (0); | ||
107 | |||
108 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (handle != NULL) |
109 | { | ||
110 | 1 | gate_process_close(&handle); | |
111 | } | ||
112 | 1 | return ret; | |
113 | } | ||
114 | |||
115 | |||
116 | |||
117 | |||
118 | |||
119 | |||
120 | |||
121 | |||
122 | #if defined(GATE_PROCESSES_WASM_IMPL) | ||
123 | |||
124 | #include "gate/platform/wasm/wasm_gate.h" | ||
125 | #include <stdlib.h> | ||
126 | |||
127 | gate_result_t gate_process_get_id(gate_process_id_t* pid) | ||
128 | { | ||
129 | *pid = 1; | ||
130 | return GATE_RESULT_OK; | ||
131 | } | ||
132 | void gate_process_quit(int exitcode) | ||
133 | { | ||
134 | exit(exitcode); | ||
135 | } | ||
136 | |||
137 | gate_result_t gate_process_enum(gate_process_enum_callback_t callback, void* userparam, gate_enumint_t flags) | ||
138 | { | ||
139 | return GATE_RESULT_OK; | ||
140 | } | ||
141 | gate_result_t gate_process_getinfo(gate_process_infobuffer_t* dest_buffer, gate_process_id_t proc_id, gate_enumint_t flags) | ||
142 | { | ||
143 | return GATE_RESULT_NOTSUPPORTED; | ||
144 | } | ||
145 | gate_result_t gate_process_start( | ||
146 | gate_string_t const* executablepath, | ||
147 | gate_string_t const* args, gate_size_t argcount, | ||
148 | gate_string_t const* workdir, gate_string_t const* envvars, gate_size_t envvarcount, gate_enumint_t flags, | ||
149 | gate_process_handle_t* result_handle, gate_process_id_t* result_pid, gate_process_stream_t** result_stream) | ||
150 | { | ||
151 | return GATE_RESULT_NOTSUPPORTED; | ||
152 | } | ||
153 | gate_result_t gate_process_start_ex( | ||
154 | gate_string_t const* executablepath, | ||
155 | gate_string_t const* args, gate_size_t argcount, | ||
156 | gate_string_t const* workdir, gate_string_t const* envvars, gate_size_t envvarcount, gate_enumint_t flags, | ||
157 | char const* sessionlocation, | ||
158 | char const* username, char const* password, | ||
159 | gate_process_handle_t* result_handle, gate_process_id_t* result_pid, gate_process_stream_t** result_stream) | ||
160 | { | ||
161 | return GATE_RESULT_NOTSUPPORTED; | ||
162 | } | ||
163 | gate_result_t gate_process_close(gate_process_handle_t* handle) | ||
164 | { | ||
165 | return GATE_RESULT_NOTSUPPORTED; | ||
166 | } | ||
167 | gate_result_t gate_process_get_exitcode(gate_process_handle_t* handle, int* exitcode) | ||
168 | { | ||
169 | return GATE_RESULT_NOTSUPPORTED; | ||
170 | } | ||
171 | |||
172 | gate_result_t gate_process_wait(gate_process_handle_t* handle, gate_uint32_t timeoutms) | ||
173 | { | ||
174 | return GATE_RESULT_NOTSUPPORTED; | ||
175 | } | ||
176 | gate_result_t gate_process_wait_pid(gate_process_id_t pid, gate_uint32_t timeoutms) | ||
177 | { | ||
178 | return GATE_RESULT_NOTSUPPORTED; | ||
179 | } | ||
180 | gate_result_t gate_process_terminate(gate_process_handle_t* handle, gate_bool_t system_request) | ||
181 | { | ||
182 | return GATE_RESULT_NOTSUPPORTED; | ||
183 | } | ||
184 | gate_result_t gate_process_terminate_pid(gate_process_id_t pid, gate_bool_t system_request) | ||
185 | { | ||
186 | return GATE_RESULT_NOTSUPPORTED; | ||
187 | } | ||
188 | gate_result_t gate_process_kill(gate_process_handle_t* handle) | ||
189 | { | ||
190 | return GATE_RESULT_NOTSUPPORTED; | ||
191 | } | ||
192 | gate_result_t gate_process_kill_pid(gate_process_id_t pid) | ||
193 | { | ||
194 | return GATE_RESULT_NOTSUPPORTED; | ||
195 | } | ||
196 | gate_result_t gate_process_suspend(gate_process_handle_t* handle) | ||
197 | { | ||
198 | return GATE_RESULT_NOTSUPPORTED; | ||
199 | } | ||
200 | gate_result_t gate_process_suspend_pid(gate_process_id_t pid) | ||
201 | { | ||
202 | return GATE_RESULT_NOTSUPPORTED; | ||
203 | } | ||
204 | gate_result_t gate_process_resume(gate_process_handle_t* handle) | ||
205 | { | ||
206 | return GATE_RESULT_NOTSUPPORTED; | ||
207 | } | ||
208 | gate_result_t gate_process_resume_pid(gate_process_id_t pid) | ||
209 | { | ||
210 | return GATE_RESULT_NOTSUPPORTED; | ||
211 | } | ||
212 | |||
213 | #endif /* GATE_PROCESSES_WASM_IMPL */ | ||
214 | |||
215 | |||
216 | |||
217 | #if defined(GATE_PROCESSES_BEOS_IMPL) | ||
218 | |||
219 | #include <kernel/OS.h> | ||
220 | #include <support/SupportDefs.h> | ||
221 | |||
222 | #include <stdlib.h> | ||
223 | |||
224 | gate_result_t gate_process_get_id(gate_process_id_t* pid) | ||
225 | { | ||
226 | thread_id tid = find_thread(NULL); | ||
227 | thread_info info; | ||
228 | status_t status = get_thread_info(tid, &info); | ||
229 | if (status != B_OK) | ||
230 | { | ||
231 | return GATE_RESULT_FAILED; | ||
232 | } | ||
233 | if (pid) | ||
234 | { | ||
235 | *pid = (gate_process_id_t)info.team; | ||
236 | } | ||
237 | return GATE_RESULT_OK; | ||
238 | } | ||
239 | void gate_process_quit(int exitcode) | ||
240 | { | ||
241 | exit(exitcode); | ||
242 | } | ||
243 | |||
244 | gate_result_t gate_process_enum(gate_process_enum_callback_t callback, void* userparam, gate_enumint_t flags) | ||
245 | { | ||
246 | return GATE_RESULT_OK; | ||
247 | } | ||
248 | gate_result_t gate_process_getinfo(gate_process_infobuffer_t* dest_buffer, gate_process_id_t proc_id, gate_enumint_t flags) | ||
249 | { | ||
250 | return GATE_RESULT_NOTSUPPORTED; | ||
251 | } | ||
252 | gate_result_t gate_process_start( | ||
253 | gate_string_t const* executablepath, | ||
254 | gate_string_t const* args, gate_size_t argcount, | ||
255 | gate_string_t const* workdir, gate_string_t const* envvars, gate_size_t envvarcount, gate_enumint_t flags, | ||
256 | gate_process_handle_t* result_handle, gate_process_id_t* result_pid, gate_process_stream_t** result_stream) | ||
257 | { | ||
258 | return GATE_RESULT_NOTSUPPORTED; | ||
259 | } | ||
260 | gate_result_t gate_process_start_ex( | ||
261 | gate_string_t const* executablepath, | ||
262 | gate_string_t const* args, gate_size_t argcount, | ||
263 | gate_string_t const* workdir, gate_string_t const* envvars, gate_size_t envvarcount, gate_enumint_t flags, | ||
264 | char const* sessionlocation, | ||
265 | char const* username, char const* password, | ||
266 | gate_process_handle_t* result_handle, gate_process_id_t* result_pid, gate_process_stream_t** result_stream) | ||
267 | { | ||
268 | return GATE_RESULT_NOTSUPPORTED; | ||
269 | } | ||
270 | gate_result_t gate_process_close(gate_process_handle_t* handle) | ||
271 | { | ||
272 | return GATE_RESULT_NOTSUPPORTED; | ||
273 | } | ||
274 | gate_result_t gate_process_get_exitcode(gate_process_handle_t* handle, int* exitcode) | ||
275 | { | ||
276 | return GATE_RESULT_NOTSUPPORTED; | ||
277 | } | ||
278 | |||
279 | gate_result_t gate_process_wait(gate_process_handle_t* handle, gate_uint32_t timeoutms) | ||
280 | { | ||
281 | return GATE_RESULT_NOTSUPPORTED; | ||
282 | } | ||
283 | gate_result_t gate_process_wait_pid(gate_process_id_t pid, gate_uint32_t timeoutms) | ||
284 | { | ||
285 | return GATE_RESULT_NOTSUPPORTED; | ||
286 | } | ||
287 | gate_result_t gate_process_terminate(gate_process_handle_t* handle, gate_bool_t system_request) | ||
288 | { | ||
289 | return GATE_RESULT_NOTSUPPORTED; | ||
290 | } | ||
291 | gate_result_t gate_process_terminate_pid(gate_process_id_t pid, gate_bool_t system_request) | ||
292 | { | ||
293 | return GATE_RESULT_NOTSUPPORTED; | ||
294 | } | ||
295 | gate_result_t gate_process_kill(gate_process_handle_t* handle) | ||
296 | { | ||
297 | return GATE_RESULT_NOTSUPPORTED; | ||
298 | } | ||
299 | gate_result_t gate_process_kill_pid(gate_process_id_t pid) | ||
300 | { | ||
301 | return GATE_RESULT_NOTSUPPORTED; | ||
302 | } | ||
303 | gate_result_t gate_process_suspend(gate_process_handle_t* handle) | ||
304 | { | ||
305 | return GATE_RESULT_NOTSUPPORTED; | ||
306 | } | ||
307 | gate_result_t gate_process_suspend_pid(gate_process_id_t pid) | ||
308 | { | ||
309 | return GATE_RESULT_NOTSUPPORTED; | ||
310 | } | ||
311 | gate_result_t gate_process_resume(gate_process_handle_t* handle) | ||
312 | { | ||
313 | return GATE_RESULT_NOTSUPPORTED; | ||
314 | } | ||
315 | gate_result_t gate_process_resume_pid(gate_process_id_t pid) | ||
316 | { | ||
317 | return GATE_RESULT_NOTSUPPORTED; | ||
318 | } | ||
319 | |||
320 | #endif /* GATE_PROCESSES_BEOS_IMPL */ | ||
321 | |||
322 | |||
323 | |||
324 | #if defined(GATE_PROCESSES_NOIMPL) | ||
325 | |||
326 | #include <stdlib.h> | ||
327 | |||
328 | gate_result_t gate_process_get_id(gate_process_id_t* pid) | ||
329 | { | ||
330 | *pid = 1; | ||
331 | return GATE_RESULT_OK; | ||
332 | } | ||
333 | void gate_process_quit(int exitcode) | ||
334 | { | ||
335 | exit(exitcode); | ||
336 | } | ||
337 | |||
338 | gate_result_t gate_process_enum(gate_process_enum_callback_t callback, void* userparam, gate_enumint_t flags) | ||
339 | { | ||
340 | return GATE_RESULT_OK; | ||
341 | } | ||
342 | gate_result_t gate_process_getinfo(gate_process_infobuffer_t* dest_buffer, gate_process_id_t proc_id, gate_enumint_t flags) | ||
343 | { | ||
344 | return GATE_RESULT_NOTSUPPORTED; | ||
345 | } | ||
346 | gate_result_t gate_process_start( | ||
347 | gate_string_t const* executablepath, | ||
348 | gate_string_t const* args, gate_size_t argcount, | ||
349 | gate_string_t const* workdir, gate_string_t const* envvars, gate_size_t envvarcount, gate_enumint_t flags, | ||
350 | gate_process_handle_t* result_handle, gate_process_id_t* result_pid, gate_process_stream_t** result_stream) | ||
351 | { | ||
352 | return GATE_RESULT_NOTSUPPORTED; | ||
353 | } | ||
354 | gate_result_t gate_process_start_ex( | ||
355 | gate_string_t const* executablepath, | ||
356 | gate_string_t const* args, gate_size_t argcount, | ||
357 | gate_string_t const* workdir, gate_string_t const* envvars, gate_size_t envvarcount, gate_enumint_t flags, | ||
358 | char const* sessionlocation, | ||
359 | char const* username, char const* password, | ||
360 | gate_process_handle_t* result_handle, gate_process_id_t* result_pid, gate_process_stream_t** result_stream) | ||
361 | { | ||
362 | return GATE_RESULT_NOTSUPPORTED; | ||
363 | } | ||
364 | gate_result_t gate_process_close(gate_process_handle_t* handle) | ||
365 | { | ||
366 | return GATE_RESULT_NOTSUPPORTED; | ||
367 | } | ||
368 | gate_result_t gate_process_get_exitcode(gate_process_handle_t* handle, int* exitcode) | ||
369 | { | ||
370 | return GATE_RESULT_NOTSUPPORTED; | ||
371 | } | ||
372 | |||
373 | gate_result_t gate_process_wait(gate_process_handle_t* handle, gate_uint32_t timeoutms) | ||
374 | { | ||
375 | return GATE_RESULT_NOTSUPPORTED; | ||
376 | } | ||
377 | gate_result_t gate_process_wait_pid(gate_process_id_t pid, gate_uint32_t timeoutms) | ||
378 | { | ||
379 | return GATE_RESULT_NOTSUPPORTED; | ||
380 | } | ||
381 | gate_result_t gate_process_terminate(gate_process_handle_t* handle, gate_bool_t system_request) | ||
382 | { | ||
383 | return GATE_RESULT_NOTSUPPORTED; | ||
384 | } | ||
385 | gate_result_t gate_process_terminate_pid(gate_process_id_t pid, gate_bool_t system_request) | ||
386 | { | ||
387 | return GATE_RESULT_NOTSUPPORTED; | ||
388 | } | ||
389 | gate_result_t gate_process_kill(gate_process_handle_t* handle) | ||
390 | { | ||
391 | return GATE_RESULT_NOTSUPPORTED; | ||
392 | } | ||
393 | gate_result_t gate_process_kill_pid(gate_process_id_t pid) | ||
394 | { | ||
395 | return GATE_RESULT_NOTSUPPORTED; | ||
396 | } | ||
397 | gate_result_t gate_process_suspend(gate_process_handle_t* handle) | ||
398 | { | ||
399 | return GATE_RESULT_NOTSUPPORTED; | ||
400 | } | ||
401 | gate_result_t gate_process_suspend_pid(gate_process_id_t pid) | ||
402 | { | ||
403 | return GATE_RESULT_NOTSUPPORTED; | ||
404 | } | ||
405 | gate_result_t gate_process_resume(gate_process_handle_t* handle) | ||
406 | { | ||
407 | return GATE_RESULT_NOTSUPPORTED; | ||
408 | } | ||
409 | gate_result_t gate_process_resume_pid(gate_process_id_t pid) | ||
410 | { | ||
411 | return GATE_RESULT_NOTSUPPORTED; | ||
412 | } | ||
413 | |||
414 | #endif /* GATE_PROCESSES_NOIMPL */ | ||
415 | |||
416 |