Skip to content

Commit 5682c69

Browse files
committed
rc-update: add 'export' sub-command
allows users to export variables into the starting environment of services, with usage `rc-update export FOO NYA=MEW`
1 parent 5fe432f commit 5682c69

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

man/rc-update.8

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
.Ar service
2828
.Op Ar runlevel ...
2929
.Nm
30+
.Op Fl a , -all
31+
.Ar export
32+
.Ar variable ...
33+
.Nm
3034
.Op Fl u , -update
3135
.Op Fl v , -verbose
3236
.Ar show
@@ -59,6 +63,10 @@ Delete the
5963
from the
6064
.Ar runlevel
6165
or the current one if none given.
66+
.It Ar export Ar variable ...
67+
Exports listed
68+
.Ar variables
69+
to the starting environment of all services.
6270
.It Ar show
6371
Show all enabled services and the runlevels they belong to. If you specify
6472
runlevels to show, then only those will be included in the output.

src/rc-update/rc-update.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const char *extraopts = NULL;
3535
const char *usagestring = "" \
3636
"Usage: rc-update [options] add <service> [<runlevel>...]\n" \
3737
" or: rc-update [options] del <service> [<runlevel>...]\n" \
38+
" or: rc-update [options] export <variables>...\n" \
3839
" or: rc-update [options] [show [<runlevel>...]]";
3940
const char getoptstring[] = "asu" getoptstring_COMMON;
4041
const struct option longopts[] = {
@@ -50,6 +51,8 @@ const char * const longopts_help[] = {
5051
longopts_help_COMMON
5152
};
5253

54+
extern const char **environ;
55+
5356
/* Return the number of changes made:
5457
* -1 = no changes (error)
5558
* 0 = no changes (nothing to do)
@@ -190,11 +193,26 @@ show(RC_STRINGLIST *runlevels, bool verbose)
190193
rc_stringlist_free(services);
191194
}
192195

196+
static void
197+
export(char *variable)
198+
{
199+
char *value = variable;
200+
variable = strsep(&value, "=");
201+
202+
if (!value && !(value = getenv(variable))) {
203+
ewarn("%s: environment variable %s not set, skipping.", applet, variable);
204+
return;
205+
}
206+
207+
rc_export_variable(variable, value);
208+
}
209+
193210
enum update_action {
194211
DO_NONE,
195212
DO_ADD,
196213
DO_DELETE,
197214
DO_SHOW,
215+
DO_EXPORT
198216
};
199217

200218
int main(int argc, char **argv)
@@ -239,6 +257,8 @@ int main(int argc, char **argv)
239257
action = DO_DELETE;
240258
} else if (strcmp(argv[optind], "show") == 0) {
241259
action = DO_SHOW;
260+
} else if (strcmp(argv[optind], "export") == 0) {
261+
action = DO_EXPORT;
242262
} else {
243263
eerror("%s: invalid command '%s'", applet, argv[optind]);
244264
usage(EXIT_FAILURE);
@@ -249,20 +269,22 @@ int main(int argc, char **argv)
249269
action = DO_SHOW;
250270

251271
if (optind >= argc && action != DO_SHOW) {
252-
eerror("%s: no service specified", applet);
272+
eerror("%s: no %s specified", applet, action == DO_EXPORT ? "variable" : "service");
253273
usage(EXIT_FAILURE);
254274
}
255275

256-
service = argv[optind];
257-
optind++;
276+
if (action != DO_EXPORT) {
277+
service = argv[optind];
278+
optind++;
258279

259-
runlevels = rc_stringlist_new();
260-
while (optind < argc) {
261-
if (rc_runlevel_exists(argv[optind])) {
262-
rc_stringlist_add(runlevels, argv[optind++]);
263-
} else {
264-
rc_stringlist_free(runlevels);
265-
eerrorx("%s: '%s' is not a valid runlevel", applet, argv[optind]);
280+
runlevels = rc_stringlist_new();
281+
while (optind < argc) {
282+
if (rc_runlevel_exists(argv[optind])) {
283+
rc_stringlist_add(runlevels, argv[optind++]);
284+
} else {
285+
rc_stringlist_free(runlevels);
286+
eerrorx("%s: '%s' is not a valid runlevel", applet, argv[optind]);
287+
}
266288
}
267289
}
268290

@@ -283,6 +305,10 @@ int main(int argc, char **argv)
283305
rc_stringlist_sort(&runlevels);
284306
show(runlevels, verbose);
285307
goto exit;
308+
case DO_EXPORT:
309+
while (optind < argc)
310+
export(argv[optind++]);
311+
return 0;
286312
case DO_ADD:
287313
if (all_runlevels) {
288314
rc_stringlist_free(runlevels);

0 commit comments

Comments
 (0)