blob: 538ac45d98bc88953fee29e9d22848fa04f97798 [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 Welte16f989e2017-10-29 10:37:44 +0100115 static int logging_initialized = 0;
116
117 if (logging_initialized)
118 return -EEXIST;
119
120 logging_initialized = 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200121 log_init(log_info, NULL);
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200122 osmo_stderr_target = log_target_create_stderr();
123 if (!osmo_stderr_target)
124 return -1;
125
126 log_add_target(osmo_stderr_target);
127 log_set_all_filter(osmo_stderr_target, 1);
128 return 0;
129}
Harald Welte32e1f232011-06-26 13:07:18 +0200130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! Turn the current process into a background daemon
Harald Welteba6988b2011-08-17 12:46:48 +0200132 *
133 * This function will fork the process, exit the parent and set umask,
134 * create a new session, close stdin/stdout/stderr and chdir to /tmp
135 */
Harald Welte32e1f232011-06-26 13:07:18 +0200136int osmo_daemonize(void)
137{
138 int rc;
139 pid_t pid, sid;
140
141 /* Check if parent PID == init, in which case we are already a daemon */
142 if (getppid() == 1)
Thorsten Alteholz49daf562017-03-13 01:18:49 +0100143 return 0;
Harald Welte32e1f232011-06-26 13:07:18 +0200144
145 /* Fork from the parent process */
146 pid = fork();
147 if (pid < 0) {
148 /* some error happened */
149 return pid;
150 }
151
152 if (pid > 0) {
153 /* if we have received a positive PID, then we are the parent
154 * and can exit */
155 exit(0);
156 }
157
158 /* FIXME: do we really want this? */
159 umask(0);
160
161 /* Create a new session and set process group ID */
162 sid = setsid();
163 if (sid < 0)
164 return sid;
165
166 /* Change to the /tmp directory, which prevents the CWD from being locked
167 * and unable to remove it */
168 rc = chdir("/tmp");
169 if (rc < 0)
170 return rc;
171
172 /* Redirect stdio to /dev/null */
173/* since C89/C99 says stderr is a macro, we can safely do this! */
174#ifdef stderr
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100175/*
176 * it does not make sense to check the return code here, so we just
177 * ignore the compiler warning from gcc
178 */
179#pragma GCC diagnostic push
180#pragma GCC diagnostic ignored "-Wunused-result"
Harald Welte32e1f232011-06-26 13:07:18 +0200181 freopen("/dev/null", "r", stdin);
182 freopen("/dev/null", "w", stdout);
183 freopen("/dev/null", "w", stderr);
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100184#pragma GCC diagnostic pop
Harald Welte32e1f232011-06-26 13:07:18 +0200185#endif
186
187 return 0;
188}