diff --git a/src/librc/librc-depend.c b/src/librc/librc-depend.c index d584af2cd..34cf75ba5 100644 --- a/src/librc/librc-depend.c +++ b/src/librc/librc-depend.c @@ -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; @@ -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 * @@ -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 }; @@ -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. @@ -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); } @@ -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(); @@ -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", @@ -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; diff --git a/src/librc/librc.c b/src/librc/librc.c index 33737c992..34555afe1 100644 --- a/src/librc/librc.c +++ b/src/librc/librc.c @@ -40,7 +40,8 @@ # include #endif -#define RC_RUNLEVEL RC_SVCDIR "/softlevel" +#define RC_RUNLEVEL_FOLDER "/softlevel" +#define RC_RUNLEVEL RC_SVCDIR RC_RUNLEVEL_FOLDER #ifndef S_IXUGO # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH) @@ -473,6 +474,7 @@ get_runlevel_chain(const char *runlevel, RC_STRINGLIST *level_list, RC_STRINGLIS RC_STRINGLIST *dirs; RC_STRING *d, *parent; const char *nextlevel; + char *runlevel_dir = RC_RUNLEVELDIR; /* * If we haven't been passed a runlevel or a level list, or @@ -494,7 +496,15 @@ get_runlevel_chain(const char *runlevel, RC_STRINGLIST *level_list, RC_STRINGLIS * We can now do exactly the above procedure for our chained * runlevels. */ - snprintf(path, sizeof(path), "%s/%s", RC_RUNLEVELDIR, runlevel); +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_sysconf = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconf, RC_USER_RUNLEVELS_FOLDER); + free(user_sysconf); + } +#endif + + snprintf(path, sizeof(path), "%s/%s", runlevel_dir, runlevel); dirs = ls_dir(path, LS_DIR); TAILQ_FOREACH(d, dirs, entries) { nextlevel = d->value; @@ -517,33 +527,78 @@ get_runlevel_chain(const char *runlevel, RC_STRINGLIST *level_list, RC_STRINGLIS rc_stringlist_delete(ancestor_list, nextlevel); } rc_stringlist_free(dirs); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif } bool rc_runlevel_starting(void) { - return exists(RC_STARTING); + char *rc_starting = RC_STARTING; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_svcdir = rc_user_svcdir(); + xasprintf(&rc_starting, "%s/%s", user_svcdir, RC_STARTING_FOLDER); + free(user_svcdir); + } +#endif + return exists(rc_starting); } bool rc_runlevel_stopping(void) { - return exists(RC_STOPPING); + char *rc_stopping = RC_STOPPING; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_svcdir = rc_user_svcdir(); + xasprintf(&rc_stopping, "%s/%s", user_svcdir, RC_STOPPING_FOLDER); + free(user_svcdir); + } +#endif + return exists(rc_stopping); } RC_STRINGLIST *rc_runlevel_list(void) { - return ls_dir(RC_RUNLEVELDIR, LS_DIR); + char *runlevel_dir = RC_RUNLEVELDIR; + RC_STRINGLIST *runlevels; +#ifdef RC_USER_SERVICES + char *user_sysconfdir; + if (rc_is_user()) { + user_sysconfdir = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconfdir, RC_USER_RUNLEVELS_FOLDER); + } +#endif + runlevels = ls_dir(runlevel_dir, LS_DIR); +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif + return runlevels; } char * rc_runlevel_get(void) { FILE *fp; - char *runlevel = NULL; + char *runlevel = NULL, *runlevel_path = RC_RUNLEVEL; size_t i; - if ((fp = fopen(RC_RUNLEVEL, "r"))) { +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *path = rc_user_svcdir(); + xasprintf(&runlevel_path, "%s/%s", path, RC_RUNLEVEL_FOLDER); + free(path); + } +#endif + + if ((fp = fopen(runlevel_path, "r"))) { runlevel = xmalloc(sizeof(char) * PATH_MAX); if (fgets(runlevel, PATH_MAX, fp)) { i = strlen(runlevel) - 1; @@ -554,6 +609,13 @@ rc_runlevel_get(void) fclose(fp); } +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_path); + } +#endif + + if (!runlevel || !*runlevel) { free(runlevel); runlevel = xstrdup(RC_LEVEL_SYSINIT); @@ -565,12 +627,30 @@ rc_runlevel_get(void) bool rc_runlevel_set(const char *runlevel) { - FILE *fp = fopen(RC_RUNLEVEL, "w"); + char *runlevel_path = RC_RUNLEVEL; + FILE *fp; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *path = rc_user_svcdir(); + xasprintf(&runlevel_path, "%s/%s", path, RC_RUNLEVEL_FOLDER); + free(path); + } +#endif + + fp = fopen(runlevel_path, "w"); if (!fp) return false; fprintf(fp, "%s", runlevel); fclose(fp); + + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_path); + } +#endif return true; } @@ -583,7 +663,19 @@ rc_runlevel_exists(const char *runlevel) if (!runlevel || strcmp(runlevel, "") == 0 || strcmp(runlevel, ".") == 0 || strcmp(runlevel, "..") == 0) return false; - snprintf(path, sizeof(path), "%s/%s", RC_RUNLEVELDIR, runlevel); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_path = rc_user_sysconfdir(); + snprintf(path, sizeof(path), "%s/%s/%s", + user_path, RC_USER_RUNLEVELS_FOLDER, runlevel); + } else { +#endif + snprintf(path, sizeof(path), "%s/%s", RC_RUNLEVELDIR, runlevel); +#ifdef RC_USER_SERVICES + } +#endif + if (stat(path, &buf) == 0 && S_ISDIR(buf.st_mode)) return true; return false; @@ -593,11 +685,28 @@ bool rc_runlevel_stack(const char *dst, const char *src) { char d[PATH_MAX], s[PATH_MAX]; + char *runlevel_dir = RC_RUNLEVELDIR; if (!rc_runlevel_exists(dst) || !rc_runlevel_exists(src)) return false; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_sysconf = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconf, RC_USER_RUNLEVELS_FOLDER); + free(user_sysconf); + } +#endif + snprintf(s, sizeof(s), "../%s", src); - snprintf(d, sizeof(s), "%s/%s/%s", RC_RUNLEVELDIR, dst, src); + snprintf(d, sizeof(s), "%s/%s/%s", runlevel_dir, dst, src); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif + return (symlink(s, d) == 0 ? true : false); } @@ -605,8 +714,23 @@ bool rc_runlevel_unstack(const char *dst, const char *src) { char path[PATH_MAX]; + char *runlevel_dir = RC_RUNLEVELDIR; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_sysconf = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconf, RC_USER_RUNLEVELS_FOLDER); + free(user_sysconf); + } +#endif + + snprintf(path, sizeof(path), "%s/%s/%s", runlevel_dir, dst, src); - snprintf(path, sizeof(path), "%s/%s/%s", RC_RUNLEVELDIR, dst, src); +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif return (unlink(path) == 0 ? true : false); } @@ -628,9 +752,16 @@ rc_service_resolve(const char *service) { char buffer[PATH_MAX]; char file[PATH_MAX]; + char *svcdir = RC_SVCDIR; int r; struct stat buf; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif + if (!service) return NULL; @@ -638,14 +769,20 @@ rc_service_resolve(const char *service) return xstrdup(service); /* First check started services */ - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", "started", service); + snprintf(file, sizeof(file), "%s/%s/%s", svcdir, "started", service); if (lstat(file, &buf) || !S_ISLNK(buf.st_mode)) { - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "inactive", service); + snprintf(file, sizeof(file), "%s/%s/%s", + svcdir, "inactive", service); if (lstat(file, &buf) || !S_ISLNK(buf.st_mode)) *file = '\0'; } +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(svcdir); + } +#endif + if (*file) { memset(buffer, 0, sizeof(buffer)); r = readlink(file, buffer, sizeof(buffer)-1); @@ -653,6 +790,24 @@ rc_service_resolve(const char *service) return xstrdup(buffer); } +#ifdef RC_USER_SERVICES + /* If we're in user services mode, try user services*/ + if (rc_is_user()) { + /* User defined services have preference to system provided */ + char *user_sysconfdir = rc_user_sysconfdir(); + snprintf(file, sizeof(file), "%s/%s/%s", + user_sysconfdir, RC_USER_INITDIR_FOLDER, service); + free(user_sysconfdir); + if (stat(file, &buf) == 0) + return xstrdup(file); + + snprintf(file, sizeof(file), "%s/%s", + RC_SYS_USER_INITDIR, service); + if (stat(file, &buf) == 0) + return xstrdup(file); + } +#endif + #ifdef RC_LOCAL_INITDIR /* Nope, so lets see if the user has written it */ snprintf(file, sizeof(file), RC_LOCAL_INITDIR "/%s", service); @@ -787,9 +942,24 @@ bool rc_service_in_runlevel(const char *service, const char *runlevel) { char file[PATH_MAX]; + char *runlevel_dir = RC_RUNLEVELDIR; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_sysconf = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconf, RC_USER_RUNLEVELS_FOLDER); + free(user_sysconf); + } +#endif + snprintf(file, sizeof(file), "%s/%s/%s", + runlevel_dir, runlevel, basename_c(service)); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif - snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", - runlevel, basename_c(service)); return exists(file); } @@ -807,6 +977,12 @@ rc_service_mark(const char *service, const RC_SERVICE state) RC_STRINGLIST *dirs; RC_STRING *dir; int serrno; + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif if (!init) return false; @@ -818,8 +994,8 @@ rc_service_mark(const char *service, const RC_SERVICE state) return false; } - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - rc_parse_service_state(state), base); + snprintf(file, sizeof(file), "%s/%s/%s", + svcdir,rc_parse_service_state(state), base); if (exists(file)) unlink(file); i = symlink(init, file); @@ -845,15 +1021,16 @@ rc_service_mark(const char *service, const RC_SERVICE state) s != RC_SERVICE_SCHEDULED) && (!skip_wasinactive || s != RC_SERVICE_WASINACTIVE)) { - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - rc_service_state_names[i].name, base); + snprintf(file, sizeof(file), "%s/%s/%s", + svcdir, rc_service_state_names[i].name, base); if (exists(file)) { if ((state == RC_SERVICE_STARTING || state == RC_SERVICE_STOPPING) && s == RC_SERVICE_INACTIVE) { snprintf(was, sizeof(was), - RC_SVCDIR "/%s/%s", + "%s/%s/%s", + svcdir, rc_parse_service_state(RC_SERVICE_WASINACTIVE), base); if (symlink(init, was) == -1) { @@ -875,19 +1052,19 @@ rc_service_mark(const char *service, const RC_SERVICE state) state == RC_SERVICE_STOPPED || state == RC_SERVICE_INACTIVE) { - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "exclusive", base); + snprintf(file, sizeof(file), "%s/%s/%s", + svcdir, "exclusive", base); unlink(file); } /* Remove any options and daemons the service may have stored */ if (state == RC_SERVICE_STOPPED) { - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "options", base); + snprintf(file, sizeof(file), "%s/%s/%s", + svcdir, "options", base); rm_dir(file, true); - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - "daemons", base); + snprintf(file, sizeof(file), "%s/%s/%s", + svcdir, "daemons", base); rm_dir(file, true); rc_service_schedule_clear(service); @@ -895,7 +1072,7 @@ rc_service_mark(const char *service, const RC_SERVICE state) /* These are final states, so remove us from scheduled */ if (state == RC_SERVICE_STARTED || state == RC_SERVICE_STOPPED) { - snprintf(file, sizeof(file), RC_SVCDIR "/%s", "scheduled"); + snprintf(file, sizeof(file), "%s/%s", svcdir, "scheduled"); dirs = ls_dir(file, 0); TAILQ_FOREACH(dir, dirs, entries) { snprintf(was, sizeof(was), "%s/%s/%s", @@ -923,10 +1100,16 @@ rc_service_state(const char *service) RC_STRINGLIST *dirs; RC_STRING *dir; const char *base = basename_c(service); + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif for (i = 0; rc_service_state_names[i].name; i++) { - snprintf(file, sizeof(file), RC_SVCDIR "/%s/%s", - rc_service_state_names[i].name, base); + snprintf(file, sizeof(file), "%s/%s/%s", + svcdir, rc_service_state_names[i].name, base); if (exists(file)) { if (rc_service_state_names[i].state <= 0x10) state = rc_service_state_names[i].state; @@ -940,11 +1123,14 @@ rc_service_state(const char *service) state |= RC_SERVICE_CRASHED; } if (state & RC_SERVICE_STOPPED) { - dirs = ls_dir(RC_SVCDIR "/scheduled", 0); + char *scheduled; + xasprintf(&scheduled, "%s/%s", svcdir, "/scheduled"); + dirs = ls_dir(scheduled, 0); + free(scheduled); TAILQ_FOREACH(dir, dirs, entries) { snprintf(file, sizeof(file), - RC_SVCDIR "/scheduled/%s/%s", - dir->value, service); + "%s/scheduled/%s/%s", + svcdir, dir->value, service); if (exists(file)) { state |= RC_SERVICE_SCHEDULED; break; @@ -962,9 +1148,15 @@ rc_service_value_get(const char *service, const char *option) char *buffer = NULL; size_t len = 0; char file[PATH_MAX]; + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif - snprintf(file, sizeof(file), RC_SVCDIR "/options/%s/%s", - service, option); + snprintf(file, sizeof(file), "%s/options/%s/%s", + svcdir, service, option); rc_getfile(file, &buffer, &len); return buffer; @@ -977,8 +1169,14 @@ rc_service_value_set(const char *service, const char *option, FILE *fp; char file[PATH_MAX]; char *p = file; + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif - p += snprintf(file, sizeof(file), RC_SVCDIR "/options/%s", service); + p += snprintf(file, sizeof(file), "%s/options/%s", svcdir, service); if (mkdir(file, 0755) != 0 && errno != EEXIST) return false; @@ -1002,13 +1200,19 @@ rc_service_schedule_start(const char *service, const char *service_to_start) char *p = file; char *init; bool retval; + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif /* service may be a provided service, like net */ if (!service || !rc_service_exists(service_to_start)) return false; - p += snprintf(file, sizeof(file), RC_SVCDIR "/scheduled/%s", - basename_c(service)); + p += snprintf(file, sizeof(file), "%s/scheduled/%s", + svcdir, basename_c(service)); if (mkdir(file, 0755) != 0 && errno != EEXIST) return false; @@ -1024,9 +1228,15 @@ bool rc_service_schedule_clear(const char *service) { char dir[PATH_MAX]; + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif - snprintf(dir, sizeof(dir), RC_SVCDIR "/scheduled/%s", - basename_c(service)); + snprintf(dir, sizeof(dir), "%s/scheduled/%s", + svcdir, basename_c(service)); if (!rm_dir(dir, true) && errno == ENOENT) return true; return false; @@ -1037,6 +1247,7 @@ rc_services_in_runlevel(const char *runlevel) { char dir[PATH_MAX]; RC_STRINGLIST *list = NULL; + char *runlevel_dir = RC_RUNLEVELDIR; if (!runlevel) { #ifdef RC_PKG_INITDIR @@ -1045,9 +1256,28 @@ rc_services_in_runlevel(const char *runlevel) #ifdef RC_LOCAL_INITDIR RC_STRINGLIST *local = ls_dir(RC_LOCAL_INITDIR, LS_INITD); #endif +#ifdef RC_USER_SERVICES + RC_STRINGLIST *local_user; + RC_STRINGLIST *sys_user; + char *user_sysconf; + char *user_initdir; + if (rc_is_user()) { + user_sysconf = rc_user_sysconfdir(); + xasprintf(&user_initdir, "%s/%s", user_sysconf, RC_USER_INITDIR_FOLDER); + + local_user = ls_dir(user_initdir, LS_INITD); + sys_user = ls_dir(RC_SYS_USER_INITDIR, LS_INITD); + + list = rc_stringlist_new(); + } else { +#endif list = ls_dir(RC_INITDIR, LS_INITD); +#ifdef RC_USER_SERVICES + } +#endif + #ifdef RC_PKG_INITDIR TAILQ_CONCAT(list, pkg, entries); rc_stringlist_free(pkg); @@ -1055,14 +1285,38 @@ rc_services_in_runlevel(const char *runlevel) #ifdef RC_LOCAL_INITDIR TAILQ_CONCAT(list, local, entries); rc_stringlist_free(local); +#endif +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + TAILQ_CONCAT(list, local_user, entries); + TAILQ_CONCAT(list, sys_user, entries); + rc_stringlist_free(local_user); + rc_stringlist_free(sys_user); + free(user_sysconf); + free(user_initdir); + } #endif return list; } /* These special levels never contain any services */ if (strcmp(runlevel, RC_LEVEL_SINGLE) != 0) { - snprintf(dir, sizeof(dir), RC_RUNLEVELDIR "/%s", runlevel); +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_sysconf = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconf, RC_USER_RUNLEVELS_FOLDER); + free(user_sysconf); + } +#endif + snprintf(dir, sizeof(dir), "%s/%s", runlevel_dir, runlevel); list = ls_dir(dir, LS_INITD); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif + } if (!list) list = rc_stringlist_new(); @@ -1095,9 +1349,16 @@ rc_services_in_state(RC_SERVICE state) RC_STRING *d; char dir[PATH_MAX]; char *p = dir; + char *svcdir = RC_SVCDIR; - p += snprintf(dir, sizeof(dir), RC_SVCDIR "/%s", - rc_parse_service_state(state)); +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif + + p += snprintf(dir, sizeof(dir), "%s/%s", + svcdir, rc_parse_service_state(state)); if (state != RC_SERVICE_SCHEDULED) return ls_dir(dir, LS_INITD); @@ -1116,6 +1377,13 @@ rc_services_in_state(RC_SERVICE state) } } rc_stringlist_free(dirs); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(svcdir); + } +#endif + return list; } @@ -1127,7 +1395,15 @@ rc_service_add(const char *runlevel, const char *service) char file[PATH_MAX]; char path[MAXPATHLEN] = { '\0' }; char binit[PATH_MAX]; + char *runlevel_dir = RC_RUNLEVELDIR; char *i; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_sysconf = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconf, RC_USER_RUNLEVELS_FOLDER); + free(user_sysconf); + } +#endif if (!rc_runlevel_exists(runlevel)) { errno = ENOENT; @@ -1140,8 +1416,14 @@ rc_service_add(const char *runlevel, const char *service) } i = init = rc_service_resolve(service); - snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", - runlevel, basename_c(service)); + snprintf(file, sizeof(file), "%s/%s/%s", + runlevel_dir, runlevel, basename_c(service)); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif /* We need to ensure that only things in /etc/init.d are added * to the boot runlevel */ @@ -1168,9 +1450,25 @@ bool rc_service_delete(const char *runlevel, const char *service) { char file[PATH_MAX]; + char *runlevel_dir = RC_RUNLEVELDIR; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + char *user_sysconf = rc_user_sysconfdir(); + xasprintf(&runlevel_dir, "%s/%s", user_sysconf, RC_USER_RUNLEVELS_FOLDER); + free(user_sysconf); + } +#endif + + snprintf(file, sizeof(file), "%s/%s/%s", + runlevel_dir, runlevel, basename_c(service)); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(runlevel_dir); + } +#endif - snprintf(file, sizeof(file), RC_RUNLEVELDIR "/%s/%s", - runlevel, basename_c(service)); if (unlink(file) == 0) return true; return false; @@ -1179,14 +1477,25 @@ rc_service_delete(const char *runlevel, const char *service) RC_STRINGLIST * rc_services_scheduled_by(const char *service) { - RC_STRINGLIST *dirs = ls_dir(RC_SVCDIR "/scheduled", 0); + RC_STRINGLIST *dirs; RC_STRINGLIST *list = rc_stringlist_new(); RC_STRING *dir; char file[PATH_MAX]; + char *scheduled; + char *svcdir = RC_SVCDIR; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif + xasprintf(&scheduled, "%s/%s", svcdir, "/scheduled"); + dirs = ls_dir(scheduled, 0); + free(scheduled); TAILQ_FOREACH(dir, dirs, entries) { - snprintf(file, sizeof(file), RC_SVCDIR "/scheduled/%s/%s", - dir->value, service); + snprintf(file, sizeof(file), "%s/scheduled/%s/%s", + svcdir, dir->value, service); if (exists(file)) rc_stringlist_add(list, file); } @@ -1198,8 +1507,15 @@ RC_STRINGLIST * rc_services_scheduled(const char *service) { char dir[PATH_MAX]; + char *svcdir = RC_SVCDIR; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif - snprintf(dir, sizeof(dir), RC_SVCDIR "/scheduled/%s", - basename_c(service)); + snprintf(dir, sizeof(dir), "%s/scheduled/%s", + svcdir, basename_c(service)); return ls_dir(dir, LS_INITD); } diff --git a/src/openrc-run/meson.build b/src/openrc-run/meson.build index 2e4b29a0e..5de604560 100644 --- a/src/openrc-run/meson.build +++ b/src/openrc-run/meson.build @@ -1,6 +1,6 @@ executable('openrc-run', ['openrc-run.c', misc_c, plugin_c, selinux_c, usage_c, version_h], - c_args : [cc_audit_flags, cc_branding_flags, cc_pam_flags, cc_selinux_flags], + c_args : [cc_audit_flags, cc_branding_flags, cc_pam_flags, cc_selinux_flags, cc_user_services_flags], link_with: [libeinfo, librc], dependencies: [audit_dep, dl_dep, pam_dep, pam_misc_dep, selinux_dep, util_dep, crypt_dep], include_directories: [incdir, einfo_incdir, rc_incdir], @@ -9,7 +9,7 @@ executable('openrc-run', executable('runscript', ['openrc-run.c', misc_c, plugin_c, selinux_c, usage_c, version_h], - c_args : [cc_audit_flags, cc_branding_flags, cc_pam_flags, cc_selinux_flags], + c_args : [cc_audit_flags, cc_branding_flags, cc_pam_flags, cc_selinux_flags, cc_user_services_flags], link_with: [libeinfo, librc], dependencies: [audit_dep, dl_dep, pam_dep, pam_misc_dep, selinux_dep, util_dep, crypt_dep], include_directories: [incdir, einfo_incdir, rc_incdir], diff --git a/src/openrc-run/openrc-run.c b/src/openrc-run/openrc-run.c index 2b1d37462..4484445a0 100644 --- a/src/openrc-run/openrc-run.c +++ b/src/openrc-run/openrc-run.c @@ -348,6 +348,13 @@ svc_exec(const char *arg1, const char *arg2) int slave_tty; sigset_t sigchldmask; sigset_t oldmask; + char *runsh; + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif /* Setup our signal pipe */ if (pipe(signal_pipe) == -1) @@ -388,20 +395,19 @@ svc_exec(const char *arg1, const char *arg2) dup2(slave_tty, STDERR_FILENO); } - if (exists(RC_SVCDIR "/openrc-run.sh")) { + xasprintf(&runsh, "%s/%s", svcdir, "openrc-run.sh"); + if (exists(runsh)) { if (arg2) einfov("Executing: %s %s %s %s %s", - RC_SVCDIR "/openrc-run.sh", RC_SVCDIR "/openrc-run.sh", + runsh, runsh, service, arg1, arg2); else einfov("Executing: %s %s %s %s", - RC_SVCDIR "/openrc-run.sh", RC_SVCDIR "/openrc-run.sh", + runsh, runsh, service, arg1); - execl(RC_SVCDIR "/openrc-run.sh", - RC_SVCDIR "/openrc-run.sh", - service, arg1, arg2, (char *) NULL); - eerror("%s: exec `" RC_SVCDIR "/openrc-run.sh': %s", - service, strerror(errno)); + execl(runsh, runsh, service, arg1, arg2, (char *) NULL); + eerror("%s: exec '%s': %s", + service, runsh, strerror(errno)); _exit(EXIT_FAILURE); } else { if (arg2) @@ -421,7 +427,14 @@ svc_exec(const char *arg1, const char *arg2) service, strerror(errno)); _exit(EXIT_FAILURE); } + free(runsh); + } + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(svcdir); } +#endif buffer = xmalloc(sizeof(char) * BUFSIZ); fd[0].fd = signal_pipe[0]; @@ -491,6 +504,12 @@ svc_wait(const char *svc) bool forever = false; RC_STRINGLIST *keywords; struct timespec interval, timeout, warn; + char *svcdir = RC_SVCDIR; +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif /* Some services don't have a timeout, like fsck */ keywords = rc_deptree_depend(deptree, svc, "keyword"); @@ -499,7 +518,13 @@ svc_wait(const char *svc) forever = true; rc_stringlist_free(keywords); - xasprintf(&file, RC_SVCDIR "/exclusive/%s", basename_c(svc)); + xasprintf(&file, "%s/exclusive/%s", svcdir, basename_c(svc)); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(svcdir); + } +#endif interval.tv_sec = 0; interval.tv_nsec = WAIT_INTERVAL; diff --git a/src/openrc/meson.build b/src/openrc/meson.build index 9ab32ef42..3ad0d62b0 100644 --- a/src/openrc/meson.build +++ b/src/openrc/meson.build @@ -1,6 +1,6 @@ executable('openrc', ['rc.c', 'rc-logger.c', misc_c, plugin_c, usage_c, version_h], - c_args : cc_branding_flags, + c_args : [cc_branding_flags, cc_user_services_flags], link_with: [libeinfo, librc], dependencies: [dl_dep, util_dep], include_directories: [incdir, einfo_incdir, rc_incdir], @@ -9,7 +9,7 @@ executable('openrc', executable('rc', ['rc.c', 'rc-logger.c', misc_c, plugin_c, usage_c, version_h], - c_args : cc_branding_flags, + c_args : [cc_branding_flags, cc_user_services_flags], link_with: [libeinfo, librc], dependencies: [dl_dep, util_dep], include_directories: [incdir, einfo_incdir, rc_incdir], diff --git a/src/openrc/rc.c b/src/openrc/rc.c index aaf1043e9..5e026ee42 100644 --- a/src/openrc/rc.c +++ b/src/openrc/rc.c @@ -49,12 +49,20 @@ #include "helpers.h" const char *extraopts = NULL; -const char getoptstring[] = "a:no:s:S" getoptstring_COMMON; +const char getoptstring[] = "a:no:s:S" getoptstring_COMMON +#ifdef RC_USER_SERVICES +getoptstring_USER_SERVICES +#endif +; + const struct option longopts[] = { { "no-stop", 0, NULL, 'n' }, { "override", 1, NULL, 'o' }, { "service", 1, NULL, 's' }, { "sys", 0, NULL, 'S' }, +#ifdef RC_USER_SERVICES + longopts_USER_SERVICES +#endif longopts_COMMON }; const char * const longopts_help[] = { @@ -62,6 +70,9 @@ const char * const longopts_help[] = { "override the next runlevel to change into\nwhen leaving single user or boot runlevels", "runs the service specified with the rest\nof the arguments", "output the RC system type, if any", +#ifdef RC_USER_SERVICES + longopts_help_USER_SERVICES +#endif longopts_help_COMMON }; const char *usagestring = "" \ @@ -118,6 +129,12 @@ cleanup(void) { RC_PID *p1 = LIST_FIRST(&service_pids); RC_PID *p2; + char *rc_starting = RC_STARTING; + char *rc_stopping = RC_STOPPING; + +#ifdef RC_USER_SERVICES + char *user_svcdir; +#endif if (!rc_in_logger && !rc_in_plugin && applet && (strcmp(applet, "rc") == 0 || strcmp(applet, "openrc") == 0)) @@ -132,11 +149,27 @@ cleanup(void) free(termios_orig); } +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + user_svcdir = rc_user_svcdir(); + xasprintf(&rc_starting, "%s/%s", user_svcdir, RC_STARTING_FOLDER); + xasprintf(&rc_stopping, "%s/%s", user_svcdir, RC_STOPPING_FOLDER); + free(user_svcdir); + } +#endif + /* Clean runlevel start, stop markers */ - rmdir(RC_STARTING); - rmdir(RC_STOPPING); + rmdir(rc_starting); + rmdir(rc_stopping); clean_failed(); rc_logger_close(); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(rc_starting); + free(rc_stopping); + } +#endif } while (p1) { @@ -727,10 +760,16 @@ int main(int argc, char **argv) int depoptions = RC_DEP_STRICT | RC_DEP_TRACE; char *krunlevel = NULL; char *pidstr = NULL; + char *rc_starting = RC_STARTING; + char *rc_stopping = RC_STOPPING; int opt; bool parallel; int regen = 0; bool nostop = false; +#ifdef RC_USER_SERVICES + char *user_svcdir = NULL; + char *user_sysconfdir = NULL; +#endif #ifdef __linux__ char *proc; char *p; @@ -756,11 +795,6 @@ int main(int argc, char **argv) if (chdir("/") == -1) eerror("chdir: %s", strerror(errno)); - /* Ensure our environment is pure - * Also, add our configuration to it */ - env_filter(); - env_config(); - /* complain about old configuration settings if they exist */ if (exists(RC_CONF_OLD)) { ewarn("%s still exists on your system and should be removed.", @@ -805,10 +839,18 @@ int main(int argc, char **argv) printf("%s\n", systype); exit(EXIT_SUCCESS); /* NOTREACHED */ +#ifdef RC_USER_SERVICES + case_RC_USER_SERVICES +#endif case_RC_COMMON_GETOPT } } + /* Ensure our environment is pure + * Also, add our configuration to it */ + env_filter(); + env_config(); + if (strcmp(applet, "rc") == 0) ewarn("rc is deprecated, please use openrc instead."); newlevel = argv[optind++]; @@ -830,6 +872,15 @@ int main(int argc, char **argv) setenv("RC_PID", pidstr, 1); free(pidstr); +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + user_svcdir = rc_user_svcdir(); + xasprintf(&rc_stopping, "%s/%s", user_svcdir, RC_STOPPING_FOLDER); + xasprintf(&rc_starting, "%s/%s", user_svcdir, RC_STARTING_FOLDER); + free(user_svcdir); + } +#endif + /* Create a list of all services which should be started for the new or * current runlevel including those in boot, sysinit and hotplugged * runlevels. Clearly, some of these will already be started so we @@ -932,7 +983,7 @@ int main(int argc, char **argv) /* Clean the failed services state dir */ clean_failed(); - if (mkdir(RC_STOPPING, 0755) != 0) { + if (mkdir(rc_stopping, 0755) != 0) { if (errno == EACCES) eerrorx("%s: superuser access required", applet); eerrorx("%s: failed to create stopping dir `%s': %s", @@ -1018,7 +1069,7 @@ int main(int argc, char **argv) going_down ? newlevel : runlevel); hook_out = 0; - rmdir(RC_STOPPING); + rmdir(rc_stopping); /* Store the new runlevel */ if (newlevel) { @@ -1035,7 +1086,7 @@ int main(int argc, char **argv) rc_logger_close(); #endif - mkdir(RC_STARTING, 0755); + mkdir(rc_starting, 0755); rc_plugin_run(RC_HOOK_RUNLEVEL_START_IN, runlevel); hook_out = RC_HOOK_RUNLEVEL_START_OUT; @@ -1083,6 +1134,12 @@ int main(int argc, char **argv) } rc_stringlist_free(runlevel_chain); } +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(rc_stopping); + free(rc_starting); + } +#endif #ifdef __linux__ /* If the "noinit" parameter was passed on the kernel command line then diff --git a/src/rc-service/meson.build b/src/rc-service/meson.build index 3a109d8e6..543daf451 100644 --- a/src/rc-service/meson.build +++ b/src/rc-service/meson.build @@ -1,6 +1,6 @@ executable('rc-service', ['rc-service.c', misc_c, usage_c, version_h], - c_args : cc_branding_flags, + c_args : [cc_branding_flags, cc_user_services_flags], link_with: [libeinfo, librc], include_directories: [incdir, einfo_incdir, rc_incdir], install: true, diff --git a/src/rc-service/rc-service.c b/src/rc-service/rc-service.c index 3d2eb730b..5127e75f2 100644 --- a/src/rc-service/rc-service.c +++ b/src/rc-service/rc-service.c @@ -31,7 +31,11 @@ const char *applet = NULL; const char *extraopts = NULL; -const char getoptstring[] = "cdDe:ilr:INsSZ" getoptstring_COMMON; +const char getoptstring[] = "cdDe:ilr:INsSZ" getoptstring_COMMON +#ifdef RC_USER_SERVICES +getoptstring_USER_SERVICES +#endif +; const struct option longopts[] = { { "debug", 0, NULL, 'd' }, { "nodeps", 0, NULL, 'D' }, @@ -45,6 +49,9 @@ const struct option longopts[] = { { "list", 0, NULL, 'l' }, { "resolve", 1, NULL, 'r' }, { "dry-run", 0, NULL, 'Z' }, +#ifdef RC_USER_SERVICES + longopts_USER_SERVICES +#endif longopts_COMMON }; const char * const longopts_help[] = { @@ -60,6 +67,9 @@ const char * const longopts_help[] = { "list all available services", "resolve the service name to an init script", "dry run (show what would happen)", +#ifdef RC_USER_SERVICES + longopts_help_USER_SERVICES +#endif longopts_help_COMMON }; const char *usagestring = "" \ @@ -141,7 +151,9 @@ int main(int argc, char **argv) case 'Z': setenv("IN_DRYRUN", "yes", 1); break; - +#ifdef RC_USER_SERVICES + case_RC_USER_SERVICES +#endif case_RC_COMMON_GETOPT } } diff --git a/src/rc-status/meson.build b/src/rc-status/meson.build index 7377d8372..ee3f3782f 100644 --- a/src/rc-status/meson.build +++ b/src/rc-status/meson.build @@ -1,6 +1,6 @@ executable('rc-status', ['rc-status.c', misc_c, usage_c, version_h], - c_args : cc_branding_flags, + c_args : [cc_branding_flags, cc_user_services_flags], link_with: [libeinfo, librc], dependencies: [util_dep], include_directories: [incdir, einfo_incdir, rc_incdir], diff --git a/src/rc-status/rc-status.c b/src/rc-status/rc-status.c index 78d5a6af0..d56fe8307 100644 --- a/src/rc-status/rc-status.c +++ b/src/rc-status/rc-status.c @@ -39,7 +39,11 @@ enum format_t { const char *applet = NULL; const char *extraopts = NULL; +#ifdef RC_USER_SERVICES +const char getoptstring[] = "acf:lmrsSu" getoptstring_COMMON getoptstring_USER_SERVICES; +#else const char getoptstring[] = "acf:lmrsSu" getoptstring_COMMON; +#endif const struct option longopts[] = { {"all", 0, NULL, 'a'}, {"crashed", 0, NULL, 'c'}, @@ -50,6 +54,9 @@ const struct option longopts[] = { {"servicelist", 0, NULL, 's'}, {"supervised", 0, NULL, 'S'}, {"unused", 0, NULL, 'u'}, +#ifdef RC_USER_SERVICES + longopts_USER_SERVICES +#endif longopts_COMMON }; const char * const longopts_help[] = { @@ -62,6 +69,9 @@ const char * const longopts_help[] = { "Show service list", "show supervised services", "Show services not assigned to any runlevel", +#ifdef RC_USER_SERVICES + longopts_help_USER_SERVICES +#endif longopts_help_COMMON }; const char *usagestring = "" \ @@ -354,6 +364,9 @@ int main(int argc, char **argv) goto exit; /* NOTREACHED */ +#ifdef RC_USER_SERVICES + case_RC_USER_SERVICES +#endif case_RC_COMMON_GETOPT } diff --git a/src/rc-update/meson.build b/src/rc-update/meson.build index 7d2849791..4d3b9015d 100644 --- a/src/rc-update/meson.build +++ b/src/rc-update/meson.build @@ -1,6 +1,6 @@ executable('rc-update', ['rc-update.c', misc_c, usage_c, version_h], - c_args : cc_branding_flags, + c_args : [cc_branding_flags, cc_user_services_flags], link_with: [libeinfo, librc], include_directories: [incdir, einfo_incdir, rc_incdir], install: true, diff --git a/src/rc-update/rc-update.c b/src/rc-update/rc-update.c index 64d781a58..f4bbbe88a 100644 --- a/src/rc-update/rc-update.c +++ b/src/rc-update/rc-update.c @@ -36,17 +36,27 @@ const char *usagestring = "" \ "Usage: rc-update [options] add [...]\n" \ " or: rc-update [options] del [...]\n" \ " or: rc-update [options] [show [...]]"; -const char getoptstring[] = "asu" getoptstring_COMMON; +const char getoptstring[] = "asu" getoptstring_COMMON +#ifdef RC_USER_SERVICES +getoptstring_USER_SERVICES +#endif +; const struct option longopts[] = { { "all", 0, NULL, 'a' }, { "stack", 0, NULL, 's' }, { "update", 0, NULL, 'u' }, +#ifdef RC_USER_SERVICES + longopts_USER_SERVICES +#endif longopts_COMMON }; const char * const longopts_help[] = { "Process all runlevels", "Stack a runlevel instead of a service", "Force an update of the dependency tree", +#ifdef RC_USER_SERVICES + longopts_help_USER_SERVICES +#endif longopts_help_COMMON }; @@ -236,6 +246,9 @@ int main(int argc, char **argv) if (deptree) rc_deptree_free(deptree); return ret; +#ifdef RC_USER_SERVICES + case_RC_USER_SERVICES +#endif case_RC_COMMON_GETOPT } diff --git a/src/shared/_usage.h b/src/shared/_usage.h index 91b956e06..3264ad6f6 100644 --- a/src/shared/_usage.h +++ b/src/shared/_usage.h @@ -45,6 +45,16 @@ case 'q': case_RC_COMMON_getopt_case_q; break; \ default: case_RC_COMMON_getopt_default; break; +#ifdef RC_USER_SERVICES +#define case_RC_USER_SERVICES \ + case 'U': rc_set_user(); break; +#define getoptstring_USER_SERVICES "U" +#define longopts_USER_SERVICES \ + { "user", 0, NULL, 'U'}, +#define longopts_help_USER_SERVICES \ + "use user facing services", +#endif + extern const char *applet; extern const char *extraopts; extern const char getoptstring[]; diff --git a/src/shared/misc.c b/src/shared/misc.c index 28f95b34a..e64ba32d3 100644 --- a/src/shared/misc.c +++ b/src/shared/misc.c @@ -70,6 +70,16 @@ env_filter(void) /* Add the user defined list of vars */ env_allow = rc_stringlist_split(rc_conf_value("rc_env_allow"), " "); + +#ifdef RC_USER_SERVICES + /* Needed for local user services to be found */ + if (rc_is_user()) { + rc_stringlist_addu(env_allow, "HOME"); + rc_stringlist_addu(env_allow, "XDG_RUNTIME_DIR"); + rc_stringlist_addu(env_allow, "XDG_CONFIG_HOME"); + rc_stringlist_addu(env_allow, "RC_USER_SERVICES"); + } +#endif /* * If '*' is an entry in rc_env_allow, do nothing as we are to pass * through all environment variables. @@ -238,8 +248,22 @@ svc_lock(const char *applet) { char *file = NULL; int fd; + char *svcdir = RC_SVCDIR; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif + + xasprintf(&file, "%s/exclusive/%s", svcdir, applet); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(svcdir); + } +#endif - xasprintf(&file, RC_SVCDIR "/exclusive/%s", applet); fd = open(file, O_WRONLY | O_CREAT | O_NONBLOCK, 0664); free(file); if (fd == -1) @@ -256,8 +280,22 @@ int svc_unlock(const char *applet, int fd) { char *file = NULL; + char *svcdir = RC_SVCDIR; + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + svcdir = rc_user_svcdir(); + } +#endif + + xasprintf(&file, "%s/exclusive/%s", svcdir, applet); + +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(svcdir); + } +#endif - xasprintf(&file, RC_SVCDIR "/exclusive/%s", applet); close(fd); unlink(file); free(file); @@ -368,15 +406,33 @@ RC_DEPTREE * _rc_deptree_load(int force, int *regen) struct stat st; struct utimbuf ut; FILE *fp; + char *cache = RC_DEPTREE_CACHE; + char *skew = RC_DEPTREE_SKEWED; +#ifdef RC_USER_SERVICES + char *user_svcdir; + if (rc_is_user()) { + user_svcdir = rc_user_svcdir(); + xasprintf(&cache, "%s%s", user_svcdir, RC_DEPTREE_CACHE_FILE); + xasprintf(&skew, "%s%s", user_svcdir, RC_DEPTREE_SKEWED_FILE); + free(user_svcdir); + } +#endif t = 0; if (rc_deptree_update_needed(&t, file) || force != 0) { /* Test if we have permission to update the deptree */ - fd = open(RC_DEPTREE_CACHE, O_WRONLY); + fd = open(cache, O_WRONLY); merrno = errno; errno = serrno; - if (fd == -1 && merrno == EACCES) + if (fd == -1 && merrno == EACCES) { +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(cache); + free(skew); + } +#endif return rc_deptree_load(); + } close(fd); if (regen) @@ -386,30 +442,41 @@ 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)); + if (stat(cache, &st) != 0) { + eerror("stat(%s): %s", cache, strerror(errno)); +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(cache); + free(skew); + } +#endif return NULL; } 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", cache, ctime(&t)); + fp = fopen(skew, "w"); if (fp != NULL) { fprintf(fp, "%s\n", file); fclose(fp); } ut.actime = t; ut.modtime = t; - utime(RC_DEPTREE_CACHE, &ut); + utime(cache, &ut); } else { - if (exists(RC_DEPTREE_SKEWED)) - unlink(RC_DEPTREE_SKEWED); + if (exists(skew)) + unlink(skew); } } if (force == -1 && regen != NULL) *regen = retval; } +#ifdef RC_USER_SERVICES + if (rc_is_user()) { + free(cache); + free(skew); + } +#endif return rc_deptree_load(); } diff --git a/src/shared/misc.h b/src/shared/misc.h index 099206c82..db854bdf5 100644 --- a/src/shared/misc.h +++ b/src/shared/misc.h @@ -33,11 +33,15 @@ #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_FILE "/deptree" +#define RC_DEPTREE_CACHE RC_SVCDIR RC_DEPTREE_CACHE_FILE +#define RC_DEPTREE_SKEWED_FILE "/clock-skewed" +#define RC_DEPTREE_SKEWED RC_SVCDIR RC_DEPTREE_SKEWED_FILE #define RC_KRUNLEVEL RC_SVCDIR "/krunlevel" -#define RC_STARTING RC_SVCDIR "/rc.starting" -#define RC_STOPPING RC_SVCDIR "/rc.stopping" +#define RC_STARTING_FOLDER "/rc.starting" +#define RC_STARTING RC_SVCDIR RC_STARTING_FOLDER +#define RC_STOPPING_FOLDER "/rc.stopping" +#define RC_STOPPING RC_SVCDIR RC_STOPPING_FOLDER #define RC_SVCDIR_STARTING RC_SVCDIR "/starting" #define RC_SVCDIR_INACTIVE RC_SVCDIR "/inactive"