blob: 8f37c938ab9254f50ffc741ee93faee2b48ed33d [file] [log] [blame]
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +02001/* Utility functions to setup applications */
2/*
Harald Welte32e1f232011-06-26 13:07:18 +02003 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +02004 * (C) 2011 by Holger Hans Peter Freyther
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welteba6988b2011-08-17 12:46:48 +020024/*! \file application.c
25 * \brief Routines for helping with the osmocom application setup.
26 */
27
Harald Welted38c8b82011-08-30 11:32:56 +020028/*! \mainpage libosmocore Documentation
29 * \section sec_intro Introduction
30 * This library is a collection of common code used in various
31 * sub-projects inside the Osmocom family of projects. It includes a
32 * logging framework, select() loop abstraction, timers with callbacks,
33 * bit vectors, bit packing/unpacking, convolutional decoding, GSMTAP, a
34 * generic plugin interface, statistics counters, memory allocator,
35 * socket abstraction, message buffers, etc.
36 * \n\n
Harald Welte71658802017-06-12 15:40:52 +020037 * libosmocodec is developed as part of the Osmocom (Open Source Mobile
38 * Communications) project, a community-based, collaborative development
39 * project to create Free and Open Source implementations of mobile
40 * communications systems. For more information about Osmocom, please
41 * see https://osmocom.org/
42 *
Harald Welted38c8b82011-08-30 11:32:56 +020043 * Please note that C language projects inside Osmocom are typically
44 * single-threaded event-loop state machine designs. As such,
45 * routines in libosmocore are not thread-safe. If you must use them in
46 * a multi-threaded context, you have to add your own locking.
47 *
48 * \section sec_copyright Copyright and License
Harald Welte2d2e2cc2016-04-25 12:11:20 +020049 * Copyright © 2008-2016 - Harald Welte, Holger Freyther and contributors\n
Harald Welted38c8b82011-08-30 11:32:56 +020050 * All rights reserved. \n\n
51 * The source code of libosmocore is licensed under the terms of the GNU
52 * General Public License as published by the Free Software Foundation;
53 * either version 2 of the License, or (at your option) any later
54 * version.\n
55 * See <http://www.gnu.org/licenses/> or COPYING included in the source
56 * code package istelf.\n
57 * The information detailed here is provided AS IS with NO WARRANTY OF
58 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
59 * FITNESS FOR A PARTICULAR PURPOSE.
60 * \n\n
61 *
Harald Welte71658802017-06-12 15:40:52 +020062 * \section sec_tracker Homepage + Issue Tracker
63 * The libosmocore project home page can be found at
64 * https://osmocom.org/projects/libosmocore
65 *
66 * An Issue Tracker can be found at
67 * https://osmocom.org/projects/libosmocore/issues
68 *
Harald Welted38c8b82011-08-30 11:32:56 +020069 * \section sec_contact Contact and Support
70 * Community-based support is available at the OpenBSC mailing list
71 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
72 * Commercial support options available upon request from
73 * <http://sysmocom.de/>
74 */
75
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020076#include <osmocom/core/application.h>
77#include <osmocom/core/logging.h>
78
79#include <signal.h>
Harald Welte32e1f232011-06-26 13:07:18 +020080#include <stdio.h>
81#include <stdlib.h>
82#include <unistd.h>
83#include <errno.h>
84#include <sys/stat.h>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020085
86struct log_target *osmo_stderr_target;
87
Harald Welte8e878732013-03-18 19:06:13 +010088static void sighup_hdlr(int signal)
89{
90 log_targets_reopen();
91}
92
Harald Welteba6988b2011-08-17 12:46:48 +020093/*! \brief Ignore \ref SIGPIPE, \ref SIGALRM, \ref SIGHUP and \ref SIGIO */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020094void osmo_init_ignore_signals(void)
95{
96 /* Signals that by default would terminate */
Harald Weltee15ac062014-12-04 14:15:36 +010097#ifdef SIGPIPE
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020098 signal(SIGPIPE, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010099#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200100 signal(SIGALRM, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +0100101#ifdef SIGHUP
Harald Welte8e878732013-03-18 19:06:13 +0100102 signal(SIGHUP, &sighup_hdlr);
Harald Weltee15ac062014-12-04 14:15:36 +0100103#endif
104#ifdef SIGIO
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200105 signal(SIGIO, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +0100106#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200107}
108
Harald Welteba6988b2011-08-17 12:46:48 +0200109/*! \brief Initialize the osmocom logging framework
110 * \param[in] log_info Array of available logging sub-systems
111 * \returns 0 on success, -1 in case of error
112 *
113 * This function initializes the osmocom logging systems. It also
114 * creates the default (stderr) logging target.
115 */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200116int osmo_init_logging(const struct log_info *log_info)
117{
Harald Welteb43bc042011-06-27 10:29:17 +0200118 log_init(log_info, NULL);
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200119 osmo_stderr_target = log_target_create_stderr();
120 if (!osmo_stderr_target)
121 return -1;
122
123 log_add_target(osmo_stderr_target);
124 log_set_all_filter(osmo_stderr_target, 1);
125 return 0;
126}
Harald Welte32e1f232011-06-26 13:07:18 +0200127
Harald Welteba6988b2011-08-17 12:46:48 +0200128/*! \brief Turn the current process into a background daemon
129 *
130 * This function will fork the process, exit the parent and set umask,
131 * create a new session, close stdin/stdout/stderr and chdir to /tmp
132 */
Harald Welte32e1f232011-06-26 13:07:18 +0200133int osmo_daemonize(void)
134{
135 int rc;
136 pid_t pid, sid;
137
138 /* Check if parent PID == init, in which case we are already a daemon */
139 if (getppid() == 1)
Thorsten Alteholz49daf562017-03-13 01:18:49 +0100140 return 0;
Harald Welte32e1f232011-06-26 13:07:18 +0200141
142 /* Fork from the parent process */
143 pid = fork();
144 if (pid < 0) {
145 /* some error happened */
146 return pid;
147 }
148
149 if (pid > 0) {
150 /* if we have received a positive PID, then we are the parent
151 * and can exit */
152 exit(0);
153 }
154
155 /* FIXME: do we really want this? */
156 umask(0);
157
158 /* Create a new session and set process group ID */
159 sid = setsid();
160 if (sid < 0)
161 return sid;
162
163 /* Change to the /tmp directory, which prevents the CWD from being locked
164 * and unable to remove it */
165 rc = chdir("/tmp");
166 if (rc < 0)
167 return rc;
168
169 /* Redirect stdio to /dev/null */
170/* since C89/C99 says stderr is a macro, we can safely do this! */
171#ifdef stderr
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100172/*
173 * it does not make sense to check the return code here, so we just
174 * ignore the compiler warning from gcc
175 */
176#pragma GCC diagnostic push
177#pragma GCC diagnostic ignored "-Wunused-result"
Harald Welte32e1f232011-06-26 13:07:18 +0200178 freopen("/dev/null", "r", stdin);
179 freopen("/dev/null", "w", stdout);
180 freopen("/dev/null", "w", stderr);
Thorsten Alteholz18a62b02017-03-13 00:58:53 +0100181#pragma GCC diagnostic pop
Harald Welte32e1f232011-06-26 13:07:18 +0200182#endif
183
184 return 0;
185}