Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
4 : *
5 : * @file main.c
6 : * @date 25 June 2022
7 : * @brief Core module for the Machine Learning agent
8 : * @see https://github.com/nnstreamer/deviceMLOps.MLAgent
9 : * @author Sangjung Woo <sangjung.woo@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : */
12 :
13 : #include <stdio.h>
14 : #include <fcntl.h>
15 : #include <glib.h>
16 : #include <gio/gio.h>
17 : #include <errno.h>
18 :
19 : #include "common.h"
20 : #include "modules.h"
21 : #include "gdbus-util.h"
22 : #include "log.h"
23 : #include "dbus-interface.h"
24 : #include "mlops-agent-internal.h"
25 :
26 : static GMainLoop *g_mainloop = NULL;
27 : static gboolean verbose = FALSE;
28 : static gboolean is_session = FALSE;
29 : static gchar *db_path = NULL;
30 :
31 : /**
32 : * @brief Handle the SIGTERM signal and quit the main loop
33 : */
34 : static void
35 0 : handle_sigterm (int signo)
36 : {
37 0 : ml_logd ("received SIGTERM signal %d", signo);
38 0 : g_main_loop_quit (g_mainloop);
39 0 : }
40 :
41 : /**
42 : * @brief Handle the post init tasks before starting the main loop.
43 : * @return @c 0 on success. Otherwise a negative error value.
44 : */
45 : static int
46 0 : postinit (void)
47 : {
48 : int ret;
49 :
50 : /** Register signal handler */
51 0 : signal (SIGTERM, handle_sigterm);
52 :
53 0 : ret = gdbus_get_name (DBUS_ML_BUS_NAME);
54 0 : if (ret < 0)
55 0 : return ret;
56 :
57 0 : return 0;
58 : }
59 :
60 : /**
61 : * @brief Parse commandline option.
62 : * @return @c 0 on success. Otherwise a negative error value.
63 : */
64 : static int
65 0 : parse_args (gint *argc, gchar ***argv)
66 : {
67 0 : GError *err = NULL;
68 0 : GOptionContext *context = NULL;
69 : gboolean ret;
70 :
71 : static GOptionEntry entries[] = {
72 : { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
73 : { "session", 's', 0, G_OPTION_ARG_NONE, &is_session, "Bus type is session", NULL },
74 : { "path", 'p', 0, G_OPTION_ARG_STRING, &db_path, "Path to database", NULL },
75 : { NULL }
76 : };
77 :
78 0 : context = g_option_context_new (NULL);
79 0 : if (!context) {
80 0 : ml_loge ("Failed to call g_option_context_new\n");
81 0 : return -ENOMEM;
82 : }
83 :
84 0 : g_option_context_add_main_entries (context, entries, NULL);
85 0 : g_option_context_set_help_enabled (context, TRUE);
86 0 : g_option_context_set_ignore_unknown_options (context, TRUE);
87 :
88 0 : ret = g_option_context_parse (context, argc, argv, &err);
89 0 : g_option_context_free (context);
90 0 : if (!ret) {
91 0 : ml_loge ("Fail to option parsing: %s", err->message);
92 0 : g_clear_error (&err);
93 0 : return -EINVAL;
94 : }
95 :
96 0 : return 0;
97 : }
98 :
99 : /**
100 : * @brief main function of the Machine Learning agent daemon.
101 : */
102 : int
103 0 : main (int argc, char **argv)
104 : {
105 0 : int ret = 0;
106 :
107 0 : if (parse_args (&argc, &argv)) {
108 0 : ret = -EINVAL;
109 0 : goto error;
110 : }
111 :
112 : /* path to database */
113 0 : if (!db_path)
114 0 : db_path = g_strdup (DB_PATH);
115 :
116 0 : ml_agent_initialize (db_path);
117 :
118 0 : g_mainloop = g_main_loop_new (NULL, FALSE);
119 0 : gdbus_get_system_connection (is_session);
120 :
121 0 : init_modules (NULL);
122 0 : if (postinit () < 0)
123 0 : ml_loge ("cannot init system");
124 :
125 0 : g_main_loop_run (g_mainloop);
126 0 : exit_modules (NULL);
127 :
128 0 : gdbus_put_system_connection ();
129 0 : g_main_loop_unref (g_mainloop);
130 0 : g_mainloop = NULL;
131 :
132 0 : error:
133 0 : ml_agent_finalize ();
134 :
135 0 : is_session = verbose = FALSE;
136 0 : g_free (db_path);
137 0 : db_path = NULL;
138 0 : return ret;
139 : }
|