1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*
rc-update
Manage init scripts and runlevels
Copyright 2007 Gentoo Foundation
Released under the GPLv2
*/
#define APPLET "rc-update"
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "builtins.h"
#include "einfo.h"
#include "rc.h"
#include "rc-misc.h"
#include "strlist.h"
static char *applet = NULL;
/* Return the number of changes made:
* -1 = no changes (error)
* 0 = no changes (nothing to do)
* 1+ = number of runlevels updated
*/
static ssize_t add (const char *runlevel, const char *service)
{
ssize_t retval = -1;
if (! rc_service_exists (service))
eerror ("%s: service `%s' does not exist", applet, service);
else if (! rc_runlevel_exists (runlevel))
eerror ("%s: runlevel `%s' does not exist", applet, runlevel);
else if (rc_service_in_runlevel (service, runlevel)) {
ewarn ("%s: %s already installed in runlevel `%s'; skipping",
applet, service, runlevel);
retval = 0;
} else if (rc_service_add (runlevel, service)) {
einfo ("%s added to runlevel %s", service, runlevel);
retval = 1;
} else
eerror ("%s: failed to add service `%s' to runlevel `%s': %s",
applet, service, runlevel, strerror (errno));
return (retval);
}
static ssize_t delete (const char *runlevel, const char *service)
{
ssize_t retval = -1;
if (rc_service_in_runlevel (service, runlevel)) {
if (rc_service_delete (runlevel, service)) {
einfo ("%s removed from runlevel %s", service, runlevel);
retval = 1;
} else
eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
applet, service, runlevel, strerror (errno));
} else if (! rc_service_exists (service))
eerror ("%s: service `%s' does not exist", applet, service);
else if (! rc_runlevel_exists (runlevel))
eerror ("%s: runlevel `%s' does not exist", applet, runlevel);
else
retval = 0;
return (retval);
}
static void show (char **runlevels, bool verbose)
{
char *service;
char **services = rc_services_in_runlevel (NULL);
char *runlevel;
int i;
int j;
STRLIST_FOREACH (services, service, i) {
char **in = NULL;
bool inone = false;
STRLIST_FOREACH (runlevels, runlevel, j) {
if (rc_service_in_runlevel (service, runlevel)) {
rc_strlist_add (&in, runlevel);
inone = true;
} else {
char buffer[PATH_MAX];
memset (buffer, ' ', strlen (runlevel));
buffer[strlen (runlevel)] = 0;
rc_strlist_add (&in, buffer);
}
}
if (! inone && ! verbose)
continue;
printf (" %20s |", service);
STRLIST_FOREACH (in, runlevel, j)
printf (" %s", runlevel);
printf ("\n");
rc_strlist_free (in);
}
rc_strlist_free (services);
}
#include "_usage.h"
#define getoptstring "adsv" getoptstring_COMMON
static struct option longopts[] = {
{ "add", 0, NULL, 'a'},
{ "delete", 0, NULL, 'd'},
{ "show", 0, NULL, 's'},
{ "verbose", 0, NULL, 'v'},
longopts_COMMON
{ NULL, 0, NULL, 0}
};
static const char * const longopts_help[] = {
"Add the init.d to runlevels",
"Delete init.d from runlevels",
"Show init.d's in runlevels",
"Be verbose!",
longopts_help_COMMON
};
#include "_usage.c"
#define DOADD (1 << 0)
#define DODELETE (1 << 1)
#define DOSHOW (1 << 2)
int rc_update (int argc, char **argv)
{
int i;
char *service = NULL;
char **runlevels = NULL;
char *runlevel;
int action = 0;
bool verbose = false;
int opt;
int retval = EXIT_FAILURE;
applet = argv[0];
while ((opt = getopt_long (argc, argv, getoptstring,
longopts, (int *) 0)) != -1)
{
switch (opt) {
case 'a':
action |= DOADD;
break;
case 'd':
action |= DODELETE;
break;
case 's':
action |= DOSHOW;
break;
case 'v':
verbose = true;
break;
case_RC_COMMON_GETOPT
}
}
if ((action & DOSHOW && action != DOSHOW) ||
(action & DOADD && action != DOADD) ||
(action & DODELETE && action != DODELETE))
eerrorx ("%s: cannot mix commands", applet);
/* We need to be backwards compatible */
if (! action) {
if (optind < argc) {
if (strcmp (argv[optind], "add") == 0)
action = DOADD;
else if (strcmp (argv[optind], "delete") == 0 ||
strcmp (argv[optind], "del") == 0)
action = DODELETE;
else if (strcmp (argv[optind], "show") == 0)
action = DOSHOW;
if (action)
optind++;
else
eerrorx ("%s: invalid command `%s'", applet, argv[optind]);
}
if (! action && opt)
action = DOSHOW;
}
if (optind >= argc) {
if (! action & DOSHOW)
eerrorx ("%s: no service specified", applet);
} else {
service = argv[optind];
optind++;
while (optind < argc)
if (rc_runlevel_exists (argv[optind]))
rc_strlist_add (&runlevels, argv[optind++]);
else {
rc_strlist_free (runlevels);
eerrorx ("%s: `%s' is not a valid runlevel", applet, argv[optind]);
}
}
retval = EXIT_SUCCESS;
if (action & DOSHOW) {
if (service)
rc_strlist_add (&runlevels, service);
if (! runlevels)
runlevels = rc_get_runlevels ();
show (runlevels, verbose);
} else {
if (! service)
eerror ("%s: no service specified", applet);
else if (! rc_service_exists (service))
eerror ("%s: service `%s' does not exist", applet, service);
else {
ssize_t num_updated = 0;
ssize_t (*actfunc)(const char *runlevel, const char *service);
if (action & DOADD)
actfunc = add;
else if (action & DODELETE)
actfunc = delete;
else
eerrorx ("%s: invalid action", applet);
if (! runlevels)
runlevels = rc_get_runlevels ();
STRLIST_FOREACH (runlevels, runlevel, i) {
ssize_t ret = actfunc (runlevel, service);
if (ret < 0)
retval = EXIT_FAILURE;
num_updated += ret;
}
if (retval == EXIT_SUCCESS && num_updated == 0)
ewarnx ("%s: service `%s' not found in any of the specified runlevels", applet, service);
}
}
rc_strlist_free (runlevels);
return (retval);
}
|