summaryrefslogtreecommitdiffstats
path: root/src/ck-job.c
blob: 87fe33abaf3ebf6d3000d48fe1be17934dd1f7dd (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * 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 <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>

#include "ck-job.h"
#include "ck-marshal.h"

#define CK_JOB_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CK_TYPE_JOB, CkJobPrivate))

struct CkJobPrivate
{
        guint       err_watch_id;
        guint       out_watch_id;

        char       *command;
        GString    *std_out;
        GString    *std_err;
        GPid        child_pid;

};

enum {
        COMPLETED,
        LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0, };

static void     ck_job_class_init  (CkJobClass *klass);
static void     ck_job_init        (CkJob      *job);
static void     ck_job_finalize    (GObject     *object);

G_DEFINE_TYPE (CkJob, ck_job, G_TYPE_OBJECT)

GQuark
ck_job_error_quark (void)
{
        static GQuark ret = 0;
        if (ret == 0) {
                ret = g_quark_from_static_string ("ck_job_error");
        }

        return ret;
}

static int
wait_on_child (int pid)
{
        int status;

 wait_again:
        if (waitpid (pid, &status, 0) < 0) {
                if (errno == EINTR) {
                        goto wait_again;
                } else if (errno == ECHILD) {
                        ; /* do nothing, child already reaped */
                } else {
                        g_debug ("waitpid () should not fail");
                }
        }

        return status;
}

static int
wait_on_job (CkJob *job)
{
        int status;

        status = wait_on_child (job->priv->child_pid);

        g_spawn_close_pid (job->priv->child_pid);
        job->priv->child_pid = 0;

        return WEXITSTATUS(status);
}

static void
maybe_complete_job (CkJob *job)
{
        if (job->priv->out_watch_id == 0
            && job->priv->err_watch_id == 0) {
                int status;

                status = wait_on_job (job);

                g_debug ("Emitting completed");
                g_signal_emit (job, signals [COMPLETED], 0, status);
        }
}

static gboolean
error_watch (GIOChannel   *source,
             GIOCondition  condition,
             CkJob        *job)
{
        gboolean finished = FALSE;

        if (condition & G_IO_IN) {
                GIOStatus status;
                GError   *error = NULL;
                char     *line;

                line = NULL;
                status = g_io_channel_read_line (source, &line, NULL, NULL, &error);

                switch (status) {
                case G_IO_STATUS_NORMAL:
                        g_debug ("command error output: %s", line);
                        g_string_append (job->priv->std_err, line);
                        break;
                case G_IO_STATUS_EOF:
                        finished = TRUE;
                        break;
                case G_IO_STATUS_ERROR:
                        finished = TRUE;
                        g_debug ("Error reading from child: %s\n", error->message);
                        break;
                case G_IO_STATUS_AGAIN:
                default:
                        break;
                }
                g_free (line);
        } else if (condition & G_IO_HUP) {
                finished = TRUE;
        }

        if (finished) {
                job->priv->err_watch_id = 0;
                maybe_complete_job (job);
                return FALSE;
        }

        return TRUE;
}

static gboolean
out_watch (GIOChannel   *source,
           GIOCondition  condition,
           CkJob        *job)
{
        gboolean finished = FALSE;

        if (condition & G_IO_IN) {
                GIOStatus status;
                GError   *error = NULL;
                char     *line;

                line = NULL;
                status = g_io_channel_read_line (source, &line, NULL, NULL, &error);

                switch (status) {
                case G_IO_STATUS_NORMAL:
                        g_string_append (job->priv->std_out, line);
                        break;
                case G_IO_STATUS_EOF:
                        finished = TRUE;
                        break;
                case G_IO_STATUS_ERROR:
                        finished = TRUE;
                        g_debug ("Error reading from child: %s\n", error->message);
                        break;
                case G_IO_STATUS_AGAIN:
                default:
                        break;
                }
                g_free (line);
        } else if (condition & G_IO_HUP) {
                finished = TRUE;
        }

        if (finished) {
                job->priv->out_watch_id = 0;
                maybe_complete_job (job);
                return FALSE;
        }

        return TRUE;
}

gboolean
ck_job_execute (CkJob   *job,
                GError **error)
{
        GError     *local_error;
        gboolean    res;
        GIOChannel *channel;
        int         standard_output;
        int         standard_error;
        int         argc;
        char      **argv;

        g_debug ("Executing %s", job->priv->command);
        local_error = NULL;
        if (! g_shell_parse_argv (job->priv->command, &argc, &argv, &local_error)) {
                g_debug ("Could not parse command: %s", local_error->message);
                g_propagate_error (error, local_error);
                return FALSE;
        }

        local_error = NULL;
        res = g_spawn_async_with_pipes (NULL,
                                        argv,
                                        NULL,
                                        G_SPAWN_DO_NOT_REAP_CHILD,
                                        NULL,
                                        NULL,
                                        &job->priv->child_pid,
                                        NULL,
                                        &standard_output,
                                        &standard_error,
                                        &local_error);

        g_strfreev (argv);
        if (! res) {
                g_debug ("Could not start command '%s': %s",
                          job->priv->command,
                          local_error->message);
                g_propagate_error (error, local_error);
                return FALSE;
        }

        /* output channel */
        channel = g_io_channel_unix_new (standard_output);
        g_io_channel_set_close_on_unref (channel, TRUE);
        g_io_channel_set_flags (channel,
                                g_io_channel_get_flags (channel) | G_IO_FLAG_NONBLOCK,
                                NULL);
        job->priv->out_watch_id = g_io_add_watch (channel,
                                                  G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
                                                  (GIOFunc)out_watch,
                                                  job);
        g_io_channel_unref (channel);

        /* error channel */
        channel = g_io_channel_unix_new (standard_error);
        g_io_channel_set_close_on_unref (channel, TRUE);
        g_io_channel_set_flags (channel,
                                g_io_channel_get_flags (channel) | G_IO_FLAG_NONBLOCK,
                                NULL);
        job->priv->err_watch_id = g_io_add_watch (channel,
                                                  G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
                                                  (GIOFunc)error_watch,
                                                  job);
        g_io_channel_unref (channel);

        return res;
}

gboolean
ck_job_get_stdout (CkJob *job,
                   char **std_outp)
{
        if (std_outp != NULL) {
                *std_outp = g_strdup (job->priv->std_out->str);
        }
        return TRUE;
}

gboolean
ck_job_set_command (CkJob      *job,
                    const char *command)
{
        g_free (job->priv->command);
        job->priv->command = g_strdup (command);
        return TRUE;
}

gboolean
ck_job_get_command (CkJob      *job,
                    char      **command)
{
        if (command != NULL) {
                *command = g_strdup (job->priv->command);
        }

        return TRUE;
}

static void
ck_job_class_init (CkJobClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = ck_job_finalize;

        signals [COMPLETED] =
                g_signal_new ("completed",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (CkJobClass, completed),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__INT,
                              G_TYPE_NONE,
                              1, G_TYPE_INT);

        g_type_class_add_private (klass, sizeof (CkJobPrivate));
}

static void
ck_job_init (CkJob *job)
{
        job->priv = CK_JOB_GET_PRIVATE (job);

        job->priv->std_err = g_string_new (NULL);
        job->priv->std_out = g_string_new (NULL);
}

gboolean
ck_job_cancel (CkJob *job)
{
        if (job->priv->child_pid > 0) {
                kill (job->priv->child_pid, SIGTERM);
                wait_on_job (job);
                return TRUE;
        }

        return FALSE;
}

static void
ck_job_finalize (GObject *object)
{
        CkJob *job;

        g_debug ("Finalizing job");

        g_return_if_fail (object != NULL);
        g_return_if_fail (CK_IS_JOB (object));

        job = CK_JOB (object);

        g_return_if_fail (job->priv != NULL);

        ck_job_cancel (job);

        if (job->priv->out_watch_id > 0) {
                g_source_remove (job->priv->out_watch_id);
        }
        if (job->priv->err_watch_id > 0) {
                g_source_remove (job->priv->err_watch_id);
        }
        g_free (job->priv->command);
        g_string_free (job->priv->std_out, TRUE);
        g_string_free (job->priv->std_err, TRUE);

        G_OBJECT_CLASS (ck_job_parent_class)->finalize (object);
}

CkJob *
ck_job_new (void)
{
        GObject *object;

        object = g_object_new (CK_TYPE_JOB, NULL);

        return CK_JOB (object);
}