1 |
commit: 8a7e4d38a74c714e1a532e1b7a53fd2a5c528b63 |
2 |
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com> |
3 |
AuthorDate: Sun Sep 21 18:54:51 2014 +0000 |
4 |
Commit: William Hubbs <williamh <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Jan 28 18:57:11 2016 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=8a7e4d38 |
7 |
|
8 |
rc-service: add --ifinactive and --ifnotstarted flags |
9 |
|
10 |
X-Gentoo-Bug: 523174 |
11 |
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=523174 |
12 |
|
13 |
man/rc-service.8 | 15 +++++++++++++++ |
14 |
src/rc/rc-service.c | 20 +++++++++++++++++++- |
15 |
2 files changed, 34 insertions(+), 1 deletion(-) |
16 |
|
17 |
diff --git a/man/rc-service.8 b/man/rc-service.8 |
18 |
index 9260329..80deb5e 100644 |
19 |
--- a/man/rc-service.8 |
20 |
+++ b/man/rc-service.8 |
21 |
@@ -20,6 +20,14 @@ |
22 |
.Ar service cmd |
23 |
.Op Ar ... |
24 |
.Nm |
25 |
+.Op Fl I , -ifinactive |
26 |
+.Ar service cmd |
27 |
+.Op Ar ... |
28 |
+.Nm |
29 |
+.Op Fl N , -ifnotstarted |
30 |
+.Ar service cmd |
31 |
+.Op Ar ... |
32 |
+.Nm |
33 |
.Fl e , -exists |
34 |
.Ar service |
35 |
.Nm |
36 |
@@ -36,6 +44,13 @@ If |
37 |
is given then |
38 |
.Nm |
39 |
returns 0 even if the service does not exist. |
40 |
+If |
41 |
+.Fl I , -ifinactive |
42 |
+or |
43 |
+.Fl N , -ifnotstarted |
44 |
+is given then |
45 |
+.Nm |
46 |
+returns 0 if the service exists but is in the wrong state. |
47 |
.Pp |
48 |
If given the |
49 |
.Fl l , -list |
50 |
|
51 |
diff --git a/src/rc/rc-service.c b/src/rc/rc-service.c |
52 |
index 8e9da44..3fd94b2 100644 |
53 |
--- a/src/rc/rc-service.c |
54 |
+++ b/src/rc/rc-service.c |
55 |
@@ -29,10 +29,12 @@ |
56 |
|
57 |
const char *applet = NULL; |
58 |
const char *extraopts = NULL; |
59 |
-const char *getoptstring = "e:ilr:" getoptstring_COMMON; |
60 |
+const char *getoptstring = "e:ilr:IN" getoptstring_COMMON; |
61 |
const struct option longopts[] = { |
62 |
{ "exists", 1, NULL, 'e' }, |
63 |
{ "ifexists", 0, NULL, 'i' }, |
64 |
+ { "ifinactive", 0, NULL, 'I' }, |
65 |
+ { "ifnotstarted", 0, NULL, 'N' }, |
66 |
{ "list", 0, NULL, 'l' }, |
67 |
{ "resolve", 1, NULL, 'r' }, |
68 |
longopts_COMMON |
69 |
@@ -40,6 +42,8 @@ const struct option longopts[] = { |
70 |
const char * const longopts_help[] = { |
71 |
"tests if the service exists or not", |
72 |
"if the service exists then run the command", |
73 |
+ "if the service is inactive then run the command", |
74 |
+ "if the service is not started then run the command", |
75 |
"list all available services", |
76 |
"resolve the service name to an init script", |
77 |
longopts_help_COMMON |
78 |
@@ -56,7 +60,10 @@ int main(int argc, char **argv) |
79 |
char *service; |
80 |
RC_STRINGLIST *list; |
81 |
RC_STRING *s; |
82 |
+ RC_SERVICE state; |
83 |
bool if_exists = false; |
84 |
+ bool if_inactive = false; |
85 |
+ bool if_notstarted = false; |
86 |
|
87 |
applet = basename_c(argv[0]); |
88 |
/* Ensure that we are only quiet when explicitly told to be */ |
89 |
@@ -77,6 +84,12 @@ int main(int argc, char **argv) |
90 |
case 'i': |
91 |
if_exists = true; |
92 |
break; |
93 |
+ case 'I': |
94 |
+ if_inactive = true; |
95 |
+ break; |
96 |
+ case 'N': |
97 |
+ if_notstarted = true; |
98 |
+ break; |
99 |
case 'l': |
100 |
list = rc_services_in_runlevel(NULL); |
101 |
if (TAILQ_FIRST(list) == NULL) |
102 |
@@ -113,6 +126,11 @@ int main(int argc, char **argv) |
103 |
return 0; |
104 |
eerrorx("%s: service `%s' does not exist", applet, *argv); |
105 |
} |
106 |
+ state = rc_service_state(*argv); |
107 |
+ if (if_inactive && ! (state & RC_SERVICE_INACTIVE)) |
108 |
+ return 0; |
109 |
+ if (if_notstarted && (state & RC_SERVICE_STARTED)) |
110 |
+ return 0; |
111 |
*argv = service; |
112 |
execv(*argv, argv); |
113 |
eerrorx("%s: %s", applet, strerror(errno)); |