41 |
|
|
42 |
|
|
43 |
/* Private Prototypes */ |
/* Private Prototypes */ |
44 |
static void control_process(Process_T p, char *action, int toggle_validate_flag); |
static void do_stop(Process_T p); |
45 |
static void atomic_control_process(Process_T p, char *action); |
static void do_start(Process_T p); |
46 |
static void control_dependant_process(Process_T p, char *action, int toggle_validate_flag); |
static void do_restart_dependandt(Process_T p); |
47 |
static void mark_dependants(Process_T p); |
|
48 |
|
|
49 |
/** |
/** |
50 |
* Methods for controlling processes managed by monit. |
* Methods for controlling processes managed by monit. |
70 |
|
|
71 |
ASSERT(action); |
ASSERT(action); |
72 |
|
|
|
setup_dependants(); |
|
|
|
|
73 |
for(p= processlist; p; p= p->next) { |
for(p= processlist; p; p= p->next) { |
74 |
|
if(p->visited) |
75 |
|
continue; |
76 |
if(exist_daemon()) { |
if(exist_daemon()) { |
77 |
d_check_process(p->name, action); |
d_check_process(p->name, action); |
78 |
} else { |
} else { |
79 |
check_process(p->name, action, toggle_validate_flag); |
check_process(p->name, action, toggle_validate_flag); |
80 |
} |
} |
81 |
} |
} |
82 |
|
|
83 |
|
reset_depend(); |
84 |
|
|
85 |
} |
} |
86 |
|
|
97 |
ASSERT(G); |
ASSERT(G); |
98 |
ASSERT(action); |
ASSERT(action); |
99 |
|
|
|
setup_dependants(); |
|
|
|
|
100 |
for(p= processlist; p; p= p->next) { |
for(p= processlist; p; p= p->next) { |
101 |
|
if(p->visited) |
102 |
|
continue; |
103 |
if(is(p->group, G)) { |
if(is(p->group, G)) { |
104 |
if(exist_daemon()) { |
if(exist_daemon()) { |
105 |
d_check_process(p->name, action); |
d_check_process(p->name, action); |
107 |
check_process(p->name, action, toggle_validate_flag); |
check_process(p->name, action, toggle_validate_flag); |
108 |
} |
} |
109 |
} |
} |
|
|
|
110 |
} |
} |
111 |
|
|
112 |
|
reset_depend(); |
113 |
|
|
114 |
} |
} |
115 |
|
|
116 |
|
|
223 |
} |
} |
224 |
|
|
225 |
} |
} |
226 |
|
|
|
|
|
|
/* If parent process isn't running, then return, as validate will |
|
|
* pick up the parent process and start the dependants. |
|
|
*/ |
|
227 |
if(is(action, "start")) { |
if(is(action, "start")) { |
228 |
|
|
229 |
if( is_process_running(p) ) { |
if(is_process_running(p)) { |
230 |
return; |
return; |
231 |
} |
} |
232 |
|
|
236 |
return; |
return; |
237 |
} |
} |
238 |
|
|
239 |
if(p->parent) { |
do_start(p); |
240 |
if( !is_process_running(get_process(p->parent)) ) { |
do_restart_dependandt(p); |
241 |
log("Skipping '%s', waiting for '%s' to start\n", |
|
242 |
p->name, p->parent); |
} else if(is(action, "stop")) { |
243 |
return; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if(is(action, "stop")) { |
|
|
|
|
244 |
if(!p->stop) { |
if(!p->stop) { |
245 |
error("%s: Stop method not defined -- process %s\n", |
error("%s: Stop method not defined -- process %s\n", |
246 |
prog, P); |
prog, P); |
247 |
return; |
return; |
248 |
} |
} |
249 |
|
|
250 |
|
do_stop(p); |
251 |
|
|
252 |
} |
} |
253 |
|
|
|
control_process(p, action, toggle_validate_flag); |
|
|
|
|
254 |
} |
} |
255 |
|
|
256 |
|
|
257 |
/** |
/* |
258 |
* Setup dependant processes for checking |
* Reset the visited flag used when handling dependencies |
259 |
*/ |
*/ |
260 |
|
void reset_depend() { |
|
void setup_dependants() { |
|
261 |
|
|
262 |
Process_T p; |
Process_T p; |
263 |
|
|
264 |
for(p= processlist; p; p= p->next) { |
for (p= processlist; p; p= p->next) { |
|
|
|
265 |
p->visited= FALSE; |
p->visited= FALSE; |
266 |
p->parent= NULL; |
p->depend_visited= FALSE; |
|
|
|
|
} |
|
|
|
|
|
for(p= processlist; p; p= p->next) { |
|
|
|
|
|
if(p->dependantlist != NULL) { |
|
|
|
|
|
mark_dependants(p); |
|
|
|
|
|
} |
|
|
|
|
267 |
} |
} |
268 |
|
|
269 |
} |
} |
270 |
|
|
271 |
|
|
272 |
/* ----------------------------------------------------------------- Private */ |
/* ----------------------------------------------------------------- Private */ |
273 |
|
|
274 |
|
|
275 |
/** |
/* |
276 |
* Execute an action for the submited process |
* This is a post-recursive function for first starting all |
277 |
* @param p A Process object |
* non-dependending processes before staring the given processes. |
278 |
* @param action A string describing the action to execute |
* @param p A Process_T object |
|
* @param daemon Flag indicating if the monit daemon should execute the action |
|
279 |
*/ |
*/ |
280 |
void control_process(Process_T p, char *action, int toggle_validate_flag) { |
static void do_start(Process_T p) { |
281 |
|
|
282 |
ASSERT(action); |
int max_tries= Run.polltime; |
283 |
|
|
284 |
/* Stop dependant processes */ |
ASSERT(p); |
|
control_dependant_process(p, "stop", toggle_validate_flag); |
|
|
|
|
|
/* Start/stop the process we asked for */ |
|
|
atomic_control_process(p, action); |
|
285 |
|
|
286 |
/* Restart dependant processes if needed */ |
if(p->visited) |
287 |
if(is(action, "start")) { |
return; |
288 |
control_dependant_process(p, action, toggle_validate_flag); |
|
289 |
|
p->visited= TRUE; |
290 |
|
|
291 |
|
if(p->dependantlist) { |
292 |
|
|
293 |
|
Dependant_T d; |
294 |
|
|
295 |
|
for(d= p->dependantlist; d; d= d->next ) { |
296 |
|
|
297 |
|
Process_T dp= get_process(d->dependant); |
298 |
|
ASSERT(dp); |
299 |
|
do_start(dp); |
300 |
|
|
301 |
|
} |
302 |
} |
} |
303 |
|
|
304 |
|
if(p->start && (!is_process_running(p))) { |
305 |
|
log("start: (%s) %s\n", p->name, p->start->arg[0]); |
306 |
|
spawn(p, p->start); |
307 |
|
/* Wait until the process is actually started */ |
308 |
|
while(max_tries--) { |
309 |
|
if(is_process_running(p)) |
310 |
|
break; |
311 |
|
sleep(1); |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
} |
} |
316 |
|
|
317 |
|
|
318 |
/* |
/* |
319 |
* Execute the given action for the process |
* This is a pre-recursive function for first stopping all |
320 |
|
* dependending processes before stopping the given processes. |
321 |
* @param p A Process_T object |
* @param p A Process_T object |
|
* @param action A string describing the action to execute |
|
322 |
*/ |
*/ |
323 |
static void atomic_control_process(Process_T p, char *action) { |
static void do_stop(Process_T p) { |
324 |
|
|
325 |
int max_tries= Run.polltime; |
int max_tries= Run.polltime; |
326 |
|
|
327 |
ASSERT(action); |
ASSERT(p); |
|
|
|
|
if(p->visited) { |
|
|
return; |
|
|
} |
|
328 |
|
|
329 |
if(is(action, "start")) { |
if(p->depend_visited) |
330 |
ASSERT(p->start); |
return; |
331 |
if(! is_process_running(p)) { |
|
332 |
log("%s: (%s) %s\n", action, p->name, p->start->arg[0]); |
p->depend_visited= TRUE; |
333 |
spawn(p, p->start); |
|
334 |
p->visited= TRUE; |
if(p->stop && is_process_running(p)) { |
335 |
while( max_tries--) { |
log("stop: (%s) %s\n", p->name, p->stop->arg[0]); |
336 |
if(!is_process_running(p)) { |
spawn(p, p->stop); |
337 |
sleep(1); |
memset(p->procinfo, 0, sizeof *(p->procinfo)); |
338 |
} else { |
/* Wait until the process is actually stopped */ |
339 |
max_tries= 0; |
while(max_tries--) { |
340 |
} |
if(!is_process_running(p)) |
341 |
} |
break; |
342 |
|
sleep(1); |
343 |
} |
} |
344 |
} |
} |
345 |
|
|
346 |
else if(is(action, "stop")) { |
if(p->dependantlist) { |
347 |
ASSERT(p->stop); |
|
348 |
if(is_process_running(p)) { |
Dependant_T d; |
349 |
log("%s: (%s) %s\n", action, p->name, p->stop->arg[0]); |
|
350 |
spawn(p, p->stop); |
for(d= p->dependantlist; d; d= d->next ) { |
351 |
memset(p->procinfo, 0, sizeof *(p->procinfo)); |
Process_T dp= get_process(d->dependant); |
352 |
p->visited= TRUE; |
ASSERT(dp); |
353 |
|
do_stop(dp); |
354 |
} |
} |
355 |
} |
} |
356 |
|
|
357 |
} |
} |
358 |
|
|
359 |
|
|
360 |
/* |
/* |
361 |
* Start/stop dependant processes |
* This is a pre-recursive function called after p was started and |
362 |
* @param p A Process_T object @param action A start/stop action |
* used to restart every process that depends on p, in reverse order. |
363 |
* @param daemon Flag indicating if the monit daemon should execute the action |
* @param p A Process_T object |
364 |
*/ |
*/ |
365 |
static void control_dependant_process(Process_T p, char *action, |
static void do_restart_dependandt(Process_T p) { |
|
int toggle_validate_flag) { |
|
|
|
|
|
Dependant_T d; |
|
|
|
|
|
ASSERT(action); |
|
366 |
|
|
367 |
for(d= p->dependantlist; d; d= d->next) { |
Process_T parent; |
368 |
if(d->dependant == NULL) { |
int max_tries= Run.polltime; |
|
break; |
|
|
} |
|
|
check_process(d->dependant, action, toggle_validate_flag); |
|
|
} |
|
369 |
|
|
370 |
} |
ASSERT(p); |
|
|
|
|
/** |
|
|
* Mark the visited flag on dependant so we don't visit them more than once |
|
|
*/ |
|
|
void mark_dependants(Process_T p) { |
|
|
|
|
|
Process_T dp; |
|
|
Dependant_T d; |
|
|
|
|
|
for(d= p->dependantlist; d; d= d->next ) { |
|
371 |
|
|
372 |
if((dp= get_process(d->dependant)) != NULL) { |
for(parent= processlist; parent; parent= parent->next) { |
373 |
|
|
374 |
dp->parent= p->name; |
if(parent->dependantlist) { |
|
|
|
|
} else { |
|
|
|
|
|
break; |
|
375 |
|
|
376 |
} |
Dependant_T d; |
377 |
|
|
378 |
|
for(d= parent->dependantlist; d; d= d->next ) |
379 |
|
if(is(d->dependant, p->name)) |
380 |
|
break; |
381 |
|
if(d && p->stop && p->start) { |
382 |
|
if(is_process_running(parent)) { |
383 |
|
log("stop: (%s) %s\n", parent->name, parent->stop->arg[0]); |
384 |
|
spawn(parent, parent->stop); |
385 |
|
memset(parent->procinfo, 0, sizeof *(parent->procinfo)); |
386 |
|
/* Wait until the process is actually stopped before atempting |
387 |
|
* to restart it */ |
388 |
|
while(max_tries--) { |
389 |
|
if(!is_process_running(parent)) |
390 |
|
break; |
391 |
|
sleep(1); |
392 |
|
} |
393 |
|
} |
394 |
|
if(! is_process_running(parent)) { |
395 |
|
log("start: (%s) %s\n", parent->name, parent->start->arg[0]); |
396 |
|
spawn(parent, parent->start); |
397 |
|
} |
398 |
|
do_restart_dependandt(parent); |
399 |
|
} |
400 |
|
} |
401 |
} |
} |
402 |
|
|
403 |
} |
} |
404 |
|
|
|
|
|