#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
*/
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");
}
/**
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);
}
/**
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;
}
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;
}
}
/**
- * @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;
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;
}
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);
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 ();
+}