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 19 : handle_sigterm (int signo)
36 : {
37 19 : ml_logd ("received SIGTERM signal %d", signo);
38 19 : g_main_loop_quit (g_mainloop);
39 19 : }
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 19 : postinit (void)
47 : {
48 : int ret;
49 :
50 : /** Register signal handler */
51 19 : signal (SIGTERM, handle_sigterm);
52 :
53 19 : ret = gdbus_get_name (DBUS_ML_BUS_NAME);
54 19 : if (ret < 0) {
55 0 : ml_loge ("Failed to get mlops-agent dbus.");
56 0 : return ret;
57 : }
58 :
59 19 : return 0;
60 : }
61 :
62 : /**
63 : * @brief Parse commandline option.
64 : * @return @c 0 on success. Otherwise a negative error value.
65 : */
66 : static int
67 19 : parse_args (gint *argc, gchar ***argv)
68 : {
69 19 : GError *err = NULL;
70 19 : GOptionContext *context = NULL;
71 : gboolean ret;
72 :
73 : static GOptionEntry entries[] = {
74 : { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
75 : { "session", 's', 0, G_OPTION_ARG_NONE, &is_session, "Bus type is session", NULL },
76 : { "path", 'p', 0, G_OPTION_ARG_STRING, &db_path, "Path to database", NULL },
77 : { NULL }
78 : };
79 :
80 19 : context = g_option_context_new (NULL);
81 19 : if (!context) {
82 0 : ml_loge ("Failed to call g_option_context_new\n");
83 19 : return -ENOMEM;
84 : }
85 :
86 19 : g_option_context_add_main_entries (context, entries, NULL);
87 19 : g_option_context_set_help_enabled (context, TRUE);
88 19 : g_option_context_set_ignore_unknown_options (context, TRUE);
89 :
90 19 : ret = g_option_context_parse (context, argc, argv, &err);
91 19 : g_option_context_free (context);
92 19 : if (!ret) {
93 0 : ml_loge ("Fail to option parsing: %s", err->message);
94 0 : g_clear_error (&err);
95 0 : return -EINVAL;
96 : }
97 :
98 19 : return 0;
99 : }
100 :
101 : /**
102 : * @brief main function of the Machine Learning agent daemon.
103 : */
104 : int
105 19 : main (int argc, char **argv)
106 : {
107 19 : int ret = 0;
108 :
109 19 : ret = parse_args (&argc, &argv);
110 19 : if (ret < 0)
111 0 : goto error;
112 :
113 : /* path to database */
114 19 : if (!db_path)
115 0 : db_path = g_strdup (DB_PATH);
116 :
117 19 : ret = ml_agent_initialize (db_path);
118 19 : if (ret < 0)
119 0 : goto error;
120 :
121 19 : g_mainloop = g_main_loop_new (NULL, FALSE);
122 19 : ret = gdbus_get_system_connection (is_session);
123 19 : if (ret < 0)
124 0 : goto error;
125 :
126 19 : init_modules (NULL);
127 :
128 19 : ret = postinit ();
129 19 : if (ret < 0)
130 0 : goto error;
131 :
132 19 : g_main_loop_run (g_mainloop);
133 19 : exit_modules (NULL);
134 :
135 19 : gdbus_put_system_connection ();
136 19 : g_main_loop_unref (g_mainloop);
137 19 : g_mainloop = NULL;
138 :
139 19 : error:
140 19 : ml_agent_finalize ();
141 :
142 19 : is_session = verbose = FALSE;
143 19 : g_clear_pointer (&db_path, g_free);
144 19 : return ret;
145 : }
|