[daemon] Add command-line option for DBus bus type
authorSangjung Woo <sangjung.woo@samsung.com>
Mon, 18 Jul 2022 05:32:03 +0000 (14:32 +0900)
committergichan-jang <56856496+gichan-jang@users.noreply.github.com>
Wed, 27 Jul 2022 02:46:18 +0000 (11:46 +0900)
To run the unit tests in the GBS environment, the session type of the
DBus is required. This patch adds the command-line option to support
the session bus.

Signed-off-by: Sangjung Woo <sangjung.woo@samsung.com>
daemon/gdbus-util.c
daemon/includes/gdbus-util.h
daemon/main.c

index 3f76994..c7f3858 100644 (file)
@@ -146,13 +146,15 @@ gdbus_put_instance_pipeline (MachinelearningServicePipeline ** instance)
  * @brief Connect to the DBus message bus, which type is SYSTEM.
  */
 int
-gdbus_get_system_connection (void)
+gdbus_get_system_connection (gboolean is_session)
 {
   GError *error = NULL;
+  GBusType bus_type = is_session ? G_BUS_TYPE_SESSION : G_BUS_TYPE_SYSTEM;
 
-  g_dbus_sys_conn = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
+  g_dbus_sys_conn = g_bus_get_sync (bus_type, NULL, &error);
   if (g_dbus_sys_conn == NULL) {
     _E ("cannot connect to the system message bus: %s\n", error->message);
+    g_clear_error(&error);
     return -ENOSYS;
   }
 
index d55d22d..818b09d 100644 (file)
@@ -86,10 +86,11 @@ MachinelearningServicePipeline *gdbus_get_instance_pipeline (void);
 void gdbus_put_instance_pipeline (MachinelearningServicePipeline ** instance);
 
 /**
- * @brief Connect to the DBus message bus, which type is SYSTEM.
+ * @brief Connect to the DBus message bus
+ * @param is_session Ture is DBus Bus type is session.
  * @return @c 0 on success. Otherwise a negative error value.
  */
-int gdbus_get_system_connection (void);
+int gdbus_get_system_connection (gboolean is_session);
 
 /**
  * @brief Disconnect the DBus message bus.
index af613a7..2262af0 100644 (file)
@@ -22,6 +22,8 @@
 #include "dbus-interface.h"
 
 static GMainLoop *g_mainloop;
+static gboolean verbose = FALSE;
+static gboolean is_session = FALSE;
 
 /**
  * @brief Handle the SIGTERM signal and quit the main loop
@@ -52,13 +54,56 @@ postinit (void)
 }
 
 /**
+ * @brief Parse commandline option.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+static int
+parse_args (gint *argc, gchar ***argv)
+{
+  GError *err;
+  GOptionContext *context = NULL;
+  gboolean ret;
+
+  static GOptionEntry entries[] = {
+    { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
+    { "session", 's', 0, G_OPTION_ARG_NONE, &is_session, "Bus type is session", NULL },
+    { NULL }
+  };
+
+  context = g_option_context_new (NULL);
+  if (!context) {
+    _E ("Failed to call g_option_context_new\n");
+    return -ENOMEM;
+  }
+
+  g_option_context_add_main_entries (context, entries, NULL);
+  g_option_context_set_help_enabled (context, TRUE);
+  g_option_context_set_ignore_unknown_options (context, TRUE);
+
+  err = NULL;
+  ret = g_option_context_parse (context, argc, argv, &err);
+  g_option_context_free(context);
+  if (!ret) {
+    _E ("Fail to option parsing: %s", err->message);
+    g_clear_error(&err);
+    return -EINVAL;
+  }
+
+  return 0;
+}
+
+/**
  * @brief main function of the Machine Learning agent daemon.
  */
 int
 main (int argc, char **argv)
 {
+  if (parse_args(&argc, &argv)) {
+    return -EINVAL;
+  }
+
   g_mainloop = g_main_loop_new (NULL, FALSE);
-  gdbus_get_system_connection ();
+  gdbus_get_system_connection (is_session);
 
   init_modules (NULL);
   if (postinit () < 0)