Add -i, --ifexists so that we can do this

rc-sercice -i foo -- restart
instead of this
  rc-service -e foo && rc-service foo -- restart
This commit is contained in:
Roy Marples 2009-05-01 08:38:57 +01:00
parent caf29a6480
commit fb051bf81a
2 changed files with 29 additions and 14 deletions

View File

@ -1,4 +1,4 @@
.\" Copyright 2008 Roy Marples .\" Copyright 2008-2009 Roy Marples
.\" All rights reserved .\" All rights reserved
.\" .\"
.\" Redistribution and use in source and binary forms, with or without .\" Redistribution and use in source and binary forms, with or without
@ -22,7 +22,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.Dd Mar 26, 2008 .Dd May 1, 2009
.Dt RC-SERVICE 8 SMM .Dt RC-SERVICE 8 SMM
.Os OpenRC .Os OpenRC
.Sh NAME .Sh NAME
@ -30,20 +30,26 @@
.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 i , -ifexists
.Ar service cmd .Ar service cmd
.Op Ar ... .Op Ar ...
.Nm .Nm
.Fl l , -list
.Nm
.Fl e , -exists .Fl e , -exists
.Ar service .Ar service
.Nm .Nm
.Fl l , -list
.Nm
.Fl r , -resolve .Fl r , -resolve
.Ar service .Ar service
.Sh DESCRIPTION .Sh DESCRIPTION
Service scripts could be in different places on different systems. Service scripts could be in different places on different systems.
.Nm .Nm
locates the specified service and runs it with the given arguments. locates the specified service and runs it with the given arguments.
If
.Fl i , -ifexists
is given then
.Nm
returns 0 even if the service does not exist.
.Pp .Pp
If given the If given the
.Fl l , -list .Fl l , -list
@ -51,11 +57,11 @@ argument then
.Nm .Nm
will list all available services. will list all available services.
.Pp .Pp
.Fl -e , exists .Fl e , -exists
return 0 if it can find return 0 if it can find
.Ar service , .Ar service ,
otherwise -1. otherwise -1.
.Fl -r , resolve .Fl r , -resolve
does the same and also prints the full path of the service to stdout. does the same and also prints the full path of the service to stdout.
.Sh SEE ALSO .Sh SEE ALSO
.Xr rc 8 , .Xr rc 8 ,

View File

@ -43,15 +43,17 @@
extern char *applet; extern char *applet;
#include "_usage.h" #include "_usage.h"
#define getoptstring "e:lr:" getoptstring_COMMON #define getoptstring "e:ilr:" getoptstring_COMMON
static const struct option longopts[] = { static const struct option longopts[] = {
{ "exists", 1, NULL, 'e' }, { "exists", 1, NULL, 'e' },
{ "ifexists", 0, NULL, 'i' },
{ "list", 0, NULL, 'l' }, { "list", 0, NULL, 'l' },
{ "resolve", 1, NULL, 'r' }, { "resolve", 1, NULL, 'r' },
longopts_COMMON longopts_COMMON
}; };
static const char * const longopts_help[] = { static const char * const longopts_help[] = {
"tests if the service exists or not", "tests if the service exists or not",
"if the service exsits 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
@ -65,6 +67,7 @@ rc_service(int argc, char **argv)
char *service; char *service;
RC_STRINGLIST *list; RC_STRINGLIST *list;
RC_STRING *s; RC_STRING *s;
bool if_exists = false;
/* Ensure that we are only quiet when explicitly told to be */ /* Ensure that we are only quiet when explicitly told to be */
unsetenv("EINFO_QUIET"); unsetenv("EINFO_QUIET");
@ -81,9 +84,12 @@ rc_service(int argc, char **argv)
#endif #endif
return opt; return opt;
/* NOTREACHED */ /* NOTREACHED */
case 'i':
if_exists = true;
break;
case 'l': case 'l':
list = rc_services_in_runlevel(NULL); list = rc_services_in_runlevel(NULL);
if (!TAILQ_FIRST(list)) if (TAILQ_FIRST(list) == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
rc_stringlist_sort(&list); rc_stringlist_sort(&list);
TAILQ_FOREACH(s, list, entries) TAILQ_FOREACH(s, list, entries)
@ -95,7 +101,7 @@ rc_service(int argc, char **argv)
/* NOTREACHED */ /* NOTREACHED */
case 'r': case 'r':
service = rc_service_resolve(optarg); service = rc_service_resolve(optarg);
if (!service) if (service == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
printf("%s\n", service); printf("%s\n", service);
#ifdef DEBUG_MEMORY #ifdef DEBUG_MEMORY
@ -110,10 +116,13 @@ rc_service(int argc, char **argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (!*argv) if (*argv == NULL)
eerrorx("%s: you need to specify a service", applet); eerrorx("%s: you need to specify a service", applet);
if (!(service = rc_service_resolve(*argv))) if ((service = rc_service_resolve(*argv)) == NULL) {
if (if_exists)
return 0;
eerrorx("%s: service `%s' does not exist", applet, *argv); eerrorx("%s: service `%s' does not exist", applet, *argv);
}
*argv = service; *argv = service;
execv(*argv, argv); execv(*argv, argv);
eerrorx("%s: %s", applet, strerror(errno)); eerrorx("%s: %s", applet, strerror(errno));