summaryrefslogtreecommitdiff
path: root/node-startup-controller/target-startup-monitor.c
blob: 4a425cfc22ddbf032528017d755385a2dd023c1d (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
/* 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 <dlt/dlt.h>

#include <common/nsm-enum-types.h>
#include <common/nsm-lifecycle-control-dbus.h>

#include <node-startup-controller/target-startup-monitor.h>
#include <node-startup-controller/systemd-unit-dbus.h>



/**
 * SECTION: target-startup-monitor
 * @title: TargetStartupMonitor
 * @short_description: Listens to JobRemoved signals and sets the node state.
 * @stability: Internal
 * 
 * The #TargetStartupMonitor class is responsible for setting the state of the
 * Node State Manager using states from #NSMNodeState. It sets the node state
 * under the following * circumstances:
 * 
 * The Node Startup Controller is starting up. In the GENIVI lifecycle subsystem,
 * this means that all mandatory applications have been started - set the state to
 * %NSM_NODE_STATE_BASE_RUNNING.
 * 
 * The systemd unit %focussed.target has started - set the state to
 * %NSM_NODE_STATE_LUC_RUNNING.
 * 
 * The systemd unit %unfocussed.target has started - set the state to
 * %NSM_NODE_STATE_FULLY_RUNNING.
 * 
 * The systemd unit %lazy.target has started - set the state to
 * %NSM_NODE_STATE_FULLY_OPERATIONAL.
 */



DLT_IMPORT_CONTEXT (controller_context);



/* property identifier */
enum
{
  PROP_0,
  PROP_SYSTEMD_MANAGER,
};



typedef struct _GetUnitData GetUnitData;



static void     target_startup_monitor_finalize                (GObject              *object);
static void     target_startup_monitor_constructed             (GObject              *object);
static void     target_startup_monitor_get_property            (GObject              *object,
                                                                guint                 prop_id,
                                                                GValue               *value,
                                                                GParamSpec           *pspec);
static void     target_startup_monitor_set_property            (GObject              *object,
                                                                guint                 prop_id,
                                                                const GValue         *value,
                                                                GParamSpec           *pspec);
static void     target_startup_monitor_job_removed             (SystemdManager       *manager,
                                                                guint                 id,
                                                                const gchar          *job_name,
                                                                const gchar          *unit,
                                                                const gchar          *result,
                                                                TargetStartupMonitor *monitor);
static void     target_startup_monitor_get_unit_finish         (GObject              *object,
                                                                GAsyncResult         *res,
                                                                gpointer              user_data);
static void     target_startup_monitor_unit_proxy_new_finish   (GObject              *object,
                                                                GAsyncResult         *res,
                                                                gpointer              user_data);
static void     target_startup_monitor_set_node_state          (TargetStartupMonitor *monitor,
                                                                NSMNodeState          state);
static void     target_startup_monitor_set_node_state_finish   (GObject              *object,
                                                                GAsyncResult         *res,
                                                                gpointer              user_data);



struct _TargetStartupMonitorClass
{
  GObjectClass __parent__;
};

struct _TargetStartupMonitor
{
  GObject              __parent__;

  SystemdManager      *systemd_manager;

  NSMLifecycleControl *nsm_lifecycle_control;

  /* list of systemd units for the targets we are interested in */
  GList               *units;

  /* map of systemd target names to corresponding node states */
  GHashTable          *targets_to_states;
};

struct _GetUnitData
{
  TargetStartupMonitor *monitor;
  gchar                *unit_name;
  gchar                *object_path;
};



G_DEFINE_TYPE (TargetStartupMonitor, target_startup_monitor, G_TYPE_OBJECT);



static void
target_startup_monitor_class_init (TargetStartupMonitorClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = target_startup_monitor_finalize;
  gobject_class->constructed = target_startup_monitor_constructed;
  gobject_class->get_property = target_startup_monitor_get_property;
  gobject_class->set_property = target_startup_monitor_set_property;

  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
target_startup_monitor_init (TargetStartupMonitor *monitor)
{
  GError *error = NULL;

  /* create proxy to talk to the Node State Manager's lifecycle control */
  monitor->nsm_lifecycle_control =
    nsm_lifecycle_control_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                  G_DBUS_PROXY_FLAGS_NONE,
                                                  "com.contiautomotive.NodeStateManager",
                                                  "/com/contiautomotive/NodeStateManager/LifecycleControl",
                                                  NULL, &error);
  if (error != NULL)
    {
      DLT_LOG (controller_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to connect to the NSM lifecycle control:"),
               DLT_STRING (error->message));
      g_error_free (error);
    }

  /* set the initial state to base running, which means that
   * the mandatory.target has been started (this is done before the
   * node startup controller itself is brought up) */
  target_startup_monitor_set_node_state (monitor, NSM_NODE_STATE_BASE_RUNNING);

  /* create the table of targets and their node states */
  monitor->targets_to_states = g_hash_table_new (g_str_hash, g_str_equal);
  g_hash_table_insert (monitor->targets_to_states, "focussed.target",
                       GUINT_TO_POINTER (NSM_NODE_STATE_LUC_RUNNING));
  g_hash_table_insert (monitor->targets_to_states, "unfocussed.target",
                       GUINT_TO_POINTER (NSM_NODE_STATE_FULLY_RUNNING));
  g_hash_table_insert (monitor->targets_to_states, "lazy.target",
                       GUINT_TO_POINTER (NSM_NODE_STATE_FULLY_OPERATIONAL));
}



static void
target_startup_monitor_constructed (GObject *object)
{
  TargetStartupMonitor *monitor = TARGET_STARTUP_MONITOR (object);

  g_signal_connect (monitor->systemd_manager, "job-removed",
                    G_CALLBACK (target_startup_monitor_job_removed), monitor);
}



static void
target_startup_monitor_finalize (GObject *object)
{
  TargetStartupMonitor *monitor = TARGET_STARTUP_MONITOR (object);
  GList                *lp;

  /* disconnect from all the unit proxies and release them */
  for (lp = monitor->units; lp != NULL; lp = lp->next)
    {
      g_signal_handlers_disconnect_matched (lp->data, G_SIGNAL_MATCH_DATA,
                                            0, 0, NULL, NULL, monitor);
      g_object_unref (lp->data);
    }
  g_list_free (lp->data);

  /* release the mapping of systemd targets to node states */
  g_hash_table_destroy (monitor->targets_to_states);

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

  if (monitor->nsm_lifecycle_control != NULL)
    g_object_unref (monitor->nsm_lifecycle_control);

  (*G_OBJECT_CLASS (target_startup_monitor_parent_class)->finalize) (object);
}



static void
target_startup_monitor_get_property (GObject    *object,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  TargetStartupMonitor *monitor = TARGET_STARTUP_MONITOR (object);

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



static void
target_startup_monitor_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  TargetStartupMonitor *monitor = TARGET_STARTUP_MONITOR (object);

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



static void
target_startup_monitor_job_removed (SystemdManager       *manager,
                                    guint                 id,
                                    const gchar          *job_name,
                                    const gchar          *unit,
                                    const gchar          *result,
                                    TargetStartupMonitor *monitor)
{
  GetUnitData *data;

  g_return_if_fail (IS_SYSTEMD_MANAGER (manager));
  g_return_if_fail (job_name != NULL && *job_name != '\0');
  g_return_if_fail (unit != NULL && *unit != '\0');
  g_return_if_fail (result != NULL && *result != '\0');
  g_return_if_fail (IS_TARGET_STARTUP_MONITOR (monitor));

  /* create a temporary struct to bundle information about the unit */
  data = g_slice_new0 (GetUnitData);
  data->monitor = g_object_ref (monitor);
  data->unit_name = g_strdup (unit);

  /* ask systemd to return the object path for this unit */
  systemd_manager_call_get_unit (monitor->systemd_manager, unit, NULL,
                                 target_startup_monitor_get_unit_finish, data);


}



static void
target_startup_monitor_get_unit_finish (GObject      *object,
                                        GAsyncResult *res,
                                        gpointer      user_data)
{
  GetUnitData *data = user_data;
  GError      *error = NULL;
  gchar       *object_path;

  g_return_if_fail (IS_SYSTEMD_MANAGER (object));
  g_return_if_fail (G_IS_ASYNC_RESULT (res));
  g_return_if_fail (data != NULL);

  /* finish obtaining the object path for the unit from systemd */
  if (!systemd_manager_call_get_unit_finish (SYSTEMD_MANAGER (object), &object_path,
                                             res, &error))
    {
      /* there was an error, log it */
      DLT_LOG (controller_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to get a unit from systemd:"),
               DLT_STRING ("unit"), DLT_STRING (data->unit_name),
               DLT_STRING ("error message"), DLT_STRING (error->message));
      g_error_free (error);

      /* release the get unit data */
      g_object_unref (data->monitor);
      g_free (data->unit_name);
      g_slice_free (GetUnitData, data);
    }
  else
    {
      DLT_LOG (controller_context, DLT_LOG_INFO,
               DLT_STRING ("Creating D-Bus proxy:"),
               DLT_STRING ("object path"), DLT_STRING (object_path));

      /* remember the object path */
      data->object_path = object_path;

      /* create a proxy for this unit D-Bus object */
      systemd_unit_proxy_new (g_dbus_proxy_get_connection (G_DBUS_PROXY (data->monitor->systemd_manager)),
                              G_DBUS_PROXY_FLAGS_NONE,
                              "org.freedesktop.systemd1",
                              object_path,
                              NULL,
                              target_startup_monitor_unit_proxy_new_finish,
                              data);
    }
}



static void
target_startup_monitor_unit_proxy_new_finish (GObject      *object,
                                              GAsyncResult *res,
                                              gpointer      user_data)
{
  GetUnitData *data = user_data;
  SystemdUnit *unit;
  const gchar *state;
  gpointer     node_state;
  GError      *error = NULL;

  g_return_if_fail (G_IS_ASYNC_RESULT (res));
  g_return_if_fail (data != NULL);

  /* finish creating the proxy for this systemd unit */
  unit = systemd_unit_proxy_new_finish (res, &error);
  if (error != NULL)
    {
      /* there was an error, log it */
      DLT_LOG (controller_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to create D-Bus proxy:"),
               DLT_STRING ("object path"), DLT_STRING (data->object_path),
               DLT_STRING ("error message"), DLT_STRING (error->message));
      g_error_free (error);
    }
  else
    {
      /* query the unit for its current active state */
      state = systemd_unit_get_active_state (unit);

      /* log the the active state has changed */
      DLT_LOG (controller_context, DLT_LOG_INFO,
               DLT_STRING ("Active state of unit changed:"),
               DLT_STRING ("unit"), DLT_STRING (data->unit_name),
               DLT_STRING ("state"), DLT_STRING (state));

      /* check if the new state is active */
      if (g_strcmp0 (state, "active") == 0)
        {
          /* look up the node state corresponding to this unit, if there is one */
          if (g_hash_table_lookup_extended (data->monitor->targets_to_states,
                                            data->unit_name, NULL, &node_state))
            {
              /* we do have a state for this unit, so apply it now */
              target_startup_monitor_set_node_state (data->monitor,
                                                     GPOINTER_TO_UINT (node_state));
            }
        }

      /* release the unit proxy */
      g_object_unref (unit);
    }

  /* free the get unit data */
  g_object_unref (data->monitor);
  g_free (data->unit_name);
  g_free (data->object_path);
  g_slice_free (GetUnitData, data);
}



static void
target_startup_monitor_set_node_state (TargetStartupMonitor *monitor,
                                       NSMNodeState          state)
{
  g_return_if_fail (IS_TARGET_STARTUP_MONITOR (monitor));

  /* set node state in the Node State Manager */
  nsm_lifecycle_control_call_set_node_state (monitor->nsm_lifecycle_control,
                                             (gint) state, NULL,
                                             target_startup_monitor_set_node_state_finish,
                                             NULL);
}



static void
target_startup_monitor_set_node_state_finish (GObject      *object,
                                              GAsyncResult *res,
                                              gpointer      user_data)
{
  NSMLifecycleControl *nsm_lifecycle_control = NSM_LIFECYCLE_CONTROL (object);
  GError              *error = NULL;
  NSMErrorStatus       error_code = NSM_ERROR_STATUS_OK;

  g_return_if_fail (IS_NSM_LIFECYCLE_CONTROL (nsm_lifecycle_control));
  g_return_if_fail (G_IS_ASYNC_RESULT (res));

  /* finish setting the node state in the NSM */
  if (!nsm_lifecycle_control_call_set_node_state_finish (nsm_lifecycle_control,
                                                         (gint *) &error_code, res,
                                                         &error))
    {
      DLT_LOG (controller_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to set the node state:"),
               DLT_STRING (error->message));
      g_error_free (error);
    }
  else if (error_code != NSM_ERROR_STATUS_OK)
    {
      DLT_LOG (controller_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to set the node state:"),
               DLT_STRING ("error code"), DLT_UINT (error_code));
    }
}



/**
 * target_startup_monitor_new:
 * @systemd_manager: An interface to the systemd manager created with
 * systemd_manager_proxy_new_for_bus_sync()
 * 
 * Creates a new target startup monitor and begins listening to %JobRemoved signals from
 * systemd.
 * 
 * Returns: A new instance of the #TargetStartupMonitor.
 */
TargetStartupMonitor *
target_startup_monitor_new (SystemdManager *systemd_manager)
{
  g_return_val_if_fail (IS_SYSTEMD_MANAGER (systemd_manager), NULL);

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