blob: 6871df17b0968ffcd4ad109021a5c400475bab5a [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
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020028#include <osmocom/core/application.h>
29#include <osmocom/core/logging.h>
30
31#include <signal.h>
Harald Welte32e1f232011-06-26 13:07:18 +020032#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <errno.h>
36#include <sys/stat.h>
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020037
38struct log_target *osmo_stderr_target;
39
Harald Welteba6988b2011-08-17 12:46:48 +020040/*! \brief Ignore \ref SIGPIPE, \ref SIGALRM, \ref SIGHUP and \ref SIGIO */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020041void osmo_init_ignore_signals(void)
42{
43 /* Signals that by default would terminate */
44 signal(SIGPIPE, SIG_IGN);
45 signal(SIGALRM, SIG_IGN);
46 signal(SIGHUP, SIG_IGN);
47 signal(SIGIO, SIG_IGN);
48}
49
Harald Welteba6988b2011-08-17 12:46:48 +020050/*! \brief Initialize the osmocom logging framework
51 * \param[in] log_info Array of available logging sub-systems
52 * \returns 0 on success, -1 in case of error
53 *
54 * This function initializes the osmocom logging systems. It also
55 * creates the default (stderr) logging target.
56 */
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020057int osmo_init_logging(const struct log_info *log_info)
58{
Harald Welteb43bc042011-06-27 10:29:17 +020059 log_init(log_info, NULL);
Holger Hans Peter Freytherba01fa42011-05-12 13:46:33 +020060 osmo_stderr_target = log_target_create_stderr();
61 if (!osmo_stderr_target)
62 return -1;
63
64 log_add_target(osmo_stderr_target);
65 log_set_all_filter(osmo_stderr_target, 1);
66 return 0;
67}
Harald Welte32e1f232011-06-26 13:07:18 +020068
Harald Welteba6988b2011-08-17 12:46:48 +020069/*! \brief Turn the current process into a background daemon
70 *
71 * This function will fork the process, exit the parent and set umask,
72 * create a new session, close stdin/stdout/stderr and chdir to /tmp
73 */
Harald Welte32e1f232011-06-26 13:07:18 +020074int osmo_daemonize(void)
75{
76 int rc;
77 pid_t pid, sid;
78
79 /* Check if parent PID == init, in which case we are already a daemon */
80 if (getppid() == 1)
81 return -EEXIST;
82
83 /* Fork from the parent process */
84 pid = fork();
85 if (pid < 0) {
86 /* some error happened */
87 return pid;
88 }
89
90 if (pid > 0) {
91 /* if we have received a positive PID, then we are the parent
92 * and can exit */
93 exit(0);
94 }
95
96 /* FIXME: do we really want this? */
97 umask(0);
98
99 /* Create a new session and set process group ID */
100 sid = setsid();
101 if (sid < 0)
102 return sid;
103
104 /* Change to the /tmp directory, which prevents the CWD from being locked
105 * and unable to remove it */
106 rc = chdir("/tmp");
107 if (rc < 0)
108 return rc;
109
110 /* Redirect stdio to /dev/null */
111/* since C89/C99 says stderr is a macro, we can safely do this! */
112#ifdef stderr
113 freopen("/dev/null", "r", stdin);
114 freopen("/dev/null", "w", stdout);
115 freopen("/dev/null", "w", stderr);
116#endif
117
118 return 0;
119}