Refactor main function to use separated init/exit function 76/324976/1
authorSangYoun Kwak <sy.kwak@samsung.com>
Thu, 29 May 2025 10:58:37 +0000 (19:58 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Thu, 29 May 2025 10:58:37 +0000 (19:58 +0900)
To make the main function's init/exit sequences can be used by other
source files such as plugin library, the init/exit sequences are
separated as 'sensord_init' and 'sensord_exit' respectively. The prefix
is 'sensord', because sensord_init initializes sensord and sensord_exit
exits sensord.
Accordingly, main.cpp(includes main function) is renamed to sensord.cpp.

Also, to make them callable, header file sensord.h is added.

Change-Id: Ic954911587eec9203ebbdf2db46f59377fd014f0
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
src/server/main.cpp [deleted file]
src/server/sensord.cpp [new file with mode: 0644]
src/server/sensord.h [new file with mode: 0644]

diff --git a/src/server/main.cpp b/src/server/main.cpp
deleted file mode 100644 (file)
index c4ec048..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * sensord
- *
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#include <new>
-#include <csignal>
-
-#include <glib-unix.h>
-
-#include <sensor_log.h>
-#include "server.h"
-
-#define NEW_FAIL_LIMIT 3
-
-using namespace sensor;
-
-static void handle_signal_std(int signum)
-{
-       _W("Received SIGNAL(%d : %s)", signum, strsignal(signum));
-
-       /* raise SIGTERM manually to call handle_signal with glib dispatcher */
-       raise(SIGTERM);
-}
-
-static gboolean handle_signal(gpointer data)
-{
-       long signum = (long) data;
-       _W("Received SIGNAL(%ld : %s)", signum, strsignal(signum));
-
-       server::stop();
-
-       return G_SOURCE_REMOVE;
-}
-
-static void on_new_failed(void)
-{
-       static unsigned fail_count = 0;
-       _E("Failed to allocate memory");
-
-       fail_count += 1;
-       if (fail_count >= NEW_FAIL_LIMIT) {
-               raise(SIGTERM);
-               return;
-       }
-}
-
-int main(int argc, char *argv[])
-{
-       _I("Started");
-
-       /* register signal handler with glib to prevent deadlock */
-       g_unix_signal_add(SIGINT, handle_signal, (gpointer) SIGINT);
-       g_unix_signal_add(SIGHUP, handle_signal, (gpointer) SIGHUP);
-       g_unix_signal_add(SIGTERM, handle_signal, (gpointer) SIGTERM);
-
-       /* register signal handler with std for not supported signal in glib */
-       std::signal(SIGQUIT, handle_signal_std);
-       std::signal(SIGABRT, handle_signal_std);
-       std::signal(SIGCHLD, SIG_IGN);
-       std::signal(SIGPIPE, SIG_IGN);
-
-       std::set_new_handler(on_new_failed);
-
-       server::run();
-
-       _I("Stopped");
-
-       return 0;
-}
diff --git a/src/server/sensord.cpp b/src/server/sensord.cpp
new file mode 100644 (file)
index 0000000..694d557
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <new>
+#include <csignal>
+
+#include <glib-unix.h>
+
+#include <sensor_log.h>
+#include "server.h"
+#include "sensord.h"
+
+#define NEW_FAIL_LIMIT 3
+
+using namespace sensor;
+
+static void handle_signal_std(int signum)
+{
+       _W("Received SIGNAL(%d : %s)", signum, strsignal(signum));
+
+       /* raise SIGTERM manually to call handle_signal with glib dispatcher */
+       raise(SIGTERM);
+}
+
+static gboolean handle_signal(gpointer data)
+{
+       long signum = (long) data;
+       _W("Received SIGNAL(%ld : %s)", signum, strsignal(signum));
+
+       sensord_exit();
+
+       return G_SOURCE_REMOVE;
+}
+
+static void on_new_failed(void)
+{
+       static unsigned fail_count = 0;
+       _E("Failed to allocate memory");
+
+       fail_count += 1;
+       if (fail_count >= NEW_FAIL_LIMIT) {
+               raise(SIGTERM);
+               return;
+       }
+}
+
+void sensord_init(void)
+{
+       /* register signal handler with glib to prevent deadlock */
+       g_unix_signal_add(SIGINT, handle_signal, (gpointer) SIGINT);
+       g_unix_signal_add(SIGHUP, handle_signal, (gpointer) SIGHUP);
+       g_unix_signal_add(SIGTERM, handle_signal, (gpointer) SIGTERM);
+
+       /* register signal handler with std for not supported signal in glib */
+       std::signal(SIGQUIT, handle_signal_std);
+       std::signal(SIGABRT, handle_signal_std);
+       std::signal(SIGCHLD, SIG_IGN);
+       std::signal(SIGPIPE, SIG_IGN);
+
+       std::set_new_handler(on_new_failed);
+
+       server::run();
+}
+
+void sensord_exit(void)
+{
+       server::stop();
+}
+
+int main(int argc, char *argv[])
+{
+       _I("Started");
+
+       sensord_init();
+
+       _I("Stopped");
+
+       return 0;
+}
diff --git a/src/server/sensord.h b/src/server/sensord.h
new file mode 100644 (file)
index 0000000..e0a04b6
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * sensord
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#pragma once
+
+void sensord_init(void);
+void sensord_exit(void);