Skip to content

Commit

Permalink
Initial path support for user services in librc
Browse files Browse the repository at this point in the history
This introduces new variables and functions to allow for setting paths
(RC_SVCDIR, RC_RUNLEVELDIR, RC_INITDIR and RC_CONFDIR) at runtime,
needed for user services. This also patches librc to use said functions
instead of the #define'd variables.

Signed-off-by: anna <[email protected]>
  • Loading branch information
navi-desu committed Jan 19, 2023
1 parent 6f44445 commit 52a6b29
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 88 deletions.
8 changes: 4 additions & 4 deletions src/librc/librc-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ rc_service_daemon_set(const char *service, const char *exec,
return false;
}

xasprintf(&dirpath, RC_SVCDIR "/daemons/%s", basename_c(service));
xasprintf(&dirpath, "%s/daemons/%s", rc_svcdir_get(), basename_c(service));

/* Regardless, erase any existing daemon info */
if ((dp = opendir(dirpath))) {
Expand Down Expand Up @@ -470,7 +470,7 @@ rc_service_started_daemon(const char *service,
if (!service || !exec)
return false;

xasprintf(&dirpath, RC_SVCDIR "/daemons/%s", basename_c(service));
xasprintf(&dirpath, "%s/daemons/%s", rc_svcdir_get(), basename_c(service));
match = _match_list(exec, argv, NULL);

if (indx > 0) {
Expand Down Expand Up @@ -522,8 +522,8 @@ rc_service_daemons_crashed(const char *service)
char *ch_root;
char *spidfile;

path += snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s",
basename_c(service));
path += snprintf(dirpath, sizeof(dirpath), "%s/daemons/%s",
rc_svcdir_get(), basename_c(service));

if (!(dp = opendir(dirpath)))
return false;
Expand Down
71 changes: 44 additions & 27 deletions src/librc/librc-depend.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@

#define GENDEP RC_LIBEXECDIR "/sh/gendepends.sh"

#define RC_DEPCONFIG RC_SVCDIR "/depconfig"
#define RC_DEPCONFIG "/depconfig"

static char *configpath = NULL;
static char*
depconfigpath_get() {
if (!configpath) {
configpath = xmalloc(sizeof(char) * PATH_MAX);
snprintf(configpath, PATH_MAX, "%s/%s", rc_svcdir_get(), RC_DEPCONFIG);
}
return configpath;
}

static const char *bootlevel = NULL;

Expand Down Expand Up @@ -108,7 +118,7 @@ get_deptype(const RC_DEPINFO *depinfo, const char *type)

RC_DEPTREE *
rc_deptree_load(void) {
return rc_deptree_load_file(RC_DEPTREE_CACHE);
return rc_deptree_load_file(rc_deptree_cache_path_get());
}

RC_DEPTREE *
Expand Down Expand Up @@ -655,19 +665,19 @@ static const DEPPAIR deppairs[] = {

static const char *const depdirs[] =
{
RC_SVCDIR,
RC_SVCDIR "/starting",
RC_SVCDIR "/started",
RC_SVCDIR "/stopping",
RC_SVCDIR "/inactive",
RC_SVCDIR "/wasinactive",
RC_SVCDIR "/failed",
RC_SVCDIR "/hotplugged",
RC_SVCDIR "/daemons",
RC_SVCDIR "/options",
RC_SVCDIR "/exclusive",
RC_SVCDIR "/scheduled",
RC_SVCDIR "/tmp",
"",
"starting",
"started",
"stopping",
"inactive",
"wasinactive",
"failed",
"hotplugged",
"daemons",
"options",
"exclusive",
"scheduled",
"tmp",
NULL
};

Expand All @@ -682,14 +692,17 @@ rc_deptree_update_needed(time_t *newest, char *file)
time_t mtime;

/* Create base directories if needed */
for (i = 0; depdirs[i]; i++)
if (mkdir(depdirs[i], 0755) != 0 && errno != EEXIST)
fprintf(stderr, "mkdir `%s': %s\n", depdirs[i], strerror(errno));
char depdir[sizeof(char) * PATH_MAX];
for (i = 0; depdirs[i]; i++) {
snprintf(depdir, PATH_MAX, "%s/%s", rc_svcdir_get(), depdirs[i]);
if (mkdir(depdir, 0755) != 0 && errno != EEXIST)
fprintf(stderr, "mkdir `%s': %s\n", depdir, strerror(errno));
}

/* Quick test to see if anything we use has changed and we have
* data in our deptree. */

if (stat(RC_DEPTREE_CACHE, &buf) == 0) {
if (stat(rc_deptree_cache_path_get(), &buf) == 0) {
mtime = buf.st_mtime;
} else {
/* No previous cache found.
Expand All @@ -700,8 +713,8 @@ rc_deptree_update_needed(time_t *newest, char *file)
mtime = time(NULL);
}

newer |= !deep_mtime_check(RC_INITDIR,true,&mtime,file);
newer |= !deep_mtime_check(RC_CONFDIR,true,&mtime,file);
newer |= !deep_mtime_check(rc_initdir_get(),true,&mtime,file);
newer |= !deep_mtime_check(rc_confdir_get(),true,&mtime,file);
#ifdef RC_PKG_INITDIR
newer |= !deep_mtime_check(RC_PKG_INITDIR,true,&mtime,file);
#endif
Expand All @@ -718,7 +731,9 @@ rc_deptree_update_needed(time_t *newest, char *file)

/* Some init scripts dependencies change depending on config files
* outside of baselayout, like syslog-ng, so we check those too. */
config = rc_config_list(RC_DEPCONFIG);
char *depconf = depconfigpath_get();
config = rc_config_list(depconf);
free(depconf);
TAILQ_FOREACH(s, config, entries) {
newer |= !deep_mtime_check(s->value, true, &mtime, file);
}
Expand Down Expand Up @@ -1035,7 +1050,7 @@ rc_deptree_update(void)
This works and should be entirely shell parseable provided that depend
names don't have any non shell variable characters in
*/
if ((fp = fopen(RC_DEPTREE_CACHE, "w"))) {
if ((fp = fopen(rc_deptree_cache_path_get(), "w"))) {
i = 0;
TAILQ_FOREACH(depinfo, deptree, entries) {
fprintf(fp, "depinfo_%zu_service='%s'\n",
Expand All @@ -1054,24 +1069,26 @@ rc_deptree_update(void)
fclose(fp);
} else {
fprintf(stderr, "fopen `%s': %s\n",
RC_DEPTREE_CACHE, strerror(errno));
rc_deptree_cache_path_get(), strerror(errno));
retval = false;
}

/* Save our external config files to disk */
char *depconf = depconfigpath_get();
if (TAILQ_FIRST(config)) {
if ((fp = fopen(RC_DEPCONFIG, "w"))) {
if ((fp = fopen(depconf, "w"))) {
TAILQ_FOREACH(s, config, entries)
fprintf(fp, "%s\n", s->value);
fclose(fp);
} else {
fprintf(stderr, "fopen `%s': %s\n",
RC_DEPCONFIG, strerror(errno));
depconf, strerror(errno));
retval = false;
}
} else {
unlink(RC_DEPCONFIG);
unlink(depconf);
}
free(depconf);

rc_stringlist_free(config);
free(deptree);
Expand Down
Loading

0 comments on commit 52a6b29

Please sign in to comment.