rc-service: add --ifcrashed option

This works like the other --if options. If the service is crashed, run
the command.

This fixes #154.
This commit is contained in:
William Hubbs 2017-12-04 17:17:17 -06:00
parent cee3919908
commit a2447dfb42
2 changed files with 13 additions and 1 deletions

View File

@ -16,6 +16,10 @@
.Nd locate and run an OpenRC service with the given arguments .Nd locate and run an OpenRC service with the given arguments
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl c , -ifcrashed
.Ar service cmd
.Op Ar ...
.Nm
.Op Fl i , -ifexists .Op Fl i , -ifexists
.Ar service cmd .Ar service cmd
.Op Ar ... .Op Ar ...

View File

@ -29,9 +29,10 @@
const char *applet = NULL; const char *applet = NULL;
const char *extraopts = NULL; const char *extraopts = NULL;
const char *getoptstring = "e:ilr:IN" getoptstring_COMMON; const char *getoptstring = "ce:ilr:IN" getoptstring_COMMON;
const struct option longopts[] = { const struct option longopts[] = {
{ "exists", 1, NULL, 'e' }, { "exists", 1, NULL, 'e' },
{ "ifcrashed", 0, NULL, 'c' },
{ "ifexists", 0, NULL, 'i' }, { "ifexists", 0, NULL, 'i' },
{ "ifinactive", 0, NULL, 'I' }, { "ifinactive", 0, NULL, 'I' },
{ "ifnotstarted", 0, NULL, 'N' }, { "ifnotstarted", 0, NULL, 'N' },
@ -41,6 +42,7 @@ 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 is crashed then run the command",
"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 inactive then run the command",
"if the service is not started then run the command", "if the service is not started then run the command",
@ -61,6 +63,7 @@ int main(int argc, char **argv)
RC_STRINGLIST *list; RC_STRINGLIST *list;
RC_STRING *s; RC_STRING *s;
RC_SERVICE state; RC_SERVICE state;
bool if_crashed = false;
bool if_exists = false; bool if_exists = false;
bool if_inactive = false; bool if_inactive = false;
bool if_notstarted = false; bool if_notstarted = false;
@ -79,6 +82,9 @@ int main(int argc, char **argv)
free(service); free(service);
return opt; return opt;
/* NOTREACHED */ /* NOTREACHED */
case 'c':
if_crashed = true;
break;
case 'i': case 'i':
if_exists = true; if_exists = true;
break; break;
@ -121,6 +127,8 @@ int main(int argc, char **argv)
eerrorx("%s: service `%s' does not exist", applet, *argv); eerrorx("%s: service `%s' does not exist", applet, *argv);
} }
state = rc_service_state(*argv); state = rc_service_state(*argv);
if (if_crashed && ! (rc_service_daemons_crashed(*argv) && errno != EACCES))
return 0;
if (if_inactive && ! (state & RC_SERVICE_INACTIVE)) if (if_inactive && ! (state & RC_SERVICE_INACTIVE))
return 0; return 0;
if (if_notstarted && (state & RC_SERVICE_STARTED)) if (if_notstarted && (state & RC_SERVICE_STARTED))