summaryrefslogtreecommitdiff
path: root/legacy-app-handler/main.c
blob: ca0ef67dc0886fb27ddbf2ebebe61dfc8d247a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* vi:set et ai sw=2 sts=2 ts=2: */
/* SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2012, GENIVI
 *
 * This file is part of node-startup-controller.
 *
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License (MPL), v. 2.0.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 *
 * List of changes:
 * 2015-04-30, Jonathan Maw, List of changes started
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <glib.h>
#include <gio/gio.h>

#include <dlt/dlt.h>

#include <common/la-handler-dbus.h>
#include <common/nsm-enum-types.h>



static gchar          *unit = NULL;
static gint            timeout = 1000;
static NSMShutdownType shutdown_mode = NSM_SHUTDOWN_TYPE_NOT;



static GOptionEntry entries[] =
{
  { "unit",          'u', 0, G_OPTION_ARG_STRING, &unit,          "Legacy application unit",            NULL },
  { "timeout",       't', 0, G_OPTION_ARG_INT,    &timeout,       "Shutdown timeout in milliseconds",   NULL },
  { "shutdown-mode", 'm', 0, G_OPTION_ARG_INT,    &shutdown_mode, "Shutdown mode",                      NULL },
  { NULL },
};



DLT_DECLARE_CONTEXT (la_handler_context);



static void
unregister_dlt (void)
{
  DLT_UNREGISTER_CONTEXT (la_handler_context);
  DLT_UNREGISTER_APP ();
}



int
main (int    argc,
      char **argv)
{
  GOptionContext *context;
  LAHandler      *service;
  GError         *error = NULL;

  /* register the application and context with the DLT */
  DLT_REGISTER_APP ("NSC", "GENIVI Node Startup Controller");
  DLT_REGISTER_CONTEXT (la_handler_context, "LAH", "Legacy Application Handler");

  /* make sure to unregister the DLT at exit */
  atexit (unregister_dlt);

  /* initialize the GType type system */
  g_type_init ();

  /* prepare command line option parsing */
  context = g_option_context_new (NULL);
  g_option_context_set_help_enabled (context, TRUE);
  g_option_context_add_main_entries (context, entries, NULL);

  /* try to parse command line options */
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      /* parsing failed, exit with an error */
      DLT_LOG (la_handler_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to parse command line options:"),
               DLT_STRING (error->message));

      /* clean up */
      g_option_context_free (context);
      g_error_free (error);
      g_free (unit);

      return EXIT_FAILURE;
    }
  g_option_context_free (context);

  /* abort if no unit file was specified */
  if (unit == NULL || *unit == '\0')
    {
      DLT_LOG (la_handler_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to register legacy application:"),
               DLT_STRING ("no unit specified"));

      /* free command line options */
      g_free (unit);

      return EXIT_FAILURE;
    }

  /* validate the shutdown mode */
  if (shutdown_mode != NSM_SHUTDOWN_TYPE_NORMAL
      && shutdown_mode != NSM_SHUTDOWN_TYPE_FAST
      && shutdown_mode != (NSM_SHUTDOWN_TYPE_NORMAL | NSM_SHUTDOWN_TYPE_FAST))
    {
      DLT_LOG (la_handler_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to register legacy application: "
                           "invalid shutdown mode"), DLT_INT (shutdown_mode));

      /* free command line options */
      g_free (unit);

      return EXIT_FAILURE;
    }

  /* validate the timeout */
  if (timeout < 0)
    {
      DLT_LOG (la_handler_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to register legacy application:"),
               DLT_STRING ("shutdown timeout must be non-negative"));

      /* free command line options */
      g_free (unit);

      return EXIT_FAILURE;
    }

  /* create a proxy to talk to the legacy app handler D-Bus service */
  service =
    la_handler_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                       G_DBUS_PROXY_FLAGS_NONE,
                                       "org.genivi.NodeStartupController1",
                                       "/org/genivi/NodeStartupController1/LegacyAppHandler",
                                       NULL, &error);

  /* abort if the proxy could not be created */
  if (error != NULL)
    {
      DLT_LOG (la_handler_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to register legacy application:"),
               DLT_STRING (error->message));

      /* clean up */
      g_error_free (error);

      /* free command line options */
      g_free (unit);

      return EXIT_FAILURE;
    }

  /* forward the register request to the legacy app handler D-Bus service */
  if (!la_handler_call_register_sync (service, unit, shutdown_mode, timeout,
                                      NULL, &error))
    {
      DLT_LOG (la_handler_context, DLT_LOG_ERROR,
               DLT_STRING ("Failed to register legacy application:"),
               DLT_STRING (error->message));

      /* clean up */
      g_error_free (error);
      g_object_unref (service);

      /* free command line options */
      g_free (unit);

      return EXIT_FAILURE;
    }

  /* release the legacy app handler proxy */
  g_object_unref (service);

  /* free command line options */
  g_free (unit);

  return EXIT_SUCCESS;
}