Skip to content

Commit

Permalink
rc-update: add 'env' sub-command
Browse files Browse the repository at this point in the history
allows users to export variables into the starting environment of
services, with usage `rc-update env FOO NYA=MEW`
  • Loading branch information
navi-desu committed Mar 4, 2025
1 parent cf305c3 commit 5fa5e74
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
8 changes: 8 additions & 0 deletions man/rc-update.8
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
.Ar service
.Op Ar runlevel ...
.Nm
.Op Fl a , -all
.Ar env
.Ar variable ...
.Nm
.Op Fl u , -update
.Op Fl v , -verbose
.Ar show
Expand Down Expand Up @@ -59,6 +63,10 @@ Delete the
from the
.Ar runlevel
or the current one if none given.
.It Ar env Ar variable ...
Exports listed
.Ar variables
to the starting environment of all services.
.It Ar show
Show all enabled services and the runlevels they belong to. If you specify
runlevels to show, then only those will be included in the output.
Expand Down
44 changes: 35 additions & 9 deletions src/rc-update/rc-update.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const char *extraopts = NULL;
const char *usagestring = "" \
"Usage: rc-update [options] add <service> [<runlevel>...]\n" \
" or: rc-update [options] del <service> [<runlevel>...]\n" \
" or: rc-update [options] env <variables>...\n" \
" or: rc-update [options] [show [<runlevel>...]]";
const char getoptstring[] = "asu" getoptstring_COMMON;
const struct option longopts[] = {
Expand All @@ -50,6 +51,8 @@ const char * const longopts_help[] = {
longopts_help_COMMON
};

extern const char **environ;

/* Return the number of changes made:
* -1 = no changes (error)
* 0 = no changes (nothing to do)
Expand Down Expand Up @@ -190,11 +193,26 @@ show(RC_STRINGLIST *runlevels, bool verbose)
rc_stringlist_free(services);
}

static void
env(char *variable)
{
char *value = variable;
variable = strsep(&value, "=");

if (!value && !(value = getenv(variable))) {
ewarn("%s: environment variable %s not set, skipping.", applet, variable);
return;
}

rc_export_variable(variable, value);
}

enum update_action {
DO_NONE,
DO_ADD,
DO_DELETE,
DO_SHOW,
DO_ENV
};

int main(int argc, char **argv)
Expand Down Expand Up @@ -239,6 +257,8 @@ int main(int argc, char **argv)
action = DO_DELETE;
else if (strcmp(argv[optind], "show") == 0)
action = DO_SHOW;
else if (strcmp(argv[optind], "env") == 0)
action = DO_ENV;
else
usage(EXIT_FAILURE);
optind++;
Expand All @@ -249,16 +269,18 @@ int main(int argc, char **argv)
if (optind >= argc && action != DO_SHOW)
usage(EXIT_FAILURE);

service = argv[optind];
optind++;
if (action != DO_ENV) {
service = argv[optind];
optind++;

runlevels = rc_stringlist_new();
while (optind < argc) {
if (rc_runlevel_exists(argv[optind])) {
rc_stringlist_add(runlevels, argv[optind++]);
} else {
rc_stringlist_free(runlevels);
eerrorx("%s: '%s' is not a valid runlevel", applet, argv[optind]);
runlevels = rc_stringlist_new();
while (optind < argc) {
if (rc_runlevel_exists(argv[optind])) {
rc_stringlist_add(runlevels, argv[optind++]);
} else {
rc_stringlist_free(runlevels);
eerrorx("%s: '%s' is not a valid runlevel", applet, argv[optind]);
}
}
}

Expand All @@ -279,6 +301,10 @@ int main(int argc, char **argv)
rc_stringlist_sort(&runlevels);
show(runlevels, verbose);
goto exit;
case DO_ENV:
while (optind < argc)
env(argv[optind++]);
return 0;
case DO_ADD:
if (all_runlevels) {
rc_stringlist_free(runlevels);
Expand Down

0 comments on commit 5fa5e74

Please sign in to comment.