/[monit]/monit/control.c
ViewVC logotype

Diff of /monit/control.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.28 by hauk, Thu Dec 19 22:03:46 2002 UTC revision 1.29 by hauk, Fri Dec 20 17:45:38 2002 UTC
# Line 41  Line 41 
41    
42    
43  /* Private Prototypes */  /* Private Prototypes */
44  static void do_stop(Process_T);  static int wait_stop(Process_T);
45  static void do_start(Process_T);  static int wait_start(Process_T);
46  static void do_dependandt(Process_T, char *);  static void do_stop(Process_T, int);
47    static void do_start(Process_T, int);
48    static void do_dependant(Process_T, char *, int);
49    
50    
51  /**  /**
# Line 186  void d_check_process(char *P, char *acti Line 188  void d_check_process(char *P, char *acti
188   * Check to see if we should try to start/stop process   * Check to see if we should try to start/stop process
189   * @param name A process name as stated in the config file   * @param name A process name as stated in the config file
190   * @param action A string describing the action to execute   * @param action A string describing the action to execute
191     * @param toggle_validate_flag passed on to the start/stop functions
192   */   */
193  void check_process(char *P, char *action, int toggle_validate_flag) {  void check_process(char *P, char *action, int toggle_validate_flag) {
194    
# Line 200  void check_process(char *P, char *action Line 203  void check_process(char *P, char *action
203      return;      return;
204    }    }
205    
   if(toggle_validate_flag) {  
       
     if( is(action, "start")) {  
       LOCK(Run.mutex)  
           p->do_validate= TRUE;  
       END_LOCK;  
         
       if(Run.debug)  
           log("Monitoring enabled -- process %s\n", p->name);  
     }  
   }  
   
206    if(is(action, "start")) {    if(is(action, "start")) {
207            
208      if(is_process_running(p)) {      if(is_process_running(p)) {
# Line 224  void check_process(char *P, char *action Line 215  void check_process(char *P, char *action
215        return;        return;
216      }      }
217            
218      do_dependandt(p, "stop");      do_dependant(p, "stop", toggle_validate_flag);
219      do_start(p);      do_start(p, toggle_validate_flag);
220      do_dependandt(p, "start");      do_dependant(p, "start", toggle_validate_flag);
221            
222    } else if(is(action, "stop")) {    } else if(is(action, "stop")) {
223            
# Line 236  void check_process(char *P, char *action Line 227  void check_process(char *P, char *action
227        return;        return;
228      }      }
229            
230      do_stop(p);      do_dependant(p, "stop", toggle_validate_flag);
231        do_stop(p, toggle_validate_flag);
232            
233    }    }
     
   if(toggle_validate_flag) {  
234            
     if( is(action, "stop")) {  
       LOCK(Run.mutex)  
           p->do_validate= FALSE;  
       END_LOCK;  
         
       if(Run.debug)  
           log("Monitoring disabled -- process %s\n", p->name);  
     }  
   }  
     
235  }  }
236    
237    
# Line 274  void reset_depend() { Line 254  void reset_depend() {
254    
255    
256  /*  /*
257   * This is a post-recursive function for first starting all   * This is a post-recursive function for starting every process that p
258   * non-dependending processes before staring the given processes.   * depends on before starting p.
259   * @param p A Process_T object   * @param p A Process_T object
260     * @param toggle_validate_flag Turn on validate if defined
261   */   */
262  static void do_start(Process_T p) {  static void do_start(Process_T p, int toggle_validate_flag) {
   
   int max_tries= Run.polltime;  
263    
264    ASSERT(p);    ASSERT(p);
265    
# Line 297  static void do_start(Process_T p) { Line 276  static void do_start(Process_T p) {
276                
277        Process_T dp= get_process(d->dependant);        Process_T dp= get_process(d->dependant);
278        ASSERT(dp);        ASSERT(dp);
279        do_start(dp);        do_start(dp, toggle_validate_flag);
280                
281      }      }
282    }    }
283        
284      if(toggle_validate_flag) {
285        
286        LOCK(Run.mutex)
287            p->do_validate= TRUE;
288        END_LOCK;
289        
290        if(Run.debug)
291            log("Monitoring enabled -- process %s\n", p->name);
292      }
293    
294    if(p->start && p->do_validate && (!is_process_running(p))) {    if(p->start && p->do_validate && (!is_process_running(p))) {
295      log("start: (%s) %s\n", p->name, p->start->arg[0]);      log("start: (%s) %s\n", p->name, p->start->arg[0]);
296      spawn(p, p->start);      spawn(p, p->start);
297      /* Wait until the process is actually started */      wait_start(p);
     while(max_tries--) {  
       if(is_process_running(p))  
           break;  
       sleep(1);  
     }  
298    }    }
299        
300  }  }
301    
302    
303  /*  /*
304   * This is a pre-recursive function for first stopping all   * This function simply stops the process p.
  * dependending processes before stopping the given processes.  
305   * @param p A Process_T object   * @param p A Process_T object
306     * @param toggle_validate_flag Turn off validate if defined
307   */   */
308  static void do_stop(Process_T p) {  static void do_stop(Process_T p, int toggle_validate_flag) {
   
   int max_tries= Run.polltime;  
309    
310    ASSERT(p);    ASSERT(p);
311    
# Line 336  static void do_stop(Process_T p) { Line 318  static void do_stop(Process_T p) {
318      log("stop: (%s) %s\n", p->name, p->stop->arg[0]);      log("stop: (%s) %s\n", p->name, p->stop->arg[0]);
319      spawn(p, p->stop);      spawn(p, p->stop);
320      memset(p->procinfo, 0, sizeof *(p->procinfo));      memset(p->procinfo, 0, sizeof *(p->procinfo));
321      /* Wait until the process is actually stopped */      wait_stop(p);
     while(max_tries--) {  
       if(!is_process_running(p))  
           break;  
       sleep(1);  
     }  
322    }    }
323    
324    if(p->dependantlist) {    memset(p->procinfo, 0, sizeof *(p->procinfo));
325          
326      Dependant_T d;    if(toggle_validate_flag) {
327            LOCK(Run.mutex)
328      for(d= p->dependantlist; d; d= d->next ) {          p->do_validate= FALSE;
329        Process_T dp= get_process(d->dependant);      END_LOCK;
330        ASSERT(dp);      if(Run.debug)
331        do_stop(dp);          log("Monitoring disabled -- process %s\n", p->name);
     }  
332    }    }
333    
334  }  }
# Line 366  static void do_stop(Process_T p) { Line 342  static void do_stop(Process_T p) {
342   * procceses that depends on p.   * procceses that depends on p.
343   * @param p A Process_T object   * @param p A Process_T object
344   * @param action An action to do on the dependant processes   * @param action An action to do on the dependant processes
345     * @param toggle_validate_flag passed to the start/stop functions
346   */   */
347  static void do_dependandt(Process_T p, char *action) {  static void do_dependant(Process_T p, char *action, int toggle_validate_flag) {
348    
349    Process_T parent;    Process_T parent;
   int max_tries= Run.polltime;  
350        
351    ASSERT(p);    ASSERT(p);
352    
353    for(parent= processlist; parent; parent= parent->next) {    for(parent= processlist; parent; parent= parent->next) {
354            
355      if(parent->dependantlist) {      if(parent->dependantlist) {
356          
357        Dependant_T d;        Dependant_T d;
358            
359        for(d= parent->dependantlist; d; d= d->next)        for(d= parent->dependantlist; d; d= d->next)
360            if(is(d->dependant, p->name))            if(is(d->dependant, p->name))
361                break;               break;
362          
363        if(d && p->stop && p->start && p->do_validate) {        if(d && p->stop && p->start && p->do_validate) {
364    
365          if(is(action, "stop")) {          if(is(action, "stop")) {
366            if(is_process_running(parent)) {            if(is_process_running(parent)) {
367              log("stop: (%s) %s\n", parent->name, parent->stop->arg[0]);              do_stop(parent, toggle_validate_flag);
             spawn(parent, parent->stop);  
             memset(parent->procinfo, 0, sizeof *(parent->procinfo));  
             /* Wait until the process is actually stopped */  
             while(max_tries--) {  
               if(!is_process_running(parent))  
                   break;  
               sleep(1);  
             }  
368            }            }
369          } else if(is(action, "start")) {          } else if(is(action, "start")) {
370            if(! is_process_running(parent)) {            if(! is_process_running(parent)) {
371              log("start: (%s) %s\n", parent->name, parent->start->arg[0]);              do_start(parent, toggle_validate_flag);
             spawn(parent, parent->start);  
372            }            }
373          }          }
374          do_dependandt(parent, action);          do_dependant(parent, action, toggle_validate_flag);
375        }        }
376          
377      }      }
378    }    }
379        
380  }  }
381            
382    
383    /*
384     * This function suspend the control until the process p is running.
385     * @param p A Process to wait for
386     * @return TRUE if the wait succeded otherwise FALSE if the process
387     * was not started during the wait.
388     */
389    static int wait_start(Process_T p) {
390    
391      int max_tries= Run.polltime;
392      
393      while(max_tries--) {
394        if(is_process_running(p))
395            break;
396        sleep(1);
397      }
398    
399      return is_process_running(p);
400      
401    }
402    
403    
404    /*
405     * This function suspend the control until the process p is stopped.
406     * @param p A Process to wait for
407     * @return TRUE if the wait succeded otherwise FALSE if the process
408     * was not stopped during the wait.
409     */
410    static int wait_stop(Process_T p) {
411    
412      int max_tries= Run.polltime;
413      
414      while(max_tries--) {
415        if(!is_process_running(p))
416            break;
417        sleep(1);
418      }
419    
420      return !is_process_running(p);
421      
422    }

Legend:
Removed from v.1.28  
changed lines
  Added in v.1.29

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26