[VD/NPUMGR] Daemonize NPUMGR runnable in FastModel
authorDongju Chae <dongju.chae@samsung.com>
Tue, 25 May 2021 04:46:39 +0000 (13:46 +0900)
committer채동주/On-Device Lab(SR)/Staff Engineer/삼성전자 <dongju.chae@samsung.com>
Tue, 25 May 2021 08:20:55 +0000 (17:20 +0900)
This patch makes daemonized NPUMGR which is runnable in FastModel.

Signed-off-by: Dongju Chae <dongju.chae@samsung.com>
tests/apptests/npumgr/meson.build
tests/apptests/npumgr/npumgr.cc
tests/apptests/npumgr/npumgr_api.cc

index f7c9252..7bc7edd 100644 (file)
@@ -9,6 +9,10 @@ if glib_dep.found() and giounix_dep.found()
     giounix_dep
   ]
 
+  if build_platform == 'tizen'
+    npumgr_deps += dependency('dlog')
+  endif
+
   npumgr_lib = shared_library ('npumgr',
     'npumgr_api.cc',
     dependencies: npumgr_deps,
index 698d2fc..8f9f540 100644 (file)
@@ -16,6 +16,9 @@
 #include <vector>
 #include <string>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
 #include <gmodule.h>
 #include <gio/gio.h>
 #include <gio/gunixfdlist.h>
 #include <npumgr_api.h>
 #include <npumgr_device.h>
 
+#define NPUMGR_DAEMON_NAME "npumgr-dummy"
 #define NPUMGR_DEVICE_GET_CLASS(obj) \
   (G_TYPE_INSTANCE_GET_CLASS ((obj), NPUMGR_TYPE_DEVICE, NpumgrDeviceClass))
 
+#ifdef __TIZEN__
+#include <dlog.h>
+#define TAG_NAME NPUMGR_DAEMON_NAME
+#define logi(...) dlog_print (DLOG_INFO, TAG_NAME, __VA_ARGS__)
+#define logw(...) dlog_print (DLOG_WARN, TAG_NAME, __VA_ARGS__)
+#define loge(...) dlog_print (DLOG_ERROR, TAG_NAME, __VA_ARGS__)
+#else
+#include <syslog.h>
+#define logi(...) syslog (LOG_INFO, __VA_ARGS__)
+#define logw(...) syslog (LOG_WARNING, __VA_ARGS__)
+#define loge(...) syslog (LOG_ERR, __VA_ARGS__)
+#endif
+
 /**
  * @brief Internal data structure for NPU Manager Context
  */
@@ -517,7 +534,7 @@ on_bus_acquired (GDBusConnection *connection, const gchar *name,
       connection, "/sr/odl/NPUManager/APIObject",
       introspection_data->interfaces[0], &interface_vtable, NULL, NULL, NULL);
   if (reg_id == 0)
-    g_critical ("Failed to register object");
+    loge ("Failed to register object");
 }
 
 /**
@@ -533,7 +550,8 @@ on_name_acquired (GDBusConnection *connection, const gchar *name,
 static void
 on_name_lost (GDBusConnection *connection, const gchar *name,
               gpointer user_data) {
-  exit (1);
+  loge ("Unable to acquire name %s", name);
+  exit (EXIT_FAILURE);
 }
 
 /**
@@ -576,7 +594,7 @@ init_plugins (void) {
     gchar *module_name = g_strdup_printf ("libnpumgr_%s", name);
     module = g_module_open (module_name, G_MODULE_BIND_LAZY);
     if (!module) {
-      g_critical ("Unable to open %s", module_name);
+      logw ("Unable to open plugin %s", module_name);
       g_free (module_name);
       continue;
     }
@@ -584,7 +602,7 @@ init_plugins (void) {
 
     gchar *module_symbol = g_strdup_printf ("npumgr_device_%s_new", name);
     if (!g_module_symbol (module, module_symbol, (gpointer *) &func)) {
-      g_critical ("Unable to find symbol %s", module_symbol);
+      logw ("Unable to find symbol %s", module_symbol);
       g_free (module_symbol);
       continue;
     }
@@ -601,10 +619,10 @@ init_plugins (void) {
 }
 
 /**
- * @brief Main function for gdbus-based server
+ * @brief Main routine for gdbus-based npumgr daemon
  */
-int
-main (int argc, char *argv[]) {
+static int
+start_npumgr (void) {
   guint owner_id;
   GMainLoop *loop;
 
@@ -614,7 +632,7 @@ main (int argc, char *argv[]) {
 
   introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
   if (!introspection_data) {
-    g_critical ("Failed to build the introspection data structure");
+    loge ("Failed to build the introspection data structure");
     return -1;
   }
 
@@ -622,7 +640,7 @@ main (int argc, char *argv[]) {
 
   ctx_table =
       g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, ctx_destroy);
-  owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, "sr.odl.NPUManager.API",
+  owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, "sr.odl.NPUManager.API",
                              G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired,
                              on_name_acquired, on_name_lost, NULL, NULL);
 
@@ -635,5 +653,45 @@ main (int argc, char *argv[]) {
   g_hash_table_destroy (ctx_table);
   g_slist_free_full (registered_plugins, (GDestroyNotify) plugin_pdata_free);
 
+  logi ("Stopping %s", NPUMGR_DAEMON_NAME);
   return 0;
 }
+
+/**
+ * @brief Daemonize the program
+ */
+int
+main (int argc, char *argv[]) {
+  pid_t pid, sid;
+
+  pid = fork ();
+  if (pid > 0)
+    exit (EXIT_SUCCESS);
+  else if (pid < 0)
+    exit (EXIT_FAILURE);
+
+  umask (0);
+
+#ifndef __TIZEN__
+  openlog (NPUMGR_DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_USER);
+#endif
+
+  sid = setsid ();
+  if (sid < 0) {
+    loge ("Unable to generate session ID for child process");
+    exit (EXIT_FAILURE);
+  }
+
+  if ((chdir ("/")) < 0) {
+    loge ("Unable to change the working directory");
+    exit (EXIT_FAILURE);
+  }
+
+  close (STDIN_FILENO);
+  close (STDOUT_FILENO);
+  close (STDERR_FILENO);
+
+  logi ("Starting %s", NPUMGR_DAEMON_NAME);
+
+  return start_npumgr ();
+}
index 44e891b..686916f 100644 (file)
@@ -634,10 +634,9 @@ static gpointer
 npumgr_thread (gpointer data) {
   guint watcher_id;
 
-  watcher_id =
-      g_bus_watch_name (G_BUS_TYPE_SESSION, "sr.odl.NPUManager.API",
-                        G_BUS_NAME_WATCHER_FLAGS_NONE, on_name_appeared,
-                        on_name_vanished, NULL, NULL);
+  watcher_id = g_bus_watch_name (
+      G_BUS_TYPE_SYSTEM, "sr.odl.NPUManager.API", G_BUS_NAME_WATCHER_FLAGS_NONE,
+      on_name_appeared, on_name_vanished, NULL, NULL);
 
   _loop = g_main_loop_new (NULL, FALSE);