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

Diff of /monit/control.c

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

revision 1.61 by hauk, Sun Aug 17 02:21:21 2003 UTC revision 1.62 by martinp, Wed Aug 27 01:42:40 2003 UTC
# Line 69  Line 69 
69  /* -------------------------------------------------------------- Prototypes */  /* -------------------------------------------------------------- Prototypes */
70    
71    
72  static int  do_stop(Service_T);  static void  do_start(Service_T);
73  static void do_start(Service_T);  static int   do_stop(Service_T);
74    static void  do_monitor(Service_T);
75    static void  do_unmonitor(Service_T);
76    static void  monitor_set(Service_T);
77    static void  monitor_unset(Service_T);
78  static void *wait_start(void *);  static void *wait_start(void *);
79  static int  wait_stop(Service_T);  static int   wait_stop(Service_T);
80  static void do_depend(Service_T, char *);  static void  do_depend(Service_T, char *);
81    
82    
83  /* ------------------------------------------------------------------ Public */  /* ------------------------------------------------------------------ Public */
# Line 190  void check_service(char *P, char *action Line 194  void check_service(char *P, char *action
194    
195    if(IS(action, "start")) {    if(IS(action, "start")) {
196            
     if(!s->do_monitor) {  
         
       LOCK(Run.mutex)  
           s->do_monitor= TRUE;  
       END_LOCK;  
         
       DEBUG("Monitoring enabled -- process %s\n", s->name);  
   
     }  
     
197      if(s->type==TYPE_PROCESS && is_process_running(s)) {      if(s->type==TYPE_PROCESS && is_process_running(s)) {
198          DEBUG("%s: Process already running -- process %s\n", prog, P);
199          monitor_set(s);
200        return;        return;
201      }      }
202            
203      if(s->type==TYPE_PROCESS && !s->start) {      if(s->type==TYPE_PROCESS && !s->start) {
204        DEBUG("%s: Start method not defined -- process %s\n",        DEBUG("%s: Start method not defined -- process %s\n", prog, P);
205              prog, P);        monitor_set(s);
206        return;        return;
207      }      }
208            
# Line 217  void check_service(char *P, char *action Line 213  void check_service(char *P, char *action
213    } else if(IS(action, "stop")) {    } else if(IS(action, "stop")) {
214            
215      if(s->type==TYPE_PROCESS && !s->stop) {      if(s->type==TYPE_PROCESS && !s->stop) {
216        DEBUG("%s: Stop method not defined -- process %s\n",        DEBUG("%s: Stop method not defined -- process %s\n", prog, P);
217              prog, P);        monitor_unset(s);
218        return;        return;
219      }      }
220            
# Line 228  void check_service(char *P, char *action Line 224  void check_service(char *P, char *action
224    } else if(IS(action, "restart")) {    } else if(IS(action, "restart")) {
225            
226      if(s->type==TYPE_PROCESS && (!s->start || !s->stop)) {      if(s->type==TYPE_PROCESS && (!s->start || !s->stop)) {
227        DEBUG("%s: Start or stop method not defined -- process %s\n",        DEBUG("%s: Start or stop method not defined -- process %s\n", prog, P);
228              prog, P);        monitor_set(s);
229        return;        return;
230      } else {      } else {
231        log("Trying to restart '%s'\n", s->name);        log("Trying to restart '%s'\n", s->name);
# Line 242  void check_service(char *P, char *action Line 238  void check_service(char *P, char *action
238        do_depend(s, "start");        do_depend(s, "start");
239      }      }
240            
241      } else if(IS(action, "monitor")) {
242        
243        do_monitor(s);
244        
245      } else if(IS(action, "unmonitor")) {
246        
247        do_unmonitor(s);
248        
249    }    }
250            
251  }  }
# Line 275  static void do_start(Service_T s) { Line 279  static void do_start(Service_T s) {
279    ASSERT(s);    ASSERT(s);
280    
281    if(s->visited)    if(s->visited)
282        return;      return;
283        
284    s->visited= TRUE;    s->visited= TRUE;
285        
# Line 285  static void do_start(Service_T s) { Line 289  static void do_start(Service_T s) {
289            
290      for(d= s->dependantlist; d; d= d->next ) {      for(d= s->dependantlist; d; d= d->next ) {
291                
292        Service_T dp= get_service(d->dependant);        Service_T parent= get_service(d->dependant);
293        ASSERT(dp);        ASSERT(parent);
294        do_start(dp);        do_start(parent);
295                
296      }      }
297    
298    }    }
299        
300    if(s->start && (s->type!=TYPE_PROCESS || !is_process_running(s))) {    if(s->start && (s->type!=TYPE_PROCESS || !is_process_running(s))) {
301      int status;      int status;
302      pthread_t thread;      pthread_t thread;
303    
304      log("start: (%s) %s\n", s->name, s->start->arg[0]);      log("start: (%s) %s\n", s->name, s->start->arg[0]);
305      spawn(s, s->start);      spawn(s, s->start);
306      if(s->type==TYPE_PROCESS) {      if(s->type==TYPE_PROCESS) {
# Line 308  static void do_start(Service_T s) { Line 314  static void do_start(Service_T s) {
314      }      }
315    }    }
316    
317    LOCK(Run.mutex)    monitor_set(s);
     s->do_monitor= TRUE;  
   END_LOCK;  
   
   DEBUG("Monitoring enabled -- process %s\n", s->name);  
318    
319  }  }
320    
# Line 327  static int do_stop(Service_T s) { Line 329  static int do_stop(Service_T s) {
329    ASSERT(s);    ASSERT(s);
330    
331    if(s->depend_visited)    if(s->depend_visited)
332        return TRUE;      return TRUE;
333      
334    s->depend_visited= TRUE;    s->depend_visited= TRUE;
335      
336    LOCK(Run.mutex)    monitor_unset(s);
     s->do_monitor= FALSE;  
   END_LOCK;  
337    
   DEBUG("Monitoring disabled -- service %s\n", s->name);  
       
338    /* Reset the proc info object in case of a later restart */    /* Reset the proc info object in case of a later restart */
339    memset(s->procinfo, 0, sizeof *(s->procinfo));    memset(s->procinfo, 0, sizeof *(s->procinfo));
340      
341    if(s->stop && (s->type!=TYPE_PROCESS || is_process_running(s))) {    if(s->stop && (s->type!=TYPE_PROCESS || is_process_running(s))) {
342      log("stop: (%s) %s\n", s->name, s->stop->arg[0]);      log("stop: (%s) %s\n", s->name, s->stop->arg[0]);
343      spawn(s, s->stop);      spawn(s, s->stop);
# Line 348  static int do_stop(Service_T s) { Line 346  static int do_stop(Service_T s) {
346        /* Only wait for process service types */        /* Only wait for process service types */
347        return wait_stop(s);        return wait_stop(s);
348      }      }
349        
350    }    }
351    
352    return TRUE;    return TRUE;
353    
354    }
355    
356    
357    /*
358     * This is a post- fix recursive function for enabling monitoring every service
359     * that s depends on before monitor s.
360     * @param s A Service_T object
361     */
362    static void do_monitor(Service_T s) {
363    
364      ASSERT(s);
365    
366      if(s->visited)
367        return;
368      
369      s->visited= TRUE;
370      
371      if(s->dependantlist) {
372        
373        Dependant_T d;
374        
375        for(d= s->dependantlist; d; d= d->next ) {
376          
377          Service_T parent= get_service(d->dependant);
378          ASSERT(parent);
379          do_monitor(parent);
380          
381        }
382      }
383      
384      monitor_set(s);
385      
386    }
387    
388    
389    /*
390     * This is a post- fix recursive function for disabling monitoring every service
391     * that s depends on before unmonitor s.
392     * @param s A Service_T object
393     */
394    static void do_unmonitor(Service_T s) {
395    
396      ASSERT(s);
397    
398      if(s->depend_visited)
399        return;
400    
401      s->depend_visited= TRUE;
402      
403      monitor_unset(s);
404      
405      /* Reset the proc info object in case of a later restart */
406      memset(s->procinfo, 0, sizeof *(s->procinfo));
407        
408      if(s->dependantlist) {
409        
410        Dependant_T d;
411        
412        for(d= s->dependantlist; d; d= d->next ) {
413          
414          Service_T parent= get_service(d->dependant);
415          ASSERT(parent);
416          do_unmonitor(parent);
417          
418        }
419      }
420    }
421    
422    
423    /*
424     * This will enable service monitoring in the case that it was disabled
425     * @param s A Service_T object
426     */
427    static void monitor_set(Service_T s) {
428    
429      ASSERT(s);
430    
431      if(!s->do_monitor) {
432        LOCK(Run.mutex)
433          s->do_monitor= TRUE;
434        END_LOCK;
435        DEBUG("Monitoring enabled -- process %s\n", s->name);
436      }
437    
438    }
439    
440    
441    /*
442     * This will disable service monitoring in the case that it is enabled
443     * @param s A Service_T object
444     */
445    static void monitor_unset(Service_T s) {
446    
447      ASSERT(s);
448    
449      if(s->do_monitor) {
450        LOCK(Run.mutex)
451          s->do_monitor= FALSE;
452        END_LOCK;
453        DEBUG("Monitoring disabled -- process %s\n", s->name);
454      }
455    
456  }  }
457    
458    
# Line 367  static int do_stop(Service_T s) { Line 467  static int do_stop(Service_T s) {
467   */   */
468  static void do_depend(Service_T s, char *action) {  static void do_depend(Service_T s, char *action) {
469    
470    Service_T parent;    Service_T child;
471        
472    ASSERT(s);    ASSERT(s);
473    
474    for(parent= servicelist; parent; parent= parent->next) {    for(child= servicelist; child; child= child->next) {
475            
476      if(parent->dependantlist) {      if(child->dependantlist) {
477    
478        Dependant_T d;        Dependant_T d;
479            
480        for(d= parent->dependantlist; d; d= d->next)        for(d= child->dependantlist; d; d= d->next) {
481            if(IS(d->dependant, s->name))  
482               break;          if(IS(d->dependant, s->name)) {
         
       if(d) {  
           
         if(IS(action, "start"))  
             do_start(parent);  
483                    
484          do_depend(parent, action);            if(IS(action, "start"))
485                do_start(child);
486                    
487          if(IS(action, "stop"))            do_depend(child, action);
             do_stop(parent);  
488                    
489              if(IS(action, "stop"))
490                do_stop(child);
491    
492              return;
493    
494            }
495        }        }
496      }      }
497    }    }

Legend:
Removed from v.1.61  
changed lines
  Added in v.1.62

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