Skip to content

Commit

Permalink
openrc: Add support for user services.
Browse files Browse the repository at this point in the history
Modifies many functions where filesystem paths were hardcoded. In
non-user-services mode, they still are. In user-services mode, they are
allocated, since XDG_ dirs are to be set via environment variables.

Signed-off-by: Anna (navi) Figueiredo Gomes <[email protected]>
  • Loading branch information
navi-desu committed Mar 14, 2023
1 parent ccd6b06 commit 7436c74
Show file tree
Hide file tree
Showing 15 changed files with 726 additions and 123 deletions.
135 changes: 110 additions & 25 deletions src/librc/librc-depend.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@

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

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

static const char *bootlevel = NULL;

Expand Down Expand Up @@ -121,7 +122,26 @@ get_deptype(const RC_DEPINFO *depinfo, const char *type)

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

char *cache = RC_DEPTREE_CACHE;
#ifdef RC_USER_SERVICES
char *user_svcdir;
RC_DEPTREE *tree;

if (rc_is_user()) {
user_svcdir = rc_user_svcdir();
xasprintf(&cache, "%s/%s", user_svcdir, RC_DEPTREE_CACHE_FILE);

tree = rc_deptree_load_file(cache);

free(cache);
free(user_svcdir);

return tree;
}
#endif

return rc_deptree_load_file(cache);
}

RC_DEPTREE *
Expand Down Expand Up @@ -668,19 +688,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 @@ -693,16 +713,38 @@ rc_deptree_update_needed(time_t *newest, char *file)
int i;
struct stat buf;
time_t mtime;
char *depconfig = RC_DEPCONFIG, *cache = RC_DEPTREE_CACHE;
char *depdir = NULL;
#ifdef RC_USER_SERVICES
char *user_sysconfdir, *user_svcdir, *tmp;

if (rc_is_user()) {
user_svcdir = rc_user_svcdir();
user_sysconfdir = rc_user_sysconfdir();
xasprintf(&cache, "%s/%s", user_svcdir, RC_DEPTREE_CACHE_FILE);
}
#endif

/* 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));
for (i = 0; depdirs[i]; i++) {
#ifdef RC_USER_SERVICES
if (rc_is_user()) {
xasprintf(&depdir, "%s/%s", user_svcdir, depdirs[i]);
} else {
#endif
xasprintf(&depdir, "%s/%s", RC_SVCDIR, depdirs[i]);
#ifdef RC_USER_SERVICES
}
#endif
if (mkdir(depdir, 0755) != 0 && errno != EEXIST)
fprintf(stderr, "mkdir `%s': %s\n", depdir, strerror(errno));
free(depdir);
}

/* 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(cache, &buf) == 0) {
mtime = buf.st_mtime;
} else {
/* No previous cache found.
Expand All @@ -726,12 +768,34 @@ rc_deptree_update_needed(time_t *newest, char *file)
#endif
#ifdef RC_LOCAL_CONFDIR
newer |= !deep_mtime_check(RC_LOCAL_CONFDIR,true,&mtime,file);
#endif
#ifdef RC_USER_SERVICES
if (rc_is_user()) {
newer |= !deep_mtime_check(RC_SYS_USER_INITDIR,true,&mtime,file);
newer |= !deep_mtime_check(RC_SYS_USER_CONFDIR,true,&mtime,file);

xasprintf(&tmp, "%s/%s", user_sysconfdir, RC_USER_INITDIR_FOLDER);
newer |= !deep_mtime_check(tmp,true,&mtime,file);
free(tmp);

xasprintf(&tmp, "%s/%s", user_sysconfdir, RC_USER_CONFDIR_FOLDER);
newer |= !deep_mtime_check(tmp,true,&mtime,file);
free(tmp);

free(user_sysconfdir);
}
#endif
newer |= !deep_mtime_check(RC_CONF,true,&mtime,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);
#ifdef RC_USER_SERVICES
if (rc_is_user()) {
xasprintf(&depconfig, "%s/%s", user_svcdir, RC_DEPCONFIG_FILE);
free(user_svcdir);
}
#endif
config = rc_config_list(depconfig);
TAILQ_FOREACH(s, config, entries) {
newer |= !deep_mtime_check(s->value, true, &mtime, file);
}
Expand Down Expand Up @@ -768,6 +832,11 @@ rc_deptree_update(void)
char *line = NULL;
size_t len = 0;
char *depend, *depends, *service, *type, *nosys, *onosys;
char *cache = RC_DEPTREE_CACHE;
char *depconfig = RC_DEPCONFIG;
#ifdef RC_USER_SERVICES
char *user_svcdir;
#endif
size_t i, k, l;
bool retval = true;
const char *sys = rc_sys();
Expand Down Expand Up @@ -1048,7 +1117,16 @@ 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"))) {

#ifdef RC_USER_SERVICES
if (rc_is_user()) {
user_svcdir = rc_user_svcdir();
xasprintf(&cache, "%s/%s", user_svcdir, RC_DEPTREE_CACHE_FILE);
xasprintf(&depconfig, "%s/%s", user_svcdir, RC_DEPCONFIG_FILE);
free(user_svcdir);
}
#endif
if ((fp = fopen(cache, "w"))) {
i = 0;
TAILQ_FOREACH(depinfo, deptree, entries) {
fprintf(fp, "depinfo_%zu_service='%s'\n",
Expand All @@ -1067,25 +1145,32 @@ rc_deptree_update(void)
fclose(fp);
} else {
fprintf(stderr, "fopen `%s': %s\n",
RC_DEPTREE_CACHE, strerror(errno));
cache, strerror(errno));
retval = false;
}

/* Save our external config files to disk */
if (TAILQ_FIRST(config)) {
if ((fp = fopen(RC_DEPCONFIG, "w"))) {
if ((fp = fopen(depconfig, "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));
depconfig, strerror(errno));
retval = false;
}
} else {
unlink(RC_DEPCONFIG);
unlink(depconfig);
}

#ifdef RC_USER_SERVICES
if (rc_is_user()) {
free(cache);
free(depconfig);
}
#endif

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

0 comments on commit 7436c74

Please sign in to comment.