gsm-process-helper.c 3.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2010 Novell, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * 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 20
 */

#include <config.h>

21
#include <gio/gio.h>
22
#include <glib/gi18n.h>
23 24 25 26

#include "gsm-process-helper.h"

typedef struct {
27 28 29 30 31 32
        gboolean        done;
        GSubprocess    *process;
        gboolean        caught_error;
        GError        **error;
        GMainContext   *maincontext;
        GSource        *timeout_source;
33 34 35
} GsmProcessHelper;

static void
36 37 38
on_child_exited (GObject        *source,
                 GAsyncResult   *result,
                 gpointer        data)
39 40 41
{
        GsmProcessHelper *helper = data;

42 43 44 45 46
        helper->done = TRUE;

        if (!g_subprocess_wait_check_finish ((GSubprocess*)source, result,
                                             helper->caught_error ? NULL : helper->error))
                helper->caught_error = TRUE;
47

48 49 50
        g_clear_pointer (&helper->timeout_source, g_source_destroy);

        g_main_context_wakeup (helper->maincontext);
51 52 53 54 55 56
}

static gboolean
on_child_timeout (gpointer data)
{
        GsmProcessHelper *helper = data;
57 58 59 60 61 62 63 64 65 66 67 68
        
        g_assert (!helper->done);
        
        g_subprocess_force_exit (helper->process);
        
        g_set_error_literal (helper->error,
                             G_IO_CHANNEL_ERROR,
                             G_IO_CHANNEL_ERROR_FAILED,
                             "Timed out");

        helper->timeout_source = NULL;
        
69 70 71
        return FALSE;
}

72
gboolean
73
gsm_process_helper (const char   *command_line,
74 75
                    unsigned int  timeout,
                    GError      **error)
76
{
77 78
        gboolean ret = FALSE;
        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 88
        subcontext = g_main_context_new ();
        g_main_context_push_thread_default (subcontext);
89

90 91 92
        helper.process = g_subprocess_newv ((const char*const*)argv, 0, error);
        if (!helper.process)
                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 99
        g_source_set_callback (helper.timeout_source, on_child_timeout, &helper, NULL);
        g_source_attach (helper.timeout_source, subcontext);
100

101 102
        while (!helper.done)
                g_main_context_iteration (subcontext, TRUE);
103

104 105 106 107 108 109
        ret = helper.caught_error;
 out:
        g_strfreev (argv);
        if (subcontext) {
                g_main_context_pop_thread_default (subcontext);
                g_main_context_unref (subcontext);
110
        }
111
        g_clear_object (&helper.process);
112
        return ret;
113
}