1 /* vi:set et ai sw=2 sts=2 ts=2: */
3 * Copyright (c) 2012 GENIVI.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
23 #include <node-startup-controller/la-handler-service.h>
24 #include <node-startup-controller/node-startup-controller-application.h>
25 #include <node-startup-controller/node-startup-controller-dbus.h>
26 #include <node-startup-controller/node-startup-controller-service.h>
27 #include <node-startup-controller/systemd-manager-dbus.h>
28 #include <node-startup-controller/target-startup-monitor.h>
32 DLT_DECLARE_CONTEXT (controller_context);
33 DLT_DECLARE_CONTEXT (la_handler_context);
40 DLT_UNREGISTER_CONTEXT (controller_context);
41 DLT_UNREGISTER_CONTEXT (la_handler_context);
42 DLT_UNREGISTER_APP ();
51 NodeStartupControllerApplication *application;
52 NodeStartupControllerService *node_startup_controller;
53 TargetStartupMonitor *target_startup_monitor;
54 LAHandlerService *la_handler_service;
55 GDBusConnection *connection;
56 SystemdManager *systemd_manager;
57 JobManager *job_manager;
61 /* register the application and context in DLT */
62 DLT_REGISTER_APP ("NSC", "GENIVI Node Startup Controller");
63 DLT_REGISTER_CONTEXT (controller_context, "CTRL",
64 "Context of the Node Startup Controller itself");
65 DLT_REGISTER_CONTEXT (la_handler_context, "LAH",
66 "Context of the Legacy Application Handler that hooks legacy "
67 "applications up with the shutdown concept of the Node State "
70 /* have DLT unregistered at exit */
71 atexit (unregister_dlt);
73 /* initialize the GType type system */
76 /* attempt to connect to D-Bus */
77 connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
78 if (connection == NULL)
80 DLT_LOG (controller_context, DLT_LOG_FATAL,
81 DLT_STRING ("Failed to connect to the system bus:"),
82 DLT_STRING (error->message));
90 /* attempt to connect to the systemd manager */
92 systemd_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
93 G_DBUS_PROXY_FLAGS_NONE,
94 "org.freedesktop.systemd1",
95 "/org/freedesktop/systemd1",
97 if (systemd_manager == NULL)
99 DLT_LOG (controller_context, DLT_LOG_FATAL,
100 DLT_STRING ("Failed to connect to the systemd manager:"),
101 DLT_STRING (error->message));
104 g_error_free (error);
105 g_object_unref (connection);
110 /* subscribe to the systemd manager */
111 if (!systemd_manager_call_subscribe_sync (systemd_manager, NULL, &error))
113 DLT_LOG (controller_context, DLT_LOG_FATAL,
114 DLT_STRING ("Failed to subscribe to the systemd manager:"),
115 DLT_STRING (error->message));
118 g_error_free (error);
119 g_object_unref (connection);
124 /* instantiate the node startup controller service implementation */
125 node_startup_controller = node_startup_controller_service_new (connection);
127 /* attempt to start the node startup controller service */
128 if (!node_startup_controller_service_start_up (node_startup_controller, &error))
130 DLT_LOG (controller_context, DLT_LOG_ERROR,
131 DLT_STRING ("Failed to start the node startup controller service:"),
132 DLT_STRING (error->message));
135 g_error_free (error);
136 g_object_unref (node_startup_controller);
137 g_object_unref (systemd_manager);
138 g_object_unref (connection);
143 /* instantiate the job manager */
144 job_manager = job_manager_new (connection, systemd_manager);
146 /* instantiate the legacy app handler */
147 la_handler_service = la_handler_service_new (connection, job_manager);
149 /* start the legacy app handler */
150 if (!la_handler_service_start (la_handler_service, &error))
152 DLT_LOG (controller_context, DLT_LOG_FATAL,
153 DLT_STRING ("Failed to start the legacy app handler service:"),
154 DLT_STRING (error->message));
157 g_clear_error (&error);
158 g_object_unref (la_handler_service);
159 g_object_unref (job_manager);
160 g_object_unref (node_startup_controller);
161 g_object_unref (systemd_manager);
162 g_object_unref (connection);
167 /* create the main loop */
168 main_loop = g_main_loop_new (NULL, FALSE);
170 /* create the target startup monitor */
171 target_startup_monitor = target_startup_monitor_new (systemd_manager);
173 /* create and run the main application */
174 application = node_startup_controller_application_new (main_loop, connection,
175 job_manager, la_handler_service,
176 node_startup_controller);
178 /* run the main loop */
179 g_main_loop_run (main_loop);
180 g_main_loop_unref (main_loop);
182 /* release allocated objects */
183 g_object_unref (application);
184 g_object_unref (target_startup_monitor);
185 g_object_unref (systemd_manager);
186 g_object_unref (job_manager);
187 g_object_unref (node_startup_controller);
188 g_object_unref (connection);