Skip to content

Commit dec4e6a

Browse files
committed
openrc: Add support for user services.
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]>
1 parent e61807b commit dec4e6a

File tree

15 files changed

+724
-123
lines changed

15 files changed

+724
-123
lines changed

src/librc/librc-depend.c

Lines changed: 110 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

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

38-
#define RC_DEPCONFIG RC_SVCDIR "/depconfig"
38+
#define RC_DEPCONFIG_FILE "/depconfig"
39+
#define RC_DEPCONFIG RC_SVCDIR RC_DEPCONFIG_FILE
3940

4041
static const char *bootlevel = NULL;
4142

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

122123
RC_DEPTREE *
123124
rc_deptree_load(void) {
124-
return rc_deptree_load_file(RC_DEPTREE_CACHE);
125+
126+
char *cache = RC_DEPTREE_CACHE;
127+
#ifdef RC_USER_SERVICES
128+
char *user_svcdir;
129+
RC_DEPTREE *tree;
130+
131+
if (rc_is_user()) {
132+
user_svcdir = rc_user_svcdir();
133+
xasprintf(&cache, "%s/%s", user_svcdir, RC_DEPTREE_CACHE_FILE);
134+
135+
tree = rc_deptree_load_file(cache);
136+
137+
free(cache);
138+
free(user_svcdir);
139+
140+
return tree;
141+
}
142+
#endif
143+
144+
return rc_deptree_load_file(cache);
125145
}
126146

127147
RC_DEPTREE *
@@ -668,19 +688,19 @@ static const DEPPAIR deppairs[] = {
668688

669689
static const char *const depdirs[] =
670690
{
671-
RC_SVCDIR,
672-
RC_SVCDIR "/starting",
673-
RC_SVCDIR "/started",
674-
RC_SVCDIR "/stopping",
675-
RC_SVCDIR "/inactive",
676-
RC_SVCDIR "/wasinactive",
677-
RC_SVCDIR "/failed",
678-
RC_SVCDIR "/hotplugged",
679-
RC_SVCDIR "/daemons",
680-
RC_SVCDIR "/options",
681-
RC_SVCDIR "/exclusive",
682-
RC_SVCDIR "/scheduled",
683-
RC_SVCDIR "/tmp",
691+
"",
692+
"/starting",
693+
"/started",
694+
"/stopping",
695+
"/inactive",
696+
"/wasinactive",
697+
"/failed",
698+
"/hotplugged",
699+
"/daemons",
700+
"/options",
701+
"/exclusive",
702+
"/scheduled",
703+
"/tmp",
684704
NULL
685705
};
686706

@@ -693,16 +713,38 @@ rc_deptree_update_needed(time_t *newest, char *file)
693713
int i;
694714
struct stat buf;
695715
time_t mtime;
716+
char *depconfig = RC_DEPCONFIG, *cache = RC_DEPTREE_CACHE;
717+
char *depdir = NULL;
718+
#ifdef RC_USER_SERVICES
719+
char *user_sysconfdir, *user_svcdir, *tmp;
720+
721+
if (rc_is_user()) {
722+
user_svcdir = rc_user_svcdir();
723+
user_sysconfdir = rc_user_sysconfdir();
724+
xasprintf(&cache, "%s/%s", user_svcdir, RC_DEPTREE_CACHE_FILE);
725+
}
726+
#endif
696727

697728
/* Create base directories if needed */
698-
for (i = 0; depdirs[i]; i++)
699-
if (mkdir(depdirs[i], 0755) != 0 && errno != EEXIST)
700-
fprintf(stderr, "mkdir `%s': %s\n", depdirs[i], strerror(errno));
729+
for (i = 0; depdirs[i]; i++) {
730+
#ifdef RC_USER_SERVICES
731+
if (rc_is_user()) {
732+
xasprintf(&depdir, "%s/%s", user_svcdir, depdirs[i]);
733+
} else {
734+
#endif
735+
xasprintf(&depdir, "%s/%s", RC_SVCDIR, depdirs[i]);
736+
#ifdef RC_USER_SERVICES
737+
}
738+
#endif
739+
if (mkdir(depdir, 0755) != 0 && errno != EEXIST)
740+
fprintf(stderr, "mkdir `%s': %s\n", depdir, strerror(errno));
741+
free(depdir);
742+
}
701743

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

705-
if (stat(RC_DEPTREE_CACHE, &buf) == 0) {
747+
if (stat(cache, &buf) == 0) {
706748
mtime = buf.st_mtime;
707749
} else {
708750
/* No previous cache found.
@@ -726,12 +768,34 @@ rc_deptree_update_needed(time_t *newest, char *file)
726768
#endif
727769
#ifdef RC_LOCAL_CONFDIR
728770
newer |= !deep_mtime_check(RC_LOCAL_CONFDIR,true,&mtime,file);
771+
#endif
772+
#ifdef RC_USER_SERVICES
773+
if (rc_is_user()) {
774+
newer |= !deep_mtime_check(RC_SYS_USER_INITDIR,true,&mtime,file);
775+
newer |= !deep_mtime_check(RC_SYS_USER_CONFDIR,true,&mtime,file);
776+
777+
xasprintf(&tmp, "%s/%s", user_sysconfdir, RC_USER_INITDIR_FOLDER);
778+
newer |= !deep_mtime_check(tmp,true,&mtime,file);
779+
free(tmp);
780+
781+
xasprintf(&tmp, "%s/%s", user_sysconfdir, RC_USER_CONFDIR_FOLDER);
782+
newer |= !deep_mtime_check(tmp,true,&mtime,file);
783+
free(tmp);
784+
785+
free(user_sysconfdir);
786+
}
729787
#endif
730788
newer |= !deep_mtime_check(RC_CONF,true,&mtime,file);
731789

732790
/* Some init scripts dependencies change depending on config files
733791
* outside of baselayout, like syslog-ng, so we check those too. */
734-
config = rc_config_list(RC_DEPCONFIG);
792+
#ifdef RC_USER_SERVICES
793+
if (rc_is_user()) {
794+
xasprintf(&depconfig, "%s/%s", user_svcdir, RC_DEPCONFIG_FILE);
795+
free(user_svcdir);
796+
}
797+
#endif
798+
config = rc_config_list(depconfig);
735799
TAILQ_FOREACH(s, config, entries) {
736800
newer |= !deep_mtime_check(s->value, true, &mtime, file);
737801
}
@@ -768,6 +832,11 @@ rc_deptree_update(void)
768832
char *line = NULL;
769833
size_t len = 0;
770834
char *depend, *depends, *service, *type, *nosys, *onosys;
835+
char *cache = RC_DEPTREE_CACHE;
836+
char *depconfig = RC_DEPCONFIG;
837+
#ifdef RC_USER_SERVICES
838+
char *user_svcdir;
839+
#endif
771840
size_t i, k, l;
772841
bool retval = true;
773842
const char *sys = rc_sys();
@@ -1048,7 +1117,16 @@ rc_deptree_update(void)
10481117
This works and should be entirely shell parseable provided that depend
10491118
names don't have any non shell variable characters in
10501119
*/
1051-
if ((fp = fopen(RC_DEPTREE_CACHE, "w"))) {
1120+
1121+
#ifdef RC_USER_SERVICES
1122+
if (rc_is_user()) {
1123+
user_svcdir = rc_user_svcdir();
1124+
xasprintf(&cache, "%s/%s", user_svcdir, RC_DEPTREE_CACHE_FILE);
1125+
xasprintf(&depconfig, "%s/%s", user_svcdir, RC_DEPCONFIG_FILE);
1126+
free(user_svcdir);
1127+
}
1128+
#endif
1129+
if ((fp = fopen(cache, "w"))) {
10521130
i = 0;
10531131
TAILQ_FOREACH(depinfo, deptree, entries) {
10541132
fprintf(fp, "depinfo_%zu_service='%s'\n",
@@ -1067,25 +1145,32 @@ rc_deptree_update(void)
10671145
fclose(fp);
10681146
} else {
10691147
fprintf(stderr, "fopen `%s': %s\n",
1070-
RC_DEPTREE_CACHE, strerror(errno));
1148+
cache, strerror(errno));
10711149
retval = false;
10721150
}
10731151

10741152
/* Save our external config files to disk */
10751153
if (TAILQ_FIRST(config)) {
1076-
if ((fp = fopen(RC_DEPCONFIG, "w"))) {
1154+
if ((fp = fopen(depconfig, "w"))) {
10771155
TAILQ_FOREACH(s, config, entries)
10781156
fprintf(fp, "%s\n", s->value);
10791157
fclose(fp);
10801158
} else {
10811159
fprintf(stderr, "fopen `%s': %s\n",
1082-
RC_DEPCONFIG, strerror(errno));
1160+
depconfig, strerror(errno));
10831161
retval = false;
10841162
}
10851163
} else {
1086-
unlink(RC_DEPCONFIG);
1164+
unlink(depconfig);
10871165
}
10881166

1167+
#ifdef RC_USER_SERVICES
1168+
if (rc_is_user()) {
1169+
free(cache);
1170+
free(depconfig);
1171+
}
1172+
#endif
1173+
10891174
rc_stringlist_free(config);
10901175
free(deptree);
10911176
return retval;

0 commit comments

Comments
 (0)