Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 1 | /* Utility functions to setup applications */ |
| 2 | /* |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 3 | * (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 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 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 24 | /*! \file application.c |
| 25 | * \brief Routines for helping with the osmocom application setup. |
| 26 | */ |
| 27 | |
Harald Welte | d38c8b8 | 2011-08-30 11:32:56 +0200 | [diff] [blame] | 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. |
| 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 Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 63 | #include <osmocom/core/application.h> |
| 64 | #include <osmocom/core/logging.h> |
| 65 | |
| 66 | #include <signal.h> |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 67 | #include <stdio.h> |
| 68 | #include <stdlib.h> |
| 69 | #include <unistd.h> |
| 70 | #include <errno.h> |
| 71 | #include <sys/stat.h> |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 72 | |
| 73 | struct log_target *osmo_stderr_target; |
| 74 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 75 | /*! \brief Ignore \ref SIGPIPE, \ref SIGALRM, \ref SIGHUP and \ref SIGIO */ |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 76 | void osmo_init_ignore_signals(void) |
| 77 | { |
| 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); |
| 83 | } |
| 84 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 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 |
| 88 | * |
| 89 | * This function initializes the osmocom logging systems. It also |
| 90 | * creates the default (stderr) logging target. |
| 91 | */ |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 92 | int osmo_init_logging(const struct log_info *log_info) |
| 93 | { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 94 | log_init(log_info, NULL); |
Holger Hans Peter Freyther | ba01fa4 | 2011-05-12 13:46:33 +0200 | [diff] [blame] | 95 | osmo_stderr_target = log_target_create_stderr(); |
| 96 | if (!osmo_stderr_target) |
| 97 | return -1; |
| 98 | |
| 99 | log_add_target(osmo_stderr_target); |
| 100 | log_set_all_filter(osmo_stderr_target, 1); |
| 101 | return 0; |
| 102 | } |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 103 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 104 | /*! \brief Turn the current process into a background daemon |
| 105 | * |
| 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 |
| 108 | */ |
Harald Welte | 32e1f23 | 2011-06-26 13:07:18 +0200 | [diff] [blame] | 109 | int osmo_daemonize(void) |
| 110 | { |
| 111 | int rc; |
| 112 | pid_t pid, sid; |
| 113 | |
| 114 | /* Check if parent PID == init, in which case we are already a daemon */ |
| 115 | if (getppid() == 1) |
| 116 | return -EEXIST; |
| 117 | |
| 118 | /* Fork from the parent process */ |
| 119 | pid = fork(); |
| 120 | if (pid < 0) { |
| 121 | /* some error happened */ |
| 122 | return pid; |
| 123 | } |
| 124 | |
| 125 | if (pid > 0) { |
| 126 | /* if we have received a positive PID, then we are the parent |
| 127 | * and can exit */ |
| 128 | exit(0); |
| 129 | } |
| 130 | |
| 131 | /* FIXME: do we really want this? */ |
| 132 | umask(0); |
| 133 | |
| 134 | /* Create a new session and set process group ID */ |
| 135 | sid = setsid(); |
| 136 | if (sid < 0) |
| 137 | return sid; |
| 138 | |
| 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; |
| 144 | |
| 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 |
| 152 | |
| 153 | return 0; |
| 154 | } |