add gsm48_decode_lai() to map file
[osmocom-bb.git] / src / application.c
blobe0d989e5b6989e91c22a0610f95247a4aa74009f
1 /* Utility functions to setup applications */
2 /*
3 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2011 by Holger Hans Peter Freyther
6 * All Rights Reserved
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.
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.
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.
24 /*! \file application.c
25 * \brief Routines for helping with the osmocom application setup.
28 /*! \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.
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
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/>
63 #include <osmocom/core/application.h>
64 #include <osmocom/core/logging.h>
66 #include <signal.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <unistd.h>
70 #include <errno.h>
71 #include <sys/stat.h>
73 struct log_target *osmo_stderr_target;
75 /*! \brief Ignore \ref SIGPIPE, \ref SIGALRM, \ref SIGHUP and \ref SIGIO */
76 void osmo_init_ignore_signals(void)
78 /* Signals that by default would terminate */
79 signal(SIGPIPE, SIG_IGN);
80 signal(SIGALRM, SIG_IGN);
81 signal(SIGHUP, SIG_IGN);
82 signal(SIGIO, SIG_IGN);
85 /*! \brief Initialize the osmocom logging framework
86 * \param[in] log_info Array of available logging sub-systems
87 * \returns 0 on success, -1 in case of error
89 * This function initializes the osmocom logging systems. It also
90 * creates the default (stderr) logging target.
92 int osmo_init_logging(const struct log_info *log_info)
94 log_init(log_info, NULL);
95 osmo_stderr_target = log_target_create_stderr();
96 if (!osmo_stderr_target)
97 return -1;
99 log_add_target(osmo_stderr_target);
100 log_set_all_filter(osmo_stderr_target, 1);
101 return 0;
104 /*! \brief Turn the current process into a background daemon
106 * This function will fork the process, exit the parent and set umask,
107 * create a new session, close stdin/stdout/stderr and chdir to /tmp
109 int osmo_daemonize(void)
111 int rc;
112 pid_t pid, sid;
114 /* Check if parent PID == init, in which case we are already a daemon */
115 if (getppid() == 1)
116 return -EEXIST;
118 /* Fork from the parent process */
119 pid = fork();
120 if (pid < 0) {
121 /* some error happened */
122 return pid;
125 if (pid > 0) {
126 /* if we have received a positive PID, then we are the parent
127 * and can exit */
128 exit(0);
131 /* FIXME: do we really want this? */
132 umask(0);
134 /* Create a new session and set process group ID */
135 sid = setsid();
136 if (sid < 0)
137 return sid;
139 /* Change to the /tmp directory, which prevents the CWD from being locked
140 * and unable to remove it */
141 rc = chdir("/tmp");
142 if (rc < 0)
143 return rc;
145 /* Redirect stdio to /dev/null */
146 /* since C89/C99 says stderr is a macro, we can safely do this! */
147 #ifdef stderr
148 freopen("/dev/null", "r", stdin);
149 freopen("/dev/null", "w", stdout);
150 freopen("/dev/null", "w", stderr);
151 #endif
153 return 0;