blob: 9c3fe52b266f7b5a9914ee58cde184b132495ce3 [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
37 * Please note that C language projects inside Osmocom are typically
38 * single-threaded event-loop state machine designs. As such,
39 * routines in libosmocore are not thread-safe. If you must use them in
40 * a multi-threaded context, you have to add your own locking.
41 *
42 * \section sec_copyright Copyright and License
43 * Copyright © 2008-2011 - Harald Welte, Holger Freyther and contributors\n
44 * All rights reserved. \n\n
45 * The source code of libosmocore is licensed under the terms of the GNU
46 * General Public License as published by the Free Software Foundation;
47 * either version 2 of the License, or (at your option) any later
48 * version.\n
49 * See <http://www.gnu.org/licenses/> or COPYING included in the source
50 * code package istelf.\n
51 * The information detailed here is provided AS IS with NO WARRANTY OF
52 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
53 * FITNESS FOR A PARTICULAR PURPOSE.
54 * \n\n
55 *
56 * \section sec_contact Contact and Support
57 * Community-based support is available at the OpenBSC mailing list
58 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
59 * Commercial support options available upon request from
60 * <http://sysmocom.de/>
61 */
62
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020063#include <osmocom/core/application.h>
64#include <osmocom/core/logging.h>
65
66#include <signal.h>
Harald Welte32e1f232011-06-26 13:07:18 +020067#include <stdio.h>
68#include <stdlib.h>
69#include <unistd.h>
70#include <errno.h>
71#include <sys/stat.h>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020072
73struct log_target *osmo_stderr_target;
74
Harald Welte8e878732013-03-18 19:06:13 +010075static void sighup_hdlr(int signal)
76{
77 log_targets_reopen();
78}
79
Harald Welteba6988b2011-08-17 12:46:48 +020080/*! \brief Ignore \ref SIGPIPE, \ref SIGALRM, \ref SIGHUP and \ref SIGIO */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020081void osmo_init_ignore_signals(void)
82{
83 /* Signals that by default would terminate */
Harald Weltee15ac062014-12-04 14:15:36 +010084#ifdef SIGPIPE
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020085 signal(SIGPIPE, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010086#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020087 signal(SIGALRM, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010088#ifdef SIGHUP
Harald Welte8e878732013-03-18 19:06:13 +010089 signal(SIGHUP, &sighup_hdlr);
Harald Weltee15ac062014-12-04 14:15:36 +010090#endif
91#ifdef SIGIO
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020092 signal(SIGIO, SIG_IGN);
Harald Weltee15ac062014-12-04 14:15:36 +010093#endif
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020094}
95
Harald Welteba6988b2011-08-17 12:46:48 +020096/*! \brief Initialize the osmocom logging framework
97 * \param[in] log_info Array of available logging sub-systems
98 * \returns 0 on success, -1 in case of error
99 *
100 * This function initializes the osmocom logging systems. It also
101 * creates the default (stderr) logging target.
102 */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200103int osmo_init_logging(const struct log_info *log_info)
104{
Harald Welteb43bc042011-06-27 10:29:17 +0200105 log_init(log_info, NULL);
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +0200106 osmo_stderr_target = log_target_create_stderr();
107 if (!osmo_stderr_target)
108 return -1;
109
110 log_add_target(osmo_stderr_target);
111 log_set_all_filter(osmo_stderr_target, 1);
112 return 0;
113}
Harald Welte32e1f232011-06-26 13:07:18 +0200114
Harald Welteba6988b2011-08-17 12:46:48 +0200115/*! \brief Turn the current process into a background daemon
116 *
117 * This function will fork the process, exit the parent and set umask,
118 * create a new session, close stdin/stdout/stderr and chdir to /tmp
119 */
Harald Welte32e1f232011-06-26 13:07:18 +0200120int osmo_daemonize(void)
121{
122 int rc;
123 pid_t pid, sid;
124
125 /* Check if parent PID == init, in which case we are already a daemon */
126 if (getppid() == 1)
127 return -EEXIST;
128
129 /* Fork from the parent process */
130 pid = fork();
131 if (pid < 0) {
132 /* some error happened */
133 return pid;
134 }
135
136 if (pid > 0) {
137 /* if we have received a positive PID, then we are the parent
138 * and can exit */
139 exit(0);
140 }
141
142 /* FIXME: do we really want this? */
143 umask(0);
144
145 /* Create a new session and set process group ID */
146 sid = setsid();
147 if (sid < 0)
148 return sid;
149
150 /* Change to the /tmp directory, which prevents the CWD from being locked
151 * and unable to remove it */
152 rc = chdir("/tmp");
153 if (rc < 0)
154 return rc;
155
156 /* Redirect stdio to /dev/null */
157/* since C89/C99 says stderr is a macro, we can safely do this! */
158#ifdef stderr
159 freopen("/dev/null", "r", stdin);
160 freopen("/dev/null", "w", stdout);
161 freopen("/dev/null", "w", stderr);
162#endif
163
164 return 0;
165}