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 5992833 commit 95ecd74
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 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
61 changes: 48 additions & 13 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 All @@ -205,7 +223,7 @@ int main(int argc, char **argv)
char *service = NULL;
char *p;
enum update_action action = DO_NONE;
bool verbose = false, stack = false, all_runlevels = false;
bool verbose = false, stack = false, all = false;
int opt;
int retval = EXIT_FAILURE;
int num_updated = 0;
Expand All @@ -216,7 +234,7 @@ int main(int argc, char **argv)
while ((opt = getopt_long(argc, argv, getoptstring, longopts, NULL)) != -1) {
switch (opt) {
case 'a':
all_runlevels = true;
all = true;
break;
case 's':
stack = true;
Expand All @@ -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,8 +301,21 @@ int main(int argc, char **argv)
rc_stringlist_sort(&runlevels);
show(runlevels, verbose);
goto exit;
case DO_ENV:
if (all) {
for (const char **envptr = environ; *envptr; envptr++) {
char *var = xstrdup(*envptr);
env(var);
free(var);
}
return 0;
}

while (optind < argc)
env(argv[optind++]);
return 0;
case DO_ADD:
if (all_runlevels) {
if (all) {
rc_stringlist_free(runlevels);
eerrorx("%s: the -a option is invalid with add", applet);
}
Expand All @@ -292,7 +327,7 @@ int main(int argc, char **argv)
}

if (!TAILQ_FIRST(runlevels)) {
if (all_runlevels) {
if (all) {
free(runlevels);
runlevels = rc_runlevel_list();
} else {
Expand Down

0 comments on commit 95ecd74

Please sign in to comment.