rc-service: add --ifstarted and --ifstopped options

This commit is contained in:
William Hubbs 2018-05-16 13:25:22 -05:00
parent 56870d0db1
commit 08da36149c
2 changed files with 23 additions and 1 deletions

View File

@ -40,6 +40,14 @@
.Ar service cmd .Ar service cmd
.Op Ar ... .Op Ar ...
.Nm .Nm
.Op Fl s , -ifstarted
.Ar service cmd
.Op Ar ...
.Nm
.Op Fl S , -ifstopped
.Ar service cmd
.Op Ar ...
.Nm
.Fl e , -exists .Fl e , -exists
.Ar service .Ar service
.Nm .Nm

View File

@ -29,7 +29,7 @@
const char *applet = NULL; const char *applet = NULL;
const char *extraopts = NULL; const char *extraopts = NULL;
const char *getoptstring = "cdDe:ilr:INZ" getoptstring_COMMON; const char *getoptstring = "cdDe:ilr:INsSZ" getoptstring_COMMON;
const struct option longopts[] = { const struct option longopts[] = {
{ "debug", 0, NULL, 'd' }, { "debug", 0, NULL, 'd' },
{ "nodeps", 0, NULL, 'D' }, { "nodeps", 0, NULL, 'D' },
@ -38,6 +38,8 @@ const struct option longopts[] = {
{ "ifexists", 0, NULL, 'i' }, { "ifexists", 0, NULL, 'i' },
{ "ifinactive", 0, NULL, 'I' }, { "ifinactive", 0, NULL, 'I' },
{ "ifnotstarted", 0, NULL, 'N' }, { "ifnotstarted", 0, NULL, 'N' },
{ "ifstarted", 0, NULL, 's' },
{ "ifstopped", 0, NULL, 'S' },
{ "list", 0, NULL, 'l' }, { "list", 0, NULL, 'l' },
{ "resolve", 1, NULL, 'r' }, { "resolve", 1, NULL, 'r' },
{ "dry-run", 0, NULL, 'Z' }, { "dry-run", 0, NULL, 'Z' },
@ -73,6 +75,8 @@ int main(int argc, char **argv)
bool if_exists = false; bool if_exists = false;
bool if_inactive = false; bool if_inactive = false;
bool if_notstarted = false; bool if_notstarted = false;
bool if_started = false;
bool if_stopped = 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 */
@ -124,6 +128,12 @@ int main(int argc, char **argv)
free(service); free(service);
return EXIT_SUCCESS; return EXIT_SUCCESS;
/* NOTREACHED */ /* NOTREACHED */
case 's':
if_started = true;
break;
case 'S':
if_stopped = true;
break;
case 'Z': case 'Z':
setenv("IN_DRYRUN", "yes", 1); setenv("IN_DRYRUN", "yes", 1);
break; break;
@ -148,6 +158,10 @@ int main(int argc, char **argv)
return 0; return 0;
if (if_notstarted && (state & RC_SERVICE_STARTED)) if (if_notstarted && (state & RC_SERVICE_STARTED))
return 0; return 0;
if (if_started && ! (state & RC_SERVICE_STARTED))
return 0;
if (if_stopped && ! (state & RC_SERVICE_STOPPED))
return 0;
*argv = service; *argv = service;
execv(*argv, argv); execv(*argv, argv);
eerrorx("%s: %s", applet, strerror(errno)); eerrorx("%s: %s", applet, strerror(errno));