a467e2d104ba1b550629d8ce8af2e5cf0dc5e560
[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   GError                 *error = NULL;
60   gint                    exit_status;
61
62   /* register the application and context in DLT */
63   DLT_REGISTER_APP ("BMGR", "GENIVI Boot Manager");
64   DLT_REGISTER_CONTEXT (boot_manager_context, "MGR",
65                         "Context of the boot manager itself");
66   DLT_REGISTER_CONTEXT (la_handler_context, "LAH",
67                         "Context of the legacy application handler that hooks legacy "
68                         "applications up with the shutdown concept of the Node State "
69                         "Manager");
70
71   /* have DLT unregistered at exit */
72   atexit (unregister_dlt);
73
74   /* initialize the GType type system */
75   g_type_init ();
76
77   /* attempt to connect to D-Bus */
78   connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
79   if (connection == NULL)
80     {
81       g_warning ("Failed to connect to the system bus: %s", error->message);
82
83       /* clean up */
84       g_error_free (error);
85
86       return EXIT_FAILURE;
87     }
88
89   /* Parse and react to the command-line arguments instead of starting the application
90    * if any arguments are given */
91   if (argc > 1)
92     {
93       exit_status = boot_manager_handle_command_line (argc, argv, connection);
94
95       g_object_unref (connection);
96
97       return exit_status;
98     }
99
100   /* attempt to connect to the systemd manager */
101   systemd_manager =
102     systemd_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
103                                             G_DBUS_PROXY_FLAGS_NONE,
104                                             "org.freedesktop.systemd1",
105                                             "/org/freedesktop/systemd1",
106                                             NULL, &error);
107   if (systemd_manager == NULL)
108     {
109       g_warning ("Failed to connect to the systemd manager: %s", error->message);
110
111       /* clean up */
112       g_error_free (error);
113       g_object_unref (connection);
114
115       return EXIT_FAILURE;
116     }
117
118   /* subscribe to the systemd manager */
119   if (!systemd_manager_call_subscribe_sync (systemd_manager, NULL, &error))
120     {
121       g_warning ("Failed to subscribe to the systemd manager: %s", error->message);
122
123       /* clean up */
124       g_error_free (error);
125       g_object_unref (connection);
126
127       return EXIT_FAILURE;
128     }
129
130   /* instantiate the boot manager service implementation */
131   boot_manager_service = boot_manager_service_new (connection);
132
133   /* instantiate the job manager */
134   job_manager = job_manager_new (connection, systemd_manager);
135
136   /* start up the target startup monitor */
137   target_startup_monitor = target_startup_monitor_new (systemd_manager);
138
139   /* instantiate the legacy app handler */
140   la_handler_service = la_handler_service_new (connection, job_manager);
141
142   /* create and run the main application */
143   application = boot_manager_application_new (connection, job_manager,
144                                               la_handler_service, boot_manager_service);
145   exit_status = g_application_run (G_APPLICATION (application), 0, NULL);
146   g_object_unref (application);
147
148   /* release allocated objects */
149   g_object_unref (target_startup_monitor);
150   g_object_unref (systemd_manager);
151   g_object_unref (job_manager);
152   g_object_unref (boot_manager_service);
153   g_object_unref (connection);
154
155   return exit_status;
156 }