fix the plm database tool
[paraslash.git] / daemon.c
1 /*
2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /** \file daemon.c some helpers for programs that detach from the console */
20 #include "para.h"
21 #include "daemon.h"
22 #include <pwd.h>
23 #include "string.h"
24
25 /**
26 * do the usual stuff to become a daemon
27 *
28 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
29 *
30 * \sa fork(2), setsid(2), dup(2)
31 */
32 void daemon_init(void)
33 {
34 pid_t pid;
35 int null;
36
37 PARA_INFO_LOG("%s", "daemonizing\n");
38 if ((pid = fork()) < 0) {
39 PARA_EMERG_LOG("%s", "failed to init daemon\n");
40 exit(EXIT_FAILURE);
41 } else if (pid)
42 exit(EXIT_SUCCESS); /* parent exits */
43 /* child */
44 setsid(); /* become session leader */
45 chdir("/");
46 umask(0);
47
48 null = open("/dev/null", O_RDONLY);
49 if (null < 0)
50 exit(EXIT_FAILURE);
51 dup2(null, STDIN_FILENO);
52 dup2(null, STDOUT_FILENO);
53 dup2(null, STDERR_FILENO);
54 close(null);
55 }
56
57 /**
58 * fopen() a file in append mode
59 *
60 * \param logfile_name the name of the file to open
61 *
62 * Either calls exit() or returns a valid file handle.
63 */
64 /* might be called from para_log, so we must not use para_log */
65 FILE *open_log(const char *logfile_name)
66 {
67 FILE *logfile;
68
69 if (!logfile_name)
70 return NULL;
71 if (!(logfile = fopen(logfile_name, "a")))
72 exit(EXIT_FAILURE);
73 setlinebuf(logfile);
74 return logfile;
75 }
76
77 /**
78 * close the log file of the daemon
79 *
80 * \param logfile the log file handle
81 *
82 * It's OK to call this with logfile == NULL
83 */
84 void close_log(FILE* logfile)
85 {
86 if (!logfile)
87 return;
88 PARA_INFO_LOG("%s", "closing logfile\n");
89 fclose(logfile);
90 }
91
92 /**
93 * log the startup message containing the paraslash version
94 */
95 void log_welcome(const char *whoami, int loglevel)
96 {
97 PARA_INFO_LOG("welcome to %s " VERSION " ("BUILD_DATE")\n", whoami);
98 PARA_DEBUG_LOG("using loglevel %d\n", loglevel);
99 }
100
101 /**
102 * give up superuser privileges
103 *
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.
108 *
109 * \sa getpwnam(3), getuid(2), setuid(2)
110 */
111 void para_drop_privileges(const char *username)
112 {
113 struct passwd *p;
114 char *tmp;
115
116 if (geteuid())
117 return;
118 if (!username) {
119 PARA_EMERG_LOG("%s", "root privileges, but no user option given\n");
120 exit(EXIT_FAILURE);
121 }
122 tmp = para_strdup(username);
123 p = getpwnam(tmp);
124 free(tmp);
125 if (!p) {
126 PARA_EMERG_LOG("%s", "no such user\n");
127 exit(EXIT_FAILURE);
128 }
129 PARA_NOTICE_LOG("%s", "dropping root privileges\n");
130 setuid(p->pw_uid);
131 PARA_DEBUG_LOG("uid: %d, euid: %d\n", getuid(), geteuid());
132 setuid(p->pw_uid);
133 }
134
135 /**
136 * set/get the server uptime
137 *
138 * \param set_or_get chose one of the two modes
139 *
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.
144 *
145 * \sa time(2), difftime(3)
146 */
147 time_t server_uptime(enum uptime set_or_get)
148 {
149 static time_t startuptime;
150 time_t now;
151
152 if (set_or_get == UPTIME_SET) {
153 time(&startuptime);
154 return 0;
155 }
156 time(&now);
157 return (time_t) difftime(now, startuptime);
158 }
159
160 /**
161 * construct string containing uptime
162 *
163 * The format of the returned string is "days:hours:minutes"
164 *
165 * \sa server_uptime
166 */
167 __malloc char *uptime_str(void)
168 {
169 time_t t = server_uptime(UPTIME_GET);
170 return make_message("%li:%02li:%02li", t / 86400,
171 (t / 3600) % 24, (t / 60) % 60);
172 }
173