aboutsummaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2024-03-20 23:45:47 +0100
committerAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2024-07-19 20:40:28 +0200
commitc34fcd63f05044f9034b26c52f19c91e04668da7 (patch)
tree8ef449218cf736057197cd39fba1dac6117216c3 /src/shared
parent917a7031d946c0b608517936fab0f54689167265 (diff)
openrc: dynamic paths for user services
add two api functions, `rc_service_dir` and `rc_sysconf_dir`, both are generate paths (and sub-paths) for resources, and meant to replace the hardcoded variables like `RC_SVCDIR`. those functions differ by dynamically switching between the system path, or the user path, set in their home folder or runtime directory. this lays out the intial support for user services. Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/_usage.h9
-rw-r--r--src/shared/misc.c44
-rw-r--r--src/shared/misc.h13
3 files changed, 41 insertions, 25 deletions
diff --git a/src/shared/_usage.h b/src/shared/_usage.h
index 91b956e0..eb80bb3c 100644
--- a/src/shared/_usage.h
+++ b/src/shared/_usage.h
@@ -12,8 +12,9 @@
#include <getopt.h>
#include <stdlib.h>
+#include <librc.h>
-#define getoptstring_COMMON "ChqVv"
+#define getoptstring_COMMON "ChqVvU"
#define longopts_COMMON \
{ "help", 0, NULL, 'h'}, \
@@ -21,6 +22,7 @@
{ "version", 0, NULL, 'V'}, \
{ "verbose", 0, NULL, 'v'}, \
{ "quiet", 0, NULL, 'q'}, \
+ { "user", 0, NULL, 'U'}, \
{ NULL, 0, NULL, 0 }
#define longopts_help_COMMON \
@@ -28,13 +30,15 @@
"Disable color output", \
"Display software version", \
"Run verbosely", \
- "Run quietly (repeat to suppress errors)"
+ "Run quietly (repeat to suppress errors)", \
+ "Run in user mode"
#define case_RC_COMMON_getopt_case_C setenv ("EINFO_COLOR", "NO", 1);
#define case_RC_COMMON_getopt_case_h usage (EXIT_SUCCESS);
#define case_RC_COMMON_getopt_case_V if (argc == 2) show_version();
#define case_RC_COMMON_getopt_case_v setenv ("EINFO_VERBOSE", "YES", 1);
#define case_RC_COMMON_getopt_case_q set_quiet_options();
+#define case_RC_COMMON_getopt_case_U rc_set_user();
#define case_RC_COMMON_getopt_default usage (EXIT_FAILURE);
#define case_RC_COMMON_GETOPT \
@@ -43,6 +47,7 @@
case 'V': case_RC_COMMON_getopt_case_V; break; \
case 'v': case_RC_COMMON_getopt_case_v; break; \
case 'q': case_RC_COMMON_getopt_case_q; break; \
+ case 'U': case_RC_COMMON_getopt_case_U; break; \
default: case_RC_COMMON_getopt_default; break;
extern const char *applet;
diff --git a/src/shared/misc.c b/src/shared/misc.c
index 28f4e235..e946c911 100644
--- a/src/shared/misc.c
+++ b/src/shared/misc.c
@@ -64,6 +64,8 @@ static const char *const env_whitelist[] = {
"RC_DEBUG", "RC_NODEPS",
"LANG", "LC_MESSAGES", "TERM",
"EINFO_COLOR", "EINFO_VERBOSE",
+ "RC_USER_SERVICES", "HOME",
+ "XDG_RUNTIME_DIR", "XDG_CONFIG_HOME",
NULL
};
@@ -142,9 +144,11 @@ env_config(void)
char *np;
char *npp;
char *tok;
+ char *dir;
const char *sys = rc_sys();
char *buffer = NULL;
size_t size = 0;
+ const char *svc_dir = rc_service_dir();
/* Ensure our PATH is prefixed with the system locations first
for a little extra security */
@@ -175,8 +179,10 @@ env_config(void)
setenv("RC_VERSION", VERSION, 1);
setenv("RC_LIBEXECDIR", RC_LIBEXECDIR, 1);
- setenv("RC_SVCDIR", RC_SVCDIR, 1);
- setenv("RC_TMPDIR", RC_SVCDIR "/tmp", 1);
+ setenv("RC_SVCDIR", svc_dir, 1);
+ xasprintf(&dir, "%s/tmp", svc_dir);
+ setenv("RC_TMPDIR", dir, 1);
+ free(dir);
setenv("RC_BOOTLEVEL", RC_LEVEL_BOOT, 1);
e = rc_runlevel_get();
setenv("RC_RUNLEVEL", e, 1);
@@ -248,7 +254,7 @@ svc_lock(const char *applet, bool ignore_lock_failure)
char *file = NULL;
int fd;
- xasprintf(&file, RC_SVCDIR "/exclusive/%s", applet);
+ xasprintf(&file, "%s/exclusive/%s", rc_service_dir(), applet);
fd = open(file, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
free(file);
if (fd == -1)
@@ -274,10 +280,11 @@ svc_unlock(const char *applet, int fd)
{
char *file = NULL;
- xasprintf(&file, RC_SVCDIR "/exclusive/%s", applet);
+ xasprintf(&file, "%s/exclusive/%s", rc_service_dir(), applet);
close(fd);
unlink(file);
free(file);
+
return -1;
}
@@ -388,12 +395,16 @@ RC_DEPTREE * _rc_deptree_load(int force, int *regen)
t = 0;
if (rc_deptree_update_needed(&t, file) || force != 0) {
+ char *deptree_cache;
+ xasprintf(&deptree_cache, "%s/%s", rc_service_dir(), RC_DEPTREE_CACHE);
/* Test if we have permission to update the deptree */
- fd = open(RC_DEPTREE_CACHE, O_WRONLY);
+ fd = open(deptree_cache, O_WRONLY);
merrno = errno;
errno = serrno;
- if (fd == -1 && merrno == EACCES)
+ if (fd == -1 && merrno == EACCES) {
+ free(deptree_cache);
return rc_deptree_load();
+ }
close(fd);
if (regen)
@@ -403,29 +414,34 @@ RC_DEPTREE * _rc_deptree_load(int force, int *regen)
eend (retval, "Failed to update the dependency tree");
if (retval == 0) {
- if (stat(RC_DEPTREE_CACHE, &st) != 0) {
- eerror("stat(%s): %s", RC_DEPTREE_CACHE, strerror(errno));
+ char *deptree_skewed;
+ if (stat(deptree_cache, &st) != 0) {
+ eerror("stat(%s): %s", deptree_cache, strerror(errno));
+ free(deptree_cache);
return NULL;
}
+
+ xasprintf(&deptree_skewed, "%s/%s", rc_service_dir(), RC_DEPTREE_SKEWED);
if (st.st_mtime < t) {
eerror("Clock skew detected with `%s'", file);
- eerrorn("Adjusting mtime of `" RC_DEPTREE_CACHE
- "' to %s", ctime(&t));
- fp = fopen(RC_DEPTREE_SKEWED, "w");
+ eerrorn("Adjusting mtime of %s to %s", deptree_cache, ctime(&t));
+ fp = fopen(deptree_skewed, "w");
if (fp != NULL) {
fprintf(fp, "%s\n", file);
fclose(fp);
}
ut.actime = t;
ut.modtime = t;
- utime(RC_DEPTREE_CACHE, &ut);
+ utime(deptree_cache, &ut);
} else {
- if (exists(RC_DEPTREE_SKEWED))
- unlink(RC_DEPTREE_SKEWED);
+ if (exists(deptree_skewed))
+ unlink(deptree_skewed);
}
+ free(deptree_skewed);
}
if (force == -1 && regen != NULL)
*regen = retval;
+ free(deptree_cache);
}
return rc_deptree_load();
}
diff --git a/src/shared/misc.h b/src/shared/misc.h
index b158a786..9171a881 100644
--- a/src/shared/misc.h
+++ b/src/shared/misc.h
@@ -33,16 +33,11 @@
#define RC_LEVEL_BOOT "boot"
#define RC_LEVEL_DEFAULT "default"
-#define RC_DEPTREE_CACHE RC_SVCDIR "/deptree"
-#define RC_DEPTREE_SKEWED RC_SVCDIR "/clock-skewed"
+#define RC_DEPTREE_CACHE "deptree"
+#define RC_DEPTREE_SKEWED "clock-skewed"
#define RC_KRUNLEVEL RC_SVCDIR "/krunlevel"
-#define RC_STARTING RC_SVCDIR "/rc.starting"
-#define RC_STOPPING RC_SVCDIR "/rc.stopping"
-
-#define RC_SVCDIR_STARTING RC_SVCDIR "/starting"
-#define RC_SVCDIR_INACTIVE RC_SVCDIR "/inactive"
-#define RC_SVCDIR_STARTED RC_SVCDIR "/started"
-#define RC_SVCDIR_COLDPLUGGED RC_SVCDIR "/coldplugged"
+#define RC_STARTING "rc.starting"
+#define RC_STOPPING "rc.stopping"
char *rc_conf_value(const char *var);
bool rc_conf_yesno(const char *var);