summaryrefslogtreecommitdiff
path: root/src/ck-run-programs.c
blob: 8f14388398c99d88505964246572471ca4286744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 David Zeuthen <david@fubar.dk>
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include "ck-run-programs.h"

/* The number of wall-clock seconds a program is allowed to run before we kill it */
#define TIMEOUT_SECONDS 15

/* Guaranteed by POSIX; see 'man environ' for details */
extern char **environ;

typedef struct {
        int      refcount;

        char    *path;

        gboolean child_is_running;
        guint    watch_id;
        guint    timeout_id;
        GPid     pid;
} ChildData;

static ChildData *
_child_data_new (void)
{
        ChildData *cd;

        cd = g_new0 (ChildData, 1);
        cd->refcount = 1;
        g_debug ("Allocated ChildData %p", cd);

        return cd;
}

static ChildData *
_child_data_ref (ChildData *cd)
{
        cd->refcount++;
        return cd;
}


static void
_child_data_unref (ChildData *cd)
{
        cd->refcount--;
        if (cd->refcount == 0) {
                g_free (cd->path);
                g_free (cd);
                g_debug ("Freeing ChildData %p", cd);
        }
}


static void
_child_watch (GPid       pid,
              int        status,
              ChildData *cd)
{
        g_debug ("In _child_watch for pid %d", pid);

        g_spawn_close_pid (pid);
        g_source_remove (cd->timeout_id);
        cd->timeout_id = 0;

        cd->child_is_running = FALSE;

        _child_data_unref (cd);
}

static gboolean
_child_timeout (ChildData *cd)
{
        /* The program we ran timed out; this is a bug in the program */
        g_warning ("The program %s didn't exit within %d seconds; killing it", cd->path, TIMEOUT_SECONDS);

        kill (cd->pid, SIGTERM);

        cd->child_is_running = FALSE;
        return FALSE;
}

/**
 * ck_run_programs:
 * @dirpath: Path to a directory containing programs to run
 * @action: Argument to pass to each program
 * @extra_env: Extra environment to pass
 *
 * Synchronously run all scripts with suffix .ck in the given
 * directory.
 */
void
ck_run_programs (const char *dirpath,
                 const char *action,
                 char      **extra_env)
{
        GDir       *dir;
        GError     *error;
        const char *name;
        char      **env_for_child;
        int         environ_len;
        int         extra_env_len;
        int         n;
        int         m;

        g_return_if_fail (dirpath != NULL);
        g_return_if_fail (action != NULL);

        g_debug ("Running programs in %s for action %s", dirpath, action);

        /* Construct an environment consisting of the existing and the given environment */
        environ_len = environ != NULL ? g_strv_length (environ) : 0;
        extra_env_len = extra_env != NULL ? g_strv_length (extra_env) : 0;
        env_for_child = g_new0 (char *,  environ_len + extra_env_len + 2);
        m = 0;
        for (n = 0; n < environ_len; n++) {
                env_for_child [m++] = g_strdup (environ[n]);
        }
        for (n = 0; n < extra_env_len; n++) {
                env_for_child [m++] = g_strdup (extra_env[n]);
        }
        env_for_child[m] = NULL;

        error = NULL;

        dir = g_dir_open (dirpath, 0, &error);
        if (dir == NULL) {
                /* This is unexpected; it means ConsoleKit isn't properly installed */
                g_warning ("Unable to open directory %s: %s", dirpath, error->message);
                g_error_free (error);
                goto out;
        }

        while ((name = g_dir_read_name (dir)) != NULL) {
                char      *child_argv[3];
                ChildData *cd;
                gboolean   res;

                if (!g_str_has_suffix (name, ".ck"))
                        continue;

                child_argv[0] = g_strdup_printf ("%s/%s", dirpath, name);
                child_argv[1] = (char *) action;
                child_argv[2] = NULL;

                error = NULL;
                cd = _child_data_new ();
                cd->path = g_strdup (child_argv[0]);

                /* The ChildData instance is also unreffed in _child_watch; we only ref
                 * it here to prevent cd from being destroyed while checking it in
                 * the mainloop
                 */
                _child_data_ref (cd);

                res = g_spawn_async (NULL,
                                     child_argv,
                                     env_for_child,
                                     G_SPAWN_DO_NOT_REAP_CHILD,
                                     NULL,
                                     NULL,
                                     &cd->pid,
                                     &error);
                if (! res) {
                        /* This is unexpected; it means the program to run isn't installed correctly  */
                        g_warning ("Unable to spawn %s: %s", child_argv[0], error->message);
                        g_error_free (error);
                        _child_data_unref (cd);
                        _child_data_unref (cd);
                        goto out_loop;
                }
                cd->child_is_running = TRUE;

                g_debug ("Waiting for child with pid %d", cd->pid);

                cd->watch_id = g_child_watch_add (cd->pid,
                                                  (GChildWatchFunc)_child_watch,
                                                  cd);
                cd->timeout_id = g_timeout_add (TIMEOUT_SECONDS * 1000,
                                                (GSourceFunc)_child_timeout,
                                                cd);

                /* run the mainloop; this allows the main daemon to
                 * continue serving clients (including the program we
                 * just launched) */
                while (cd->child_is_running) {
                        g_main_context_iteration (NULL, TRUE);
                }

                g_debug ("Done waiting for child with pid %d", cd->pid);
                _child_data_unref (cd);

        out_loop:
                g_free (child_argv[0]);
        }
        g_dir_close (dir);
out:
        g_strfreev (env_for_child);
}