Add -n option to allow using syslog without forking
This commit is contained in:
parent
5344028c40
commit
fe4b661fe7
5 changed files with 70 additions and 50 deletions
|
@ -1046,6 +1046,9 @@ Information messages and warnings will be logged to syslog.
|
|||
The command line options supported are as follows:
|
||||
|
||||
@table @code
|
||||
@item -n
|
||||
When run in this mode, the program will not detach itself from the
|
||||
terminal.
|
||||
@item -d
|
||||
When run in this mode, the program will not detach itself from the
|
||||
terminal, and all messages will be sent to the terminal instead of to
|
||||
|
|
|
@ -45,6 +45,10 @@ Linux.
|
|||
This option will lock chronyd into RAM so that it will never be paged out.
|
||||
This mode is only supported on Linux.
|
||||
.TP
|
||||
.B \-n
|
||||
When run in this mode, the program will not detach itself from the
|
||||
terminal.
|
||||
.TP
|
||||
.B \-d
|
||||
When run in this mode, the program will not detach itself from the
|
||||
terminal, and all messages will be sent to the terminal instead of
|
||||
|
|
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
|
||||
}
|
||||
|
||||
|
|
|
@ -86,7 +86,8 @@ extern void LOG_Fatal_Function(LOG_Facility facility, const char *format, ...);
|
|||
/* Position in code reporting function */
|
||||
extern void LOG_Position(const char *filename, int line_number, const char *function_name);
|
||||
|
||||
extern void LOG_GoDaemon(void);
|
||||
/* Log messages to syslog instead of stderr */
|
||||
extern void LOG_OpenSystemLog(void);
|
||||
|
||||
/* Return zero once per 10 seconds */
|
||||
extern int LOG_RateLimited(void);
|
||||
|
|
55
main.c
55
main.c
|
@ -210,12 +210,55 @@ write_lockfile(void)
|
|||
|
||||
/* ================================================== */
|
||||
|
||||
static void
|
||||
go_daemon(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int main
|
||||
(int argc, char **argv)
|
||||
{
|
||||
char *conf_file = NULL;
|
||||
char *user = NULL;
|
||||
int debug = 0;
|
||||
int debug = 0, nofork = 0;
|
||||
int do_init_rtc = 0;
|
||||
int other_pid;
|
||||
int lock_memory = 0, sched_priority = 0;
|
||||
|
@ -250,8 +293,11 @@ int main
|
|||
/* This write to the terminal is OK, it comes before we turn into a daemon */
|
||||
printf("chronyd (chrony) version %s\n", PROGRAM_VERSION_STRING);
|
||||
exit(0);
|
||||
} else if (!strcmp("-n", *argv)) {
|
||||
nofork = 1;
|
||||
} else if (!strcmp("-d", *argv)) {
|
||||
debug = 1;
|
||||
nofork = 1;
|
||||
} else if (!strcmp("-4", *argv)) {
|
||||
DNS_SetAddressFamily(IPADDR_INET4);
|
||||
} else if (!strcmp("-6", *argv)) {
|
||||
|
@ -270,10 +316,13 @@ int main
|
|||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
/* Turn into a daemon */
|
||||
if (!nofork) {
|
||||
go_daemon();
|
||||
}
|
||||
|
||||
if (!debug) {
|
||||
LOG_GoDaemon();
|
||||
LOG_OpenSystemLog();
|
||||
}
|
||||
|
||||
/* Check whether another chronyd may already be running. Do this after
|
||||
|
|
Loading…
Reference in a new issue