summaryrefslogtreecommitdiff
path: root/node-startup-controller/job-manager.c
blob: 85d4a0cf580f175f3b4b483b4d3fabb73e4c6201 (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* vi:set et ai sw=2 sts=2 ts=2: */
/* -
 * Copyright (c) 2012 GENIVI.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#include <node-startup-controller/job-manager.h>
#include <node-startup-controller/systemd-manager-dbus.h>



/**
 * SECTION: job-manager 
 * @title: JobManager
 * @short_description: Manages systemd jobs.
 * @stability: Internal
 * 
 * The #JobManager simplifies starting and stopping systemd units by handling all the
 * D-Bus communication with the systemd manager internally. Units can be started and
 * stopped using job_manager_start() and job_manager_stop() and will call the @callback
 * passed to the function when the unit is started or stopped.
 * 
 * The #JobManager contains an internal table which associates systemd job names with the
 * necessary information about that job, e.g. the unit name.
 * 
 * When the #JobManager is told to start or stop a unit, it creates a job in systemd to
 * start or stop that unit, stores the name of that job and waits until that job has
 * finished before calling the @callback.
 * 
 * The #JobManager finds out that a job has finished by listening to "JobRemoved" signals
 * from systemd and looking for that job by its job name.
 */



typedef struct _JobManagerJob JobManagerJob;



/* property identifiers */
enum
{
  PROP_0,
  PROP_CONNECTION,
  PROP_SYSTEMD_MANAGER,
};



static void           job_manager_constructed      (GObject           *object);
static void           job_manager_finalize         (GObject           *object);
static void           job_manager_get_property     (GObject           *object,
                                                    guint              prop_id,
                                                    GValue            *value,
                                                    GParamSpec        *pspec);
static void           job_manager_set_property     (GObject           *object,
                                                    guint              prop_id,
                                                    const GValue      *value,
                                                    GParamSpec        *pspec);
static void           job_manager_start_unit_reply (GObject           *object,
                                                    GAsyncResult      *result,
                                                    gpointer           user_data);
static void           job_manager_stop_unit_reply  (GObject           *object,
                                                    GAsyncResult      *result,
                                                    gpointer           user_data);
static void           job_manager_job_removed      (SystemdManager    *systemd_manager,
                                                    guint              id,
                                                    const gchar       *job_name,
                                                    const gchar       *unit,
                                                    const gchar       *result,
                                                    JobManager        *job_manager);
static JobManagerJob *job_manager_job_new          (JobManager        *manager,
                                                    const gchar       *unit,
                                                    GCancellable      *cancellable,
                                                    JobManagerCallback callback,
                                                    gpointer           user_data);
static void           job_manager_job_unref        (JobManagerJob     *job);
static void           job_manager_remember_job     (JobManager        *manager,
                                                    const gchar       *job_name,
                                                    JobManagerJob     *job);
static void           job_manager_forget_job       (JobManager        *manager,
                                                    const gchar       *job_name);



struct _JobManagerClass
{
  GObjectClass __parent__;
};

struct _JobManager
{
  GObject __parent__;

  GDBusConnection *connection;
  SystemdManager  *systemd_manager;

  GHashTable      *jobs;
};

struct _JobManagerJob
{
  JobManager        *manager;
  gchar             *unit;
  GCancellable      *cancellable;
  JobManagerCallback callback;
  gpointer           user_data;
};



G_DEFINE_TYPE (JobManager, job_manager, G_TYPE_OBJECT);



static void
job_manager_class_init (JobManagerClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = job_manager_finalize;
  gobject_class->constructed = job_manager_constructed;
  gobject_class->get_property = job_manager_get_property;
  gobject_class->set_property = job_manager_set_property;

  g_object_class_install_property (gobject_class,
                                   PROP_CONNECTION,
                                   g_param_spec_object ("connection",
                                                        "connection",
                                                        "connection",
                                                        G_TYPE_DBUS_CONNECTION,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class,
                                   PROP_SYSTEMD_MANAGER,
                                   g_param_spec_object ("systemd-manager",
                                                        "systemd-manager",
                                                        "systemd-manager",
                                                        TYPE_SYSTEMD_MANAGER,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));
}



static void
job_manager_init (JobManager *manager)
{
  /* create a mapping of systemd job names to job objects; we will use this
   * to remember jobs that we started */
  manager->jobs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
                                        (GDestroyNotify) job_manager_job_unref);

}



static void
job_manager_finalize (GObject *object)
{
  JobManager *manager = JOB_MANAGER (object);

  /* release all the jobs we have remembered */
  g_hash_table_unref (manager->jobs);

  /* release the D-Bus connection */
  g_object_unref (manager->connection);

  /* release the systemd manager */
  g_signal_handlers_disconnect_matched (manager->systemd_manager,
                                        G_SIGNAL_MATCH_DATA,
                                        0, 0, NULL, NULL, manager);
  g_object_unref (manager->systemd_manager);

  /* chain up to finalize parent class */
  (*G_OBJECT_CLASS (job_manager_parent_class)->finalize) (object);
}



static void
job_manager_constructed (GObject *object)
{
  JobManager *manager = JOB_MANAGER (object);

  /* connect to systemd's "JobRemoved" signal so that we are notified
   * whenever a job is finished */
  g_signal_connect (manager->systemd_manager, "job-removed",
                    G_CALLBACK (job_manager_job_removed), manager);
}



static void
job_manager_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  JobManager *manager = JOB_MANAGER (object);

  switch (prop_id)
    {
    case PROP_CONNECTION:
      g_value_set_object (value, manager->connection);
      break;
    case PROP_SYSTEMD_MANAGER:
      g_value_set_object (value, manager->systemd_manager);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
job_manager_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  JobManager *manager = JOB_MANAGER (object);

  switch (prop_id)
    {
    case PROP_CONNECTION:
      manager->connection = g_value_dup_object (value);
      break;
    case PROP_SYSTEMD_MANAGER:
      manager->systemd_manager = g_value_dup_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
job_manager_start_unit_reply (GObject       *object,
                              GAsyncResult *result,
                              gpointer      user_data)
{
  JobManagerJob *job = user_data;
  GError        *error = NULL;
  gchar         *job_name = NULL;

  g_return_if_fail (IS_SYSTEMD_MANAGER (object));
  g_return_if_fail (G_IS_ASYNC_RESULT (result));
  g_return_if_fail (user_data != NULL);

  /* finish the start unit call */
  if (!systemd_manager_call_start_unit_finish (job->manager->systemd_manager,
                                               &job_name, result, &error))
    {
      /* there was an error. notify the caller */
      job->callback (job->manager, job->unit, "failed", error, job->user_data);
      g_error_free (error);
      g_free (job_name);

      /* finish the job immediately */
      job_manager_job_unref (job);
    }
  else
    {
      /* remember the job so that we can finish it in the "job-removed" signal handler.
       * the service takes ownership of the job so we don't need to unref it here */
      job_manager_remember_job (job->manager, job_name, job);
    }
}



static void
job_manager_stop_unit_reply (GObject       *object,
                             GAsyncResult *result,
                             gpointer      user_data)
{
  JobManagerJob *job = user_data;
  GError        *error = NULL;
  gchar         *job_name = NULL;

  g_return_if_fail (IS_SYSTEMD_MANAGER (object));
  g_return_if_fail (G_IS_ASYNC_RESULT (result));
  g_return_if_fail (user_data != NULL);

  /* finish the stop unit call */
  if (!systemd_manager_call_stop_unit_finish (job->manager->systemd_manager,
                                              &job_name, result, &error))
    {
      /* there was an error. notify the caller */
      job->callback (job->manager, job->unit, "failed", error, job->user_data);
      g_error_free (error);
      g_free (job_name);

      /* finish the job immediately */
      job_manager_job_unref (job);
    }
  else
    {
      /* remember the job so that we can finish it in the "job-removed" signal handler.
       * the service takes ownership of the job so we don't need to unref it here */
      job_manager_remember_job (job->manager, job_name, job);
    }
}



static void
job_manager_job_removed (SystemdManager *systemd_manager,
                         guint           id,
                         const gchar    *job_name,
                         const gchar    *unit,
                         const gchar    *result,
                         JobManager     *job_manager)
{
  JobManagerJob *job;

  g_return_if_fail (IS_SYSTEMD_MANAGER (systemd_manager));
  g_return_if_fail (job_name != NULL && *job_name != '\0');
  g_return_if_fail (result != NULL && *result != '\0');
  g_return_if_fail (unit != NULL && *unit != '\0');
  g_return_if_fail (IS_JOB_MANAGER (job_manager));

  /* look up the remembered job for this job name */
  job = g_hash_table_lookup (job_manager->jobs, job_name);

  /* if no job is found, ignore this job-removed signal */
  if (job == NULL)
    return;

  /* finish the job by notifying the caller */
  job->callback (job_manager, job->unit, result, NULL, job->user_data);

  /* forget about this job */
  job_manager_forget_job (job_manager, job_name);
}



static JobManagerJob *
job_manager_job_new (JobManager        *manager,
                     const gchar       *unit,
                     GCancellable      *cancellable,
                     JobManagerCallback callback,
                     gpointer           user_data)
{
  JobManagerJob *job;

  g_return_val_if_fail (IS_JOB_MANAGER (manager), NULL);
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);

  /* allocate a new job struct */
  job = g_slice_new0 (JobManagerJob);
  job->manager = g_object_ref (manager);
  job->unit = g_strdup(unit);
  if (cancellable != NULL)
    job->cancellable = g_object_ref (cancellable);
  job->callback = callback;
  job->user_data = user_data;

  return job;
}


static void
job_manager_job_unref (JobManagerJob *job)
{
  if (job == NULL)
    return;

  /* release all memory and references held by job */
  if (job->cancellable != NULL)
    g_object_unref (job->cancellable);
  g_free (job->unit);
  g_object_unref (job->manager);
  g_slice_free (JobManagerJob, job);
}



static void
job_manager_remember_job (JobManager    *manager,
                          const char    *job_name,
                          JobManagerJob *job)
{
  JobManagerJob *existing_job;

  g_return_if_fail (IS_JOB_MANAGER (manager));
  g_return_if_fail (job_name != NULL && *job_name != '\0');
  g_return_if_fail (job != NULL);

  /* if the job is already being remembered, there is a programming error that should be
   * notified */
  existing_job = g_hash_table_lookup (manager->jobs, job_name);
  if (existing_job != NULL)
    {
      g_critical ("Trying to remember the same job twice.");
      return;
    }
  /* associate the job name with the job */
  g_hash_table_insert (manager->jobs, g_strdup (job_name), job);
}



static void
job_manager_forget_job (JobManager  *manager,
                        const gchar *job_name)
{
  g_return_if_fail (IS_JOB_MANAGER (manager));
  g_return_if_fail (job_name != NULL && *job_name != '\0');

  g_hash_table_remove (manager->jobs, job_name);
}


/**
 * job_manager_new:
 * @connection: A connection to the system bus. 
 * @systemd_manager: An interface to the systemd manager created with 
 * systemd_manager_proxy_new_for_bus_sync()
 * 
 * Creates a new JobManager object.
 * 
 * Returns: A new instance of the #JobManager.
 */
JobManager *
job_manager_new (GDBusConnection *connection,
                 SystemdManager  *systemd_manager)
{
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
  g_return_val_if_fail (IS_SYSTEMD_MANAGER (systemd_manager), NULL);

  return g_object_new (TYPE_JOB_MANAGER,
                       "connection", connection,
                       "systemd-manager", systemd_manager,
                       NULL);
}



/**
 * job_manager_start:
 * @unit: The name of the systemd unit to start.
 * @callback: a #JobManagerCallback that is called after the job is started.
 * @user_data: userdata that is available in the #JobManagerCallback.
 * 
 * Asynchronously starts @unit, and calls @callback with @user_data when it is finished.
 */
void
job_manager_start (JobManager        *manager,
                   const gchar       *unit,
                   GCancellable      *cancellable,
                   JobManagerCallback callback,
                   gpointer           user_data)
{
  JobManagerJob *job;

  g_return_if_fail (IS_JOB_MANAGER (manager));
  g_return_if_fail (unit != NULL);
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_return_if_fail (callback != NULL);

  /* create a new job object */
  job = job_manager_job_new (manager, unit, cancellable, callback, user_data);

  /* ask systemd to start the unit asynchronously */
  systemd_manager_call_start_unit (manager->systemd_manager, unit, "fail", cancellable,
                                   job_manager_start_unit_reply, job);
}



/**
 * job_manager_stop:
 * @unit: The name of the systemd unit to stop.
 * @callback: a #JobManagerCallback that is called after the job is stopped.
 * @user_data: userdata that is available in the #JobManagerCallback.
 * 
 * Asynchronously stops @unit, and calls @callback with @user_data when it is finished.
 */
void
job_manager_stop (JobManager        *manager,
                  const gchar       *unit,
                  GCancellable      *cancellable,
                  JobManagerCallback callback,
                  gpointer           user_data)
{
  JobManagerJob *job;

  g_return_if_fail (IS_JOB_MANAGER (manager));
  g_return_if_fail (unit != NULL);
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_return_if_fail (callback != NULL);

  /* create a new job object */
  job = job_manager_job_new (manager, unit, cancellable, callback, user_data);

  /* ask systemd to stop the unit asynchronously */
  systemd_manager_call_stop_unit (manager->systemd_manager, unit, "fail", cancellable,
                                  job_manager_stop_unit_reply, job);
}