Merge branch 'franred/drop-rebase' of roadtrain.codethink.co.uk:genivi/boot-manager
[profile/ivi/node-startup-controller.git] / boot-manager / boot-manager-command-line.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-command-line.h>
24 #include <boot-manager/la-handler-dbus.h>
25
26
27 DLT_IMPORT_CONTEXT (la_handler_context);
28
29
30
31 gint
32 boot_manager_handle_command_line (int              argc,
33                                   char           **argv,
34                                   GDBusConnection *connection)
35 {
36   GOptionContext *context = g_option_context_new (NULL);
37   LAHandler      *legacy_app_handler = NULL;
38   gboolean        do_register = FALSE;
39   gboolean        do_deregister = FALSE;
40   GError         *error = NULL;
41   gchar          *unit = NULL;
42   gchar          *log_message = NULL;
43   gchar          *mode = NULL;
44   gint            timeout = 0;
45   int             exit_status;
46
47   GOptionEntry entries[] = {
48     {"deregister",    0, 0, G_OPTION_ARG_NONE,   &do_deregister, NULL, NULL},
49     {"register",      0, 0, G_OPTION_ARG_NONE,   &do_register,   NULL, NULL},
50     {"unit",          0, 0, G_OPTION_ARG_STRING, &unit,          NULL, NULL},
51     {"timeout",       0, 0, G_OPTION_ARG_INT,    &timeout,       NULL, NULL},
52     {"shutdown-mode", 0, 0, G_OPTION_ARG_STRING, &mode,          NULL, NULL},
53     {NULL},
54   };
55
56   /* set up the option context */
57   g_option_context_set_help_enabled (context, FALSE);
58   g_option_context_add_main_entries (context, entries, NULL);
59
60   /* parse the arguments into argument data */
61   if (!g_option_context_parse (context, &argc, &argv, &error) || error != NULL)
62     {
63       /* an error occurred */
64       log_message =
65         g_strdup_printf ("Error occurred parsing arguments: %s\n", error->message);
66       DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
67
68       exit_status = EXIT_FAILURE;
69       goto finish;
70     }
71
72   /* validate the argument data */
73   if (unit == NULL || *unit == '\0')
74     {
75       DLT_LOG (la_handler_context, DLT_LOG_ERROR,
76                DLT_STRING ("Invalid arguments: unit must be defined"));
77       exit_status = EXIT_FAILURE;
78       goto finish;
79     }
80
81   if (!(do_register ^ do_deregister))
82     {
83       DLT_LOG (la_handler_context, DLT_LOG_ERROR,
84                DLT_STRING ("Invalid arguments: Please select either --register or "
85                            "--deregister"));
86       exit_status = EXIT_FAILURE;
87       goto finish;
88     }
89
90   if (do_register && timeout < 0)
91     {
92       DLT_LOG (la_handler_context, DLT_LOG_ERROR,
93                DLT_STRING ("Invalid arguments: Timeout must be non-negative"));
94       exit_status = EXIT_FAILURE;
95       goto finish;
96     }
97
98   /* get a legacy app handler interface */
99   legacy_app_handler =
100     la_handler_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE,
101                                "org.genivi.BootManager1",
102                                "/org/genivi/BootManager1/LegacyAppHandler", NULL,
103                                &error);
104   if (error != NULL)
105     {
106       /* failed to connect to the legacy app handler */
107       log_message =
108         g_strdup_printf ("Error occurred connecting to legacy app handler "
109                          "interface: %s", error->message);
110       DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
111
112       g_free (log_message);
113       g_error_free (error);
114
115       exit_status = EXIT_FAILURE;
116       goto finish;
117     }
118
119   if (do_register)
120     {
121       /* call the legacy app handler's Register() method */
122       la_handler_call_register_sync (legacy_app_handler, unit, mode ? mode : "normal",
123                                      (guint) timeout, NULL, &error);
124
125       if (error != NULL)
126         {
127           /* failed to register the legacy app */
128           log_message = g_strdup_printf ("Error occurred registering legacy app: %s",
129                                          error->message);
130           DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
131
132           exit_status = EXIT_FAILURE;
133         }
134       else
135         {
136           exit_status = EXIT_SUCCESS;
137         }
138       goto finish;
139     }
140   else if (do_deregister)
141     {
142       /* call the legacy app handler's Deregister() method */
143       la_handler_call_deregister_sync (legacy_app_handler, unit, NULL, &error);
144       if (error != NULL)
145         {
146           /* failed to deregister the legacy app */
147           log_message = g_strdup_printf ("Error occurred deregistering legacy app: %s",
148                                          error->message);
149           DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
150
151           exit_status = EXIT_FAILURE;
152         }
153       else
154         {
155           exit_status = EXIT_SUCCESS;
156         }
157       goto finish;
158     }
159   else
160     {
161       DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("unexpected input"));
162       exit_status = EXIT_FAILURE;
163       goto finish;
164     }
165
166   finish:
167   g_option_context_free (context);
168   if (legacy_app_handler != NULL)
169     g_object_unref (legacy_app_handler);
170   if (error != NULL)
171     g_error_free (error);
172   g_free (unit);
173   g_free (log_message);
174   g_free (mode);
175   return exit_status;
176 }