From 63b5c10f310059320609bc63843e0b47e0534595 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 17 Oct 2016 10:53:38 +0300 Subject: [PATCH] Move daemonize() to gateway.c The one remaining function in gw_utils.c was moved to gateway.c, so gw_utils.c could be removed. --- include/maxscale/gw.h | 2 -- server/core/CMakeLists.txt | 2 +- server/core/gateway.c | 38 +++++++++++++++++++- server/core/gw_utils.c | 72 -------------------------------------- 4 files changed, 38 insertions(+), 76 deletions(-) delete mode 100644 server/core/gw_utils.c diff --git a/include/maxscale/gw.h b/include/maxscale/gw.h index 22f4b5529..3bcd8763f 100644 --- a/include/maxscale/gw.h +++ b/include/maxscale/gw.h @@ -38,8 +38,6 @@ MXS_BEGIN_DECLS -bool gw_daemonize(void); - MXS_END_DECLS #endif diff --git a/server/core/CMakeLists.txt b/server/core/CMakeLists.txt index 11a616a62..3c7c630b8 100644 --- a/server/core/CMakeLists.txt +++ b/server/core/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(maxscale-common SHARED adminusers.c alloc.c authenticator.c atomic.c buffer.c config.c dbusers.c dcb.c filter.c externcmd.c gwbitmask.c gwdirs.c gw_utils.c hashtable.c hint.c housekeeper.c listmanager.c load_utils.c log_manager.cc maxscale_pcre2.c memlog.c misc.c mlist.c modutil.c monitor.c queuemanager.c query_classifier.c poll.c random_jkiss.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c skygw_utils.cc statistics.c listener.c gw_ssl.c mysql_utils.c mysql_binlog.c) +add_library(maxscale-common SHARED adminusers.c alloc.c authenticator.c atomic.c buffer.c config.c dbusers.c dcb.c filter.c externcmd.c gwbitmask.c gwdirs.c hashtable.c hint.c housekeeper.c listmanager.c load_utils.c log_manager.cc maxscale_pcre2.c memlog.c misc.c mlist.c modutil.c monitor.c queuemanager.c query_classifier.c poll.c random_jkiss.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c skygw_utils.cc statistics.c listener.c gw_ssl.c mysql_utils.c mysql_binlog.c) target_link_libraries(maxscale-common ${MARIADB_CONNECTOR_LIBRARIES} ${LZMA_LINK_FLAGS} ${PCRE2_LIBRARIES} ${CURL_LIBRARIES} ssl pthread crypt dl crypto inih z rt m stdc++) diff --git a/server/core/gateway.c b/server/core/gateway.c index 057fdd1b6..64688565f 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -190,6 +190,7 @@ void write_child_exit_code(int fd, int code); static bool change_cwd(); void shutdown_server(); static void log_exit_status(); +static bool daemonize(); /** SSL multi-threading functions and structures */ @@ -1578,7 +1579,7 @@ int main(int argc, char **argv) /** Daemonize the process and wait for the child process to notify * the parent process of its exit status. */ - parent_process = gw_daemonize(); + parent_process = daemonize(); if (parent_process) { @@ -2663,3 +2664,38 @@ static void log_exit_status() break; } } + +/** + * Daemonize the process by forking and putting the process into the + * background. + * + * @return True if context is that of the parent process, false if that of the + * child process. + */ +static bool daemonize(void) +{ + pid_t pid; + + pid = fork(); + + if (pid < 0) + { + char errbuf[MXS_STRERROR_BUFLEN]; + fprintf(stderr, "fork() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf))); + exit(1); + } + + if (pid != 0) + { + /* exit from main */ + return true; + } + + if (setsid() < 0) + { + char errbuf[MXS_STRERROR_BUFLEN]; + fprintf(stderr, "setsid() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf))); + exit(1); + } + return false; +} diff --git a/server/core/gw_utils.c b/server/core/gw_utils.c deleted file mode 100644 index 60cc9bed5..000000000 --- a/server/core/gw_utils.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2016 MariaDB Corporation Ab - * - * Use of this software is governed by the Business Source License included - * in the LICENSE.TXT file and at www.mariadb.com/bsl. - * - * Change Date: 2019-07-01 - * - * On the date above, in accordance with the Business Source License, use - * of this software will be governed by version 2 or later of the General - * Public License. - */ - -/** - * @file gw_utils.c - A set if utility functions useful within the context - * of the gateway. - * - * @verbatim - * Revision History - * - * Date Who Description - * 03-06-2013 Massimiliano Pinto gateway utils - * 12-06-2013 Massimiliano Pinto gw_read_gwbuff - * with error detection - * and its handling - * 01-07-2013 Massimiliano Pinto Removed session->backends - * from gw_read_gwbuff() - * 25-09-2013 Massimiliano Pinto setipaddress uses getaddrinfo - * 06-02-2014 Mark Riddoch Added parse_bindconfig - * 10-02-2014 Massimiliano Pinto Added return code to setipaddress - * 02-09-2014 Martin Brampton Replace C++ comment with C comment - * 02-03-2016 Martin Brampton Remove default from parse_bindconfig - * - *@endverbatim - */ - -#include -#include -#include -#include - -/** - * Daemonize the process by forking and putting the process into the - * background. - */ -bool gw_daemonize(void) -{ - pid_t pid; - - pid = fork(); - - if (pid < 0) - { - char errbuf[MXS_STRERROR_BUFLEN]; - fprintf(stderr, "fork() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf))); - exit(1); - } - - if (pid != 0) - { - /* exit from main */ - return true; - } - - if (setsid() < 0) - { - char errbuf[MXS_STRERROR_BUFLEN]; - fprintf(stderr, "setsid() error %s\n", strerror_r(errno, errbuf, sizeof(errbuf))); - exit(1); - } - return false; -}