2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file daemon.c some helpers for programs that detach from the console */
25 #include <sys/types.h>
31 * do the usual stuff to become a daemon
33 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
35 * \sa fork(2), setsid(2), dup(2)
37 void daemon_init(void)
42 PARA_INFO_LOG("%s", "daemonizing\n");
43 if ((pid = fork()) < 0) {
44 PARA_EMERG_LOG("%s", "failed to init daemon\n");
47 exit(EXIT_SUCCESS); /* parent exits */
49 setsid(); /* become session leader */
53 null = open("/dev/null", O_RDONLY);
56 dup2(null, STDIN_FILENO);
57 dup2(null, STDOUT_FILENO);
58 dup2(null, STDERR_FILENO);
63 * fopen() a file in append mode
65 * \param logfile_name the name of the file to open
67 * Either calls exit() or returns a valid file handle.
69 FILE *open_log(const char *logfile_name)
75 if (!(logfile = fopen(logfile_name, "a"))) {
76 PARA_EMERG_LOG("can not open %s, uid: %d\n", logfile_name,
85 * close the log file of the daemon
87 * \param logfile the log file handle
89 * It's OK to call this with logfile == NULL
91 void close_log(FILE* logfile)
95 PARA_INFO_LOG("%s", "closing logfile\n");
100 * log the startup message containing the paraslash version
102 void log_welcome(const char *whoami, int loglevel)
104 PARA_INFO_LOG("welcome to %s " VERSION " ("BUILD_DATE")\n", whoami);
105 PARA_DEBUG_LOG("using loglevel %d\n", loglevel);
109 * give up superuser privileges
111 * This function returns immediately if not invoked with EUID zero. Otherwise,
112 * it tries to obtain the GID of \a groupname and the UID of \a username. On
113 * success, effective and real GID/UID and the saved set-group-ID/set-user-ID
114 * are all set accordingly. On errors, an appropriate message is logged and exit()
115 * is called to terminate the process.
117 * \sa getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)
119 void para_drop_privileges(const char *username, const char *groupname)
127 struct group *g = getgrnam(groupname);
129 PARA_EMERG_LOG("failed to get group %s\n", groupname);
132 if (setgid(g->gr_gid) < 0) {
133 PARA_EMERG_LOG("failed to set group id %d (%s)\n",
134 g->gr_gid, strerror(errno));
139 PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
142 tmp = para_strdup(username);
146 PARA_EMERG_LOG("%s", "no such user\n");
149 PARA_INFO_LOG("%s", "dropping root privileges\n");
151 PARA_DEBUG_LOG("uid: %d, euid: %d\n", getuid(), geteuid());
156 * set/get the server uptime
158 * \param set_or_get chose one of the two modes
160 * This should be called at startup time with \a set_or_get equal to \p
161 * UPTIME_SET which sets the uptime to zero. Subsequent calls with \a
162 * set_or_get equal to \p UPTIME_GET return the number of seconds ellapsed
163 * since the last reset.
165 * \sa time(2), difftime(3)
167 time_t server_uptime(enum uptime set_or_get)
169 static time_t startuptime;
172 if (set_or_get == UPTIME_SET) {
177 return (time_t) difftime(now, startuptime);
181 * construct string containing uptime
183 * The format of the returned string is "days:hours:minutes"
187 __malloc char *uptime_str(void)
189 time_t t = server_uptime(UPTIME_GET);
190 return make_message("%li:%02li:%02li", t / 86400,
191 (t / 3600) % 24, (t / 60) % 60);