Skip to content

Commit 49be101

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 cf305c3 commit 49be101

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

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: 35 additions & 9 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
usage(EXIT_FAILURE);
244264
optind++;
@@ -249,16 +269,18 @@ int main(int argc, char **argv)
249269
if (optind >= argc && action != DO_SHOW)
250270
usage(EXIT_FAILURE);
251271

252-
service = argv[optind];
253-
optind++;
272+
if (action != DO_EXPORT) {
273+
service = argv[optind];
274+
optind++;
254275

255-
runlevels = rc_stringlist_new();
256-
while (optind < argc) {
257-
if (rc_runlevel_exists(argv[optind])) {
258-
rc_stringlist_add(runlevels, argv[optind++]);
259-
} else {
260-
rc_stringlist_free(runlevels);
261-
eerrorx("%s: '%s' is not a valid runlevel", applet, argv[optind]);
276+
runlevels = rc_stringlist_new();
277+
while (optind < argc) {
278+
if (rc_runlevel_exists(argv[optind])) {
279+
rc_stringlist_add(runlevels, argv[optind++]);
280+
} else {
281+
rc_stringlist_free(runlevels);
282+
eerrorx("%s: '%s' is not a valid runlevel", applet, argv[optind]);
283+
}
262284
}
263285
}
264286

@@ -279,6 +301,10 @@ int main(int argc, char **argv)
279301
rc_stringlist_sort(&runlevels);
280302
show(runlevels, verbose);
281303
goto exit;
304+
case DO_EXPORT:
305+
while (optind < argc)
306+
export(argv[optind++]);
307+
return 0;
282308
case DO_ADD:
283309
if (all_runlevels) {
284310
rc_stringlist_free(runlevels);

0 commit comments

Comments
 (0)