39 |
#include "monitor.h" |
#include "monitor.h" |
40 |
#include "net.h" |
#include "net.h" |
41 |
#include "socket.h" |
#include "socket.h" |
42 |
|
#include "event.h" |
43 |
|
|
44 |
|
|
45 |
/** |
/** |
58 |
/* -------------------------------------------------------------- Prototypes */ |
/* -------------------------------------------------------------- Prototypes */ |
59 |
|
|
60 |
|
|
61 |
|
static void *wait_stop(void *); |
62 |
|
static void *wait_start(void *); |
63 |
static void do_stop(Service_T); |
static void do_stop(Service_T); |
64 |
static void do_start(Service_T); |
static void do_start(Service_T); |
|
static void wait_stop(Service_T); |
|
|
static void wait_start(Service_T); |
|
65 |
static void do_depend(Service_T, char *); |
static void do_depend(Service_T, char *); |
66 |
|
|
67 |
|
|
283 |
if(s->start && (s->task!=TASK_PROCESS || !is_process_running(s))) { |
if(s->start && (s->task!=TASK_PROCESS || !is_process_running(s))) { |
284 |
log("start: (%s) %s\n", s->name, s->start->arg[0]); |
log("start: (%s) %s\n", s->name, s->start->arg[0]); |
285 |
spawn(s, s->start); |
spawn(s, s->start); |
286 |
if(s->task==TASK_PROCESS) |
if(s->task==TASK_PROCESS) { |
287 |
wait_start(s); |
int status; |
288 |
|
pthread_t thread; |
289 |
|
status= pthread_create(&thread, NULL, wait_start, s); |
290 |
|
if(status != 0) { |
291 |
|
log("Warning: Failed to create the start control thread. " |
292 |
|
"Thread error -- %s.\n", strerror(status)); |
293 |
|
} |
294 |
|
} |
295 |
} |
} |
296 |
|
|
297 |
|
|
298 |
LOCK(Run.mutex) |
LOCK(Run.mutex) |
299 |
s->do_monitor= TRUE; |
s->do_monitor= TRUE; |
326 |
if(s->stop && (s->task!=TASK_PROCESS || is_process_running(s))) { |
if(s->stop && (s->task!=TASK_PROCESS || is_process_running(s))) { |
327 |
log("stop: (%s) %s\n", s->name, s->stop->arg[0]); |
log("stop: (%s) %s\n", s->name, s->stop->arg[0]); |
328 |
spawn(s, s->stop); |
spawn(s, s->stop); |
329 |
if(s->task==TASK_PROCESS) |
if(s->task==TASK_PROCESS) { |
330 |
wait_stop(s); |
int status; |
331 |
|
pthread_t thread; |
332 |
|
status= pthread_create(&thread, NULL, wait_stop, s); |
333 |
|
if(status != 0) { |
334 |
|
log("Warning: Failed to create the stop control thread. " |
335 |
|
"Thread error -- %s.\n", strerror(status)); |
336 |
|
} |
337 |
|
} |
338 |
} |
} |
339 |
|
|
340 |
/* Reset the proc info object in case of a later restart */ |
/* Reset the proc info object in case of a later restart */ |
341 |
memset(s->procinfo, 0, sizeof *(s->procinfo)); |
memset(s->procinfo, 0, sizeof *(s->procinfo)); |
342 |
|
|
386 |
|
|
387 |
/* |
/* |
388 |
* This function suspend the control until the service s is running. |
* This function suspend the control until the service s is running. |
389 |
* @param s A Service to wait for |
* @param service A Service to wait for |
390 |
*/ |
*/ |
391 |
static void wait_start(Service_T s) { |
static void *wait_start(void *service) { |
392 |
|
|
393 |
|
Service_T s= service; |
394 |
int max_tries= Run.polltime; |
int max_tries= Run.polltime; |
395 |
|
|
396 |
ASSERT(s); |
ASSERT(s); |
397 |
|
|
398 |
while(max_tries--) { |
pthread_detach(pthread_self()); |
399 |
|
|
400 |
|
while(max_tries-- && !Run.stopped) { |
401 |
if(is_process_running(s)) |
if(is_process_running(s)) |
402 |
break; |
break; |
403 |
sleep(1); |
sleep(1); |
404 |
} |
} |
405 |
|
|
406 |
if(!is_process_running(s)) |
if(!is_process_running(s)) { |
407 |
log("failed: to start (%s)\n", s->name); |
Event_post(s, EVENT_FAILED, "Failed to start '%s'\n", s->name); |
408 |
|
} |
409 |
|
|
410 |
|
return NULL; |
411 |
|
|
412 |
} |
} |
413 |
|
|
414 |
|
|
415 |
/* |
/* |
416 |
* This function suspend the control until the service s is stopped. |
* This function suspend the control until the service s is stopped. |
417 |
* @param s A Service to wait for |
* @param service A Service to wait for |
418 |
*/ |
*/ |
419 |
static void wait_stop(Service_T s) { |
static void *wait_stop(void *service) { |
420 |
|
|
421 |
|
Service_T s= service; |
422 |
int max_tries= Run.polltime; |
int max_tries= Run.polltime; |
423 |
|
|
424 |
ASSERT(s); |
ASSERT(s); |
425 |
|
|
426 |
while(max_tries--) { |
pthread_detach(pthread_self()); |
427 |
|
|
428 |
|
while(max_tries-- && !Run.stopped) { |
429 |
if(!is_process_running(s)) |
if(!is_process_running(s)) |
430 |
break; |
break; |
431 |
sleep(1); |
sleep(1); |
432 |
} |
} |
433 |
|
|
434 |
if(is_process_running(s)) |
if(is_process_running(s)) { |
435 |
log("failed: to stop (%s)\n", s->name); |
Event_post(s, EVENT_FAILED, "Failed to stop '%s'\n", s->name); |
436 |
|
} |
437 |
|
|
438 |
|
return NULL; |
439 |
|
|
440 |
} |
} |