Adjust NSM Consumer D-Bus interface to match the real NSM
[profile/ivi/node-startup-controller.git] / node-startup-controller / main.c
1 /* vi:set et ai sw=2 sts=2 ts=2: */
2 /* -
3  * Copyright (c) 2012 GENIVI.
4  *
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/.
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #ifdef HAVE_STDLIB_H
15 #include <stdlib.h>
16 #endif
17
18 #include <glib.h>
19 #include <gio/gio.h>
20
21 #include <dlt/dlt.h>
22
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>
29
30
31
32 DLT_DECLARE_CONTEXT (controller_context);
33 DLT_DECLARE_CONTEXT (la_handler_context);
34
35
36
37 static void
38 unregister_dlt (void)
39 {
40   DLT_UNREGISTER_CONTEXT (controller_context);
41   DLT_UNREGISTER_CONTEXT (la_handler_context);
42   DLT_UNREGISTER_APP ();
43 }
44
45
46
47 int
48 main (int    argc,
49       char **argv)
50 {
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;
58   GMainLoop                        *main_loop;
59   GError                           *error = NULL;
60
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 "
68                         "Manager");
69
70   /* have DLT unregistered at exit */
71   atexit (unregister_dlt);
72
73   /* initialize the GType type system */
74   g_type_init ();
75
76   /* attempt to connect to D-Bus */
77   connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
78   if (connection == NULL)
79     {
80       DLT_LOG (controller_context, DLT_LOG_FATAL,
81                DLT_STRING ("Failed to connect to the system bus:"),
82                DLT_STRING (error->message));
83
84       /* clean up */
85       g_error_free (error);
86
87       return EXIT_FAILURE;
88     }
89
90   /* attempt to connect to the systemd manager */
91   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",
96                                             NULL, &error);
97   if (systemd_manager == NULL)
98     {
99       DLT_LOG (controller_context, DLT_LOG_FATAL,
100                DLT_STRING ("Failed to connect to the systemd manager:"),
101                DLT_STRING (error->message));
102
103       /* clean up */
104       g_error_free (error);
105       g_object_unref (connection);
106
107       return EXIT_FAILURE;
108     }
109
110   /* subscribe to the systemd manager */
111   if (!systemd_manager_call_subscribe_sync (systemd_manager, NULL, &error))
112     {
113       DLT_LOG (controller_context, DLT_LOG_FATAL,
114                DLT_STRING ("Failed to subscribe to the systemd manager:"),
115                DLT_STRING (error->message));
116
117       /* clean up */
118       g_error_free (error);
119       g_object_unref (connection);
120
121       return EXIT_FAILURE;
122     }
123
124   /* instantiate the node startup controller service implementation */
125   node_startup_controller = node_startup_controller_service_new (connection);
126
127   /* attempt to start the node startup controller service */
128   if (!node_startup_controller_service_start_up (node_startup_controller, &error))
129     {
130       DLT_LOG (controller_context, DLT_LOG_ERROR,
131                DLT_STRING ("Failed to start the node startup controller service:"),
132                DLT_STRING (error->message));
133
134       /* clean up */
135       g_error_free (error);
136       g_object_unref (node_startup_controller);
137       g_object_unref (systemd_manager);
138       g_object_unref (connection);
139
140       return EXIT_FAILURE;
141     }
142
143   /* instantiate the job manager */
144   job_manager = job_manager_new (connection, systemd_manager);
145
146   /* instantiate the legacy app handler */
147   la_handler_service = la_handler_service_new (connection, job_manager);
148
149   /* start the legacy app handler */
150   if (!la_handler_service_start (la_handler_service, &error))
151     {
152       DLT_LOG (controller_context, DLT_LOG_FATAL,
153                DLT_STRING ("Failed to start the legacy app handler service:"),
154                DLT_STRING (error->message));
155
156       /* clean up */
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);
163
164       return EXIT_FAILURE;
165     }
166
167   /* create the main loop */
168   main_loop = g_main_loop_new (NULL, FALSE);
169
170   /* create the target startup monitor */
171   target_startup_monitor = target_startup_monitor_new (systemd_manager);
172
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);
177
178   /* run the main loop */
179   g_main_loop_run (main_loop);
180   g_main_loop_unref (main_loop);
181
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);
189
190   return EXIT_SUCCESS;
191 }