cae63ea7f48488124085d79360458ee44b5620a8
2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file daemon.c Some helpers for programs that detach from the console. */
11 #include <sys/types.h> /* getgrnam() */
17 /** The internal state of the daemon. */
19 /** See \ref daemon_flags. */
21 /** Set by \ref daemon_set_logfile(). */
23 /** Current loglevel, see \ref daemon_set_loglevel(). */
26 /** Used by \ref server_uptime() and \ref uptime_str(). */
28 /** The file pointer if the logfile is open. */
30 /** Used by para_log() if \p DF_LOG_HOSTNAME is set. */
32 /** Used for colored log messages. */
33 char log_colors
[NUM_LOGLEVELS
][COLOR_MAXLEN
];
36 static struct daemon the_daemon
, *me
= &the_daemon
;
39 * Activate default log colors.
41 * This should be called early if color support is wanted.
43 void daemon_set_default_log_colors(void)
46 static const char *default_log_colors
[NUM_LOGLEVELS
] = {
47 [LL_DEBUG
] = "normal",
49 [LL_NOTICE
] = "normal",
50 [LL_WARNING
] = "yellow",
52 [LL_CRIT
] = "magenta bold",
53 [LL_EMERG
] = "red bold",
55 for (i
= 0; i
< NUM_LOGLEVELS
; i
++) {
56 int ret
= color_parse(default_log_colors
[i
], me
->log_colors
[i
]);
62 * Set the color for one loglevel.
64 * \param arg The loglevel/color specifier.
66 * \a arg must be of the form "ll:[fg [bg]] [attr]".
68 * \return 1 On success, -1 on errors.
70 int daemon_set_log_color(char const *arg
)
72 char *p
= strchr(arg
, ':');
77 ret
= get_loglevel_by_name(arg
);
82 ret
= color_parse(p
, me
->log_colors
[ll
]);
87 PARA_ERROR_LOG("%s: color syntax error\n", arg
);
93 * Init or change the name of the log file.
95 * \param logfile_name The full path of the logfile.
97 void daemon_set_logfile(char *logfile_name
)
99 free(me
->logfile_name
);
100 me
->logfile_name
= NULL
;
102 me
->logfile_name
= para_strdup(logfile_name
);
106 * Supress log messages with severity lower than the given loglevel.
108 * \param loglevel The smallest level that should be logged.
110 void daemon_set_loglevel(char *loglevel
)
112 int ret
= get_loglevel_by_name(loglevel
);
119 * Set one of the daemon config flags.
121 * \param flag The flag to set.
123 * \sa \ref daemon_flags.
125 void daemon_set_flag(unsigned flag
)
131 * Clear one of the daemon config flags.
133 * \param flag The flag to clear.
135 * \sa \ref daemon_flags.
137 void daemon_clear_flag(unsigned flag
)
142 static unsigned daemon_test_flag(unsigned flag
)
144 return me
->flags
& flag
;
148 * Do the usual stuff to become a daemon.
150 * Fork, become session leader, dup fd 0, 1, 2 to /dev/null.
152 * \sa fork(2), setsid(2), dup(2).
159 PARA_INFO_LOG("daemonizing\n");
164 exit(EXIT_SUCCESS
); /* parent exits */
165 /* become session leader */
171 null
= open("/dev/null", O_RDONLY
);
174 if (dup2(null
, STDIN_FILENO
) < 0)
176 if (dup2(null
, STDOUT_FILENO
) < 0)
178 if (dup2(null
, STDERR_FILENO
) < 0)
183 PARA_EMERG_LOG("fatal: %s\n", strerror(errno
));
188 * Close the log file of the daemon.
190 void daemon_close_log(void)
194 PARA_INFO_LOG("closing logfile\n");
200 * fopen() the logfile in append mode.
202 * \return Either succeeds or exits.
204 void daemon_open_log_or_die(void)
207 if (!me
->logfile_name
)
209 me
->logfile
= fopen(me
->logfile_name
, "a");
211 PARA_EMERG_LOG("can not open %s: %s\n", me
->logfile_name
,
215 setlinebuf(me
->logfile
);
219 * Log the startup message containing the paraslash version.
221 void log_welcome(const char *whoami
)
223 PARA_INFO_LOG("welcome to %s " PACKAGE_VERSION
" ("BUILD_DATE
")\n",
228 * Give up superuser privileges.
230 * \param username The user to switch to.
231 * \param groupname The group to switch to.
233 * This function returns immediately if not invoked with EUID zero. Otherwise,
234 * it tries to obtain the GID of \a groupname and the UID of \a username. On
235 * success, effective and real GID/UID and the saved set-group-ID/set-user-ID
236 * are all set accordingly. On errors, an appropriate message is logged and
237 * exit() is called to terminate the process.
239 * \sa getpwnam(3), getuid(2), setuid(2), getgrnam(2), setgid(2)
241 void drop_privileges_or_die(const char *username
, const char *groupname
)
249 struct group
*g
= getgrnam(groupname
);
251 PARA_EMERG_LOG("failed to get group %s: %s\n",
252 groupname
, strerror(errno
));
255 if (setgid(g
->gr_gid
) < 0) {
256 PARA_EMERG_LOG("failed to set group id %d: %s\n",
257 (int)g
->gr_gid
, strerror(errno
));
262 PARA_EMERG_LOG("root privileges, but no user option given\n");
265 tmp
= para_strdup(username
);
269 PARA_EMERG_LOG("%s: no such user\n", username
);
272 PARA_INFO_LOG("dropping root privileges\n");
274 PARA_DEBUG_LOG("uid: %d, euid: %d\n", (int)getuid(), (int)geteuid());
278 * Set/get the server uptime.
280 * \param set_or_get Chose one of the two modes.
282 * This should be called at startup time with \a set_or_get equal to \p
283 * UPTIME_SET which sets the uptime to zero. Subsequent calls with \a
284 * set_or_get equal to \p UPTIME_GET return the uptime.
286 * \return Zero if called with \a set_or_get equal to \p UPTIME_SET, the number
287 * of seconds ellapsed since the last reset otherwise.
289 * \sa time(2), difftime(3).
291 time_t server_uptime(enum uptime set_or_get
)
296 if (set_or_get
== UPTIME_SET
) {
297 time(&me
->startuptime
);
301 diff
= difftime(now
, me
->startuptime
);
302 return (time_t) diff
;
306 * Construct string containing uptime.
308 * \return A dynamically allocated string of the form "days:hours:minutes".
312 __malloc
char *uptime_str(void)
314 long t
= server_uptime(UPTIME_GET
);
315 return make_message("%li:%02li:%02li", t
/ 86400,
316 (t
/ 3600) % 24, (t
/ 60) % 60);
320 * The log function for para_server and para_audiod.
322 * \param ll The log level.
323 * \param fmt The format string describing the log message.
325 __printf_2_3
void para_log(int ll
, const char* fmt
,...)
331 char *color
, str
[MAXLINE
] = "";
333 ll
= PARA_MIN(ll
, NUM_LOGLEVELS
- 1);
334 ll
= PARA_MAX(ll
, LL_DEBUG
);
335 if (ll
< me
->loglevel
)
338 fp
= me
->logfile
? me
->logfile
: stderr
;
339 color
= daemon_test_flag(DF_COLOR_LOG
)? me
->log_colors
[ll
] : NULL
;
341 fprintf(fp
, "%s", color
);
342 if (daemon_test_flag(DF_LOG_TIME
)) {
346 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
347 fprintf(fp
, "%s ", str
);
349 if (daemon_test_flag(DF_LOG_HOSTNAME
)) {
351 me
->hostname
= para_hostname();
352 fprintf(fp
, "%s ", me
->hostname
);
354 if (daemon_test_flag(DF_LOG_LL
)) /* log loglevel */
355 fprintf(fp
, "(%d) ", ll
);
356 if (daemon_test_flag(DF_LOG_PID
)) { /* log pid */
357 pid_t mypid
= getpid();
358 fprintf(fp
, "(%d) ", (int)mypid
);
361 vfprintf(fp
, fmt
, argp
);
364 fprintf(fp
, "%s", COLOR_RESET
);