#include <pwd.h>
#include <sys/types.h> /* getgrnam() */
#include <grp.h>
+#include <sys/time.h>
+#include <stdbool.h>
#include "para.h"
#include "daemon.h"
}
/**
- * 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.
*/
me->flags &= ~flag;
}
-static unsigned daemon_test_flag(unsigned flag)
+static bool daemon_test_flag(unsigned flag)
{
return me->flags & flag;
}
goto err;
if (chdir("/") < 0)
goto err;
- umask(0);
null = open("/dev/null", O_RDONLY);
if (null < 0)
goto err;
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());
}
* 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).
*/
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);
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)