summaryrefslogtreecommitdiff
path: root/node-startup-controller/main.c
blob: 74bb409b6430dc696ecf6ef2fe640c19935e40ba (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
/* 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

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

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

#include <dlt/dlt.h>

#include <node-startup-controller/la-handler-service.h>
#include <node-startup-controller/node-startup-controller-application.h>
#include <node-startup-controller/node-startup-controller-dbus.h>
#include <node-startup-controller/node-startup-controller-service.h>
#include <node-startup-controller/systemd-manager-dbus.h>
#include <node-startup-controller/target-startup-monitor.h>



DLT_DECLARE_CONTEXT (controller_context);
DLT_DECLARE_CONTEXT (la_handler_context);



static void
unregister_dlt (void)
{
  DLT_UNREGISTER_CONTEXT (controller_context);
  DLT_UNREGISTER_CONTEXT (la_handler_context);
  DLT_UNREGISTER_APP ();
}



int
main (int    argc,
      char **argv)
{
  NodeStartupControllerApplication *application;
  NodeStartupControllerService     *node_startup_controller;
  TargetStartupMonitor             *target_startup_monitor;
  LAHandlerService                 *la_handler_service;
  GDBusConnection                  *connection;
  SystemdManager                   *systemd_manager;
  JobManager                       *job_manager;
  GMainLoop                        *main_loop;
  GError                           *error = NULL;

  /* register the application and context in DLT */
  DLT_REGISTER_APP ("NSC", "GENIVI Node Startup Controller");
  DLT_REGISTER_CONTEXT (controller_context, "CTRL",
                        "Context of the Node Startup Controller itself");
  DLT_REGISTER_CONTEXT (la_handler_context, "LAH",
                        "Context of the Legacy Application Handler that hooks legacy "
                        "applications up with the shutdown concept of the Node State "
                        "Manager");

  /* have DLT unregistered at exit */
  atexit (unregister_dlt);

  /* initialize the GType type system */
  g_type_init ();

  /* attempt to connect to D-Bus */
  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
  if (connection == NULL)
    {
      DLT_LOG (controller_context, DLT_LOG_FATAL,
               DLT_STRING ("Failed to connect to the system bus:"),
               DLT_STRING (error->message));

      /* clean up */
      g_error_free (error);

      return EXIT_FAILURE;
    }

  /* attempt to connect to the systemd manager */
  systemd_manager =
    systemd_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                            G_DBUS_PROXY_FLAGS_NONE,
                                            "org.freedesktop.systemd1",
                                            "/org/freedesktop/systemd1",
                                            NULL, &error);
  if (systemd_manager == NULL)
    {
      DLT_LOG (controller_context, DLT_LOG_FATAL,
               DLT_STRING ("Failed to connect to the systemd manager:"),
               DLT_STRING (error->message));

      /* clean up */
      g_error_free (error);
      g_object_unref (connection);

      return EXIT_FAILURE;
    }

  /* subscribe to the systemd manager */
  if (!systemd_manager_call_subscribe_sync (systemd_manager, NULL, &error))
    {
      DLT_LOG (controller_context, DLT_LOG_FATAL,
               DLT_STRING ("Failed to subscribe to the systemd manager:"),
               DLT_STRING (error->message));

      /* clean up */
      g_error_free (error);
      g_object_unref (connection);

      return EXIT_FAILURE;
    }

  /* instantiate the node startup controller service implementation */
  node_startup_controller = node_startup_controller_service_new (connection);

  /* attempt to start the node startup controller service */
  if (!node_startup_controller_service_start_up (node_startup_controller, &error))
    {
      DLT_LOG (controller_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to start the node startup controller service:"),
               DLT_STRING (error->message));

      /* clean up */
      g_error_free (error);
      g_object_unref (node_startup_controller);
      g_object_unref (systemd_manager);
      g_object_unref (connection);

      return EXIT_FAILURE;
    }

  /* instantiate the job manager */
  job_manager = job_manager_new (connection, systemd_manager);

  /* instantiate the legacy app handler */
  la_handler_service = la_handler_service_new (connection, job_manager);

  /* start the legacy app handler */
  if (!la_handler_service_start (la_handler_service, &error))
    {
      DLT_LOG (controller_context, DLT_LOG_FATAL,
               DLT_STRING ("Failed to start the legacy app handler service:"),
               DLT_STRING (error->message));

      /* clean up */
      g_clear_error (&error);
      g_object_unref (la_handler_service);
      g_object_unref (job_manager);
      g_object_unref (node_startup_controller);
      g_object_unref (systemd_manager);
      g_object_unref (connection);

      return EXIT_FAILURE;
    }

  /* create the main loop */
  main_loop = g_main_loop_new (NULL, FALSE);

  /* create the target startup monitor */
  target_startup_monitor = target_startup_monitor_new (systemd_manager);

  /* create and run the main application */
  application = node_startup_controller_application_new (main_loop, connection,
                                                         job_manager, la_handler_service,
                                                         node_startup_controller);

  /* run the main loop */
  g_main_loop_run (main_loop);
  g_main_loop_unref (main_loop);

  /* release allocated objects */
  g_object_unref (application);
  g_object_unref (target_startup_monitor);
  g_object_unref (systemd_manager);
  g_object_unref (job_manager);
  g_object_unref (node_startup_controller);
  g_object_unref (connection);

  return EXIT_SUCCESS;
}