Sometimes it is necessary to stop a generator from running. Either
because of a bug, or for testing, or some other reason. The only way
to do that would be to rename or chmod the generator binary, which is
inconvenient and does not survive upgrades. Allow masking and
overriding generators similarly to units and other configuration
files.
For the systemd instance, masking would be more common, rather than
overriding generators. For the user instances, it may also be useful
for users to have generators in $XDG_CONFIG_HOME to augment or
override system-wide generators.
Directories are searched according to the usual scheme (/usr/lib,
/usr/local/lib, /run, /etc), and files with the same name in higher
priority directories override files with the same name in lower
priority directories. Empty files and links to /dev/null mask a given
name.
https://bugs.freedesktop.org/show_bug.cgi?id=87230
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
+static int manager_run_generators(Manager *m);
+static void manager_undo_generators(Manager *m);
static int manager_watch_jobs_in_progress(Manager *m) {
usec_t next;
static int manager_watch_jobs_in_progress(Manager *m) {
usec_t next;
assert(m);
dual_timestamp_get(&m->generators_start_timestamp);
assert(m);
dual_timestamp_get(&m->generators_start_timestamp);
- manager_run_generators(m);
+ r = manager_run_generators(m);
dual_timestamp_get(&m->generators_finish_timestamp);
dual_timestamp_get(&m->generators_finish_timestamp);
r = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
r = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
lookup_paths_free(&m->lookup_paths);
/* Find new unit paths */
lookup_paths_free(&m->lookup_paths);
/* Find new unit paths */
- manager_run_generators(m);
+ q = manager_run_generators(m);
+ if (q < 0 && r >= 0)
+ r = q;
q = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
q = lookup_paths_init(
&m->lookup_paths, m->running_as, true,
m->generator_unit_path,
m->generator_unit_path_early,
m->generator_unit_path_late);
m->generator_unit_path,
m->generator_unit_path_early,
m->generator_unit_path_late);
r = q;
manager_build_unit_path_cache(m);
/* First, enumerate what we can from all config files */
q = manager_enumerate(m);
r = q;
manager_build_unit_path_cache(m);
/* First, enumerate what we can from all config files */
q = manager_enumerate(m);
r = q;
/* Second, deserialize our stored data */
q = manager_deserialize(m, f, fds);
r = q;
/* Second, deserialize our stored data */
q = manager_deserialize(m, f, fds);
/* Re-register notify_fd as event source */
q = manager_setup_notify(m);
/* Re-register notify_fd as event source */
q = manager_setup_notify(m);
r = q;
/* Third, fire things up! */
q = manager_coldplug(m);
r = q;
/* Third, fire things up! */
q = manager_coldplug(m);
r = q;
assert(m->n_reloading > 0);
r = q;
assert(m->n_reloading > 0);
-void manager_run_generators(Manager *m) {
- const char *generator_path;
+static int manager_run_generators(Manager *m) {
+ _cleanup_free_ char **paths = NULL;
int r;
assert(m);
if (m->test_run)
int r;
assert(m);
if (m->test_run)
- generator_path = m->running_as == SYSTEMD_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
+ paths = generator_paths(m->running_as);
+ if (!paths)
+ return log_oom();
/* Optimize by skipping the whole process by not creating output directories
* if no generators are found. */
/* Optimize by skipping the whole process by not creating output directories
* if no generators are found. */
- if (access(generator_path, F_OK) != 0) {
+ STRV_FOREACH(path, paths) {
+ r = access(*path, F_OK);
+ if (r == 0)
+ goto found;
- log_error_errno(errno, "Failed to open generator directory %s: %m",
- generator_path);
- return;
+ log_warning_errno(errno, "Failed to open generator directory %s: %m", *path);
r = create_generator_dir(m, &m->generator_unit_path, "generator");
if (r < 0)
goto finish;
r = create_generator_dir(m, &m->generator_unit_path, "generator");
if (r < 0)
goto finish;
argv[4] = NULL;
RUN_WITH_UMASK(0022)
argv[4] = NULL;
RUN_WITH_UMASK(0022)
- execute_directory(generator_path, DEFAULT_TIMEOUT_USEC, (char**) argv);
+ execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, (char**) argv);
finish:
trim_generator_dir(m, &m->generator_unit_path);
trim_generator_dir(m, &m->generator_unit_path_early);
trim_generator_dir(m, &m->generator_unit_path_late);
finish:
trim_generator_dir(m, &m->generator_unit_path);
trim_generator_dir(m, &m->generator_unit_path_early);
trim_generator_dir(m, &m->generator_unit_path_late);
}
static void remove_generator_dir(Manager *m, char **generator) {
}
static void remove_generator_dir(Manager *m, char **generator) {
-void manager_undo_generators(Manager *m) {
+static void manager_undo_generators(Manager *m) {
assert(m);
remove_generator_dir(m, &m->generator_unit_path);
assert(m);
remove_generator_dir(m, &m->generator_unit_path);
void manager_check_finished(Manager *m);
void manager_check_finished(Manager *m);
-void manager_run_generators(Manager *m);
-void manager_undo_generators(Manager *m);
-
void manager_recheck_journal(Manager *m);
void manager_set_show_status(Manager *m, ShowStatus mode);
void manager_recheck_journal(Manager *m);
void manager_set_show_status(Manager *m, ShowStatus mode);
#include "cgroup-util.h"
#include "def.h"
#include "switch-root.h"
#include "cgroup-util.h"
#include "def.h"
#include "switch-root.h"
#define FINALIZE_ATTEMPTS 50
#define FINALIZE_ATTEMPTS 50
char *arguments[3];
unsigned retries;
int cmd, r;
char *arguments[3];
unsigned retries;
int cmd, r;
+ static const char* const dirs[] = {SYSTEM_SHUTDOWN_PATH, NULL};
log_parse_environment();
r = parse_argv(argc, argv);
log_parse_environment();
r = parse_argv(argc, argv);
arguments[0] = NULL;
arguments[1] = arg_verb;
arguments[2] = NULL;
arguments[0] = NULL;
arguments[1] = arg_verb;
arguments[2] = NULL;
- execute_directory(SYSTEM_SHUTDOWN_PATH, DEFAULT_TIMEOUT_USEC, arguments);
+ execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
if (!in_container && !in_initrd() &&
access("/run/initramfs/shutdown", X_OK) == 0) {
if (!in_container && !in_initrd() &&
access("/run/initramfs/shutdown", X_OK) == 0) {
+static int user_data_home_dir(char **dir, const char *suffix) {
+ const char *e;
+ char *res;
+
+ /* We don't treat /etc/xdg/systemd here as the spec
+ * suggests because we assume that that is a link to
+ * /etc/systemd/ anyway. */
+
+ e = getenv("XDG_DATA_HOME");
+ if (e)
+ res = strappend(e, suffix);
+ else {
+ const char *home;
+
+ home = getenv("HOME");
+ if (home)
+ res = strjoin(home, "/.local/share", suffix, NULL);
+ else
+ return 0;
+ }
+ if (!res)
+ return -ENOMEM;
+
+ *dir = res;
+ return 0;
+}
+
static char** user_dirs(
const char *generator,
const char *generator_early,
static char** user_dirs(
const char *generator,
const char *generator_early,
_cleanup_free_ char *config_home = NULL, *runtime_dir = NULL, *data_home = NULL;
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
_cleanup_free_ char *config_home = NULL, *runtime_dir = NULL, *data_home = NULL;
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
+ _cleanup_free_ char **res = NULL;
+ char **tmp;
+ int r;
/* Implement the mechanisms defined in
*
/* Implement the mechanisms defined in
*
*/
if (user_config_home(&config_home) < 0)
*/
if (user_config_home(&config_home) < 0)
if (user_runtime_dir(&runtime_dir) < 0)
if (user_runtime_dir(&runtime_dir) < 0)
- goto fail;
-
- home = getenv("HOME");
e = getenv("XDG_CONFIG_DIRS");
if (e) {
config_dirs = strv_split(e, ":");
if (!config_dirs)
e = getenv("XDG_CONFIG_DIRS");
if (e) {
config_dirs = strv_split(e, ":");
if (!config_dirs)
- /* We don't treat /etc/xdg/systemd here as the spec
- * suggests because we assume that that is a link to
- * /etc/systemd/ anyway. */
-
- e = getenv("XDG_DATA_HOME");
- if (e) {
- if (asprintf(&data_home, "%s/systemd/user", e) < 0)
- goto fail;
-
- } else if (home) {
- if (asprintf(&data_home, "%s/.local/share/systemd/user", home) < 0)
- goto fail;
- }
+ r = user_data_home_dir(&data_home, "/systemd/user");
+ if (r < 0)
+ return NULL;
e = getenv("XDG_DATA_DIRS");
if (e)
e = getenv("XDG_DATA_DIRS");
if (e)
"/usr/share",
NULL);
if (!data_dirs)
"/usr/share",
NULL);
if (!data_dirs)
/* Now merge everything we found. */
if (generator_early)
/* Now merge everything we found. */
if (generator_early)
- if (strv_extend(&r, generator_early) < 0)
- goto fail;
+ if (strv_extend(&res, generator_early) < 0)
+ return NULL;
- if (strv_extend(&r, config_home) < 0)
- goto fail;
+ if (strv_extend(&res, config_home) < 0)
+ return NULL;
if (!strv_isempty(config_dirs))
if (!strv_isempty(config_dirs))
- if (strv_extend_strv_concat(&r, config_dirs, "/systemd/user") < 0)
- goto fail;
+ if (strv_extend_strv_concat(&res, config_dirs, "/systemd/user") < 0)
+ return NULL;
- if (strv_extend_strv(&r, (char**) config_unit_paths) < 0)
- goto fail;
+ if (strv_extend_strv(&res, (char**) config_unit_paths) < 0)
+ return NULL;
- if (strv_extend(&r, runtime_dir) < 0)
- goto fail;
+ if (strv_extend(&res, runtime_dir) < 0)
+ return NULL;
- if (strv_extend(&r, runtime_unit_path) < 0)
- goto fail;
+ if (strv_extend(&res, runtime_unit_path) < 0)
+ return NULL;
- if (strv_extend(&r, generator) < 0)
- goto fail;
+ if (strv_extend(&res, generator) < 0)
+ return NULL;
- if (strv_extend(&r, data_home) < 0)
- goto fail;
+ if (strv_extend(&res, data_home) < 0)
+ return NULL;
if (!strv_isempty(data_dirs))
if (!strv_isempty(data_dirs))
- if (strv_extend_strv_concat(&r, data_dirs, "/systemd/user") < 0)
- goto fail;
+ if (strv_extend_strv_concat(&res, data_dirs, "/systemd/user") < 0)
+ return NULL;
- if (strv_extend_strv(&r, (char**) data_unit_paths) < 0)
- goto fail;
+ if (strv_extend_strv(&res, (char**) data_unit_paths) < 0)
+ return NULL;
- if (strv_extend(&r, generator_late) < 0)
- goto fail;
+ if (strv_extend(&res, generator_late) < 0)
+ return NULL;
- if (!path_strv_make_absolute_cwd(r))
- goto fail;
+ if (!path_strv_make_absolute_cwd(res))
+ return NULL;
+ tmp = res;
+ res = NULL;
+ return tmp;
+}
-fail:
- strv_free(r);
- return NULL;
+char **generator_paths(SystemdRunningAs running_as) {
+ if (running_as == SYSTEMD_USER)
+ return strv_new("/etc/systemd/user-generators",
+ "/run/systemd/user-generators",
+ "/usr/local/lib/systemd/user-generators",
+ USER_GENERATOR_PATH,
+ NULL);
+ else
+ return strv_new("/etc/systemd/system-generators",
+ "/run/systemd/system-generators",
+ "/usr/local/lib/systemd/system-generators",
+ SYSTEM_GENERATOR_PATH,
+ NULL);
int user_config_home(char **config_home);
int user_runtime_dir(char **runtime_dir);
int user_config_home(char **config_home);
int user_runtime_dir(char **runtime_dir);
+char **generator_paths(SystemdRunningAs running_as);
+
int lookup_paths_init(LookupPaths *p,
SystemdRunningAs running_as,
bool personal,
int lookup_paths_init(LookupPaths *p,
SystemdRunningAs running_as,
bool personal,
return endswith(de->d_name, suffix);
}
return endswith(de->d_name, suffix);
}
-static int do_execute(const char *directory, usec_t timeout, char *argv[]) {
+static int do_execute(char **directories, usec_t timeout, char *argv[]) {
_cleanup_hashmap_free_free_ Hashmap *pids = NULL;
_cleanup_hashmap_free_free_ Hashmap *pids = NULL;
- _cleanup_closedir_ DIR *d;
- struct dirent *de;
+ _cleanup_set_free_free_ Set *seen = NULL;
+ char **directory;
/* We fork this all off from a child process so that we can
* somewhat cleanly make use of SIGALRM to set a time limit */
/* We fork this all off from a child process so that we can
* somewhat cleanly make use of SIGALRM to set a time limit */
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
- d = opendir(directory);
- if (!d) {
- if (errno == ENOENT)
- return 0;
-
- return log_error_errno(errno, "Failed to open directory %s: %m", directory);
- }
-
pids = hashmap_new(NULL);
if (!pids)
return log_oom();
pids = hashmap_new(NULL);
if (!pids)
return log_oom();
- FOREACH_DIRENT(de, d, break) {
- _cleanup_free_ char *path = NULL;
- pid_t pid;
- int r;
+ seen = set_new(&string_hash_ops);
+ if (!seen)
+ return log_oom();
- if (!dirent_is_file(de))
- continue;
+ STRV_FOREACH(directory, directories) {
+ _cleanup_closedir_ DIR *d;
+ struct dirent *de;
- path = strjoin(directory, "/", de->d_name, NULL);
- if (!path)
- return log_oom();
+ d = opendir(*directory);
+ if (!d) {
+ if (errno == ENOENT)
+ continue;
- pid = fork();
- if (pid < 0) {
- log_error_errno(errno, "Failed to fork: %m");
- continue;
- } else if (pid == 0) {
- char *_argv[2];
+ return log_error_errno(errno, "Failed to open directory %s: %m", *directory);
+ }
- assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
+ FOREACH_DIRENT(de, d, break) {
+ _cleanup_free_ char *path = NULL;
+ pid_t pid;
+ int r;
- if (!argv) {
- _argv[0] = path;
- _argv[1] = NULL;
- argv = _argv;
+ if (!dirent_is_file(de))
+ continue;
+
+ if (set_contains(seen, de->d_name)) {
+ log_debug("%1$s/%2$s skipped (%2$s was already seen).", *directory, de->d_name);
+ continue;
+ }
+
+ r = set_put_strdup(seen, de->d_name);
+ if (r < 0)
+ return log_oom();
+
+ path = strjoin(*directory, "/", de->d_name, NULL);
+ if (!path)
+ return log_oom();
+
+ if (null_or_empty_path(path)) {
+ log_debug("%s is empty (a mask).", path);
+ continue;
+ log_debug("%s will be executed.", path);
- execv(path, argv);
- return log_error_errno(errno, "Failed to execute %s: %m", path);
- }
+ pid = fork();
+ if (pid < 0) {
+ log_error_errno(errno, "Failed to fork: %m");
+ continue;
+ } else if (pid == 0) {
+ char *_argv[2];
- log_debug("Spawned %s as " PID_FMT ".", path, pid);
+ assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
- r = hashmap_put(pids, UINT_TO_PTR(pid), path);
- if (r < 0)
- return log_oom();
+ if (!argv) {
+ _argv[0] = path;
+ _argv[1] = NULL;
+ argv = _argv;
+ } else
+ argv[0] = path;
+
+ execv(path, argv);
+ return log_error_errno(errno, "Failed to execute %s: %m", path);
+ }
+
+ log_debug("Spawned %s as " PID_FMT ".", path, pid);
+ r = hashmap_put(pids, UINT_TO_PTR(pid), path);
+ if (r < 0)
+ return log_oom();
+ path = NULL;
+ }
}
/* Abort execution of this process after the timout. We simply
}
/* Abort execution of this process after the timout. We simply
-void execute_directory(const char *directory, usec_t timeout, char *argv[]) {
+void execute_directories(const char* const* directories, usec_t timeout, char *argv[]) {
pid_t executor_pid;
int r;
pid_t executor_pid;
int r;
+ char *name;
+ char **dirs = (char**) directories;
+
+ assert(!strv_isempty(dirs));
+ name = basename(dirs[0]);
+ assert(!isempty(name));
- /* Executes all binaries in the directory in parallel and waits
- * for them to finish. Optionally a timeout is applied. */
+ /* Executes all binaries in the directories in parallel and waits
+ * for them to finish. Optionally a timeout is applied. If a file
+ * with the same name exists in more than one directory, the
+ * earliest one wins. */
executor_pid = fork();
if (executor_pid < 0) {
executor_pid = fork();
if (executor_pid < 0) {
return;
} else if (executor_pid == 0) {
return;
} else if (executor_pid == 0) {
- r = do_execute(directory, timeout, argv);
+ r = do_execute(dirs, timeout, argv);
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
}
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
}
- wait_for_terminate_and_warn(directory, executor_pid, true);
+ wait_for_terminate_and_warn(name, executor_pid, true);
}
int kill_and_sigcont(pid_t pid, int sig) {
}
int kill_and_sigcont(pid_t pid, int sig) {
int vtnr_from_tty(const char *tty);
const char *default_term_for_tty(const char *tty);
int vtnr_from_tty(const char *tty);
const char *default_term_for_tty(const char *tty);
-void execute_directory(const char *directory, usec_t timeout, char *argv[]);
+void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
int kill_and_sigcont(pid_t pid, int sig);
int kill_and_sigcont(pid_t pid, int sig);
+ static const char* const dirs[] = {SYSTEM_SLEEP_PATH, NULL};
int r;
_cleanup_fclose_ FILE *f = NULL;
int r;
_cleanup_fclose_ FILE *f = NULL;
- execute_directory(SYSTEM_SLEEP_PATH, DEFAULT_TIMEOUT_USEC, arguments);
+ execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
log_struct(LOG_INFO,
LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
log_struct(LOG_INFO,
LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
NULL);
arguments[1] = (char*) "post";
NULL);
arguments[1] = (char*) "post";
- execute_directory(SYSTEM_SLEEP_PATH, DEFAULT_TIMEOUT_USEC, arguments);
+ execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
}
static void test_execute_directory(void) {
}
static void test_execute_directory(void) {
- char name[] = "/tmp/test-execute_directory/script1";
- char name2[] = "/tmp/test-execute_directory/script2";
- char name3[] = "/tmp/test-execute_directory/useless";
- char tempdir[] = "/tmp/test-execute_directory/";
+ char template[] = "/tmp/test-readlink_and_make_absolute.XXXXXXX";
+ const char const* dirs[] = {template, NULL};
+ const char *name, *name2, *name3;
- assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
- assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works") == 0);
- assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works2") == 0);
+ assert_se(mkdtemp(template));
+
+ name = strappenda(template, "/script");
+ name2 = strappenda(template, "/script2");
+ name3 = strappenda(template, "/useless");
+
+ assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works") == 0);
+ assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2") == 0);
assert_se(chmod(name, 0755) == 0);
assert_se(chmod(name2, 0755) == 0);
assert_se(touch(name3) >= 0);
assert_se(chmod(name, 0755) == 0);
assert_se(chmod(name2, 0755) == 0);
assert_se(touch(name3) >= 0);
- execute_directory(tempdir, DEFAULT_TIMEOUT_USEC, NULL);
- assert_se(access("/tmp/test-execute_directory/it_works", F_OK) >= 0);
- assert_se(access("/tmp/test-execute_directory/it_works2", F_OK) >= 0);
+ assert(chdir(template) == 0);
+ execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL);
+ assert_se(access("it_works", F_OK) >= 0);
+ assert_se(access("it_works2", F_OK) >= 0);
- rm_rf_dangerous(tempdir, false, true, false);
+ rm_rf_dangerous(template, false, true, false);
}
static void test_unquote_first_word(void) {
}
static void test_unquote_first_word(void) {