Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file application.c |
| 2 | * Routines for helping with the osmocom application setup. */ |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 3 | /* |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 4 | * (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 5 | * (C) 2011 by Holger Hans Peter Freyther |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
Harald Welte | d38c8b8 | 2011-08-30 11:32:56 +0200 | [diff] [blame] | 23 | /*! \mainpage libosmocore Documentation |
| 24 | * \section sec_intro Introduction |
| 25 | * This library is a collection of common code used in various |
| 26 | * sub-projects inside the Osmocom family of projects. It includes a |
| 27 | * logging framework, select() loop abstraction, timers with callbacks, |
| 28 | * bit vectors, bit packing/unpacking, convolutional decoding, GSMTAP, a |
| 29 | * generic plugin interface, statistics counters, memory allocator, |
| 30 | * socket abstraction, message buffers, etc. |
| 31 | * \n\n |
Harald Welte | 7165880 | 2017-06-12 15:40:52 +0200 | [diff] [blame] | 32 | * libosmocodec is developed as part of the Osmocom (Open Source Mobile |
| 33 | * Communications) project, a community-based, collaborative development |
| 34 | * project to create Free and Open Source implementations of mobile |
| 35 | * communications systems. For more information about Osmocom, please |
| 36 | * see https://osmocom.org/ |
| 37 | * |
Harald Welte | d38c8b8 | 2011-08-30 11:32:56 +0200 | [diff] [blame] | 38 | * Please note that C language projects inside Osmocom are typically |
| 39 | * single-threaded event-loop state machine designs. As such, |
| 40 | * routines in libosmocore are not thread-safe. If you must use them in |
| 41 | * a multi-threaded context, you have to add your own locking. |
| 42 | * |
| 43 | * \section sec_copyright Copyright and License |
Neels Hofmeyr | b697df0 | 2017-09-28 19:41:50 +0200 | [diff] [blame] | 44 | * Copyright © 2008-2017 - Harald Welte, Holger Freyther and contributors\n |
Harald Welte | d38c8b8 | 2011-08-30 11:32:56 +0200 | [diff] [blame] | 45 | * All rights reserved. \n\n |
| 46 | * The source code of libosmocore is licensed under the terms of the GNU |
| 47 | * General Public License as published by the Free Software Foundation; |
| 48 | * either version 2 of the License, or (at your option) any later |
| 49 | * version.\n |
| 50 | * See <http://www.gnu.org/licenses/> or COPYING included in the source |
| 51 | * code package istelf.\n |
| 52 | * The information detailed here is provided AS IS with NO WARRANTY OF |
| 53 | * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND |
| 54 | * FITNESS FOR A PARTICULAR PURPOSE. |
| 55 | * \n\n |
| 56 | * |
Harald Welte | 7165880 | 2017-06-12 15:40:52 +0200 | [diff] [blame] | 57 | * \section sec_tracker Homepage + Issue Tracker |
| 58 | * The libosmocore project home page can be found at |
| 59 | * https://osmocom.org/projects/libosmocore |
| 60 | * |
| 61 | * An Issue Tracker can be found at |
| 62 | * https://osmocom.org/projects/libosmocore/issues |
| 63 | * |
Harald Welte | d38c8b8 | 2011-08-30 11:32:56 +0200 | [diff] [blame] | 64 | * \section sec_contact Contact and Support |
| 65 | * Community-based support is available at the OpenBSC mailing list |
| 66 | * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n |
| 67 | * Commercial support options available upon request from |
| 68 | * <http://sysmocom.de/> |
| 69 | */ |
| 70 | |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 71 | #include <osmocom/core/application.h> |
| 72 | #include <osmocom/core/logging.h> |
| 73 | |
| 74 | #include <signal.h> |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 75 | #include <stdio.h> |
| 76 | #include <stdlib.h> |
| 77 | #include <unistd.h> |
| 78 | #include <errno.h> |
| 79 | #include <sys/stat.h> |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 80 | |
| 81 | struct log_target *osmo_stderr_target; |
| 82 | |
Harald Welte | 8e87873 | 2013-03-18 19:06:13 +0100 | [diff] [blame] | 83 | static void sighup_hdlr(int signal) |
| 84 | { |
| 85 | log_targets_reopen(); |
| 86 | } |
| 87 | |
Vadim Yanitskiy | ff04a85 | 2019-04-25 02:22:52 +0700 | [diff] [blame] | 88 | /*! Ignore SIGPIPE, SIGALRM, SIGHUP and SIGIO */ |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 89 | void osmo_init_ignore_signals(void) |
| 90 | { |
| 91 | /* Signals that by default would terminate */ |
Harald Welte | e15ac06 | 2014-12-04 14:15:36 +0100 | [diff] [blame] | 92 | #ifdef SIGPIPE |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 93 | signal(SIGPIPE, SIG_IGN); |
Harald Welte | e15ac06 | 2014-12-04 14:15:36 +0100 | [diff] [blame] | 94 | #endif |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 95 | signal(SIGALRM, SIG_IGN); |
Harald Welte | e15ac06 | 2014-12-04 14:15:36 +0100 | [diff] [blame] | 96 | #ifdef SIGHUP |
Harald Welte | 8e87873 | 2013-03-18 19:06:13 +0100 | [diff] [blame] | 97 | signal(SIGHUP, &sighup_hdlr); |
Harald Welte | e15ac06 | 2014-12-04 14:15:36 +0100 | [diff] [blame] | 98 | #endif |
| 99 | #ifdef SIGIO |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 100 | signal(SIGIO, SIG_IGN); |
Harald Welte | e15ac06 | 2014-12-04 14:15:36 +0100 | [diff] [blame] | 101 | #endif |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 104 | /*! Initialize the osmocom logging framework |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 105 | * \param[in] log_info Array of available logging sub-systems |
| 106 | * \returns 0 on success, -1 in case of error |
| 107 | * |
| 108 | * This function initializes the osmocom logging systems. It also |
| 109 | * creates the default (stderr) logging target. |
| 110 | */ |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 111 | int osmo_init_logging(const struct log_info *log_info) |
| 112 | { |
Neels Hofmeyr | 3d8b47f | 2018-03-05 15:46:38 +0100 | [diff] [blame] | 113 | return osmo_init_logging2(NULL, log_info); |
| 114 | } |
| 115 | |
| 116 | int osmo_init_logging2(void *ctx, const struct log_info *log_info) |
| 117 | { |
Harald Welte | 16f989e | 2017-10-29 10:37:44 +0100 | [diff] [blame] | 118 | static int logging_initialized = 0; |
| 119 | |
| 120 | if (logging_initialized) |
| 121 | return -EEXIST; |
| 122 | |
| 123 | logging_initialized = 1; |
Neels Hofmeyr | 3d8b47f | 2018-03-05 15:46:38 +0100 | [diff] [blame] | 124 | log_init(log_info, ctx); |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 125 | osmo_stderr_target = log_target_create_stderr(); |
| 126 | if (!osmo_stderr_target) |
| 127 | return -1; |
| 128 | |
| 129 | log_add_target(osmo_stderr_target); |
| 130 | log_set_all_filter(osmo_stderr_target, 1); |
| 131 | return 0; |
| 132 | } |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 133 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 134 | /*! Turn the current process into a background daemon |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 135 | * |
| 136 | * This function will fork the process, exit the parent and set umask, |
| 137 | * create a new session, close stdin/stdout/stderr and chdir to /tmp |
| 138 | */ |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 139 | int osmo_daemonize(void) |
| 140 | { |
| 141 | int rc; |
| 142 | pid_t pid, sid; |
| 143 | |
| 144 | /* Check if parent PID == init, in which case we are already a daemon */ |
| 145 | if (getppid() == 1) |
Thorsten Alteholz | 49daf56 | 2017-03-13 01:18:49 +0100 | [diff] [blame] | 146 | return 0; |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 147 | |
| 148 | /* Fork from the parent process */ |
| 149 | pid = fork(); |
| 150 | if (pid < 0) { |
| 151 | /* some error happened */ |
| 152 | return pid; |
| 153 | } |
| 154 | |
| 155 | if (pid > 0) { |
| 156 | /* if we have received a positive PID, then we are the parent |
| 157 | * and can exit */ |
| 158 | exit(0); |
| 159 | } |
| 160 | |
| 161 | /* FIXME: do we really want this? */ |
| 162 | umask(0); |
| 163 | |
| 164 | /* Create a new session and set process group ID */ |
| 165 | sid = setsid(); |
| 166 | if (sid < 0) |
| 167 | return sid; |
| 168 | |
| 169 | /* Change to the /tmp directory, which prevents the CWD from being locked |
| 170 | * and unable to remove it */ |
| 171 | rc = chdir("/tmp"); |
| 172 | if (rc < 0) |
| 173 | return rc; |
| 174 | |
| 175 | /* Redirect stdio to /dev/null */ |
| 176 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
| 177 | #ifdef stderr |
Thorsten Alteholz | 18a62b0 | 2017-03-13 00:58:53 +0100 | [diff] [blame] | 178 | /* |
| 179 | * it does not make sense to check the return code here, so we just |
| 180 | * ignore the compiler warning from gcc |
| 181 | */ |
| 182 | #pragma GCC diagnostic push |
| 183 | #pragma GCC diagnostic ignored "-Wunused-result" |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 184 | freopen("/dev/null", "r", stdin); |
| 185 | freopen("/dev/null", "w", stdout); |
| 186 | freopen("/dev/null", "w", stderr); |
Thorsten Alteholz | 18a62b0 | 2017-03-13 00:58:53 +0100 | [diff] [blame] | 187 | #pragma GCC diagnostic pop |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 188 | #endif |
| 189 | |
| 190 | return 0; |
| 191 | } |