blob: d912eb72d609922eae0625c599e3bbe0fbe19bfa [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file application.c
2 * Routines for helping with the osmocom application setup. */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +02003/*
Harald Welte32e1f232011-06-26 13:07:18 +02004 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +02005 * (C) 2011 by Holger Hans Peter Freyther
6 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020011 * 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 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26
Harald Welted38c8b82011-08-30 11:32:56 +020027/*! \mainpage libosmocore Documentation
28 * \section sec_intro Introduction
29 * This library is a collection of common code used in various
30 * sub-projects inside the Osmocom family of projects. It includes a
31 * logging framework, select() loop abstraction, timers with callbacks,
32 * bit vectors, bit packing/unpacking, convolutional decoding, GSMTAP, a
33 * generic plugin interface, statistics counters, memory allocator,
34 * socket abstraction, message buffers, etc.
35 * \n\n
Harald Welte71658802017-06-12 15:40:52 +020036 * libosmocodec is developed as part of the Osmocom (Open Source Mobile
37 * Communications) project, a community-based, collaborative development
38 * project to create Free and Open Source implementations of mobile
39 * communications systems. For more information about Osmocom, please
40 * see https://osmocom.org/
41 *
Harald Welted38c8b82011-08-30 11:32:56 +020042 * Please note that C language projects inside Osmocom are typically
43 * single-threaded event-loop state machine designs. As such,
44 * routines in libosmocore are not thread-safe. If you must use them in
45 * a multi-threaded context, you have to add your own locking.
46 *
47 * \section sec_copyright Copyright and License
Neels Hofmeyrb697df02017-09-28 19:41:50 +020048 * Copyright © 2008-2017 - Harald Welte, Holger Freyther and contributors\n
Harald Welted38c8b82011-08-30 11:32:56 +020049 * All rights reserved. \n\n
50 * The source code of libosmocore is licensed under the terms of the GNU
51 * General Public License as published by the Free Software Foundation;
52 * either version 2 of the License, or (at your option) any later
53 * version.\n
54 * See <http://www.gnu.org/licenses/> or COPYING included in the source
55 * code package istelf.\n
56 * The information detailed here is provided AS IS with NO WARRANTY OF
57 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
58 * FITNESS FOR A PARTICULAR PURPOSE.
59 * \n\n
60 *
Harald Welte71658802017-06-12 15:40:52 +020061 * \section sec_tracker Homepage + Issue Tracker
62 * The libosmocore project home page can be found at
63 * https://osmocom.org/projects/libosmocore
64 *
65 * An Issue Tracker can be found at
66 * https://osmocom.org/projects/libosmocore/issues
67 *
Harald Welted38c8b82011-08-30 11:32:56 +020068 * \section sec_contact Contact and Support
69 * Community-based support is available at the OpenBSC mailing list
70 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
71 * Commercial support options available upon request from
72 * <http://sysmocom.de/>
73 */
74
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020075#include <osmocom/core/application.h>
76#include <osmocom/core/logging.h>
77
78#include <signal.h>
Harald Welte32e1f232011-06-26 13:07:18 +020079#include <stdio.h>
80#include <stdlib.h>
81#include <unistd.h>
82#include <errno.h>
83#include <sys/stat.h>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020084
85struct log_target *osmo_stderr_target;
86
Harald Welte8e878732013-03-18 19:06:13 +010087static void sighup_hdlr(int signal)
88{
89 log_targets_reopen();
90}
91
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092/*! Ignore \ref SIGPIPE, \ref SIGALRM, \ref SIGHUP and \ref SIGIO */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020093void osmo_init_ignore_signals(void)
94{
95 /* Signals that by default would terminate */
Harald Weltee15ac062014-12-04 14:15:36 +010096#ifdef SIGPIPE
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020097 signal(SIGPIPE, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010098#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020099 signal(SIGALRM, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +0100100#ifdef SIGHUP
Harald Welte8e878732013-03-18 19:06:13 +0100101 signal(SIGHUP, &sighup_hdlr);
Harald Weltee15ac062014-12-04 14:15:36 +0100102#endif
103#ifdef SIGIO
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200104 signal(SIGIO, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +0100105#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200106}
107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108/*! Initialize the osmocom logging framework
Harald Welteba6988b2011-08-17 12:46:48 +0200109 * \param[in] log_info Array of available logging sub-systems
110 * \returns 0 on success, -1 in case of error
111 *
112 * This function initializes the osmocom logging systems. It also
113 * creates the default (stderr) logging target.
114 */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200115int osmo_init_logging(const struct log_info *log_info)
116{
Neels Hofmeyr3d8b47f2018-03-05 15:46:38 +0100117 return osmo_init_logging2(NULL, log_info);
118}
119
120int osmo_init_logging2(void *ctx, const struct log_info *log_info)
121{
Harald Welte16f989e2017-10-29 10:37:44 +0100122 static int logging_initialized = 0;
123
124 if (logging_initialized)
125 return -EEXIST;
126
127 logging_initialized = 1;
Neels Hofmeyr3d8b47f2018-03-05 15:46:38 +0100128 log_init(log_info, ctx);
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200129 osmo_stderr_target = log_target_create_stderr();
130 if (!osmo_stderr_target)
131 return -1;
132
133 log_add_target(osmo_stderr_target);
134 log_set_all_filter(osmo_stderr_target, 1);
135 return 0;
136}
Harald Welte32e1f232011-06-26 13:07:18 +0200137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138/*! Turn the current process into a background daemon
Harald Welteba6988b2011-08-17 12:46:48 +0200139 *
140 * This function will fork the process, exit the parent and set umask,
141 * create a new session, close stdin/stdout/stderr and chdir to /tmp
142 */
Harald Welte32e1f232011-06-26 13:07:18 +0200143int osmo_daemonize(void)
144{
145 int rc;
146 pid_t pid, sid;
147
148 /* Check if parent PID == init, in which case we are already a daemon */
149 if (getppid() == 1)
Thorsten Alteholz49daf562017-03-13 01:18:49 +0100150 return 0;
Harald Welte32e1f232011-06-26 13:07:18 +0200151
152 /* Fork from the parent process */
153 pid = fork();
154 if (pid < 0) {
155 /* some error happened */
156 return pid;
157 }
158
159 if (pid > 0) {
160 /* if we have received a positive PID, then we are the parent
161 * and can exit */
162 exit(0);
163 }
164
165 /* FIXME: do we really want this? */
166 umask(0);
167
168 /* Create a new session and set process group ID */
169 sid = setsid();
170 if (sid < 0)
171 return sid;
172
173 /* Change to the /tmp directory, which prevents the CWD from being locked
174 * and unable to remove it */
175 rc = chdir("/tmp");
176 if (rc < 0)
177 return rc;
178
179 /* Redirect stdio to /dev/null */
180/* since C89/C99 says stderr is a macro, we can safely do this! */
181#ifdef stderr
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100182/*
183 * it does not make sense to check the return code here, so we just
184 * ignore the compiler warning from gcc
185 */
186#pragma GCC diagnostic push
187#pragma GCC diagnostic ignored "-Wunused-result"
Harald Welte32e1f232011-06-26 13:07:18 +0200188 freopen("/dev/null", "r", stdin);
189 freopen("/dev/null", "w", stdout);
190 freopen("/dev/null", "w", stderr);
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100191#pragma GCC diagnostic pop
Harald Welte32e1f232011-06-26 13:07:18 +0200192#endif
193
194 return 0;
195}