X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=daemon.c;h=a669482fc27525f8c9d2c4ce64a62eb350d4cb06;hp=12b19275de9f6c561e021dad1b06ed9fae1ba4b9;hb=3010ef96e10cb15d423eef8f9802fbed78744393;hpb=5446dfe5a4d54aa49dbc961956818d751e817326 diff --git a/daemon.c b/daemon.c index 12b19275..a669482f 100644 --- a/daemon.c +++ b/daemon.c @@ -5,12 +5,16 @@ */ /** \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" @@ -139,7 +143,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; } @@ -167,7 +171,6 @@ void daemonize(void) goto err; if (chdir("/") < 0) goto err; - umask(0); null = open("/dev/null", O_RDONLY); if (null < 0) goto err; @@ -270,7 +273,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()); } @@ -327,8 +334,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); @@ -339,12 +347,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)