X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=daemon.c;h=b7a0a3267b68df5b020159f90c56337073463244;hp=2f706b01bde62c8821dc76e66ad387a905782d91;hb=64a9cf5877ab9ecf6410596d657914f26830d3f7;hpb=ae657a3b13b4ff76187dad39fede40b98d7cb6d7 diff --git a/daemon.c b/daemon.c index 2f706b01..b7a0a326 100644 --- a/daemon.c +++ b/daemon.c @@ -1,16 +1,20 @@ /* - * Copyright (C) 1997-2009 Andre Noll + * Copyright (C) 1997-2011 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ /** \file daemon.c Some helpers for programs that detach from the console. */ -#include "para.h" -#include "daemon.h" + +#include #include #include /* getgrnam() */ #include +#include +#include +#include "para.h" +#include "daemon.h" #include "string.h" #include "color.h" @@ -52,29 +56,8 @@ void daemon_set_default_log_colors(void) [LL_CRIT] = "magenta bold", [LL_EMERG] = "red bold", }; - for (i = 0; i < NUM_LOGLEVELS; i++) { - int ret = color_parse(default_log_colors[i], me->log_colors[i]); - assert(ret >= 0); - } -} - -static int get_loglevel_by_name(const char *txt, size_t n) -{ - if (!strncasecmp(txt, "debug", n)) - return LL_DEBUG; - if (!strncasecmp(txt, "info", n)) - return LL_INFO; - if (!strncasecmp(txt, "notice", n)) - return LL_NOTICE; - if (!strncasecmp(txt, "warning", n)) - return LL_WARNING; - if (!strncasecmp(txt, "error", n)) - return LL_ERROR; - if (!strncasecmp(txt, "crit", n)) - return LL_CRIT; - if (!strncasecmp(txt, "emerg", n)) - return LL_EMERG; - return -1; + for (i = 0; i < NUM_LOGLEVELS; i++) + color_parse_or_die(default_log_colors[i], me->log_colors[i]); } /** @@ -86,26 +69,23 @@ static int get_loglevel_by_name(const char *txt, size_t n) * * \return 1 On success, -1 on errors. */ -int daemon_set_log_color(char const *arg) +void daemon_set_log_color_or_die(char const *arg) { char *p = strchr(arg, ':'); int ret, ll; if (!p) goto err; - ret = get_loglevel_by_name(arg, p - arg); + ret = get_loglevel_by_name(arg); if (ret < 0) goto err; ll = ret; p++; - ret = color_parse(p, me->log_colors[ll]); - if (ret < 0) - goto err; - return 1; + color_parse_or_die(p, me->log_colors[ll]); + return; err: - PARA_ERROR_LOG("%s: color syntax error\n", arg); - return -1; - + PARA_EMERG_LOG("%s: color syntax error\n", arg); + exit(EXIT_FAILURE); } /** @@ -122,13 +102,16 @@ void daemon_set_logfile(char *logfile_name) } /** - * Supress log messages with severity lower than the given loglevel. + * Suppress log messages with severity lower than the given loglevel. * * \param loglevel The smallest level that should be logged. */ -void daemon_set_loglevel(int loglevel) +void daemon_set_loglevel(char *loglevel) { - me->loglevel = loglevel; + int ret = get_loglevel_by_name(loglevel); + + assert(ret >= 0); + me->loglevel = ret; } /** @@ -155,7 +138,7 @@ void daemon_clear_flag(unsigned flag) me->flags &= ~flag; } -static unsigned daemon_test_flag(unsigned flag) +static bool daemon_test_flag(unsigned flag) { return me->flags & flag; } @@ -183,7 +166,6 @@ void daemonize(void) goto err; if (chdir("/") < 0) goto err; - umask(0); null = open("/dev/null", O_RDONLY); if (null < 0) goto err; @@ -234,11 +216,10 @@ void daemon_open_log_or_die(void) /** * Log the startup message containing the paraslash version. */ -void log_welcome(const char *whoami, int loglevel) +void log_welcome(const char *whoami) { PARA_INFO_LOG("welcome to %s " PACKAGE_VERSION " ("BUILD_DATE")\n", whoami); - PARA_DEBUG_LOG("using loglevel %d\n", loglevel); } /** @@ -287,7 +268,11 @@ void drop_privileges_or_die(const char *username, const char *groupname) exit(EXIT_FAILURE); } PARA_INFO_LOG("dropping root privileges\n"); - setuid(p->pw_uid); + if (setuid(p->pw_uid) < 0) { + PARA_EMERG_LOG("failed to set effective user ID (%s)", + strerror(errno)); + exit(EXIT_FAILURE); + } PARA_DEBUG_LOG("uid: %d, euid: %d\n", (int)getuid(), (int)geteuid()); } @@ -301,7 +286,7 @@ void drop_privileges_or_die(const char *username, const char *groupname) * set_or_get equal to \p UPTIME_GET return the uptime. * \return Zero if called with \a set_or_get equal to \p UPTIME_SET, the number - * of seconds ellapsed since the last reset otherwise. + * of seconds elapsed since the last reset otherwise. * * \sa time(2), difftime(3). */ @@ -344,8 +329,9 @@ __printf_2_3 void para_log(int ll, const char* fmt,...) va_list argp; FILE *fp; struct tm *tm; - time_t t1; - char *color, str[MAXLINE] = ""; + char *color; + bool log_time = daemon_test_flag(DF_LOG_TIME), log_timing = + daemon_test_flag(DF_LOG_TIMING); ll = PARA_MIN(ll, NUM_LOGLEVELS - 1); ll = PARA_MAX(ll, LL_DEBUG); @@ -356,12 +342,18 @@ __printf_2_3 void para_log(int ll, const char* fmt,...) color = daemon_test_flag(DF_COLOR_LOG)? me->log_colors[ll] : NULL; if (color) fprintf(fp, "%s", color); - if (daemon_test_flag(DF_LOG_TIME)) { - /* date and time */ - time(&t1); - tm = localtime(&t1); - strftime(str, MAXLINE, "%b %d %H:%M:%S", tm); - fprintf(fp, "%s ", str); + if (log_time || log_timing) { + struct timeval tv; + gettimeofday(&tv, NULL); + if (daemon_test_flag(DF_LOG_TIME)) { /* print date and time */ + time_t t1 = tv.tv_sec; + char str[100]; + tm = localtime(&t1); + strftime(str, sizeof(str), "%b %d %H:%M:%S", tm); + fprintf(fp, "%s%s", str, log_timing? ":" : " "); + } + if (log_timing) /* print milliseconds */ + fprintf(fp, "%04lu ", (long unsigned)tv.tv_usec / 1000); } if (daemon_test_flag(DF_LOG_HOSTNAME)) { if (!me->hostname)