From: Dongwoo Lee Date: Thu, 18 Aug 2022 11:09:54 +0000 (+0900) Subject: monitor: Restructure request server X-Git-Tag: submit/sandbox/chanwoochoi/tizen/20220823.070906~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c7f7b84cf86cefc36cde68379dd042b03e4d187f;p=platform%2Fcore%2Fsystem%2Fpass.git monitor: Restructure request server In order to make all initialization about resource monitor handled in monitor module, convert request-server and request-handler from the distinguish module to a part of monitor module. Change-Id: I820147add2b44c40fb0ce1d8e9223c708037adc1 Signed-off-by: Dongwoo Lee --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 81bcff5..4ea606a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,6 @@ SET(SRCS src/monitor/monitor.c src/monitor/monitor-thread.c src/monitor/monitor-command.c - src/monitor/request-server.c src/monitor/request-handler.c ) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 08e9e21..38b4e62 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -52,4 +52,7 @@ void monitor_command_unbind_resource(struct monitor_command *cmd); int monitor_command_init(struct monitor_command **cmd); void monitor_command_exit(struct monitor_command *cmd); +int request_server_init(void); +void request_server_exit(void); + #endif diff --git a/include/util/request.h b/include/monitor/request.h similarity index 100% rename from include/util/request.h rename to include/monitor/request.h diff --git a/include/util/request-handler.h b/include/util/request-handler.h deleted file mode 100644 index 68355ce..0000000 --- a/include/util/request-handler.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * PASS (Power Aware System Service) - * - * Copyright (c) 2022 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. - */ -#ifndef __REQUEST_HANDLER_H__ -#define __REQUEST_HANDLER_H__ - -#include -#include - -#include - -struct request_client { - int socket_fd; - int nr_resources; - struct thread *worker; - GHashTable *resource_table; -}; - -int create_request_client(int socket_fd); - -#endif /* __REQUEST_HANDLER_H__ */ diff --git a/lib/resource-monitor/resource-monitor.c b/lib/resource-monitor/resource-monitor.c index b36006a..d22540e 100644 --- a/lib/resource-monitor/resource-monitor.c +++ b/lib/resource-monitor/resource-monitor.c @@ -21,7 +21,7 @@ #define _GNU_SOURCE #include #include -#include +#include #include #include diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c index 50f602c..3e1787f 100644 --- a/src/monitor/monitor.c +++ b/src/monitor/monitor.c @@ -34,9 +34,16 @@ static int monitor_setup(void *data, void *user_data) { int ret; + ret = request_server_init(); + if (ret < 0) { + _E("failed to initialize request server\n"); + return ret; + } + ret = monitor_thread_init(&g_monitor); if (ret < 0) { _E("failed to initialize monitor thread\n"); + request_server_exit(); return ret; } @@ -51,6 +58,7 @@ static void monitor_init(void *data) static void monitor_exit(void *data) { monitor_thread_exit(&g_monitor); + request_server_exit(); unregister_notifier(DEVICE_NOTIFIER_INIT_DONE, monitor_setup, NULL); } diff --git a/src/monitor/request-handler.c b/src/monitor/request-handler.c index c81bce4..068d950 100644 --- a/src/monitor/request-handler.c +++ b/src/monitor/request-handler.c @@ -17,7 +17,7 @@ */ /** - * @file request-handler-thread.c + * @file request-handler.c * @brief TBD * @ingroup TBD */ @@ -28,10 +28,8 @@ #include #include #include -#include -#include -#include -#include +#include +#include #include #include @@ -41,6 +39,19 @@ #include #include +#define PENDING_MAX 3 +#define REQUEST_SERVER_PORT 10001 + +struct request_client { + int socket_fd; + int nr_resources; + struct thread *worker; + GHashTable *resource_table; +}; + +static bool g_request_server_run; +static struct thread *g_server_thread; + static void update_resource(gpointer key, gpointer value, gpointer user_data) { struct resource *res = value; @@ -1003,7 +1014,7 @@ static int request_handler_func(void *data, void **result) return THREAD_RETURN_DONE; } -int create_request_client(int socket_fd) +static int create_request_client(int socket_fd) { struct request_client *client; @@ -1024,3 +1035,98 @@ int create_request_client(int socket_fd) return 0; } +static int request_server_func(void *ctx, void **result) +{ + struct sockaddr_in address; + struct timeval wait; + int opt = true; + int server_socket; + int addrlen; + int ret; + fd_set fds; + + if (!g_request_server_run) + return THREAD_RETURN_DONE; + + init_resource_id(); + + /* 0. initialize server socket */ + server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket < 0) { + _E("Failed to initialize socket"); + goto error_out; + } + + if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, + (char *)&opt, sizeof(opt)) < 0) { + _E("Failed to setsockopt"); + goto error_out_close; + } + + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(REQUEST_SERVER_PORT); + + if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) { + _E("Failed to bind"); + goto error_out_close; + } + + if (listen(server_socket, PENDING_MAX) < 0) { + _E("Failed to begin listenning"); + goto error_out_close; + } + + addrlen = sizeof(address); + + while (g_request_server_run) { + FD_ZERO(&fds); + FD_SET(server_socket, &fds); + + wait.tv_sec = 1; + wait.tv_usec = 0; + + ret = select(FD_SETSIZE, &fds, NULL, NULL, &wait); + if (ret < 0) { + _E("failed to select"); + goto error_out_close; + } + + if (FD_ISSET(server_socket, &fds)) { + int new_socket = accept(server_socket, (struct sockaddr *)&address, + (socklen_t *)&addrlen); + + if (new_socket < 0) { + _E("Failed to accept"); + goto error_out_close; + } + + create_request_client(new_socket); + } + } + + close(server_socket); + + return THREAD_RETURN_DONE; + +error_out_close: + close(server_socket); +error_out: + + return THREAD_RETURN_ERROR; +} + +int request_server_init(void) +{ + g_request_server_run = true; + if (create_daemon_thread(&g_server_thread, request_server_func, NULL)) + _E("Failed to create request_server_thread "); + + return 0; +} + +void request_server_exit(void) +{ + g_request_server_run = false; + destroy_thread(g_server_thread); +} diff --git a/src/monitor/request-server.c b/src/monitor/request-server.c deleted file mode 100644 index 8a221c5..0000000 --- a/src/monitor/request-server.c +++ /dev/null @@ -1,174 +0,0 @@ -/* - * PASS (Power Aware System Service) - * - * Copyright (c) 2022 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. - */ - -/** - * @file request-server.c - * @brief TBD - * @ingroup TBD - */ - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#define PENDING_MAX 3 -#define REQUEST_SERVER_PORT 10001 - -static bool g_request_server_run; - -static int request_server_func(void *ctx, void **result) -{ - struct sockaddr_in address; - struct timeval wait; - int opt = true; - int server_socket; - int addrlen; - int ret; - fd_set fds; - - if (!g_request_server_run) - return THREAD_RETURN_DONE; - - init_resource_id(); - - /* 0. initialize server socket */ - server_socket = socket(AF_INET, SOCK_STREAM, 0); - if (server_socket < 0) { - _E("Failed to initialize socket"); - goto error_out; - } - - if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, - (char *)&opt, sizeof(opt)) < 0) { - _E("Failed to setsockopt"); - goto error_out_close; - } - - address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = htons(REQUEST_SERVER_PORT); - - if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) { - _E("Failed to bind"); - goto error_out_close; - } - - if (listen(server_socket, PENDING_MAX) < 0) { - _E("Failed to begin listenning"); - goto error_out_close; - } - - addrlen = sizeof(address); - - while (g_request_server_run) { - FD_ZERO(&fds); - FD_SET(server_socket, &fds); - - wait.tv_sec = 1; - wait.tv_usec = 0; - - ret = select(FD_SETSIZE, &fds, NULL, NULL, &wait); - if (ret < 0) { - _E("failed to select"); - goto error_out_close; - } - - if (FD_ISSET(server_socket, &fds)) { - int new_socket = accept(server_socket, (struct sockaddr *)&address, - (socklen_t *)&addrlen); - - if (new_socket < 0) { - _E("Failed to accept"); - goto error_out_close; - } - - create_request_client(new_socket); - } - } - - close(server_socket); - - return THREAD_RETURN_DONE; - -error_out_close: - close(server_socket); -error_out: - - return THREAD_RETURN_ERROR; -} - -static struct thread *server_thread; - -static int request_server_init_done(void *data, void *user_data) -{ - g_request_server_run = true; - if (create_daemon_thread(&server_thread, request_server_func, NULL)) - _E("Failed to create request_server_thread "); - - return 0; -} - -static void request_server_init(void *data) -{ - /* nothing to do */ -} - -static void request_server_exit(void *data) -{ - unregister_notifier(DEVICE_NOTIFIER_INIT_DONE, request_server_init_done, NULL); - g_request_server_run = false; - destroy_thread(server_thread); -} - -static int request_server_probe(void *data) -{ - int ret = 0; - - ret = register_notifier(DEVICE_NOTIFIER_INIT_DONE, - request_server_init_done, NULL); - if (ret < 0) { - _E("Failed to register a callback function"); - return ret; - } - - return 0; -} - -static const struct device_ops request_server_device_ops = { - .name = "request-server", - .probe = request_server_probe, - .init = request_server_init, - .exit = request_server_exit, -}; - -DEVICE_OPS_REGISTER(&request_server_device_ops);