mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 18:35:06 -05:00
Add -n option to allow using syslog without forking
This commit is contained in:
55
logging.c
55
logging.c
@@ -40,7 +40,7 @@
|
||||
/* Flag indicating we have initialised */
|
||||
static int initialised = 0;
|
||||
|
||||
static int is_detached = 0;
|
||||
static int system_log = 0;
|
||||
|
||||
static time_t last_limited = 0;
|
||||
|
||||
@@ -88,7 +88,7 @@ LOG_Finalise(void)
|
||||
fclose(logfile);
|
||||
}
|
||||
#else
|
||||
if (is_detached) {
|
||||
if (system_log) {
|
||||
closelog();
|
||||
}
|
||||
#endif
|
||||
@@ -114,7 +114,7 @@ LOG_Line_Function(LOG_Severity severity, LOG_Facility facility, const char *form
|
||||
fprintf(logfile, "%s\n", buf);
|
||||
}
|
||||
#else
|
||||
if (is_detached) {
|
||||
if (system_log) {
|
||||
switch (severity) {
|
||||
case LOGS_INFO:
|
||||
syslog(LOG_INFO, "%s", buf);
|
||||
@@ -150,7 +150,7 @@ LOG_Fatal_Function(LOG_Facility facility, const char *format, ...)
|
||||
fprintf(logfile, "Fatal error : %s\n", buf);
|
||||
}
|
||||
#else
|
||||
if (is_detached) {
|
||||
if (system_log) {
|
||||
syslog(LOG_CRIT, "Fatal error : %s", buf);
|
||||
} else {
|
||||
fprintf(stderr, "Fatal error : %s\n", buf);
|
||||
@@ -172,7 +172,7 @@ LOG_Position(const char *filename, int line_number, const char *function_name)
|
||||
time_t t;
|
||||
struct tm stm;
|
||||
char buf[64];
|
||||
if (!is_detached) {
|
||||
if (!system_log) {
|
||||
/* Don't clutter up syslog with internal debugging info */
|
||||
time(&t);
|
||||
stm = *gmtime(&t);
|
||||
@@ -186,50 +186,13 @@ LOG_Position(const char *filename, int line_number, const char *function_name)
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_GoDaemon(void)
|
||||
LOG_OpenSystemLog(void)
|
||||
{
|
||||
#ifdef WINNT
|
||||
|
||||
|
||||
#else
|
||||
|
||||
int pid, fd;
|
||||
|
||||
/* Does this preserve existing signal handlers? */
|
||||
pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
LOG(LOGS_ERR, LOGF_Logging, "Could not detach, fork failed : %s", strerror(errno));
|
||||
} else if (pid > 0) {
|
||||
exit(0); /* In the 'grandparent' */
|
||||
} else {
|
||||
|
||||
setsid();
|
||||
|
||||
/* Do 2nd fork, as-per recommended practice for launching daemons. */
|
||||
pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
LOG(LOGS_ERR, LOGF_Logging, "Could not detach, fork failed : %s", strerror(errno));
|
||||
} else if (pid > 0) {
|
||||
exit(0); /* In the 'parent' */
|
||||
} else {
|
||||
/* In the child we want to leave running as the daemon */
|
||||
|
||||
/* Don't keep stdin/out/err from before. */
|
||||
for (fd=0; fd<1024; fd++) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
is_detached = 1;
|
||||
|
||||
openlog("chronyd", LOG_PID, LOG_DAEMON);
|
||||
|
||||
LOG(LOGS_INFO, LOGF_Logging, "chronyd version %s starting", PROGRAM_VERSION_STRING);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
system_log = 1;
|
||||
openlog("chronyd", LOG_PID, LOG_DAEMON);
|
||||
LOG(LOGS_INFO, LOGF_Logging, "chronyd version %s starting", PROGRAM_VERSION_STRING);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user