blob: ee2263917b2fce4621e9550eaec76aa24d326b32 [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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
Harald Welted38c8b82011-08-30 11:32:56 +020025/*! \mainpage libosmocore Documentation
26 * \section sec_intro Introduction
27 * This library is a collection of common code used in various
28 * sub-projects inside the Osmocom family of projects. It includes a
29 * logging framework, select() loop abstraction, timers with callbacks,
30 * bit vectors, bit packing/unpacking, convolutional decoding, GSMTAP, a
31 * generic plugin interface, statistics counters, memory allocator,
32 * socket abstraction, message buffers, etc.
33 * \n\n
Harald Welte71658802017-06-12 15:40:52 +020034 * libosmocodec is developed as part of the Osmocom (Open Source Mobile
35 * Communications) project, a community-based, collaborative development
36 * project to create Free and Open Source implementations of mobile
37 * communications systems. For more information about Osmocom, please
38 * see https://osmocom.org/
39 *
Harald Welted38c8b82011-08-30 11:32:56 +020040 * Please note that C language projects inside Osmocom are typically
41 * single-threaded event-loop state machine designs. As such,
42 * routines in libosmocore are not thread-safe. If you must use them in
43 * a multi-threaded context, you have to add your own locking.
44 *
45 * \section sec_copyright Copyright and License
Neels Hofmeyrb697df02017-09-28 19:41:50 +020046 * Copyright © 2008-2017 - Harald Welte, Holger Freyther and contributors\n
Harald Welted38c8b82011-08-30 11:32:56 +020047 * All rights reserved. \n\n
48 * The source code of libosmocore is licensed under the terms of the GNU
49 * General Public License as published by the Free Software Foundation;
50 * either version 2 of the License, or (at your option) any later
51 * version.\n
52 * See <http://www.gnu.org/licenses/> or COPYING included in the source
53 * code package istelf.\n
54 * The information detailed here is provided AS IS with NO WARRANTY OF
55 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
56 * FITNESS FOR A PARTICULAR PURPOSE.
57 * \n\n
58 *
Harald Welte71658802017-06-12 15:40:52 +020059 * \section sec_tracker Homepage + Issue Tracker
60 * The libosmocore project home page can be found at
61 * https://osmocom.org/projects/libosmocore
62 *
63 * An Issue Tracker can be found at
64 * https://osmocom.org/projects/libosmocore/issues
65 *
Harald Welted38c8b82011-08-30 11:32:56 +020066 * \section sec_contact Contact and Support
67 * Community-based support is available at the OpenBSC mailing list
68 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
69 * Commercial support options available upon request from
70 * <http://sysmocom.de/>
71 */
72
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020073#include <osmocom/core/application.h>
74#include <osmocom/core/logging.h>
75
76#include <signal.h>
Harald Welte32e1f232011-06-26 13:07:18 +020077#include <stdio.h>
78#include <stdlib.h>
79#include <unistd.h>
80#include <errno.h>
81#include <sys/stat.h>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020082
83struct log_target *osmo_stderr_target;
84
Harald Welte8e878732013-03-18 19:06:13 +010085static void sighup_hdlr(int signal)
86{
87 log_targets_reopen();
88}
89
Neels Hofmeyr87e45502017-06-20 00:17:59 +020090/*! Ignore \ref SIGPIPE, \ref SIGALRM, \ref SIGHUP and \ref SIGIO */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020091void osmo_init_ignore_signals(void)
92{
93 /* Signals that by default would terminate */
Harald Weltee15ac062014-12-04 14:15:36 +010094#ifdef SIGPIPE
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020095 signal(SIGPIPE, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010096#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020097 signal(SIGALRM, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010098#ifdef SIGHUP
Harald Welte8e878732013-03-18 19:06:13 +010099 signal(SIGHUP, &sighup_hdlr);
Harald Weltee15ac062014-12-04 14:15:36 +0100100#endif
101#ifdef SIGIO
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200102 signal(SIGIO, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +0100103#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200104}
105
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106/*! Initialize the osmocom logging framework
Harald Welteba6988b2011-08-17 12:46:48 +0200107 * \param[in] log_info Array of available logging sub-systems
108 * \returns 0 on success, -1 in case of error
109 *
110 * This function initializes the osmocom logging systems. It also
111 * creates the default (stderr) logging target.
112 */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200113int osmo_init_logging(const struct log_info *log_info)
114{
Harald Welteb43bc042011-06-27 10:29:17 +0200115 log_init(log_info, NULL);
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200116 osmo_stderr_target = log_target_create_stderr();
117 if (!osmo_stderr_target)
118 return -1;
119
120 log_add_target(osmo_stderr_target);
121 log_set_all_filter(osmo_stderr_target, 1);
122 return 0;
123}
Harald Welte32e1f232011-06-26 13:07:18 +0200124
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200125/*! Turn the current process into a background daemon
Harald Welteba6988b2011-08-17 12:46:48 +0200126 *
127 * This function will fork the process, exit the parent and set umask,
128 * create a new session, close stdin/stdout/stderr and chdir to /tmp
129 */
Harald Welte32e1f232011-06-26 13:07:18 +0200130int osmo_daemonize(void)
131{
132 int rc;
133 pid_t pid, sid;
134
135 /* Check if parent PID == init, in which case we are already a daemon */
136 if (getppid() == 1)
Thorsten Alteholz49daf562017-03-13 01:18:49 +0100137 return 0;
Harald Welte32e1f232011-06-26 13:07:18 +0200138
139 /* Fork from the parent process */
140 pid = fork();
141 if (pid < 0) {
142 /* some error happened */
143 return pid;
144 }
145
146 if (pid > 0) {
147 /* if we have received a positive PID, then we are the parent
148 * and can exit */
149 exit(0);
150 }
151
152 /* FIXME: do we really want this? */
153 umask(0);
154
155 /* Create a new session and set process group ID */
156 sid = setsid();
157 if (sid < 0)
158 return sid;
159
160 /* Change to the /tmp directory, which prevents the CWD from being locked
161 * and unable to remove it */
162 rc = chdir("/tmp");
163 if (rc < 0)
164 return rc;
165
166 /* Redirect stdio to /dev/null */
167/* since C89/C99 says stderr is a macro, we can safely do this! */
168#ifdef stderr
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100169/*
170 * it does not make sense to check the return code here, so we just
171 * ignore the compiler warning from gcc
172 */
173#pragma GCC diagnostic push
174#pragma GCC diagnostic ignored "-Wunused-result"
Harald Welte32e1f232011-06-26 13:07:18 +0200175 freopen("/dev/null", "r", stdin);
176 freopen("/dev/null", "w", stdout);
177 freopen("/dev/null", "w", stderr);
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100178#pragma GCC diagnostic pop
Harald Welte32e1f232011-06-26 13:07:18 +0200179#endif
180
181 return 0;
182}