From: SangYoun Kwak Date: Thu, 29 May 2025 10:58:37 +0000 (+0900) Subject: Refactor main function to use separated init/exit function X-Git-Tag: accepted/tizen/unified/20250612.143600~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8a07c6fb6f274211f1aec32de90a7352c4d96c58;p=platform%2Fcore%2Fsystem%2Fsensord.git Refactor main function to use separated init/exit function 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 --- diff --git a/src/server/main.cpp b/src/server/main.cpp deleted file mode 100644 index c4ec0488..00000000 --- a/src/server/main.cpp +++ /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 -#include - -#include - -#include -#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 index 00000000..694d5570 --- /dev/null +++ b/src/server/sensord.cpp @@ -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 +#include + +#include + +#include +#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 index 00000000..e0a04b6f --- /dev/null +++ b/src/server/sensord.h @@ -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);