diff options
author | Roy Marples <roy@marples.name> | 2007-09-28 14:53:38 +0000 |
---|---|---|
committer | Roy Marples <roy@marples.name> | 2007-09-28 14:53:38 +0000 |
commit | 7274301be24a70c094e8c91a36f3e8b6156f53ba (patch) | |
tree | 930d832f2ef37f50c99a1363958c9bfd57f05e41 /src/librc.c | |
parent | befddaf2418cf4a732a46aecfb79cb9cf64b3a40 (diff) |
rc_service_state now returns the state as a mask, which means that we can do things with just the one call making is more efficient.
Diffstat (limited to 'src/librc.c')
-rw-r--r-- | src/librc.c | 64 |
1 files changed, 31 insertions, 33 deletions
diff --git a/src/librc.c b/src/librc.c index 366c7c49..f3495cbe 100644 --- a/src/librc.c +++ b/src/librc.c @@ -23,6 +23,8 @@ typedef struct rc_service_state_name { const char *name; } rc_service_state_name_t; +/* We MUST list the states below 0x10 first + * The rest can be in any order */ static const rc_service_state_name_t rc_service_state_names[] = { { RC_SERVICE_STARTED, "started" }, { RC_SERVICE_STOPPED, "stopped" }, @@ -428,43 +430,39 @@ bool rc_mark_service (const char *service, const rc_service_state_t state) } librc_hidden_def(rc_mark_service) -bool rc_service_state (const char *service, const rc_service_state_t state) +rc_service_state_t rc_service_state (const char *service) { - char *file; - bool retval; - char *svc; + int i; + int state = RC_SERVICE_STOPPED; + char *svc = rc_xstrdup (service); - /* If the init script does not exist then we are stopped */ - if (! rc_service_exists (service)) - return (state == RC_SERVICE_STOPPED ? true : false); - - /* We check stopped state by not being in any of the others */ - if (state == RC_SERVICE_STOPPED) - return ( ! (rc_service_state (service, RC_SERVICE_STARTED) || - rc_service_state (service, RC_SERVICE_STARTING) || - rc_service_state (service, RC_SERVICE_STOPPING) || - rc_service_state (service, RC_SERVICE_INACTIVE))); - - /* The crashed state and scheduled states are virtual */ - if (state == RC_SERVICE_CRASHED) - return (rc_service_daemons_crashed (service)); - else if (state == RC_SERVICE_SCHEDULED) { + for (i = 0; rc_service_state_names[i].name; i++) { + char *file = rc_strcatpaths (RC_SVCDIR, rc_service_state_names[i].name, + basename (svc), (char*) NULL); + if (rc_exists (file)) { + if (rc_service_state_names[i].state <= 0x10) + state = rc_service_state_names[i].state; + else + state |= rc_service_state_names[i].state; + } + free (file); + } + free (svc); + + if (state & RC_SERVICE_STOPPED) { char **services = rc_services_scheduled_by (service); - retval = (services); - if (services) + if (services) { + state |= RC_SERVICE_SCHEDULED; free (services); - return (retval); + } } - /* Now we just check if a file by the service name rc_exists - in the state dir */ - svc = rc_xstrdup (service); - file = rc_strcatpaths (RC_SVCDIR, rc_parse_service_state (state), - basename (svc), (char*) NULL); - free (svc); - retval = rc_exists (file); - free (file); - return (retval); + if (state & RC_SERVICE_STARTED && geteuid () == 0) { + if (rc_service_daemons_crashed (service)) + state |= RC_SERVICE_CRASHED; + } + + return (state); } librc_hidden_def(rc_service_state) @@ -585,7 +583,7 @@ librc_hidden_def(rc_waitpid) pid_t rc_stop_service (const char *service) { - if (rc_service_state (service, RC_SERVICE_STOPPED)) + if (rc_service_state (service) & RC_SERVICE_STOPPED) return (0); return (_exec_service (service, "stop")); @@ -594,7 +592,7 @@ librc_hidden_def(rc_stop_service) pid_t rc_start_service (const char *service) { - if (! rc_service_state (service, RC_SERVICE_STOPPED)) + if (! rc_service_state (service) & RC_SERVICE_STOPPED) return (0); return (_exec_service (service, "start")); |