]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Add -n option to allow using syslog without forking
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 19 Aug 2010 15:19:37 +0000 (17:19 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 19 Aug 2010 15:21:11 +0000 (17:21 +0200)
chrony.texi
chronyd.8
logging.c
logging.h
main.c

index af8e1235e2af8a842d23508dc8e1518ea286acd4..487c401f86fe384b69d59dace675b94cf0e0e3c9 100644 (file)
@@ -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
index ae89d55af784d6a6072ced8a4e5a600097dfd751..39d23baf964b0b87b66e821587b652ab462a504f 100644 (file)
--- a/chronyd.8
+++ b/chronyd.8
@@ -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
index 15f1b6a5d81a0ff678163166698866ce3f1c87bb..61cb973200d7ed653411c76862bfad1b86cb097a 100644 (file)
--- a/logging.c
+++ b/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
 }
 
index 37ecc0e6ed0d7c06381c0e221612631d68456a6e..a47ad7b126fc8eaf365bd3979d9ce726f4e7d7ff 100644 (file)
--- a/logging.h
+++ b/logging.h
@@ -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);
diff --git a/main.c b/main.c
index 936850899fa0c92c2eab723efcb6785af470057a..8ca3fe99f687136c486cba5879326c40989bba30 100644 (file)
--- a/main.c
+++ b/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