Skip to content

Commit 1844e9b

Browse files
committed
librc: add rc_import_variables api
1 parent 2da3a78 commit 1844e9b

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/librc/librc-misc.c

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,25 @@ rc_yesno(const char *value)
5656
return false;
5757
}
5858

59-
60-
/**
61-
* Read the entire @file into the buffer and set @len to the
62-
* size of the buffer when finished. For C strings, this will
63-
* be strlen(buffer) + 1.
64-
* Don't forget to free the buffer afterwards!
65-
*/
66-
bool
67-
rc_getfile(const char *file, char **buffer, size_t *len)
59+
static bool
60+
do_getfileat(int dirfd, const char *file, char **buffer, size_t *size)
6861
{
6962
bool ret = false;
7063
FILE *fp;
71-
int fd;
7264
struct stat st;
7365
size_t done, left;
7466

75-
fp = fopen(file, "re");
67+
fp = do_fopenat(dirfd, file, O_RDONLY);
7668
if (!fp)
7769
return false;
7870

7971
/* assume fileno() never fails */
80-
fd = fileno(fp);
81-
82-
if (fstat(fd, &st))
72+
if (fstat(fileno(fp), &st))
8373
goto finished;
8474

8575
left = st.st_size;
86-
*len = left + 1; /* NUL terminator */
87-
*buffer = xrealloc(*buffer, *len);
76+
*size = left + 1; /* NUL terminator */
77+
*buffer = xrealloc(*buffer, *size);
8878
while (left) {
8979
done = fread(*buffer, sizeof(*buffer[0]), left, fp);
9080
if (done == 0 && ferror(fp))
@@ -96,13 +86,25 @@ rc_getfile(const char *file, char **buffer, size_t *len)
9686
finished:
9787
if (!ret) {
9888
free(*buffer);
99-
*len = 0;
89+
*size = 0;
10090
} else
101-
(*buffer)[*len - 1] = '\0';
91+
(*buffer)[*size - 1] = '\0';
10292
fclose(fp);
10393
return ret;
10494
}
10595

96+
/**
97+
* Read the entire @file into the buffer and set @len to the
98+
* size of the buffer when finished. For C strings, this will
99+
* be strlen(buffer) + 1.
100+
* Don't forget to free the buffer afterwards!
101+
*/
102+
bool
103+
rc_getfile(const char *file, char **buffer, size_t *len)
104+
{
105+
return do_getfileat(AT_FDCWD, file, buffer, len);
106+
}
107+
106108
char *
107109
rc_proc_getent(const char *ent RC_UNUSED)
108110
{
@@ -472,3 +474,22 @@ bool rc_export_variable(const char *service, const char *name, const char *value
472474

473475
return true;
474476
}
477+
478+
void rc_import_variables(const char *service) {
479+
DIR *envdir = do_opendirat(rc_dirfd(RC_DIR_ENVIRONMENT), service ? service : "rc");
480+
struct dirent *d;
481+
if (!envdir)
482+
return;
483+
484+
while ((d = readdir(envdir))) {
485+
char *value;
486+
size_t size;
487+
488+
if (!do_getfileat(rc_dirfd(RC_DIR_ENVIRONMENT), d->d_name, &value, &size))
489+
continue;
490+
setenv(d->d_name, value, true);
491+
free(value);
492+
}
493+
494+
closedir(envdir);
495+
}

src/librc/rc.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ bool rc_service_value_set(const char *, const char *, const char *);
354354
* @param value */
355355
bool rc_export_variable(const char *, const char *, const char *);
356356

357+
/*! Imports a variable from the enviroment of a service
358+
* @param service name, if null imports the global environment */
359+
void rc_import_variables(const char *);
360+
357361
/*! List the services in a runlevel
358362
* @param runlevel to list
359363
* @return NULL terminated list of services */

0 commit comments

Comments
 (0)