a7a9d9350e877c36dfc0c2861805456594cd3c0a
[profile/ivi/node-startup-controller.git] / legacy-app-handler / 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 <legacy-app-handler/la-handler-application.h>
24 #include <legacy-app-handler/la-handler-service.h>
25
26
27
28 DLT_DECLARE_CONTEXT (la_handler_context);
29
30
31
32 static void
33 dlt_cleanup (void)
34 {
35   DLT_UNREGISTER_CONTEXT (la_handler_context);
36   DLT_UNREGISTER_APP ();
37 }
38
39
40
41 int
42 main (int    argc,
43       char **argv)
44 {
45   LAHandlerApplication *application;
46   LAHandlerService     *service;
47   GDBusConnection      *connection;
48   gboolean              is_remote;
49   GError               *error = NULL;
50   gchar                *log_text;
51   int                   exit_status;
52
53   /* check if this program execution is meant as a remote application.
54    * if it is a remote application, then it will be called with command-line arguments. */
55   is_remote = (argc > 1) ? TRUE : FALSE;
56
57   /* register the application and context in DLT */
58   if (!is_remote)
59     {
60       DLT_REGISTER_APP ("BMGR", "GENIVI Boot Manager");
61       DLT_REGISTER_CONTEXT (la_handler_context, "LAH",
62                             "Context of the legacy application handler that hooks legacy "
63                             "applications up with the shutdown concept of the Node State "
64                             "Manager");
65       atexit (dlt_cleanup);
66     }
67
68   /* initialize the GType type system */
69   g_type_init ();
70
71   /* attempt to connect to D-Bus */
72   connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
73   if (connection == NULL || error != NULL || !G_IS_DBUS_CONNECTION (connection))
74     {
75       log_text = g_strdup_printf ("Failed to connect to D-Bus: %s", error->message);
76       DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_text));
77
78       /* clean up */
79       g_free (log_text);
80       g_error_free (error);
81
82       return EXIT_FAILURE;
83     }
84
85   /* instantiate the LegacyAppHandler service implementation */
86   service = la_handler_service_new (connection);
87   if (!la_handler_service_start (service, &error))
88     {
89       log_text = g_strdup_printf ("Failed to start the legacy app handler service: %s",
90                                   error->message);
91       DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_text));
92
93       /* clean up */
94       g_free (log_text);
95       g_error_free (error);
96       g_object_unref (service);
97       g_object_unref (connection);
98
99       return EXIT_FAILURE;
100     }
101
102   /* create and run the main application */
103   if (is_remote)
104     {
105       /* an application with the flag G_APPLICATION_IS_SERVICE tries to be the primary
106        * instance of the application, and fails if another instance already exists.
107        * setting G_APPLICATION_IS_LAUNCHER indicates that it shouldn't try to be the
108        * primary instance */
109       application =
110         la_handler_application_new (service, G_APPLICATION_HANDLES_COMMAND_LINE |
111                                              G_APPLICATION_IS_LAUNCHER);
112     }
113   else
114     {
115       /* this application is meant to be the primary instance, so
116        * G_APPLICATION_IS_LAUNCHER is not set */
117       application =
118         la_handler_application_new (service, G_APPLICATION_IS_SERVICE);
119     }
120
121   /* run the application */
122   exit_status = g_application_run (G_APPLICATION (application), argc, argv);
123   g_object_unref (application);
124
125   /* release allocated objects */
126   g_object_unref (service);
127   g_object_unref (connection);
128
129   return exit_status;
130 }