blob: f7e5816f30c478e6f2d41167f0dfc238d625c160 [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 *
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020021 */
22
Harald Welted38c8b82011-08-30 11:32:56 +020023/*! \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 Welte71658802017-06-12 15:40:52 +020032 * 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 Welted38c8b82011-08-30 11:32:56 +020038 * 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 Hofmeyrb697df02017-09-28 19:41:50 +020044 * Copyright © 2008-2017 - Harald Welte, Holger Freyther and contributors\n
Harald Welted38c8b82011-08-30 11:32:56 +020045 * 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 Welte71658802017-06-12 15:40:52 +020057 * \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 Welted38c8b82011-08-30 11:32:56 +020064 * \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 Freytherba01fa42011-05-12 13:46:33 +020071#include <osmocom/core/application.h>
72#include <osmocom/core/logging.h>
73
74#include <signal.h>
Harald Welte32e1f232011-06-26 13:07:18 +020075#include <stdio.h>
76#include <stdlib.h>
77#include <unistd.h>
78#include <errno.h>
79#include <sys/stat.h>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020080
81struct log_target *osmo_stderr_target;
82
Harald Welte8e878732013-03-18 19:06:13 +010083static void sighup_hdlr(int signal)
84{
85 log_targets_reopen();
86}
87
Vadim Yanitskiyff04a852019-04-25 02:22:52 +070088/*! Ignore SIGPIPE, SIGALRM, SIGHUP and SIGIO */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020089void osmo_init_ignore_signals(void)
90{
91 /* Signals that by default would terminate */
Harald Weltee15ac062014-12-04 14:15:36 +010092#ifdef SIGPIPE
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020093 signal(SIGPIPE, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010094#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020095 signal(SIGALRM, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010096#ifdef SIGHUP
Harald Welte8e878732013-03-18 19:06:13 +010097 signal(SIGHUP, &sighup_hdlr);
Harald Weltee15ac062014-12-04 14:15:36 +010098#endif
99#ifdef SIGIO
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200100 signal(SIGIO, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +0100101#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200102}
103
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200104/*! Initialize the osmocom logging framework
Harald Welteba6988b2011-08-17 12:46:48 +0200105 * \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 Freytherba01fa42011-05-12 13:46:33 +0200111int osmo_init_logging(const struct log_info *log_info)
112{
Neels Hofmeyr3d8b47f2018-03-05 15:46:38 +0100113 return osmo_init_logging2(NULL, log_info);
114}
115
116int osmo_init_logging2(void *ctx, const struct log_info *log_info)
117{
Harald Welte16f989e2017-10-29 10:37:44 +0100118 static int logging_initialized = 0;
119
120 if (logging_initialized)
121 return -EEXIST;
122
123 logging_initialized = 1;
Neels Hofmeyr3d8b47f2018-03-05 15:46:38 +0100124 log_init(log_info, ctx);
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200125 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 Welte32e1f232011-06-26 13:07:18 +0200133
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200134/*! Turn the current process into a background daemon
Harald Welteba6988b2011-08-17 12:46:48 +0200135 *
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 Welte32e1f232011-06-26 13:07:18 +0200139int 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 Alteholz49daf562017-03-13 01:18:49 +0100146 return 0;
Harald Welte32e1f232011-06-26 13:07:18 +0200147
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 Alteholz18a62b02017-03-13 00:58:53 +0100178/*
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 Welte32e1f232011-06-26 13:07:18 +0200184 freopen("/dev/null", "r", stdin);
185 freopen("/dev/null", "w", stdout);
186 freopen("/dev/null", "w", stderr);
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100187#pragma GCC diagnostic pop
Harald Welte32e1f232011-06-26 13:07:18 +0200188#endif
189
190 return 0;
191}