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
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
/* 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;
fclose(logfile);
}
#else
- if (is_detached) {
+ if (system_log) {
closelog();
}
#endif
fprintf(logfile, "%s\n", buf);
}
#else
- if (is_detached) {
+ if (system_log) {
switch (severity) {
case LOGS_INFO:
syslog(LOG_INFO, "%s", buf);
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);
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);
/* ================================================== */
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
}
/* 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);
/* ================================================== */
+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;
/* 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)) {
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