/src/tinysparql/subprojects/glib-2.80.3/gio/gsubprocess.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright © 2012, 2013 Red Hat, Inc. |
4 | | * Copyright © 2012, 2013 Canonical Limited |
5 | | * |
6 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
7 | | * |
8 | | * This library is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * See the included COPYING file for more information. |
14 | | * |
15 | | * Authors: Colin Walters <walters@verbum.org> |
16 | | * Ryan Lortie <desrt@desrt.ca> |
17 | | */ |
18 | | |
19 | | /** |
20 | | * GSubprocess: |
21 | | * |
22 | | * `GSubprocess` allows the creation of and interaction with child |
23 | | * processes. |
24 | | * |
25 | | * Processes can be communicated with using standard GIO-style APIs (ie: |
26 | | * [class@Gio.InputStream], [class@Gio.OutputStream]). There are GIO-style APIs |
27 | | * to wait for process termination (ie: cancellable and with an asynchronous |
28 | | * variant). |
29 | | * |
30 | | * There is an API to force a process to terminate, as well as a |
31 | | * race-free API for sending UNIX signals to a subprocess. |
32 | | * |
33 | | * One major advantage that GIO brings over the core GLib library is |
34 | | * comprehensive API for asynchronous I/O, such |
35 | | * [method@Gio.OutputStream.splice_async]. This makes `GSubprocess` |
36 | | * significantly more powerful and flexible than equivalent APIs in |
37 | | * some other languages such as the `subprocess.py` |
38 | | * included with Python. For example, using `GSubprocess` one could |
39 | | * create two child processes, reading standard output from the first, |
40 | | * processing it, and writing to the input stream of the second, all |
41 | | * without blocking the main loop. |
42 | | * |
43 | | * A powerful [method@Gio.Subprocess.communicate] API is provided similar to the |
44 | | * `communicate()` method of `subprocess.py`. This enables very easy |
45 | | * interaction with a subprocess that has been opened with pipes. |
46 | | * |
47 | | * `GSubprocess` defaults to tight control over the file descriptors open |
48 | | * in the child process, avoiding dangling-FD issues that are caused by |
49 | | * a simple `fork()`/`exec()`. The only open file descriptors in the |
50 | | * spawned process are ones that were explicitly specified by the |
51 | | * `GSubprocess` API (unless `G_SUBPROCESS_FLAGS_INHERIT_FDS` was |
52 | | * specified). |
53 | | * |
54 | | * `GSubprocess` will quickly reap all child processes as they exit, |
55 | | * avoiding ‘zombie processes’ remaining around for long periods of |
56 | | * time. [method@Gio.Subprocess.wait] can be used to wait for this to happen, |
57 | | * but it will happen even without the call being explicitly made. |
58 | | * |
59 | | * As a matter of principle, `GSubprocess` has no API that accepts |
60 | | * shell-style space-separated strings. It will, however, match the |
61 | | * typical shell behaviour of searching the `PATH` for executables that do |
62 | | * not contain a directory separator in their name. By default, the `PATH` |
63 | | * of the current process is used. You can specify |
64 | | * `G_SUBPROCESS_FLAGS_SEARCH_PATH_FROM_ENVP` to use the `PATH` of the |
65 | | * launcher environment instead. |
66 | | * |
67 | | * `GSubprocess` attempts to have a very simple API for most uses (ie: |
68 | | * spawning a subprocess with arguments and support for most typical |
69 | | * kinds of input and output redirection). See [ctor@Gio.Subprocess.new]. The |
70 | | * [class@Gio.SubprocessLauncher] API is provided for more complicated cases |
71 | | * (advanced types of redirection, environment variable manipulation, |
72 | | * change of working directory, child setup functions, etc). |
73 | | * |
74 | | * A typical use of `GSubprocess` will involve calling |
75 | | * [ctor@Gio.Subprocess.new], followed by [method@Gio.Subprocess.wait_async] or |
76 | | * [method@Gio.Subprocess.wait]. After the process exits, the status can be |
77 | | * checked using functions such as [method@Gio.Subprocess.get_if_exited] (which |
78 | | * are similar to the familiar `WIFEXITED`-style POSIX macros). |
79 | | * |
80 | | * Since: 2.40 |
81 | | **/ |
82 | | |
83 | | #include "config.h" |
84 | | |
85 | | #include "gsubprocess.h" |
86 | | #include "gsubprocesslauncher-private.h" |
87 | | #include "gasyncresult.h" |
88 | | #include "giostream.h" |
89 | | #include "gmemoryinputstream.h" |
90 | | #include "glibintl.h" |
91 | | #include "glib-private.h" |
92 | | |
93 | | #include <string.h> |
94 | | #ifdef G_OS_UNIX |
95 | | #include <gio/gunixoutputstream.h> |
96 | | #include <gio/gfiledescriptorbased.h> |
97 | | #include <gio/gunixinputstream.h> |
98 | | #include <gstdio.h> |
99 | | #include <glib-unix.h> |
100 | | #include <fcntl.h> |
101 | | #endif |
102 | | #ifdef G_OS_WIN32 |
103 | | #include <windows.h> |
104 | | #include <io.h> |
105 | | #include "giowin32-priv.h" |
106 | | #endif |
107 | | |
108 | | #ifndef O_BINARY |
109 | 0 | #define O_BINARY 0 |
110 | | #endif |
111 | | |
112 | | #ifndef O_CLOEXEC |
113 | | #define O_CLOEXEC 0 |
114 | | #else |
115 | | #define HAVE_O_CLOEXEC 1 |
116 | | #endif |
117 | | |
118 | | #define COMMUNICATE_READ_SIZE 4096 |
119 | | |
120 | | /* A GSubprocess can have two possible states: running and not. |
121 | | * |
122 | | * These two states are reflected by the value of 'pid'. If it is |
123 | | * non-zero then the process is running, with that pid. |
124 | | * |
125 | | * When a GSubprocess is first created with g_object_new() it is not |
126 | | * running. When it is finalized, it is also not running. |
127 | | * |
128 | | * During initable_init(), if the g_spawn() is successful then we |
129 | | * immediately register a child watch and take an extra ref on the |
130 | | * subprocess. That reference doesn't drop until the child has quit, |
131 | | * which is why finalize can only happen in the non-running state. In |
132 | | * the event that the g_spawn() failed we will still be finalizing a |
133 | | * non-running GSubprocess (before returning from g_subprocess_new()) |
134 | | * with NULL. |
135 | | * |
136 | | * We make extensive use of the glib worker thread to guarantee |
137 | | * race-free operation. As with all child watches, glib calls waitpid() |
138 | | * in the worker thread. It reports the child exiting to us via the |
139 | | * worker thread (which means that we can do synchronous waits without |
140 | | * running a separate loop). We also send signals to the child process |
141 | | * via the worker thread so that we don't race with waitpid() and |
142 | | * accidentally send a signal to an already-reaped child. |
143 | | */ |
144 | | static void initable_iface_init (GInitableIface *initable_iface); |
145 | | |
146 | | typedef GObjectClass GSubprocessClass; |
147 | | |
148 | | struct _GSubprocess |
149 | | { |
150 | | GObject parent; |
151 | | |
152 | | /* only used during construction */ |
153 | | GSubprocessLauncher *launcher; |
154 | | GSubprocessFlags flags; |
155 | | gchar **argv; |
156 | | |
157 | | /* state tracking variables */ |
158 | | gchar identifier[24]; |
159 | | int status; |
160 | | GPid pid; |
161 | | |
162 | | /* list of GTask */ |
163 | | GMutex pending_waits_lock; |
164 | | GSList *pending_waits; |
165 | | |
166 | | /* These are the streams created if a pipe is requested via flags. */ |
167 | | GOutputStream *stdin_pipe; |
168 | | GInputStream *stdout_pipe; |
169 | | GInputStream *stderr_pipe; |
170 | | }; |
171 | | |
172 | | G_DEFINE_TYPE_WITH_CODE (GSubprocess, g_subprocess, G_TYPE_OBJECT, |
173 | | G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)) |
174 | | |
175 | | enum |
176 | | { |
177 | | PROP_0, |
178 | | PROP_FLAGS, |
179 | | PROP_ARGV, |
180 | | N_PROPS |
181 | | }; |
182 | | |
183 | | static GInputStream * |
184 | | platform_input_stream_from_spawn_fd (gint fd) |
185 | 0 | { |
186 | 0 | if (fd < 0) |
187 | 0 | return NULL; |
188 | | |
189 | 0 | #ifdef G_OS_UNIX |
190 | 0 | return g_unix_input_stream_new (fd, TRUE); |
191 | | #else |
192 | | return g_win32_input_stream_new_from_fd (fd, TRUE); |
193 | | #endif |
194 | 0 | } |
195 | | |
196 | | static GOutputStream * |
197 | | platform_output_stream_from_spawn_fd (gint fd) |
198 | 0 | { |
199 | 0 | if (fd < 0) |
200 | 0 | return NULL; |
201 | | |
202 | 0 | #ifdef G_OS_UNIX |
203 | 0 | return g_unix_output_stream_new (fd, TRUE); |
204 | | #else |
205 | | return g_win32_output_stream_new_from_fd (fd, TRUE); |
206 | | #endif |
207 | 0 | } |
208 | | |
209 | | #ifdef G_OS_UNIX |
210 | | static gint |
211 | | unix_open_file (const char *filename, |
212 | | gint mode, |
213 | | GError **error) |
214 | 0 | { |
215 | 0 | gint my_fd; |
216 | |
|
217 | 0 | my_fd = g_open (filename, mode | O_BINARY | O_CLOEXEC, 0666); |
218 | | |
219 | | /* If we return -1 we should also set the error */ |
220 | 0 | if (my_fd < 0) |
221 | 0 | { |
222 | 0 | gint saved_errno = errno; |
223 | 0 | char *display_name; |
224 | |
|
225 | 0 | display_name = g_filename_display_name (filename); |
226 | 0 | g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno), |
227 | 0 | _("Error opening file “%s”: %s"), display_name, |
228 | 0 | g_strerror (saved_errno)); |
229 | 0 | g_free (display_name); |
230 | | /* fall through... */ |
231 | 0 | } |
232 | | #ifndef HAVE_O_CLOEXEC |
233 | | else |
234 | | fcntl (my_fd, F_SETFD, FD_CLOEXEC); |
235 | | #endif |
236 | |
|
237 | 0 | return my_fd; |
238 | 0 | } |
239 | | #endif |
240 | | |
241 | | static void |
242 | | g_subprocess_set_property (GObject *object, |
243 | | guint prop_id, |
244 | | const GValue *value, |
245 | | GParamSpec *pspec) |
246 | 0 | { |
247 | 0 | GSubprocess *self = G_SUBPROCESS (object); |
248 | |
|
249 | 0 | switch (prop_id) |
250 | 0 | { |
251 | 0 | case PROP_FLAGS: |
252 | 0 | self->flags = g_value_get_flags (value); |
253 | 0 | break; |
254 | | |
255 | 0 | case PROP_ARGV: |
256 | 0 | self->argv = g_value_dup_boxed (value); |
257 | 0 | break; |
258 | | |
259 | 0 | default: |
260 | 0 | g_assert_not_reached (); |
261 | 0 | } |
262 | 0 | } |
263 | | |
264 | | static gboolean |
265 | | g_subprocess_exited (GPid pid, |
266 | | gint status, |
267 | | gpointer user_data) |
268 | 0 | { |
269 | 0 | GSubprocess *self = user_data; |
270 | 0 | GSList *tasks; |
271 | |
|
272 | 0 | g_assert (self->pid == pid); |
273 | | |
274 | 0 | g_mutex_lock (&self->pending_waits_lock); |
275 | 0 | self->status = status; |
276 | 0 | tasks = self->pending_waits; |
277 | 0 | self->pending_waits = NULL; |
278 | 0 | self->pid = 0; |
279 | 0 | g_mutex_unlock (&self->pending_waits_lock); |
280 | | |
281 | | /* Signal anyone in g_subprocess_wait_async() to wake up now */ |
282 | 0 | while (tasks) |
283 | 0 | { |
284 | 0 | g_task_return_boolean (tasks->data, TRUE); |
285 | 0 | g_object_unref (tasks->data); |
286 | 0 | tasks = g_slist_delete_link (tasks, tasks); |
287 | 0 | } |
288 | |
|
289 | 0 | g_spawn_close_pid (pid); |
290 | |
|
291 | 0 | return FALSE; |
292 | 0 | } |
293 | | |
294 | | static gboolean |
295 | | initable_init (GInitable *initable, |
296 | | GCancellable *cancellable, |
297 | | GError **error) |
298 | 0 | { |
299 | 0 | GSubprocess *self = G_SUBPROCESS (initable); |
300 | 0 | gint *pipe_ptrs[3] = { NULL, NULL, NULL }; |
301 | 0 | gint pipe_fds[3] = { -1, -1, -1 }; |
302 | 0 | gint close_fds[3] = { -1, -1, -1 }; |
303 | 0 | #ifdef G_OS_UNIX |
304 | 0 | gint stdin_fd = -1, stdout_fd = -1, stderr_fd = -1; |
305 | 0 | #endif |
306 | 0 | GSpawnFlags spawn_flags = 0; |
307 | 0 | gboolean success = FALSE; |
308 | 0 | gint i; |
309 | | |
310 | | /* this is a programmer error */ |
311 | 0 | if (!self->argv || !self->argv[0] || !self->argv[0][0]) |
312 | 0 | return FALSE; |
313 | | |
314 | 0 | if (g_cancellable_set_error_if_cancelled (cancellable, error)) |
315 | 0 | return FALSE; |
316 | | |
317 | | /* We must setup the three fds that will end up in the child as stdin, |
318 | | * stdout and stderr. |
319 | | * |
320 | | * First, stdin. |
321 | | */ |
322 | 0 | if (self->flags & G_SUBPROCESS_FLAGS_STDIN_INHERIT) |
323 | 0 | spawn_flags |= G_SPAWN_CHILD_INHERITS_STDIN; |
324 | 0 | else if (self->flags & G_SUBPROCESS_FLAGS_STDIN_PIPE) |
325 | 0 | pipe_ptrs[0] = &pipe_fds[0]; |
326 | 0 | #ifdef G_OS_UNIX |
327 | 0 | else if (self->launcher) |
328 | 0 | { |
329 | 0 | if (self->launcher->stdin_fd != -1) |
330 | 0 | stdin_fd = self->launcher->stdin_fd; |
331 | 0 | else if (self->launcher->stdin_path != NULL) |
332 | 0 | { |
333 | 0 | stdin_fd = close_fds[0] = unix_open_file (self->launcher->stdin_path, O_RDONLY, error); |
334 | 0 | if (stdin_fd == -1) |
335 | 0 | goto out; |
336 | 0 | } |
337 | 0 | } |
338 | 0 | #endif |
339 | | |
340 | | /* Next, stdout. */ |
341 | 0 | if (self->flags & G_SUBPROCESS_FLAGS_STDOUT_SILENCE) |
342 | 0 | spawn_flags |= G_SPAWN_STDOUT_TO_DEV_NULL; |
343 | 0 | else if (self->flags & G_SUBPROCESS_FLAGS_STDOUT_PIPE) |
344 | 0 | pipe_ptrs[1] = &pipe_fds[1]; |
345 | 0 | #ifdef G_OS_UNIX |
346 | 0 | else if (self->launcher) |
347 | 0 | { |
348 | 0 | if (self->launcher->stdout_fd != -1) |
349 | 0 | stdout_fd = self->launcher->stdout_fd; |
350 | 0 | else if (self->launcher->stdout_path != NULL) |
351 | 0 | { |
352 | 0 | stdout_fd = close_fds[1] = unix_open_file (self->launcher->stdout_path, O_CREAT | O_WRONLY, error); |
353 | 0 | if (stdout_fd == -1) |
354 | 0 | goto out; |
355 | 0 | } |
356 | 0 | } |
357 | 0 | #endif |
358 | | |
359 | | /* Finally, stderr. */ |
360 | 0 | if (self->flags & G_SUBPROCESS_FLAGS_STDERR_SILENCE) |
361 | 0 | spawn_flags |= G_SPAWN_STDERR_TO_DEV_NULL; |
362 | 0 | else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_PIPE) |
363 | 0 | pipe_ptrs[2] = &pipe_fds[2]; |
364 | 0 | #ifdef G_OS_UNIX |
365 | 0 | else if (self->flags & G_SUBPROCESS_FLAGS_STDERR_MERGE) |
366 | | /* This will work because stderr gets set up after stdout. */ |
367 | 0 | stderr_fd = 1; |
368 | 0 | else if (self->launcher) |
369 | 0 | { |
370 | 0 | if (self->launcher->stderr_fd != -1) |
371 | 0 | stderr_fd = self->launcher->stderr_fd; |
372 | 0 | else if (self->launcher->stderr_path != NULL) |
373 | 0 | { |
374 | 0 | stderr_fd = close_fds[2] = unix_open_file (self->launcher->stderr_path, O_CREAT | O_WRONLY, error); |
375 | 0 | if (stderr_fd == -1) |
376 | 0 | goto out; |
377 | 0 | } |
378 | 0 | } |
379 | 0 | #endif |
380 | | |
381 | | /* argv0 has no '/' in it? We better do a PATH lookup. */ |
382 | 0 | if (strchr (self->argv[0], G_DIR_SEPARATOR) == NULL) |
383 | 0 | { |
384 | 0 | if (self->launcher && self->launcher->flags & G_SUBPROCESS_FLAGS_SEARCH_PATH_FROM_ENVP) |
385 | 0 | spawn_flags |= G_SPAWN_SEARCH_PATH_FROM_ENVP; |
386 | 0 | else |
387 | 0 | spawn_flags |= G_SPAWN_SEARCH_PATH; |
388 | 0 | } |
389 | |
|
390 | 0 | if (self->flags & G_SUBPROCESS_FLAGS_INHERIT_FDS) |
391 | 0 | spawn_flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN; |
392 | |
|
393 | 0 | spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD; |
394 | 0 | spawn_flags |= G_SPAWN_CLOEXEC_PIPES; |
395 | |
|
396 | 0 | success = g_spawn_async_with_pipes_and_fds (self->launcher ? self->launcher->cwd : NULL, |
397 | 0 | (const gchar * const *) self->argv, |
398 | 0 | (const gchar * const *) (self->launcher ? self->launcher->envp : NULL), |
399 | 0 | spawn_flags, |
400 | 0 | #ifdef G_OS_UNIX |
401 | 0 | self->launcher ? self->launcher->child_setup_func : NULL, |
402 | 0 | self->launcher ? self->launcher->child_setup_user_data : NULL, |
403 | 0 | stdin_fd, stdout_fd, stderr_fd, |
404 | 0 | self->launcher ? (const gint *) self->launcher->source_fds->data : NULL, |
405 | 0 | self->launcher ? (const gint *) self->launcher->target_fds->data : NULL, |
406 | 0 | self->launcher ? self->launcher->source_fds->len : 0, |
407 | | #else |
408 | | NULL, NULL, |
409 | | -1, -1, -1, |
410 | | NULL, NULL, 0, |
411 | | #endif |
412 | 0 | &self->pid, |
413 | 0 | pipe_ptrs[0], pipe_ptrs[1], pipe_ptrs[2], |
414 | 0 | error); |
415 | 0 | g_assert (success == (self->pid != 0)); |
416 | | |
417 | 0 | { |
418 | 0 | guint64 identifier; |
419 | 0 | gint s G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
420 | |
|
421 | | #ifdef G_OS_WIN32 |
422 | | identifier = (guint64) GetProcessId (self->pid); |
423 | | #else |
424 | 0 | identifier = (guint64) self->pid; |
425 | 0 | #endif |
426 | |
|
427 | 0 | s = g_snprintf (self->identifier, sizeof self->identifier, "%"G_GUINT64_FORMAT, identifier); |
428 | 0 | g_assert (0 < s && (gsize) s < sizeof self->identifier); |
429 | 0 | } |
430 | | |
431 | | /* Start attempting to reap the child immediately */ |
432 | 0 | if (success) |
433 | 0 | { |
434 | 0 | GMainContext *worker_context; |
435 | 0 | GSource *source; |
436 | |
|
437 | 0 | worker_context = GLIB_PRIVATE_CALL (g_get_worker_context) (); |
438 | 0 | source = g_child_watch_source_new (self->pid); |
439 | 0 | g_source_set_callback (source, (GSourceFunc) g_subprocess_exited, g_object_ref (self), g_object_unref); |
440 | 0 | g_source_attach (source, worker_context); |
441 | 0 | g_source_unref (source); |
442 | 0 | } |
443 | |
|
444 | 0 | #ifdef G_OS_UNIX |
445 | 0 | out: |
446 | 0 | #endif |
447 | | /* we don't need this past init... */ |
448 | 0 | self->launcher = NULL; |
449 | |
|
450 | 0 | for (i = 0; i < 3; i++) |
451 | 0 | if (close_fds[i] != -1) |
452 | 0 | close (close_fds[i]); |
453 | |
|
454 | 0 | self->stdin_pipe = platform_output_stream_from_spawn_fd (pipe_fds[0]); |
455 | 0 | self->stdout_pipe = platform_input_stream_from_spawn_fd (pipe_fds[1]); |
456 | 0 | self->stderr_pipe = platform_input_stream_from_spawn_fd (pipe_fds[2]); |
457 | |
|
458 | 0 | return success; |
459 | 0 | } |
460 | | |
461 | | static void |
462 | | g_subprocess_finalize (GObject *object) |
463 | 0 | { |
464 | 0 | GSubprocess *self = G_SUBPROCESS (object); |
465 | |
|
466 | 0 | g_assert (self->pending_waits == NULL); |
467 | 0 | g_assert (self->pid == 0); |
468 | | |
469 | 0 | g_clear_object (&self->stdin_pipe); |
470 | 0 | g_clear_object (&self->stdout_pipe); |
471 | 0 | g_clear_object (&self->stderr_pipe); |
472 | 0 | g_strfreev (self->argv); |
473 | |
|
474 | 0 | g_mutex_clear (&self->pending_waits_lock); |
475 | |
|
476 | 0 | G_OBJECT_CLASS (g_subprocess_parent_class)->finalize (object); |
477 | 0 | } |
478 | | |
479 | | static void |
480 | | g_subprocess_init (GSubprocess *self) |
481 | 0 | { |
482 | 0 | g_mutex_init (&self->pending_waits_lock); |
483 | 0 | } |
484 | | |
485 | | static void |
486 | | initable_iface_init (GInitableIface *initable_iface) |
487 | 0 | { |
488 | 0 | initable_iface->init = initable_init; |
489 | 0 | } |
490 | | |
491 | | static void |
492 | | g_subprocess_class_init (GSubprocessClass *class) |
493 | 0 | { |
494 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (class); |
495 | |
|
496 | 0 | gobject_class->finalize = g_subprocess_finalize; |
497 | 0 | gobject_class->set_property = g_subprocess_set_property; |
498 | | |
499 | | /** |
500 | | * GSubprocess:flags: |
501 | | * |
502 | | * Subprocess flags. |
503 | | * |
504 | | * Since: 2.40 |
505 | | */ |
506 | 0 | g_object_class_install_property (gobject_class, PROP_FLAGS, |
507 | 0 | g_param_spec_flags ("flags", NULL, NULL, |
508 | 0 | G_TYPE_SUBPROCESS_FLAGS, 0, G_PARAM_WRITABLE | |
509 | 0 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
510 | | |
511 | | /** |
512 | | * GSubprocess:argv: |
513 | | * |
514 | | * Argument vector. |
515 | | * |
516 | | * Since: 2.40 |
517 | | */ |
518 | 0 | g_object_class_install_property (gobject_class, PROP_ARGV, |
519 | 0 | g_param_spec_boxed ("argv", NULL, NULL, |
520 | 0 | G_TYPE_STRV, G_PARAM_WRITABLE | |
521 | 0 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
522 | 0 | } |
523 | | |
524 | | /** |
525 | | * g_subprocess_new: (skip) |
526 | | * @flags: flags that define the behaviour of the subprocess |
527 | | * @error: (nullable): return location for an error, or %NULL |
528 | | * @argv0: first commandline argument to pass to the subprocess |
529 | | * @...: more commandline arguments, followed by %NULL |
530 | | * |
531 | | * Create a new process with the given flags and varargs argument |
532 | | * list. By default, matching the g_spawn_async() defaults, the |
533 | | * child's stdin will be set to the system null device, and |
534 | | * stdout/stderr will be inherited from the parent. You can use |
535 | | * @flags to control this behavior. |
536 | | * |
537 | | * The argument list must be terminated with %NULL. |
538 | | * |
539 | | * Returns: A newly created #GSubprocess, or %NULL on error (and @error |
540 | | * will be set) |
541 | | * |
542 | | * Since: 2.40 |
543 | | */ |
544 | | GSubprocess * |
545 | | g_subprocess_new (GSubprocessFlags flags, |
546 | | GError **error, |
547 | | const gchar *argv0, |
548 | | ...) |
549 | 0 | { |
550 | 0 | GSubprocess *result; |
551 | 0 | GPtrArray *args; |
552 | 0 | const gchar *arg; |
553 | 0 | va_list ap; |
554 | |
|
555 | 0 | g_return_val_if_fail (argv0 != NULL && argv0[0] != '\0', NULL); |
556 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
557 | | |
558 | 0 | args = g_ptr_array_new (); |
559 | |
|
560 | 0 | va_start (ap, argv0); |
561 | 0 | g_ptr_array_add (args, (gchar *) argv0); |
562 | 0 | while ((arg = va_arg (ap, const gchar *))) |
563 | 0 | g_ptr_array_add (args, (gchar *) arg); |
564 | 0 | g_ptr_array_add (args, NULL); |
565 | 0 | va_end (ap); |
566 | |
|
567 | 0 | result = g_subprocess_newv ((const gchar * const *) args->pdata, flags, error); |
568 | |
|
569 | 0 | g_ptr_array_free (args, TRUE); |
570 | |
|
571 | 0 | return result; |
572 | 0 | } |
573 | | |
574 | | /** |
575 | | * g_subprocess_newv: (rename-to g_subprocess_new) |
576 | | * @argv: (array zero-terminated=1) (element-type filename): commandline arguments for the subprocess |
577 | | * @flags: flags that define the behaviour of the subprocess |
578 | | * @error: (nullable): return location for an error, or %NULL |
579 | | * |
580 | | * Create a new process with the given flags and argument list. |
581 | | * |
582 | | * The argument list is expected to be %NULL-terminated. |
583 | | * |
584 | | * Returns: A newly created #GSubprocess, or %NULL on error (and @error |
585 | | * will be set) |
586 | | * |
587 | | * Since: 2.40 |
588 | | */ |
589 | | GSubprocess * |
590 | | g_subprocess_newv (const gchar * const *argv, |
591 | | GSubprocessFlags flags, |
592 | | GError **error) |
593 | 0 | { |
594 | 0 | g_return_val_if_fail (argv != NULL && argv[0] != NULL && argv[0][0] != '\0', NULL); |
595 | | |
596 | 0 | return g_initable_new (G_TYPE_SUBPROCESS, NULL, error, |
597 | 0 | "argv", argv, |
598 | 0 | "flags", flags, |
599 | 0 | NULL); |
600 | 0 | } |
601 | | |
602 | | /** |
603 | | * g_subprocess_get_identifier: |
604 | | * @subprocess: a #GSubprocess |
605 | | * |
606 | | * On UNIX, returns the process ID as a decimal string. |
607 | | * On Windows, returns the result of GetProcessId() also as a string. |
608 | | * If the subprocess has terminated, this will return %NULL. |
609 | | * |
610 | | * Returns: (nullable): the subprocess identifier, or %NULL if the subprocess |
611 | | * has terminated |
612 | | * Since: 2.40 |
613 | | */ |
614 | | const gchar * |
615 | | g_subprocess_get_identifier (GSubprocess *subprocess) |
616 | 0 | { |
617 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), NULL); |
618 | | |
619 | 0 | if (subprocess->pid) |
620 | 0 | return subprocess->identifier; |
621 | 0 | else |
622 | 0 | return NULL; |
623 | 0 | } |
624 | | |
625 | | /** |
626 | | * g_subprocess_get_stdin_pipe: |
627 | | * @subprocess: a #GSubprocess |
628 | | * |
629 | | * Gets the #GOutputStream that you can write to in order to give data |
630 | | * to the stdin of @subprocess. |
631 | | * |
632 | | * The process must have been created with %G_SUBPROCESS_FLAGS_STDIN_PIPE and |
633 | | * not %G_SUBPROCESS_FLAGS_STDIN_INHERIT, otherwise %NULL will be returned. |
634 | | * |
635 | | * Returns: (nullable) (transfer none): the stdout pipe |
636 | | * |
637 | | * Since: 2.40 |
638 | | **/ |
639 | | GOutputStream * |
640 | | g_subprocess_get_stdin_pipe (GSubprocess *subprocess) |
641 | 0 | { |
642 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), NULL); |
643 | | |
644 | 0 | return subprocess->stdin_pipe; |
645 | 0 | } |
646 | | |
647 | | /** |
648 | | * g_subprocess_get_stdout_pipe: |
649 | | * @subprocess: a #GSubprocess |
650 | | * |
651 | | * Gets the #GInputStream from which to read the stdout output of |
652 | | * @subprocess. |
653 | | * |
654 | | * The process must have been created with %G_SUBPROCESS_FLAGS_STDOUT_PIPE, |
655 | | * otherwise %NULL will be returned. |
656 | | * |
657 | | * Returns: (nullable) (transfer none): the stdout pipe |
658 | | * |
659 | | * Since: 2.40 |
660 | | **/ |
661 | | GInputStream * |
662 | | g_subprocess_get_stdout_pipe (GSubprocess *subprocess) |
663 | 0 | { |
664 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), NULL); |
665 | | |
666 | 0 | return subprocess->stdout_pipe; |
667 | 0 | } |
668 | | |
669 | | /** |
670 | | * g_subprocess_get_stderr_pipe: |
671 | | * @subprocess: a #GSubprocess |
672 | | * |
673 | | * Gets the #GInputStream from which to read the stderr output of |
674 | | * @subprocess. |
675 | | * |
676 | | * The process must have been created with %G_SUBPROCESS_FLAGS_STDERR_PIPE, |
677 | | * otherwise %NULL will be returned. |
678 | | * |
679 | | * Returns: (nullable) (transfer none): the stderr pipe |
680 | | * |
681 | | * Since: 2.40 |
682 | | **/ |
683 | | GInputStream * |
684 | | g_subprocess_get_stderr_pipe (GSubprocess *subprocess) |
685 | 0 | { |
686 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), NULL); |
687 | | |
688 | 0 | return subprocess->stderr_pipe; |
689 | 0 | } |
690 | | |
691 | | /* Remove the first list element containing @data, and return %TRUE. If no |
692 | | * such element is found, return %FALSE. */ |
693 | | static gboolean |
694 | | slist_remove_if_present (GSList **list, |
695 | | gconstpointer data) |
696 | 0 | { |
697 | 0 | GSList *l, *prev; |
698 | |
|
699 | 0 | for (l = *list, prev = NULL; l != NULL; prev = l, l = prev->next) |
700 | 0 | { |
701 | 0 | if (l->data == data) |
702 | 0 | { |
703 | 0 | if (prev != NULL) |
704 | 0 | prev->next = l->next; |
705 | 0 | else |
706 | 0 | *list = l->next; |
707 | |
|
708 | 0 | g_slist_free_1 (l); |
709 | |
|
710 | 0 | return TRUE; |
711 | 0 | } |
712 | 0 | } |
713 | | |
714 | 0 | return FALSE; |
715 | 0 | } |
716 | | |
717 | | static void |
718 | | g_subprocess_wait_cancelled (GCancellable *cancellable, |
719 | | gpointer user_data) |
720 | 0 | { |
721 | 0 | GTask *task = user_data; |
722 | 0 | GSubprocess *self; |
723 | 0 | gboolean task_was_pending; |
724 | |
|
725 | 0 | self = g_task_get_source_object (task); |
726 | |
|
727 | 0 | g_mutex_lock (&self->pending_waits_lock); |
728 | 0 | task_was_pending = slist_remove_if_present (&self->pending_waits, task); |
729 | 0 | g_mutex_unlock (&self->pending_waits_lock); |
730 | |
|
731 | 0 | if (task_was_pending) |
732 | 0 | { |
733 | 0 | g_task_return_boolean (task, FALSE); |
734 | 0 | g_object_unref (task); /* ref from pending_waits */ |
735 | 0 | } |
736 | 0 | } |
737 | | |
738 | | /** |
739 | | * g_subprocess_wait_async: |
740 | | * @subprocess: a #GSubprocess |
741 | | * @cancellable: a #GCancellable, or %NULL |
742 | | * @callback: a #GAsyncReadyCallback to call when the operation is complete |
743 | | * @user_data: user_data for @callback |
744 | | * |
745 | | * Wait for the subprocess to terminate. |
746 | | * |
747 | | * This is the asynchronous version of g_subprocess_wait(). |
748 | | * |
749 | | * Since: 2.40 |
750 | | */ |
751 | | void |
752 | | g_subprocess_wait_async (GSubprocess *subprocess, |
753 | | GCancellable *cancellable, |
754 | | GAsyncReadyCallback callback, |
755 | | gpointer user_data) |
756 | 0 | { |
757 | 0 | GTask *task; |
758 | |
|
759 | 0 | task = g_task_new (subprocess, cancellable, callback, user_data); |
760 | 0 | g_task_set_source_tag (task, g_subprocess_wait_async); |
761 | |
|
762 | 0 | g_mutex_lock (&subprocess->pending_waits_lock); |
763 | 0 | if (subprocess->pid) |
764 | 0 | { |
765 | | /* Only bother with cancellable if we're putting it in the list. |
766 | | * If not, it's going to dispatch immediately anyway and we will |
767 | | * see the cancellation in the _finish(). |
768 | | */ |
769 | 0 | if (cancellable) |
770 | 0 | g_signal_connect_object (cancellable, "cancelled", |
771 | 0 | G_CALLBACK (g_subprocess_wait_cancelled), |
772 | 0 | task, G_CONNECT_DEFAULT); |
773 | |
|
774 | 0 | subprocess->pending_waits = g_slist_prepend (subprocess->pending_waits, task); |
775 | 0 | task = NULL; |
776 | 0 | } |
777 | 0 | g_mutex_unlock (&subprocess->pending_waits_lock); |
778 | | |
779 | | /* If we still have task then it's because did_exit is already TRUE */ |
780 | 0 | if (task != NULL) |
781 | 0 | { |
782 | 0 | g_task_return_boolean (task, TRUE); |
783 | 0 | g_object_unref (task); |
784 | 0 | } |
785 | 0 | } |
786 | | |
787 | | /** |
788 | | * g_subprocess_wait_finish: |
789 | | * @subprocess: a #GSubprocess |
790 | | * @result: the #GAsyncResult passed to your #GAsyncReadyCallback |
791 | | * @error: a pointer to a %NULL #GError, or %NULL |
792 | | * |
793 | | * Collects the result of a previous call to |
794 | | * g_subprocess_wait_async(). |
795 | | * |
796 | | * Returns: %TRUE if successful, or %FALSE with @error set |
797 | | * |
798 | | * Since: 2.40 |
799 | | */ |
800 | | gboolean |
801 | | g_subprocess_wait_finish (GSubprocess *subprocess, |
802 | | GAsyncResult *result, |
803 | | GError **error) |
804 | 0 | { |
805 | 0 | return g_task_propagate_boolean (G_TASK (result), error); |
806 | 0 | } |
807 | | |
808 | | /* Some generic helpers for emulating synchronous operations using async |
809 | | * operations. |
810 | | */ |
811 | | static void |
812 | | g_subprocess_sync_setup (void) |
813 | 0 | { |
814 | 0 | g_main_context_push_thread_default (g_main_context_new ()); |
815 | 0 | } |
816 | | |
817 | | static void |
818 | | g_subprocess_sync_done (GObject *source_object, |
819 | | GAsyncResult *result, |
820 | | gpointer user_data) |
821 | 0 | { |
822 | 0 | GAsyncResult **result_ptr = user_data; |
823 | |
|
824 | 0 | *result_ptr = g_object_ref (result); |
825 | 0 | } |
826 | | |
827 | | static void |
828 | | g_subprocess_sync_complete (GAsyncResult **result) |
829 | 0 | { |
830 | 0 | GMainContext *context = g_main_context_get_thread_default (); |
831 | |
|
832 | 0 | while (!*result) |
833 | 0 | g_main_context_iteration (context, TRUE); |
834 | |
|
835 | 0 | g_main_context_pop_thread_default (context); |
836 | 0 | g_main_context_unref (context); |
837 | 0 | } |
838 | | |
839 | | /** |
840 | | * g_subprocess_wait: |
841 | | * @subprocess: a #GSubprocess |
842 | | * @cancellable: a #GCancellable |
843 | | * @error: a #GError |
844 | | * |
845 | | * Synchronously wait for the subprocess to terminate. |
846 | | * |
847 | | * After the process terminates you can query its exit status with |
848 | | * functions such as g_subprocess_get_if_exited() and |
849 | | * g_subprocess_get_exit_status(). |
850 | | * |
851 | | * This function does not fail in the case of the subprocess having |
852 | | * abnormal termination. See g_subprocess_wait_check() for that. |
853 | | * |
854 | | * Cancelling @cancellable doesn't kill the subprocess. Call |
855 | | * g_subprocess_force_exit() if it is desirable. |
856 | | * |
857 | | * Returns: %TRUE on success, %FALSE if @cancellable was cancelled |
858 | | * |
859 | | * Since: 2.40 |
860 | | */ |
861 | | gboolean |
862 | | g_subprocess_wait (GSubprocess *subprocess, |
863 | | GCancellable *cancellable, |
864 | | GError **error) |
865 | 0 | { |
866 | 0 | GAsyncResult *result = NULL; |
867 | 0 | gboolean success; |
868 | |
|
869 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
870 | | |
871 | | /* Synchronous waits are actually the 'more difficult' case because we |
872 | | * need to deal with the possibility of cancellation. That more or |
873 | | * less implies that we need a main context (to dispatch either of the |
874 | | * possible reasons for the operation ending). |
875 | | * |
876 | | * So we make one and then do this async... |
877 | | */ |
878 | | |
879 | 0 | if (g_cancellable_set_error_if_cancelled (cancellable, error)) |
880 | 0 | return FALSE; |
881 | | |
882 | | /* We can shortcut in the case that the process already quit (but only |
883 | | * after we checked the cancellable). |
884 | | */ |
885 | 0 | if (subprocess->pid == 0) |
886 | 0 | return TRUE; |
887 | | |
888 | | /* Otherwise, we need to do this the long way... */ |
889 | 0 | g_subprocess_sync_setup (); |
890 | 0 | g_subprocess_wait_async (subprocess, cancellable, g_subprocess_sync_done, &result); |
891 | 0 | g_subprocess_sync_complete (&result); |
892 | 0 | success = g_subprocess_wait_finish (subprocess, result, error); |
893 | 0 | g_object_unref (result); |
894 | |
|
895 | 0 | return success; |
896 | 0 | } |
897 | | |
898 | | /** |
899 | | * g_subprocess_wait_check: |
900 | | * @subprocess: a #GSubprocess |
901 | | * @cancellable: a #GCancellable |
902 | | * @error: a #GError |
903 | | * |
904 | | * Combines g_subprocess_wait() with g_spawn_check_wait_status(). |
905 | | * |
906 | | * Returns: %TRUE on success, %FALSE if process exited abnormally, or |
907 | | * @cancellable was cancelled |
908 | | * |
909 | | * Since: 2.40 |
910 | | */ |
911 | | gboolean |
912 | | g_subprocess_wait_check (GSubprocess *subprocess, |
913 | | GCancellable *cancellable, |
914 | | GError **error) |
915 | 0 | { |
916 | 0 | return g_subprocess_wait (subprocess, cancellable, error) && |
917 | 0 | g_spawn_check_wait_status (subprocess->status, error); |
918 | 0 | } |
919 | | |
920 | | /** |
921 | | * g_subprocess_wait_check_async: |
922 | | * @subprocess: a #GSubprocess |
923 | | * @cancellable: a #GCancellable, or %NULL |
924 | | * @callback: a #GAsyncReadyCallback to call when the operation is complete |
925 | | * @user_data: user_data for @callback |
926 | | * |
927 | | * Combines g_subprocess_wait_async() with g_spawn_check_wait_status(). |
928 | | * |
929 | | * This is the asynchronous version of g_subprocess_wait_check(). |
930 | | * |
931 | | * Since: 2.40 |
932 | | */ |
933 | | void |
934 | | g_subprocess_wait_check_async (GSubprocess *subprocess, |
935 | | GCancellable *cancellable, |
936 | | GAsyncReadyCallback callback, |
937 | | gpointer user_data) |
938 | 0 | { |
939 | 0 | g_subprocess_wait_async (subprocess, cancellable, callback, user_data); |
940 | 0 | } |
941 | | |
942 | | /** |
943 | | * g_subprocess_wait_check_finish: |
944 | | * @subprocess: a #GSubprocess |
945 | | * @result: the #GAsyncResult passed to your #GAsyncReadyCallback |
946 | | * @error: a pointer to a %NULL #GError, or %NULL |
947 | | * |
948 | | * Collects the result of a previous call to |
949 | | * g_subprocess_wait_check_async(). |
950 | | * |
951 | | * Returns: %TRUE if successful, or %FALSE with @error set |
952 | | * |
953 | | * Since: 2.40 |
954 | | */ |
955 | | gboolean |
956 | | g_subprocess_wait_check_finish (GSubprocess *subprocess, |
957 | | GAsyncResult *result, |
958 | | GError **error) |
959 | 0 | { |
960 | 0 | return g_subprocess_wait_finish (subprocess, result, error) && |
961 | 0 | g_spawn_check_wait_status (subprocess->status, error); |
962 | 0 | } |
963 | | |
964 | | #ifdef G_OS_UNIX |
965 | | typedef struct |
966 | | { |
967 | | GSubprocess *subprocess; |
968 | | gint signalnum; |
969 | | } SignalRecord; |
970 | | |
971 | | static gboolean |
972 | | g_subprocess_actually_send_signal (gpointer user_data) |
973 | 0 | { |
974 | 0 | SignalRecord *signal_record = user_data; |
975 | | |
976 | | /* The pid is set to zero from the worker thread as well, so we don't |
977 | | * need to take a lock in order to prevent it from changing under us. |
978 | | */ |
979 | 0 | if (signal_record->subprocess->pid) |
980 | 0 | kill (signal_record->subprocess->pid, signal_record->signalnum); |
981 | |
|
982 | 0 | g_object_unref (signal_record->subprocess); |
983 | |
|
984 | 0 | g_slice_free (SignalRecord, signal_record); |
985 | |
|
986 | 0 | return FALSE; |
987 | 0 | } |
988 | | |
989 | | static void |
990 | | g_subprocess_dispatch_signal (GSubprocess *subprocess, |
991 | | gint signalnum) |
992 | 0 | { |
993 | 0 | SignalRecord signal_record = { g_object_ref (subprocess), signalnum }; |
994 | |
|
995 | 0 | g_return_if_fail (G_IS_SUBPROCESS (subprocess)); |
996 | | |
997 | | /* This MUST be a lower priority than the priority that the child |
998 | | * watch source uses in initable_init(). |
999 | | * |
1000 | | * Reaping processes, reporting the results back to GSubprocess and |
1001 | | * sending signals is all done in the glib worker thread. We cannot |
1002 | | * have a kill() done after the reap and before the report without |
1003 | | * risking killing a process that's no longer there so the kill() |
1004 | | * needs to have the lower priority. |
1005 | | * |
1006 | | * G_PRIORITY_HIGH_IDLE is lower priority than G_PRIORITY_DEFAULT. |
1007 | | */ |
1008 | 0 | g_main_context_invoke_full (GLIB_PRIVATE_CALL (g_get_worker_context) (), |
1009 | 0 | G_PRIORITY_HIGH_IDLE, |
1010 | 0 | g_subprocess_actually_send_signal, |
1011 | 0 | g_slice_dup (SignalRecord, &signal_record), |
1012 | 0 | NULL); |
1013 | 0 | } |
1014 | | |
1015 | | /** |
1016 | | * g_subprocess_send_signal: |
1017 | | * @subprocess: a #GSubprocess |
1018 | | * @signal_num: the signal number to send |
1019 | | * |
1020 | | * Sends the UNIX signal @signal_num to the subprocess, if it is still |
1021 | | * running. |
1022 | | * |
1023 | | * This API is race-free. If the subprocess has terminated, it will not |
1024 | | * be signalled. |
1025 | | * |
1026 | | * This API is not available on Windows. |
1027 | | * |
1028 | | * Since: 2.40 |
1029 | | **/ |
1030 | | void |
1031 | | g_subprocess_send_signal (GSubprocess *subprocess, |
1032 | | gint signal_num) |
1033 | 0 | { |
1034 | 0 | g_return_if_fail (G_IS_SUBPROCESS (subprocess)); |
1035 | | |
1036 | 0 | g_subprocess_dispatch_signal (subprocess, signal_num); |
1037 | 0 | } |
1038 | | #endif |
1039 | | |
1040 | | /** |
1041 | | * g_subprocess_force_exit: |
1042 | | * @subprocess: a #GSubprocess |
1043 | | * |
1044 | | * Use an operating-system specific method to attempt an immediate, |
1045 | | * forceful termination of the process. There is no mechanism to |
1046 | | * determine whether or not the request itself was successful; |
1047 | | * however, you can use g_subprocess_wait() to monitor the status of |
1048 | | * the process after calling this function. |
1049 | | * |
1050 | | * On Unix, this function sends %SIGKILL. |
1051 | | * |
1052 | | * Since: 2.40 |
1053 | | **/ |
1054 | | void |
1055 | | g_subprocess_force_exit (GSubprocess *subprocess) |
1056 | 0 | { |
1057 | 0 | g_return_if_fail (G_IS_SUBPROCESS (subprocess)); |
1058 | | |
1059 | 0 | #ifdef G_OS_UNIX |
1060 | 0 | g_subprocess_dispatch_signal (subprocess, SIGKILL); |
1061 | | #else |
1062 | | TerminateProcess (subprocess->pid, 1); |
1063 | | #endif |
1064 | 0 | } |
1065 | | |
1066 | | /** |
1067 | | * g_subprocess_get_status: |
1068 | | * @subprocess: a #GSubprocess |
1069 | | * |
1070 | | * Gets the raw status code of the process, as from waitpid(). |
1071 | | * |
1072 | | * This value has no particular meaning, but it can be used with the |
1073 | | * macros defined by the system headers such as WIFEXITED. It can also |
1074 | | * be used with g_spawn_check_wait_status(). |
1075 | | * |
1076 | | * It is more likely that you want to use g_subprocess_get_if_exited() |
1077 | | * followed by g_subprocess_get_exit_status(). |
1078 | | * |
1079 | | * It is an error to call this function before g_subprocess_wait() has |
1080 | | * returned. |
1081 | | * |
1082 | | * Returns: the (meaningless) waitpid() exit status from the kernel |
1083 | | * |
1084 | | * Since: 2.40 |
1085 | | **/ |
1086 | | gint |
1087 | | g_subprocess_get_status (GSubprocess *subprocess) |
1088 | 0 | { |
1089 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1090 | 0 | g_return_val_if_fail (subprocess->pid == 0, FALSE); |
1091 | | |
1092 | 0 | return subprocess->status; |
1093 | 0 | } |
1094 | | |
1095 | | /** |
1096 | | * g_subprocess_get_successful: |
1097 | | * @subprocess: a #GSubprocess |
1098 | | * |
1099 | | * Checks if the process was "successful". A process is considered |
1100 | | * successful if it exited cleanly with an exit status of 0, either by |
1101 | | * way of the exit() system call or return from main(). |
1102 | | * |
1103 | | * It is an error to call this function before g_subprocess_wait() has |
1104 | | * returned. |
1105 | | * |
1106 | | * Returns: %TRUE if the process exited cleanly with a exit status of 0 |
1107 | | * |
1108 | | * Since: 2.40 |
1109 | | **/ |
1110 | | gboolean |
1111 | | g_subprocess_get_successful (GSubprocess *subprocess) |
1112 | 0 | { |
1113 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1114 | 0 | g_return_val_if_fail (subprocess->pid == 0, FALSE); |
1115 | | |
1116 | 0 | #ifdef G_OS_UNIX |
1117 | 0 | return WIFEXITED (subprocess->status) && WEXITSTATUS (subprocess->status) == 0; |
1118 | | #else |
1119 | | return subprocess->status == 0; |
1120 | | #endif |
1121 | 0 | } |
1122 | | |
1123 | | /** |
1124 | | * g_subprocess_get_if_exited: |
1125 | | * @subprocess: a #GSubprocess |
1126 | | * |
1127 | | * Check if the given subprocess exited normally (ie: by way of exit() |
1128 | | * or return from main()). |
1129 | | * |
1130 | | * This is equivalent to the system WIFEXITED macro. |
1131 | | * |
1132 | | * It is an error to call this function before g_subprocess_wait() has |
1133 | | * returned. |
1134 | | * |
1135 | | * Returns: %TRUE if the case of a normal exit |
1136 | | * |
1137 | | * Since: 2.40 |
1138 | | **/ |
1139 | | gboolean |
1140 | | g_subprocess_get_if_exited (GSubprocess *subprocess) |
1141 | 0 | { |
1142 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1143 | 0 | g_return_val_if_fail (subprocess->pid == 0, FALSE); |
1144 | | |
1145 | 0 | #ifdef G_OS_UNIX |
1146 | 0 | return WIFEXITED (subprocess->status); |
1147 | | #else |
1148 | | return TRUE; |
1149 | | #endif |
1150 | 0 | } |
1151 | | |
1152 | | /** |
1153 | | * g_subprocess_get_exit_status: |
1154 | | * @subprocess: a #GSubprocess |
1155 | | * |
1156 | | * Check the exit status of the subprocess, given that it exited |
1157 | | * normally. This is the value passed to the exit() system call or the |
1158 | | * return value from main. |
1159 | | * |
1160 | | * This is equivalent to the system WEXITSTATUS macro. |
1161 | | * |
1162 | | * It is an error to call this function before g_subprocess_wait() and |
1163 | | * unless g_subprocess_get_if_exited() returned %TRUE. |
1164 | | * |
1165 | | * Returns: the exit status |
1166 | | * |
1167 | | * Since: 2.40 |
1168 | | **/ |
1169 | | gint |
1170 | | g_subprocess_get_exit_status (GSubprocess *subprocess) |
1171 | 0 | { |
1172 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), 1); |
1173 | 0 | g_return_val_if_fail (subprocess->pid == 0, 1); |
1174 | | |
1175 | 0 | #ifdef G_OS_UNIX |
1176 | 0 | g_return_val_if_fail (WIFEXITED (subprocess->status), 1); |
1177 | | |
1178 | 0 | return WEXITSTATUS (subprocess->status); |
1179 | | #else |
1180 | | return subprocess->status; |
1181 | | #endif |
1182 | 0 | } |
1183 | | |
1184 | | /** |
1185 | | * g_subprocess_get_if_signaled: |
1186 | | * @subprocess: a #GSubprocess |
1187 | | * |
1188 | | * Check if the given subprocess terminated in response to a signal. |
1189 | | * |
1190 | | * This is equivalent to the system WIFSIGNALED macro. |
1191 | | * |
1192 | | * It is an error to call this function before g_subprocess_wait() has |
1193 | | * returned. |
1194 | | * |
1195 | | * Returns: %TRUE if the case of termination due to a signal |
1196 | | * |
1197 | | * Since: 2.40 |
1198 | | **/ |
1199 | | gboolean |
1200 | | g_subprocess_get_if_signaled (GSubprocess *subprocess) |
1201 | 0 | { |
1202 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1203 | 0 | g_return_val_if_fail (subprocess->pid == 0, FALSE); |
1204 | | |
1205 | 0 | #ifdef G_OS_UNIX |
1206 | 0 | return WIFSIGNALED (subprocess->status); |
1207 | | #else |
1208 | | return FALSE; |
1209 | | #endif |
1210 | 0 | } |
1211 | | |
1212 | | /** |
1213 | | * g_subprocess_get_term_sig: |
1214 | | * @subprocess: a #GSubprocess |
1215 | | * |
1216 | | * Get the signal number that caused the subprocess to terminate, given |
1217 | | * that it terminated due to a signal. |
1218 | | * |
1219 | | * This is equivalent to the system WTERMSIG macro. |
1220 | | * |
1221 | | * It is an error to call this function before g_subprocess_wait() and |
1222 | | * unless g_subprocess_get_if_signaled() returned %TRUE. |
1223 | | * |
1224 | | * Returns: the signal causing termination |
1225 | | * |
1226 | | * Since: 2.40 |
1227 | | **/ |
1228 | | gint |
1229 | | g_subprocess_get_term_sig (GSubprocess *subprocess) |
1230 | 0 | { |
1231 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), 0); |
1232 | 0 | g_return_val_if_fail (subprocess->pid == 0, 0); |
1233 | | |
1234 | 0 | #ifdef G_OS_UNIX |
1235 | 0 | g_return_val_if_fail (WIFSIGNALED (subprocess->status), 0); |
1236 | | |
1237 | 0 | return WTERMSIG (subprocess->status); |
1238 | | #else |
1239 | | g_critical ("g_subprocess_get_term_sig() called on Windows, where " |
1240 | | "g_subprocess_get_if_signaled() always returns FALSE..."); |
1241 | | return 0; |
1242 | | #endif |
1243 | 0 | } |
1244 | | |
1245 | | /*< private >*/ |
1246 | | void |
1247 | | g_subprocess_set_launcher (GSubprocess *subprocess, |
1248 | | GSubprocessLauncher *launcher) |
1249 | 0 | { |
1250 | 0 | subprocess->launcher = launcher; |
1251 | 0 | } |
1252 | | |
1253 | | |
1254 | | /* g_subprocess_communicate implementation below: |
1255 | | * |
1256 | | * This is a tough problem. We have to watch 5 things at the same time: |
1257 | | * |
1258 | | * - writing to stdin made progress |
1259 | | * - reading from stdout made progress |
1260 | | * - reading from stderr made progress |
1261 | | * - process terminated |
1262 | | * - cancellable being cancelled by caller |
1263 | | * |
1264 | | * We use a GMainContext for all of these (either as async function |
1265 | | * calls or as a GSource (in the case of the cancellable). That way at |
1266 | | * least we don't have to worry about threading. |
1267 | | * |
1268 | | * For the sync case we use the usual trick of creating a private main |
1269 | | * context and iterating it until completion. |
1270 | | * |
1271 | | * It's very possible that the process will dump a lot of data to stdout |
1272 | | * just before it quits, so we can easily have data to read from stdout |
1273 | | * and see the process has terminated at the same time. We want to make |
1274 | | * sure that we read all of the data from the pipes first, though, so we |
1275 | | * do IO operations at a higher priority than the wait operation (which |
1276 | | * is at G_IO_PRIORITY_DEFAULT). Even in the case that we have to do |
1277 | | * multiple reads to get this data, the pipe() will always be polling |
1278 | | * as ready and with the async result for the read at a higher priority, |
1279 | | * the main context will not dispatch the completion for the wait(). |
1280 | | * |
1281 | | * We keep our own private GCancellable. In the event that any of the |
1282 | | * above suffers from an error condition (including the user cancelling |
1283 | | * their cancellable) we immediately dispatch the GTask with the error |
1284 | | * result and fire our cancellable to cleanup any pending operations. |
1285 | | * In the case that the error is that the user's cancellable was fired, |
1286 | | * it's vaguely wasteful to report an error because GTask will handle |
1287 | | * this automatically, so we just return FALSE. |
1288 | | * |
1289 | | * We let each pending sub-operation take a ref on the GTask of the |
1290 | | * communicate operation. We have to be careful that we don't report |
1291 | | * the task completion more than once, though, so we keep a flag for |
1292 | | * that. |
1293 | | */ |
1294 | | typedef struct |
1295 | | { |
1296 | | const gchar *stdin_data; |
1297 | | gsize stdin_length; |
1298 | | gsize stdin_offset; |
1299 | | |
1300 | | gboolean add_nul; |
1301 | | |
1302 | | GInputStream *stdin_buf; |
1303 | | GMemoryOutputStream *stdout_buf; |
1304 | | GMemoryOutputStream *stderr_buf; |
1305 | | |
1306 | | GCancellable *cancellable; |
1307 | | GSource *cancellable_source; |
1308 | | |
1309 | | guint outstanding_ops; |
1310 | | gboolean reported_error; |
1311 | | } CommunicateState; |
1312 | | |
1313 | | static void |
1314 | | g_subprocess_communicate_made_progress (GObject *source_object, |
1315 | | GAsyncResult *result, |
1316 | | gpointer user_data) |
1317 | 0 | { |
1318 | 0 | CommunicateState *state; |
1319 | 0 | GSubprocess *subprocess; |
1320 | 0 | GError *error = NULL; |
1321 | 0 | gpointer source; |
1322 | 0 | GTask *task; |
1323 | |
|
1324 | 0 | g_assert (source_object != NULL); |
1325 | | |
1326 | 0 | task = user_data; |
1327 | 0 | subprocess = g_task_get_source_object (task); |
1328 | 0 | state = g_task_get_task_data (task); |
1329 | 0 | source = source_object; |
1330 | |
|
1331 | 0 | state->outstanding_ops--; |
1332 | |
|
1333 | 0 | if (source == subprocess->stdin_pipe || |
1334 | 0 | source == state->stdout_buf || |
1335 | 0 | source == state->stderr_buf) |
1336 | 0 | { |
1337 | 0 | if (g_output_stream_splice_finish ((GOutputStream*) source, result, &error) == -1) |
1338 | 0 | goto out; |
1339 | | |
1340 | 0 | if (source == state->stdout_buf || |
1341 | 0 | source == state->stderr_buf) |
1342 | 0 | { |
1343 | | /* This is a memory stream, so it can't be cancelled or return |
1344 | | * an error really. |
1345 | | */ |
1346 | 0 | if (state->add_nul) |
1347 | 0 | { |
1348 | 0 | gsize bytes_written; |
1349 | 0 | if (!g_output_stream_write_all (source, "\0", 1, &bytes_written, |
1350 | 0 | NULL, &error)) |
1351 | 0 | goto out; |
1352 | 0 | } |
1353 | 0 | if (!g_output_stream_close (source, NULL, &error)) |
1354 | 0 | goto out; |
1355 | 0 | } |
1356 | 0 | } |
1357 | 0 | else if (source == subprocess) |
1358 | 0 | { |
1359 | 0 | (void) g_subprocess_wait_finish (subprocess, result, &error); |
1360 | 0 | } |
1361 | 0 | else |
1362 | 0 | g_assert_not_reached (); |
1363 | | |
1364 | 0 | out: |
1365 | 0 | if (error) |
1366 | 0 | { |
1367 | | /* Only report the first error we see. |
1368 | | * |
1369 | | * We might be seeing an error as a result of the cancellation |
1370 | | * done when the process quits. |
1371 | | */ |
1372 | 0 | if (!state->reported_error) |
1373 | 0 | { |
1374 | 0 | state->reported_error = TRUE; |
1375 | 0 | g_cancellable_cancel (state->cancellable); |
1376 | 0 | g_task_return_error (task, error); |
1377 | 0 | } |
1378 | 0 | else |
1379 | 0 | g_error_free (error); |
1380 | 0 | } |
1381 | 0 | else if (state->outstanding_ops == 0) |
1382 | 0 | { |
1383 | 0 | g_task_return_boolean (task, TRUE); |
1384 | 0 | } |
1385 | | |
1386 | | /* And drop the original ref */ |
1387 | 0 | g_object_unref (task); |
1388 | 0 | } |
1389 | | |
1390 | | static gboolean |
1391 | | g_subprocess_communicate_cancelled (GCancellable *cancellable, |
1392 | | gpointer user_data) |
1393 | 0 | { |
1394 | 0 | CommunicateState *state = user_data; |
1395 | |
|
1396 | 0 | g_cancellable_cancel (state->cancellable); |
1397 | |
|
1398 | 0 | return FALSE; |
1399 | 0 | } |
1400 | | |
1401 | | static void |
1402 | | g_subprocess_communicate_state_free (gpointer data) |
1403 | 0 | { |
1404 | 0 | CommunicateState *state = data; |
1405 | |
|
1406 | 0 | g_clear_object (&state->cancellable); |
1407 | 0 | g_clear_object (&state->stdin_buf); |
1408 | 0 | g_clear_object (&state->stdout_buf); |
1409 | 0 | g_clear_object (&state->stderr_buf); |
1410 | |
|
1411 | 0 | if (state->cancellable_source) |
1412 | 0 | { |
1413 | 0 | g_source_destroy (state->cancellable_source); |
1414 | 0 | g_source_unref (state->cancellable_source); |
1415 | 0 | } |
1416 | |
|
1417 | 0 | g_slice_free (CommunicateState, state); |
1418 | 0 | } |
1419 | | |
1420 | | static CommunicateState * |
1421 | | g_subprocess_communicate_internal (GSubprocess *subprocess, |
1422 | | gboolean add_nul, |
1423 | | GBytes *stdin_buf, |
1424 | | GCancellable *cancellable, |
1425 | | GAsyncReadyCallback callback, |
1426 | | gpointer user_data) |
1427 | 0 | { |
1428 | 0 | CommunicateState *state; |
1429 | 0 | GTask *task; |
1430 | |
|
1431 | 0 | task = g_task_new (subprocess, cancellable, callback, user_data); |
1432 | 0 | g_task_set_source_tag (task, g_subprocess_communicate_internal); |
1433 | |
|
1434 | 0 | state = g_slice_new0 (CommunicateState); |
1435 | 0 | g_task_set_task_data (task, state, g_subprocess_communicate_state_free); |
1436 | |
|
1437 | 0 | state->cancellable = g_cancellable_new (); |
1438 | 0 | state->add_nul = add_nul; |
1439 | |
|
1440 | 0 | if (cancellable) |
1441 | 0 | { |
1442 | 0 | state->cancellable_source = g_cancellable_source_new (cancellable); |
1443 | | /* No ref held here, but we unref the source from state's free function */ |
1444 | 0 | g_source_set_callback (state->cancellable_source, |
1445 | 0 | G_SOURCE_FUNC (g_subprocess_communicate_cancelled), |
1446 | 0 | state, NULL); |
1447 | 0 | g_source_attach (state->cancellable_source, g_main_context_get_thread_default ()); |
1448 | 0 | } |
1449 | |
|
1450 | 0 | if (subprocess->stdin_pipe) |
1451 | 0 | { |
1452 | 0 | g_assert (stdin_buf != NULL); |
1453 | | |
1454 | 0 | #ifdef G_OS_UNIX |
1455 | | /* We're doing async writes to the pipe, and the async write mechanism assumes |
1456 | | * that streams polling as writable do SOME progress (possibly partial) and then |
1457 | | * stop, but never block. |
1458 | | * |
1459 | | * However, for blocking pipes, unix will return writable if there is *any* space left |
1460 | | * but still block until the full buffer size is available before returning from write. |
1461 | | * So, to avoid async blocking on the main loop we make this non-blocking here. |
1462 | | * |
1463 | | * It should be safe to change the fd because we're the only user at this point as |
1464 | | * per the g_subprocess_communicate() docs, and all the code called by this function |
1465 | | * properly handles non-blocking fds. |
1466 | | */ |
1467 | 0 | g_unix_set_fd_nonblocking (g_unix_output_stream_get_fd (G_UNIX_OUTPUT_STREAM (subprocess->stdin_pipe)), TRUE, NULL); |
1468 | 0 | #endif |
1469 | |
|
1470 | 0 | state->stdin_buf = g_memory_input_stream_new_from_bytes (stdin_buf); |
1471 | 0 | g_output_stream_splice_async (subprocess->stdin_pipe, (GInputStream*)state->stdin_buf, |
1472 | 0 | G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, |
1473 | 0 | G_PRIORITY_DEFAULT, state->cancellable, |
1474 | 0 | g_subprocess_communicate_made_progress, g_object_ref (task)); |
1475 | 0 | state->outstanding_ops++; |
1476 | 0 | } |
1477 | | |
1478 | 0 | if (subprocess->stdout_pipe) |
1479 | 0 | { |
1480 | 0 | state->stdout_buf = (GMemoryOutputStream*)g_memory_output_stream_new_resizable (); |
1481 | 0 | g_output_stream_splice_async ((GOutputStream*)state->stdout_buf, subprocess->stdout_pipe, |
1482 | 0 | G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE, |
1483 | 0 | G_PRIORITY_DEFAULT, state->cancellable, |
1484 | 0 | g_subprocess_communicate_made_progress, g_object_ref (task)); |
1485 | 0 | state->outstanding_ops++; |
1486 | 0 | } |
1487 | |
|
1488 | 0 | if (subprocess->stderr_pipe) |
1489 | 0 | { |
1490 | 0 | state->stderr_buf = (GMemoryOutputStream*)g_memory_output_stream_new_resizable (); |
1491 | 0 | g_output_stream_splice_async ((GOutputStream*)state->stderr_buf, subprocess->stderr_pipe, |
1492 | 0 | G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE, |
1493 | 0 | G_PRIORITY_DEFAULT, state->cancellable, |
1494 | 0 | g_subprocess_communicate_made_progress, g_object_ref (task)); |
1495 | 0 | state->outstanding_ops++; |
1496 | 0 | } |
1497 | |
|
1498 | 0 | g_subprocess_wait_async (subprocess, state->cancellable, |
1499 | 0 | g_subprocess_communicate_made_progress, g_object_ref (task)); |
1500 | 0 | state->outstanding_ops++; |
1501 | |
|
1502 | 0 | g_object_unref (task); |
1503 | 0 | return state; |
1504 | 0 | } |
1505 | | |
1506 | | /** |
1507 | | * g_subprocess_communicate: |
1508 | | * @subprocess: a #GSubprocess |
1509 | | * @stdin_buf: (nullable): data to send to the stdin of the subprocess, or %NULL |
1510 | | * @cancellable: a #GCancellable |
1511 | | * @stdout_buf: (out) (nullable) (optional) (transfer full): data read from the subprocess stdout |
1512 | | * @stderr_buf: (out) (nullable) (optional) (transfer full): data read from the subprocess stderr |
1513 | | * @error: a pointer to a %NULL #GError pointer, or %NULL |
1514 | | * |
1515 | | * Communicate with the subprocess until it terminates, and all input |
1516 | | * and output has been completed. |
1517 | | * |
1518 | | * If @stdin_buf is given, the subprocess must have been created with |
1519 | | * %G_SUBPROCESS_FLAGS_STDIN_PIPE. The given data is fed to the |
1520 | | * stdin of the subprocess and the pipe is closed (ie: EOF). |
1521 | | * |
1522 | | * At the same time (as not to cause blocking when dealing with large |
1523 | | * amounts of data), if %G_SUBPROCESS_FLAGS_STDOUT_PIPE or |
1524 | | * %G_SUBPROCESS_FLAGS_STDERR_PIPE were used, reads from those |
1525 | | * streams. The data that was read is returned in @stdout and/or |
1526 | | * the @stderr. |
1527 | | * |
1528 | | * If the subprocess was created with %G_SUBPROCESS_FLAGS_STDOUT_PIPE, |
1529 | | * @stdout_buf will contain the data read from stdout. Otherwise, for |
1530 | | * subprocesses not created with %G_SUBPROCESS_FLAGS_STDOUT_PIPE, |
1531 | | * @stdout_buf will be set to %NULL. Similar provisions apply to |
1532 | | * @stderr_buf and %G_SUBPROCESS_FLAGS_STDERR_PIPE. |
1533 | | * |
1534 | | * As usual, any output variable may be given as %NULL to ignore it. |
1535 | | * |
1536 | | * If you desire the stdout and stderr data to be interleaved, create |
1537 | | * the subprocess with %G_SUBPROCESS_FLAGS_STDOUT_PIPE and |
1538 | | * %G_SUBPROCESS_FLAGS_STDERR_MERGE. The merged result will be returned |
1539 | | * in @stdout_buf and @stderr_buf will be set to %NULL. |
1540 | | * |
1541 | | * In case of any error (including cancellation), %FALSE will be |
1542 | | * returned with @error set. Some or all of the stdin data may have |
1543 | | * been written. Any stdout or stderr data that has been read will be |
1544 | | * discarded. None of the out variables (aside from @error) will have |
1545 | | * been set to anything in particular and should not be inspected. |
1546 | | * |
1547 | | * In the case that %TRUE is returned, the subprocess has exited and the |
1548 | | * exit status inspection APIs (eg: g_subprocess_get_if_exited(), |
1549 | | * g_subprocess_get_exit_status()) may be used. |
1550 | | * |
1551 | | * You should not attempt to use any of the subprocess pipes after |
1552 | | * starting this function, since they may be left in strange states, |
1553 | | * even if the operation was cancelled. You should especially not |
1554 | | * attempt to interact with the pipes while the operation is in progress |
1555 | | * (either from another thread or if using the asynchronous version). |
1556 | | * |
1557 | | * Returns: %TRUE if successful |
1558 | | * |
1559 | | * Since: 2.40 |
1560 | | **/ |
1561 | | gboolean |
1562 | | g_subprocess_communicate (GSubprocess *subprocess, |
1563 | | GBytes *stdin_buf, |
1564 | | GCancellable *cancellable, |
1565 | | GBytes **stdout_buf, |
1566 | | GBytes **stderr_buf, |
1567 | | GError **error) |
1568 | 0 | { |
1569 | 0 | GAsyncResult *result = NULL; |
1570 | 0 | gboolean success; |
1571 | |
|
1572 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1573 | 0 | g_return_val_if_fail (stdin_buf == NULL || (subprocess->flags & G_SUBPROCESS_FLAGS_STDIN_PIPE), FALSE); |
1574 | 0 | g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE); |
1575 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
1576 | | |
1577 | 0 | g_subprocess_sync_setup (); |
1578 | 0 | g_subprocess_communicate_internal (subprocess, FALSE, stdin_buf, cancellable, |
1579 | 0 | g_subprocess_sync_done, &result); |
1580 | 0 | g_subprocess_sync_complete (&result); |
1581 | 0 | success = g_subprocess_communicate_finish (subprocess, result, stdout_buf, stderr_buf, error); |
1582 | 0 | g_object_unref (result); |
1583 | |
|
1584 | 0 | return success; |
1585 | 0 | } |
1586 | | |
1587 | | /** |
1588 | | * g_subprocess_communicate_async: |
1589 | | * @subprocess: Self |
1590 | | * @stdin_buf: (nullable): Input data, or %NULL |
1591 | | * @cancellable: (nullable): Cancellable |
1592 | | * @callback: Callback |
1593 | | * @user_data: User data |
1594 | | * |
1595 | | * Asynchronous version of g_subprocess_communicate(). Complete |
1596 | | * invocation with g_subprocess_communicate_finish(). |
1597 | | */ |
1598 | | void |
1599 | | g_subprocess_communicate_async (GSubprocess *subprocess, |
1600 | | GBytes *stdin_buf, |
1601 | | GCancellable *cancellable, |
1602 | | GAsyncReadyCallback callback, |
1603 | | gpointer user_data) |
1604 | 0 | { |
1605 | 0 | g_return_if_fail (G_IS_SUBPROCESS (subprocess)); |
1606 | 0 | g_return_if_fail (stdin_buf == NULL || (subprocess->flags & G_SUBPROCESS_FLAGS_STDIN_PIPE)); |
1607 | 0 | g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); |
1608 | | |
1609 | 0 | g_subprocess_communicate_internal (subprocess, FALSE, stdin_buf, cancellable, callback, user_data); |
1610 | 0 | } |
1611 | | |
1612 | | /** |
1613 | | * g_subprocess_communicate_finish: |
1614 | | * @subprocess: Self |
1615 | | * @result: Result |
1616 | | * @stdout_buf: (out) (nullable) (optional) (transfer full): Return location for stdout data |
1617 | | * @stderr_buf: (out) (nullable) (optional) (transfer full): Return location for stderr data |
1618 | | * @error: Error |
1619 | | * |
1620 | | * Complete an invocation of g_subprocess_communicate_async(). |
1621 | | */ |
1622 | | gboolean |
1623 | | g_subprocess_communicate_finish (GSubprocess *subprocess, |
1624 | | GAsyncResult *result, |
1625 | | GBytes **stdout_buf, |
1626 | | GBytes **stderr_buf, |
1627 | | GError **error) |
1628 | 0 | { |
1629 | 0 | gboolean success; |
1630 | 0 | CommunicateState *state; |
1631 | |
|
1632 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1633 | 0 | g_return_val_if_fail (g_task_is_valid (result, subprocess), FALSE); |
1634 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
1635 | | |
1636 | 0 | g_object_ref (result); |
1637 | |
|
1638 | 0 | state = g_task_get_task_data ((GTask*)result); |
1639 | 0 | success = g_task_propagate_boolean ((GTask*)result, error); |
1640 | |
|
1641 | 0 | if (success) |
1642 | 0 | { |
1643 | 0 | if (stdout_buf) |
1644 | 0 | *stdout_buf = (state->stdout_buf != NULL) ? g_memory_output_stream_steal_as_bytes (state->stdout_buf) : NULL; |
1645 | 0 | if (stderr_buf) |
1646 | 0 | *stderr_buf = (state->stderr_buf != NULL) ? g_memory_output_stream_steal_as_bytes (state->stderr_buf) : NULL; |
1647 | 0 | } |
1648 | |
|
1649 | 0 | g_object_unref (result); |
1650 | 0 | return success; |
1651 | 0 | } |
1652 | | |
1653 | | /** |
1654 | | * g_subprocess_communicate_utf8: |
1655 | | * @subprocess: a #GSubprocess |
1656 | | * @stdin_buf: (nullable): data to send to the stdin of the subprocess, or %NULL |
1657 | | * @cancellable: a #GCancellable |
1658 | | * @stdout_buf: (out) (nullable) (optional) (transfer full): data read from the subprocess stdout |
1659 | | * @stderr_buf: (out) (nullable) (optional) (transfer full): data read from the subprocess stderr |
1660 | | * @error: a pointer to a %NULL #GError pointer, or %NULL |
1661 | | * |
1662 | | * Like g_subprocess_communicate(), but validates the output of the |
1663 | | * process as UTF-8, and returns it as a regular NUL terminated string. |
1664 | | * |
1665 | | * On error, @stdout_buf and @stderr_buf will be set to undefined values and |
1666 | | * should not be used. |
1667 | | */ |
1668 | | gboolean |
1669 | | g_subprocess_communicate_utf8 (GSubprocess *subprocess, |
1670 | | const char *stdin_buf, |
1671 | | GCancellable *cancellable, |
1672 | | char **stdout_buf, |
1673 | | char **stderr_buf, |
1674 | | GError **error) |
1675 | 0 | { |
1676 | 0 | GAsyncResult *result = NULL; |
1677 | 0 | gboolean success; |
1678 | 0 | GBytes *stdin_bytes; |
1679 | 0 | size_t stdin_buf_len = 0; |
1680 | |
|
1681 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1682 | 0 | g_return_val_if_fail (stdin_buf == NULL || (subprocess->flags & G_SUBPROCESS_FLAGS_STDIN_PIPE), FALSE); |
1683 | 0 | g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE); |
1684 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
1685 | | |
1686 | 0 | if (stdin_buf != NULL) |
1687 | 0 | stdin_buf_len = strlen (stdin_buf); |
1688 | 0 | stdin_bytes = g_bytes_new (stdin_buf, stdin_buf_len); |
1689 | |
|
1690 | 0 | g_subprocess_sync_setup (); |
1691 | 0 | g_subprocess_communicate_internal (subprocess, TRUE, stdin_bytes, cancellable, |
1692 | 0 | g_subprocess_sync_done, &result); |
1693 | 0 | g_subprocess_sync_complete (&result); |
1694 | 0 | success = g_subprocess_communicate_utf8_finish (subprocess, result, stdout_buf, stderr_buf, error); |
1695 | 0 | g_object_unref (result); |
1696 | |
|
1697 | 0 | g_bytes_unref (stdin_bytes); |
1698 | 0 | return success; |
1699 | 0 | } |
1700 | | |
1701 | | /** |
1702 | | * g_subprocess_communicate_utf8_async: |
1703 | | * @subprocess: Self |
1704 | | * @stdin_buf: (nullable): Input data, or %NULL |
1705 | | * @cancellable: Cancellable |
1706 | | * @callback: Callback |
1707 | | * @user_data: User data |
1708 | | * |
1709 | | * Asynchronous version of g_subprocess_communicate_utf8(). Complete |
1710 | | * invocation with g_subprocess_communicate_utf8_finish(). |
1711 | | */ |
1712 | | void |
1713 | | g_subprocess_communicate_utf8_async (GSubprocess *subprocess, |
1714 | | const char *stdin_buf, |
1715 | | GCancellable *cancellable, |
1716 | | GAsyncReadyCallback callback, |
1717 | | gpointer user_data) |
1718 | 0 | { |
1719 | 0 | GBytes *stdin_bytes; |
1720 | 0 | size_t stdin_buf_len = 0; |
1721 | |
|
1722 | 0 | g_return_if_fail (G_IS_SUBPROCESS (subprocess)); |
1723 | 0 | g_return_if_fail (stdin_buf == NULL || (subprocess->flags & G_SUBPROCESS_FLAGS_STDIN_PIPE)); |
1724 | 0 | g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); |
1725 | | |
1726 | 0 | if (stdin_buf != NULL) |
1727 | 0 | stdin_buf_len = strlen (stdin_buf); |
1728 | 0 | stdin_bytes = g_bytes_new (stdin_buf, stdin_buf_len); |
1729 | |
|
1730 | 0 | g_subprocess_communicate_internal (subprocess, TRUE, stdin_bytes, cancellable, callback, user_data); |
1731 | |
|
1732 | 0 | g_bytes_unref (stdin_bytes); |
1733 | 0 | } |
1734 | | |
1735 | | static gboolean |
1736 | | communicate_result_validate_utf8 (const char *stream_name, |
1737 | | char **return_location, |
1738 | | GMemoryOutputStream *buffer, |
1739 | | GError **error) |
1740 | 0 | { |
1741 | 0 | if (return_location == NULL) |
1742 | 0 | return TRUE; |
1743 | | |
1744 | 0 | if (buffer) |
1745 | 0 | { |
1746 | 0 | const char *end; |
1747 | 0 | *return_location = g_memory_output_stream_steal_data (buffer); |
1748 | 0 | if (!g_utf8_validate (*return_location, -1, &end)) |
1749 | 0 | { |
1750 | 0 | g_free (*return_location); |
1751 | 0 | *return_location = NULL; |
1752 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
1753 | 0 | "Invalid UTF-8 in child %s at offset %lu", |
1754 | 0 | stream_name, |
1755 | 0 | (unsigned long) (end - *return_location)); |
1756 | 0 | return FALSE; |
1757 | 0 | } |
1758 | 0 | } |
1759 | 0 | else |
1760 | 0 | *return_location = NULL; |
1761 | | |
1762 | 0 | return TRUE; |
1763 | 0 | } |
1764 | | |
1765 | | /** |
1766 | | * g_subprocess_communicate_utf8_finish: |
1767 | | * @subprocess: Self |
1768 | | * @result: Result |
1769 | | * @stdout_buf: (out) (nullable) (optional) (transfer full): Return location for stdout data |
1770 | | * @stderr_buf: (out) (nullable) (optional) (transfer full): Return location for stderr data |
1771 | | * @error: Error |
1772 | | * |
1773 | | * Complete an invocation of g_subprocess_communicate_utf8_async(). |
1774 | | */ |
1775 | | gboolean |
1776 | | g_subprocess_communicate_utf8_finish (GSubprocess *subprocess, |
1777 | | GAsyncResult *result, |
1778 | | char **stdout_buf, |
1779 | | char **stderr_buf, |
1780 | | GError **error) |
1781 | 0 | { |
1782 | 0 | gboolean ret = FALSE; |
1783 | 0 | CommunicateState *state; |
1784 | 0 | gchar *local_stdout_buf = NULL, *local_stderr_buf = NULL; |
1785 | |
|
1786 | 0 | g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); |
1787 | 0 | g_return_val_if_fail (g_task_is_valid (result, subprocess), FALSE); |
1788 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
1789 | | |
1790 | 0 | g_object_ref (result); |
1791 | |
|
1792 | 0 | state = g_task_get_task_data ((GTask*)result); |
1793 | 0 | if (!g_task_propagate_boolean ((GTask*)result, error)) |
1794 | 0 | goto out; |
1795 | | |
1796 | | /* TODO - validate UTF-8 while streaming, rather than all at once. |
1797 | | */ |
1798 | 0 | if (!communicate_result_validate_utf8 ("stdout", &local_stdout_buf, |
1799 | 0 | state->stdout_buf, |
1800 | 0 | error)) |
1801 | 0 | goto out; |
1802 | 0 | if (!communicate_result_validate_utf8 ("stderr", &local_stderr_buf, |
1803 | 0 | state->stderr_buf, |
1804 | 0 | error)) |
1805 | 0 | goto out; |
1806 | | |
1807 | 0 | ret = TRUE; |
1808 | 0 | out: |
1809 | 0 | g_object_unref (result); |
1810 | |
|
1811 | 0 | if (ret && stdout_buf != NULL) |
1812 | 0 | *stdout_buf = g_steal_pointer (&local_stdout_buf); |
1813 | 0 | if (ret && stderr_buf != NULL) |
1814 | 0 | *stderr_buf = g_steal_pointer (&local_stderr_buf); |
1815 | |
|
1816 | 0 | g_free (local_stderr_buf); |
1817 | 0 | g_free (local_stdout_buf); |
1818 | |
|
1819 | 0 | return ret; |
1820 | 0 | } |