monit-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: patch for SIGHUP reinitialization


From: Martin Pala
Subject: Re: patch for SIGHUP reinitialization
Date: Tue, 11 Feb 2003 17:37:22 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021226 Debian/1.2.1-9

New patch version - changes:

- small fixes
- modified documentation according to new configuration reload method
- 'monit reload' added as dicussed on the list (will send SIGHUP to running monit daemon)

TODO:
- implement '-t' option for valid configuration test

Martin

Martin Pala wrote:

Pretty patch - im sorry (forgotten attachment :)

Martin Pala wrote:

Hi,

there's patch to make monit reload configuration only on SIGHUP signal.

It simplifies present SIGHUP handling implementation as well.

Its OK to check it in?

Martin


diff -Naur monit/CHANGES.txt monit-sighup/CHANGES.txt
--- monit/CHANGES.txt   2003-02-11 14:51:58.000000000 +0100
+++ monit-sighup/CHANGES.txt    2003-02-11 16:55:43.000000000 +0100
@@ -4,6 +4,10 @@
 
 Version 3.2
 
+*  Monit now reloads configuration ONLY after it receives SIGHUP.
+   Automatic reload based on monit's control file timestamp change
+   is now deprecated.
+
 *  The ssl version for TCPSSL test can be changed in case autodetection 
    fails. (Thanks to Mark Foster <address@hidden> for the bugreport)
 
diff -Naur monit/daemonize.c monit-sighup/daemonize.c
--- monit/daemonize.c   2003-02-09 19:25:28.000000000 +0100
+++ monit-sighup/daemonize.c    2003-02-11 17:26:41.000000000 +0100
@@ -70,7 +70,6 @@
 void  daemonize() {
 
   pid_t pid;
-  void *hup_handler= NULL;
   
   /*
    * Clear file creation mask
@@ -94,11 +93,6 @@
   
   setsid();
 
-  /*
-   * Don't let future opens allocate controlling terminals
-   */
-  hup_handler= signal(SIGHUP, SIG_IGN);
-
   if((pid= fork ()) < 0) {
     
     log("Cannot fork of a new process\n");  
@@ -129,28 +123,22 @@
    */
   redirect_stdfd();
 
-  /*
-   * Reset the SIGHUP handler
-   */
-  if(hup_handler)
-      signal(SIGHUP, hup_handler);
-
 } 
 
 
 /**
- * Kill a daemon process
- * @return TRUE if the daemon was killed, otherwise FALSE
+ * Send signal to a daemon process
+ * @return TRUE if signal was send, otherwise FALSE
  */
-int kill_daemon() {
+int kill_daemon(int sig) {
   
   pid_t pid;
 
   if ( (pid= exist_daemon()) > 0 ) {
     
-    if ( kill(pid, SIGTERM) < 0 ) {
+    if ( kill(pid, sig) < 0 ) {
       
-      error("%s: Cannot kill daemon process -- %s\n",prog, STRERROR);
+      error("%s: Cannot send signal to daemon process -- %s\n",prog, STRERROR);
       return FALSE;
       
     }
@@ -162,8 +150,12 @@
     
   }
   
-  fprintf(stdout, "%s daemon with pid [%d] killed\n", prog, (int)pid);
-  fflush(stdout);
+  if(sig == SIGTERM) {
+         
+    fprintf(stdout, "%s daemon with pid [%d] killed\n", prog, (int)pid);
+    fflush(stdout);
+
+  }
   
   return TRUE;
   
diff -Naur monit/files.c monit-sighup/files.c
--- monit/files.c       2003-01-11 10:04:49.000000000 +0100
+++ monit-sighup/files.c        2003-02-11 16:42:02.000000000 +0100
@@ -237,18 +237,6 @@
 
 
 /**
- * Test the monit control file for changes.
- * @return TRUE if the Runtime control file was changed,
- * otherwise FALSE
- */
-int is_rcfile_changed() {
-  
-  return(get_timestamp(Run.controlfile, S_IFREG) != Run.timestamp);
-  
-}
-
-
-/**
  * Secure check the monitrc file. The run control file must have the
  * same uid as the REAL uid of this process, it must have permissions
  * no greater than 700 and it must not be a symbolic link.  We check
diff -Naur monit/monit.pod monit-sighup/monit.pod
--- monit/monit.pod     2003-02-11 13:39:07.000000000 +0100
+++ monit-sighup/monit.pod      2003-02-11 17:18:41.000000000 +0100
@@ -89,6 +89,11 @@
     monitrc file, after a I<check> keyword. See also 
     the MONIT HTTPD section below.
 
+B<reload>
+    Will reinitialize running monit daemon, specifically
+    it will reread its configuration, close and reopen
+    log files.
+
 B<restart>
     Stop and start all programs. If the group option is
     set, only restart the programs in the named group.
@@ -156,13 +161,6 @@
 The I<quit> argument will kill a running daemon process instead
 of waking it up.
 
-If you touch or change the I<.monitrc> file while monit is
-running in daemon mode, this will be detected at the beginning of
-the next poll cycle. When a changed I<.monitrc> is detected,
-monit rereads it and reinitialize itself. Note also that if you
-break the I<.monitrc> file's syntax, the monit daemon will exit
-after logging the appropriate error message.
-
 
 =head2 monit lock file
 
@@ -1548,9 +1546,11 @@
 If a monit daemon is running, SIGUSR1 wakes it up from its sleep
 phase and forces a poll of all processes. SIGTERM will gracefully
 terminate a monit daemon. This signal is sent to a monit daemon
-if monit is started with the I<quit> action argument. Sending a
-SIGHUP signal to a running monit daemon will force the daemon to
-reload itself, specifically it will close and reopen log files.
+if monit is started with the I<quit> action argument.
+
+Sending a SIGHUP signal to a running monit daemon will force
+the daemon to reinitialize itself, specifically it will reread
+configuration, close and reopen log files.
 
 Running monit in foreground while a background monit daemon is
 running will wake up the daemon.
diff -Naur monit/monitor.c monit-sighup/monitor.c
--- monit/monitor.c     2003-02-10 01:09:19.000000000 +0100
+++ monit-sighup/monitor.c      2003-02-11 17:32:04.000000000 +0100
@@ -69,13 +69,12 @@
 
 /* --- Private Prototypes -------------------------------------------------- */
 static void  do_init();                       /* Initialize this application */
-static void  do_reinit();           /* Re-initialize the runtime application */
+static RETSIGTYPE  do_reinit(int);  /* Re-initialize the runtime application */
 static void  do_action(char **);         /* Dispatch to the submitted action */
 static RETSIGTYPE  do_destroy(int);             /* Finalize this application */
 static void  do_default();                              /* Do default action */
 static RETSIGTYPE  do_wakeup(int); /* Signalhandler for a daemon wakeup call */
 static int   do_wakeupcall();              /* Wakeup a sleeping monit daemon */
-static RETSIGTYPE do_restart(int sig);   /* Signalhandler for daemon restart */
 static void  handle_options(int, char **);         /* Handle program options */
 static void  help();                 /* Print program help message to stdout */
 static void  version();                         /* Print version information */
@@ -130,9 +129,9 @@
   /*
    * Register interest for the SIGHUP signal,
    * in case we run in daemon mode this signal
-   * will restart the monit daemon.
+   * will reload the configuration.
    */
-  signal(SIGHUP, do_restart);
+  signal(SIGHUP, do_reinit);
 
   /*
    * Register no interest for the SIGPIPE signal,
@@ -218,9 +217,18 @@
  * the runtime control file was changed during daemon
  * mode.
  */
-static void do_reinit() {
+static RETSIGTYPE do_reinit(int sig) {
 
-  log("Reinitializing %s - Control file '%s' was changed\n",
+  char *bind_addr;
+  int port= Run.httpdport;
+
+
+  signal(SIGHUP, SIG_IGN);
+
+  bind_addr= Run.bind_addr?xstrdup(Run.bind_addr):NULL;
+
+  log("Awakened by the SIGHUP signal\n");
+  log("Reinitializing %s - Control file '%s'\n",
       prog, Run.controlfile);
   
   /* Run the garbage collector */
@@ -263,6 +271,25 @@
       
   }
 
+  if(! can_http()) {
+         
+    stop_http();
+         
+  } else if(!is(bind_addr, Run.bind_addr) || port != Run.httpdport) {
+         
+    stop_http();
+    start_http();
+         
+  } else if(! check_httpd()) {
+
+    start_http();
+         
+  }
+
+  free(bind_addr);
+
+  signal(SIGHUP, do_reinit);
+
 }
 
 
@@ -310,6 +337,11 @@
        control("stop");
     }
     
+  } else if(is(action, "reload")) {
+    
+    error("Reinitializing monit daemon\n", prog);
+    kill_daemon(SIGHUP);
+    
   } else if(is(action, "restart")) {
     
     if(P) {
@@ -348,7 +380,7 @@
   } else if(is(action, "quit")) {
     
     error("Stopping monit daemon\n", prog);
-    kill_daemon();
+    kill_daemon(SIGTERM);
     
   } else if(is(action, "validate")) {
     
@@ -380,21 +412,6 @@
 
 
 /**
- * Signalhandler for a daemon restart call. The timestamp of the
- * control file is changed with the effect that a monit daemon will
- * restart itself, including closing and reopening log files.
- */
-static RETSIGTYPE do_restart(int sig) {
-
-  signal(SIGHUP, SIG_IGN);
-  Run.timestamp= 0;
-  log("Awakened by the SIGHUP signal\n");
-  signal(SIGHUP, do_restart);
-
-}
-
-
-/**
  * Wakeup a sleeping monit daemon.
  * Returns TRUE on success otherwise FALSE
  */
@@ -497,33 +514,6 @@
 
       sleep(Run.polltime);
 
-      if(is_rcfile_changed()) {
-       
-       int port= Run.httpdport;
-       char *bind_addr= Run.bind_addr?xstrdup(Run.bind_addr):NULL;
-
-       do_reinit();
-       
-       if(! can_http()) {
-         
-         stop_http();
-         
-       } else if(!is(bind_addr, Run.bind_addr) || port != Run.httpdport) {
-         
-         stop_http();
-         start_http();
-         
-       } else if(! check_httpd()) {
-
-         start_http();
-         
-       }
-
-       free(bind_addr);
-       
-      }
-     
-
     }
     
   }
@@ -649,6 +639,7 @@
   printf(" start name   - Only start the named program in the control file\n");
   printf(" stop         - Stop all programs listed in the control file\n");
   printf(" stop name    - Only stop the named program in the control file\n");
+  printf(" reload       - Reinitialize monit\n");
   printf(" restart      - Stop and start all programs\n");
   printf(" restart name - Only restart the named program in the control 
file\n");
   printf(" status       - Print status information for each program\n");
diff -Naur monit/monitor.h monit-sighup/monitor.h
--- monit/monitor.h     2003-02-10 19:52:43.000000000 +0100
+++ monit-sighup/monitor.h      2003-02-11 17:27:11.000000000 +0100
@@ -376,9 +376,8 @@
 void  finalize_files();
 char *find_rcfile();    
 int   create_pidfile(char *);
-int   is_rcfile_changed();
 int   check_rcfile(char *);
-int   kill_daemon();
+int   kill_daemon(int);
 int   exist_daemon(); 
 void  sendmail(Mail_T);
 int   sock_msg(int sock, char *, ...);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]