1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010 Novell, Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20
21 #include <gio/gio.h>
22 #include <glib/gi18n.h>
23
24 #include "gsm-process-helper.h"
25
26 typedef struct {
27 gboolean done;
28 GSubprocess *process;
29 gboolean caught_error;
30 GError **error;
31 GMainContext *maincontext;
32 GSource *timeout_source;
33 } GsmProcessHelper;
34
35 static void
on_child_exited(GObject * source,GAsyncResult * result,gpointer data)36 on_child_exited (GObject *source,
37 GAsyncResult *result,
38 gpointer data)
39 {
40 GsmProcessHelper *helper = data;
41
42 helper->done = TRUE;
43
44 if (!g_subprocess_wait_check_finish ((GSubprocess*)source, result,
45 helper->caught_error ? NULL : helper->error))
46 helper->caught_error = TRUE;
47
48 g_clear_pointer (&helper->timeout_source, g_source_destroy);
49
50 g_main_context_wakeup (helper->maincontext);
51 }
52
53 static gboolean
on_child_timeout(gpointer data)54 on_child_timeout (gpointer data)
55 {
56 GsmProcessHelper *helper = data;
57
58 g_assert (!helper->done);
59
60 g_subprocess_force_exit (helper->process);
61
62 g_set_error_literal (helper->error,
63 G_IO_CHANNEL_ERROR,
64 G_IO_CHANNEL_ERROR_FAILED,
65 "Timed out");
66
67 helper->timeout_source = NULL;
68
69 return FALSE;
70 }
71
72 gboolean
gsm_process_helper(const char * command_line,unsigned int timeout,GError ** error)73 gsm_process_helper (const char *command_line,
74 unsigned int timeout,
75 GError **error)
76 {
77 gboolean ret = FALSE;
78 GsmProcessHelper helper = { 0, };
79 gchar **argv = NULL;
80 GMainContext *subcontext = NULL;
81
82 if (!g_shell_parse_argv (command_line, NULL, &argv, error))
83 goto out;
84
85 helper.error = error;
86
87 subcontext = g_main_context_new ();
88 g_main_context_push_thread_default (subcontext);
89
90 helper.process = g_subprocess_newv ((const char*const*)argv, 0, error);
91 if (!helper.process)
92 goto out;
93
94 g_subprocess_wait_async (helper.process, NULL, on_child_exited, &helper);
95
96 helper.timeout_source = g_timeout_source_new (timeout);
97
98 g_source_set_callback (helper.timeout_source, on_child_timeout, &helper, NULL);
99 g_source_attach (helper.timeout_source, subcontext);
100
101 while (!helper.done)
102 g_main_context_iteration (subcontext, TRUE);
103
104 ret = helper.caught_error;
105 out:
106 g_strfreev (argv);
107 if (subcontext) {
108 g_main_context_pop_thread_default (subcontext);
109 g_main_context_unref (subcontext);
110 }
111 g_clear_object (&helper.process);
112 return ret;
113 }
114