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 */
26 * do the usual stuff to become a daemon
28 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
30 * \sa fork(2), setsid(2), dup(2)
32 void daemon_init(void)
37 PARA_INFO_LOG("%s", "daemonizing\n");
38 if ((pid = fork()) < 0) {
39 PARA_EMERG_LOG("%s", "failed to init daemon\n");
42 exit(EXIT_SUCCESS); /* parent exits */
44 setsid(); /* become session leader */
48 null = open("/dev/null", O_RDONLY);
51 dup2(null, STDIN_FILENO);
52 dup2(null, STDOUT_FILENO);
53 dup2(null, STDERR_FILENO);
58 * fopen() a file in append mode
60 * \param logfile_name the name of the file to open
62 * Either calls exit() or returns a valid file handle.
64 /* might be called from para_log, so we must not use para_log */
65 FILE *open_log(const char *logfile_name)
71 if (!(logfile = fopen(logfile_name, "a")))
78 * close the log file of the daemon
80 * \param logfile the log file handle
82 * It's OK to call this with logfile == NULL
84 void close_log(FILE* logfile)
88 PARA_INFO_LOG("%s", "closing logfile\n");
93 * log the startup message containing the paraslash version
95 void log_welcome(const char *whoami, int loglevel)
97 PARA_INFO_LOG("welcome to %s " VERSION " ("BUILD_DATE")\n", whoami);
98 PARA_DEBUG_LOG("using loglevel %d\n", loglevel);
102 * give up superuser privileges
104 * This function returns immediately if not invoked with EUID zero. Otherwise,
105 * it tries to obtain the UID for the user specified by \a username and exits
106 * if this user was not found. On success, effective and real UID and the saved
107 * set-user-ID are all set to the UID of \a username.
109 * \sa getpwnam(3), getuid(2), setuid(2)
111 void para_drop_privileges(const char *username)
119 PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
122 tmp = para_strdup(username);
126 PARA_EMERG_LOG("%s", "no such user\n");
129 PARA_NOTICE_LOG("%s", "dropping root privileges\n");
131 PARA_DEBUG_LOG("uid: %d, euid: %d\n", getuid(), geteuid());
136 * set/get the server uptime
138 * \param set_or_get chose one of the two modes
140 * This should be called at startup time with \a set_or_get equal to \p
141 * UPTIME_SET which sets the uptime to zero. Subsequent calls with \a
142 * set_or_get equal to \p UPTIME_GET return the number of seconds ellapsed
143 * since the last reset.
145 * \sa time(2), difftime(3)
147 time_t server_uptime(enum uptime set_or_get)
149 static time_t startuptime;
152 if (set_or_get == UPTIME_SET) {
157 return (time_t) difftime(now, startuptime);
161 * construct string containing uptime
163 * The format of the returned string is "days:hours:minutes"
167 __malloc char *uptime_str(void)
169 time_t t = server_uptime(UPTIME_GET);
170 return make_message("%li:%02li:%02li", t / 86400,
171 (t / 3600) % 24, (t / 60) % 60);