Drop GApplication, fix D-Bus activation, further NSM integration work
[profile/ivi/node-startup-controller.git] / boot-manager / 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 <boot-manager/boot-manager-application.h>
24 #include <boot-manager/boot-manager-command-line.h>
25 #include <boot-manager/boot-manager-dbus.h>
26 #include <boot-manager/boot-manager-service.h>
27 #include <boot-manager/la-handler-service.h>
28 #include <boot-manager/systemd-manager-dbus.h>
29 #include <boot-manager/target-startup-monitor.h>
30
31
32
33 DLT_DECLARE_CONTEXT (boot_manager_context);
34 DLT_DECLARE_CONTEXT (la_handler_context);
35
36
37
38 static void
39 unregister_dlt (void)
40 {
41   DLT_UNREGISTER_CONTEXT (boot_manager_context);
42   DLT_UNREGISTER_CONTEXT (la_handler_context);
43   DLT_UNREGISTER_APP ();
44 }
45
46
47
48 int
49 main (int    argc,
50       char **argv)
51 {
52   BootManagerApplication *application;
53   TargetStartupMonitor   *target_startup_monitor;
54   BootManagerService     *boot_manager_service;
55   LAHandlerService       *la_handler_service;
56   GDBusConnection        *connection;
57   SystemdManager         *systemd_manager;
58   JobManager             *job_manager;
59   GMainLoop              *main_loop;
60   GError                 *error = NULL;
61   gint                    exit_status;
62
63   /* register the application and context in DLT */
64   DLT_REGISTER_APP ("BMGR", "GENIVI Boot Manager");
65   DLT_REGISTER_CONTEXT (boot_manager_context, "MGR",
66                         "Context of the boot manager itself");
67   DLT_REGISTER_CONTEXT (la_handler_context, "LAH",
68                         "Context of the legacy application handler that hooks legacy "
69                         "applications up with the shutdown concept of the Node State "
70                         "Manager");
71
72   /* have DLT unregistered at exit */
73   atexit (unregister_dlt);
74
75   /* initialize the GType type system */
76   g_type_init ();
77
78   /* attempt to connect to D-Bus */
79   connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
80   if (connection == NULL)
81     {
82       g_warning ("Failed to connect to the system bus: %s", error->message);
83
84       /* clean up */
85       g_error_free (error);
86
87       return EXIT_FAILURE;
88     }
89
90   /* Parse and react to the command-line arguments instead of starting the application
91    * if any arguments are given */
92   if (argc > 1)
93     {
94       exit_status = boot_manager_handle_command_line (argc, argv, connection);
95       g_object_unref (connection);
96       return exit_status;
97     }
98
99   /* attempt to connect to the systemd manager */
100   systemd_manager =
101     systemd_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
102                                             G_DBUS_PROXY_FLAGS_NONE,
103                                             "org.freedesktop.systemd1",
104                                             "/org/freedesktop/systemd1",
105                                             NULL, &error);
106   if (systemd_manager == NULL)
107     {
108       g_warning ("Failed to connect to the systemd manager: %s", error->message);
109
110       /* clean up */
111       g_error_free (error);
112       g_object_unref (connection);
113
114       return EXIT_FAILURE;
115     }
116
117   /* subscribe to the systemd manager */
118   if (!systemd_manager_call_subscribe_sync (systemd_manager, NULL, &error))
119     {
120       g_warning ("Failed to subscribe to the systemd manager: %s", error->message);
121
122       /* clean up */
123       g_error_free (error);
124       g_object_unref (connection);
125
126       return EXIT_FAILURE;
127     }
128
129   /* instantiate the boot manager service implementation */
130   boot_manager_service = boot_manager_service_new (connection);
131
132   /* instantiate the job manager */
133   job_manager = job_manager_new (connection, systemd_manager);
134
135   /* start up the target startup monitor */
136   target_startup_monitor = target_startup_monitor_new (systemd_manager);
137
138   /* instantiate the legacy app handler */
139   la_handler_service = la_handler_service_new (connection, job_manager);
140
141   /* create the main loop */
142   main_loop = g_main_loop_new (NULL, FALSE);
143
144   /* create and run the main application */
145   application = boot_manager_application_new (main_loop, connection, job_manager,
146                                               la_handler_service, boot_manager_service);
147
148   /* run the main loop */
149   g_main_loop_run (main_loop);
150   g_main_loop_unref (main_loop);
151
152   /* release allocated objects */
153   g_object_unref (application);
154   g_object_unref (target_startup_monitor);
155   g_object_unref (systemd_manager);
156   g_object_unref (job_manager);
157   g_object_unref (boot_manager_service);
158   g_object_unref (connection);
159
160   return EXIT_SUCCESS;
161 }