rc-service: add --ifinactive and --ifnotstarted flags

X-Gentoo-Bug: 523174
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=523174
This commit is contained in:
William Hubbs 2014-09-21 13:54:51 -05:00
parent 47dd5e37cb
commit 8a7e4d38a7
2 changed files with 34 additions and 1 deletions

View File

@ -20,6 +20,14 @@
.Ar service cmd .Ar service cmd
.Op Ar ... .Op Ar ...
.Nm .Nm
.Op Fl I , -ifinactive
.Ar service cmd
.Op Ar ...
.Nm
.Op Fl N , -ifnotstarted
.Ar service cmd
.Op Ar ...
.Nm
.Fl e , -exists .Fl e , -exists
.Ar service .Ar service
.Nm .Nm
@ -36,6 +44,13 @@ If
is given then is given then
.Nm .Nm
returns 0 even if the service does not exist. returns 0 even if the service does not exist.
If
.Fl I , -ifinactive
or
.Fl N , -ifnotstarted
is given then
.Nm
returns 0 if the service exists but is in the wrong state.
.Pp .Pp
If given the If given the
.Fl l , -list .Fl l , -list

View File

@ -29,10 +29,12 @@
const char *applet = NULL; const char *applet = NULL;
const char *extraopts = NULL; const char *extraopts = NULL;
const char *getoptstring = "e:ilr:" getoptstring_COMMON; const char *getoptstring = "e:ilr:IN" getoptstring_COMMON;
const struct option longopts[] = { const struct option longopts[] = {
{ "exists", 1, NULL, 'e' }, { "exists", 1, NULL, 'e' },
{ "ifexists", 0, NULL, 'i' }, { "ifexists", 0, NULL, 'i' },
{ "ifinactive", 0, NULL, 'I' },
{ "ifnotstarted", 0, NULL, 'N' },
{ "list", 0, NULL, 'l' }, { "list", 0, NULL, 'l' },
{ "resolve", 1, NULL, 'r' }, { "resolve", 1, NULL, 'r' },
longopts_COMMON longopts_COMMON
@ -40,6 +42,8 @@ const struct option longopts[] = {
const char * const longopts_help[] = { const char * const longopts_help[] = {
"tests if the service exists or not", "tests if the service exists or not",
"if the service exists then run the command", "if the service exists then run the command",
"if the service is inactive then run the command",
"if the service is not started then run the command",
"list all available services", "list all available services",
"resolve the service name to an init script", "resolve the service name to an init script",
longopts_help_COMMON longopts_help_COMMON
@ -56,7 +60,10 @@ int main(int argc, char **argv)
char *service; char *service;
RC_STRINGLIST *list; RC_STRINGLIST *list;
RC_STRING *s; RC_STRING *s;
RC_SERVICE state;
bool if_exists = false; bool if_exists = false;
bool if_inactive = false;
bool if_notstarted = false;
applet = basename_c(argv[0]); applet = basename_c(argv[0]);
/* Ensure that we are only quiet when explicitly told to be */ /* Ensure that we are only quiet when explicitly told to be */
@ -77,6 +84,12 @@ int main(int argc, char **argv)
case 'i': case 'i':
if_exists = true; if_exists = true;
break; break;
case 'I':
if_inactive = true;
break;
case 'N':
if_notstarted = true;
break;
case 'l': case 'l':
list = rc_services_in_runlevel(NULL); list = rc_services_in_runlevel(NULL);
if (TAILQ_FIRST(list) == NULL) if (TAILQ_FIRST(list) == NULL)
@ -113,6 +126,11 @@ int main(int argc, char **argv)
return 0; return 0;
eerrorx("%s: service `%s' does not exist", applet, *argv); eerrorx("%s: service `%s' does not exist", applet, *argv);
} }
state = rc_service_state(*argv);
if (if_inactive && ! (state & RC_SERVICE_INACTIVE))
return 0;
if (if_notstarted && (state & RC_SERVICE_STARTED))
return 0;
*argv = service; *argv = service;
execv(*argv, argv); execv(*argv, argv);
eerrorx("%s: %s", applet, strerror(errno)); eerrorx("%s: %s", applet, strerror(errno));