5f8f4471a79c67fbd4b5f903d0c1cef88806f960
[osmocom-bb.git] / src / application.c
1 /* Utility functions to setup applications */
2 /*
3  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  * (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
24 #include <osmocom/core/application.h>
25 #include <osmocom/core/logging.h>
26
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33
34 struct log_target *osmo_stderr_target;
35
36 void osmo_init_ignore_signals(void)
37 {
38         /* Signals that by default would terminate */
39         signal(SIGPIPE, SIG_IGN);
40         signal(SIGALRM, SIG_IGN);
41         signal(SIGHUP, SIG_IGN);
42         signal(SIGIO, SIG_IGN);
43 }
44
45 int osmo_init_logging(const struct log_info *log_info)
46 {
47         log_init(log_info);
48         osmo_stderr_target = log_target_create_stderr();
49         if (!osmo_stderr_target)
50                 return -1;
51
52         log_add_target(osmo_stderr_target);
53         log_set_all_filter(osmo_stderr_target, 1);
54         return 0;
55 }
56
57 int osmo_daemonize(void)
58 {
59         int rc;
60         pid_t pid, sid;
61
62         /* Check if parent PID == init, in which case we are already a daemon */
63         if (getppid() == 1)
64                 return -EEXIST;
65
66         /* Fork from the parent process */
67         pid = fork();
68         if (pid < 0) {
69                 /* some error happened */
70                 return pid;
71         }
72
73         if (pid > 0) {
74                 /* if we have received a positive PID, then we are the parent
75                  * and can exit */
76                 exit(0);
77         }
78
79         /* FIXME: do we really want this? */
80         umask(0);
81
82         /* Create a new session and set process group ID */
83         sid = setsid();
84         if (sid < 0)
85                 return sid;
86
87         /* Change to the /tmp directory, which prevents the CWD from being locked
88          * and unable to remove it */
89         rc = chdir("/tmp");
90         if (rc < 0)
91                 return rc;
92
93         /* Redirect stdio to /dev/null */
94 /* since C89/C99 says stderr is a macro, we can safely do this! */
95 #ifdef stderr
96         freopen("/dev/null", "r", stdin);
97         freopen("/dev/null", "w", stdout);
98         freopen("/dev/null", "w", stderr);
99 #endif
100
101         return 0;
102 }