diff options
Diffstat (limited to 'src/librc')
| -rw-r--r-- | src/librc/librc.c | 88 | ||||
| -rw-r--r-- | src/librc/meson.build | 2 | ||||
| -rw-r--r-- | src/librc/rc.h.in | 19 | 
3 files changed, 109 insertions, 0 deletions
diff --git a/src/librc/librc.c b/src/librc/librc.c index a1bd706d..b3c98adb 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -33,6 +33,7 @@  #include "queue.h"  #include "librc.h" +#include "einfo.h"  #include "misc.h"  #include "rc.h"  #ifdef __FreeBSD__ @@ -349,6 +350,93 @@ detect_vm(const char *systype RC_UNUSED)  	return NULL;  } +#ifdef RC_USER_SERVICES + +bool +rc_is_user(void) +{ +	char *env; +	if ((env = getenv("RC_USER_SERVICES"))) { +		return (strcmp(env, "YES") == 0); +	} +	return false; +} + +void +rc_set_user(void) +{ +	char *path, *tmp; + +	/* Setting the sysconf path to XDG_CONFIG_HOME, or ~/.config/, so subdirectories would go: +	* ~/.config/init.d +	* ~/.config/conf.d +	* ~/.config/runlevels +	* ~/.config/rc.conf */ +	path = rc_user_sysconfdir(); +	if (mkdir(path, 0700) != 0 && errno != EEXIST) { +		eerrorx("mkdir: %s", strerror(errno)); +	} +	xasprintf(&tmp, "%s/%s", path, RC_USER_INITDIR_FOLDER); +	if (mkdir(tmp, 0700) != 0 && errno != EEXIST) { +		eerrorx("mkdir: %s", strerror(errno)); +	} +	free(tmp); +	xasprintf(&tmp, "%s/%s", path, RC_USER_CONFDIR_FOLDER); +	if (mkdir(tmp, 0700) != 0 && errno != EEXIST) { +		eerrorx("mkdir: %s", strerror(errno)); +	} +	free(tmp); +	xasprintf(&tmp, "%s/%s", path, RC_USER_RUNLEVELS_FOLDER); +	if (mkdir(tmp, 0700) != 0 && errno != EEXIST) { +		eerrorx("mkdir: %s", strerror(errno)); +	} +	free(tmp); +	xasprintf(&tmp, "%s/%s/%s", path, RC_USER_RUNLEVELS_FOLDER, RC_LEVEL_DEFAULT); +	if (mkdir(tmp, 0700) != 0 && errno != EEXIST) { +		eerrorx("mkdir: %s", strerror(errno)); +	} +	free(tmp); +	free(path); + +	path = rc_user_svcdir(); +	if (mkdir(path, 0700) != 0 && errno != EEXIST) { +		eerrorx("mkdir: %s", strerror(errno)); +	} +	free(path); + +	setenv("RC_USER_SERVICES", "YES", 1); +} + +char * +rc_user_sysconfdir(void) +{ +	char *env, *path = NULL; +	if ((env = getenv("XDG_CONFIG_HOME"))) { +		xasprintf(&path, "%s", env); +	} else if ((env = getenv("HOME"))) { +		xasprintf(&path, "%s/.config/", env); +	} else { +		eerrorx("Both XDG_CONFIG_HOME and HOME unset."); +	} +	return path; +} + +char * +rc_user_svcdir(void) +{ +	char *env, *path = NULL; +	uid_t id; +	if ((env = getenv("XDG_RUNTIME_DIR"))) { +		xasprintf(&path, "%s%s", env, RC_USER_RUNTIME_FOLDER); +	} else { +		id = getuid(); +		xasprintf(&path, "/tmp%s/%d/", RC_USER_RUNTIME_FOLDER, id); +	} +	return path; +} + +#endif +  const char *  rc_sys(void)  { diff --git a/src/librc/meson.build b/src/librc/meson.build index 30caa1f3..3020e9f2 100644 --- a/src/librc/meson.build +++ b/src/librc/meson.build @@ -25,8 +25,10 @@ rc_h = configure_file(input : 'rc.h.in', output : 'rc.h',  librc = library('rc', librc_sources,    dependencies: kvm_dep, +  c_args : cc_user_services_flags,    include_directories : [incdir, einfo_incdir],    link_depends : 'rc.map', +  link_with: libeinfo,    version : librc_version,    install : true,    install_dir : libdir) diff --git a/src/librc/rc.h.in b/src/librc/rc.h.in index 69d9d0e4..02e7d53f 100644 --- a/src/librc/rc.h.in +++ b/src/librc/rc.h.in @@ -47,6 +47,25 @@ extern "C" {  #define RC_CONF_D         RC_SYSCONFDIR "/rc.conf.d"  #define RC_CONF_OLD     RC_SYSCONFDIR "/conf.d/rc" +#ifdef RC_USER_SERVICES + +#define RC_SYS_USER_INITDIR              RC_INITDIR "/user.d" +#define RC_SYS_USER_CONFDIR              RC_CONFDIR "/user.d" +#define RC_USER_INITDIR_FOLDER           "/init.d" +#define RC_USER_CONFDIR_FOLDER           "/conf.d" +#define RC_USER_RUNLEVELS_FOLDER         "/runlevels" +#define RC_USER_RUNTIME_FOLDER           "/openrc" + +/*! Is openrc being ran in usermode? + * @return true if yes, otherwise false */ +bool rc_is_user(void); + +void rc_set_user(void); + +char *rc_user_sysconfdir(void); +char *rc_user_svcdir(void); +#endif +  #define RC_PATH_PREFIX     RC_LIBEXECDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin"  /* PKG_PREFIX is where packages are installed if different from the base OS  | 
