1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

core: remove ManagerRunningAs enum

Previously, we had two enums ManagerRunningAs and UnitFileScope, that were
mostly identical and converted from one to the other all the time. The latter
had one more value UNIT_FILE_GLOBAL however.

Let's simplify things, and remove ManagerRunningAs and replace it by
UnitFileScope everywhere, thus making the translation unnecessary. Introduce
two new macros MANAGER_IS_SYSTEM() and MANAGER_IS_USER() to simplify checking
if we are running in one or the user context.
This commit is contained in:
Lennart Poettering 2016-02-24 21:24:23 +01:00
parent c51932be73
commit 463d0d1569
36 changed files with 259 additions and 283 deletions

View File

@ -231,14 +231,12 @@ static int verify_unit(Unit *u, bool check_man) {
return r;
}
int verify_units(char **filenames, ManagerRunningAs running_as, bool check_man) {
int verify_units(char **filenames, UnitFileScope scope, bool check_man) {
_cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
_cleanup_free_ char *var = NULL;
Manager *m = NULL;
FILE *serial = NULL;
FDSet *fdset = NULL;
_cleanup_free_ char *var = NULL;
char **filename;
int r = 0, k;
@ -255,7 +253,7 @@ int verify_units(char **filenames, ManagerRunningAs running_as, bool check_man)
assert_se(set_unit_path(var) >= 0);
r = manager_new(running_as, true, &m);
r = manager_new(scope, true, &m);
if (r < 0)
return log_error_errno(r, "Failed to initialize manager: %m");

View File

@ -23,4 +23,4 @@
#include "path-lookup.h"
int verify_units(char **filenames, ManagerRunningAs running_as, bool check_man);
int verify_units(char **filenames, UnitFileScope scope, bool check_man);

View File

@ -1443,7 +1443,7 @@ int main(int argc, char *argv[]) {
if (streq_ptr(argv[optind], "verify"))
r = verify_units(argv+optind+1,
arg_user ? MANAGER_USER : MANAGER_SYSTEM,
arg_user ? UNIT_FILE_USER : UNIT_FILE_SYSTEM,
arg_man);
else {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;

View File

@ -149,7 +149,7 @@ static int automount_add_default_dependencies(Automount *a) {
if (!UNIT(a)->default_dependencies)
return 0;
if (UNIT(a)->manager->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(UNIT(a)->manager))
return 0;
r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);

View File

@ -149,7 +149,7 @@ static int busname_add_default_default_dependencies(BusName *n) {
if (r < 0)
return r;
if (UNIT(n)->manager->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(UNIT(n)->manager)) {
r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;
@ -318,7 +318,7 @@ static int busname_open_fd(BusName *n) {
if (n->starter_fd >= 0)
return 0;
mode = UNIT(n)->manager->running_as == MANAGER_SYSTEM ? "system" : "user";
mode = MANAGER_IS_SYSTEM(UNIT(n)->manager) ? "system" : "user";
n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
if (n->starter_fd < 0)
return log_unit_warning_errno(UNIT(n), n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");

View File

@ -1265,7 +1265,7 @@ int manager_setup_cgroup(Manager *m) {
* it. This is to support live upgrades from older systemd
* versions where PID 1 was moved there. Also see
* cg_get_root_path(). */
if (!e && m->running_as == MANAGER_SYSTEM) {
if (!e && MANAGER_IS_SYSTEM(m)) {
e = endswith(m->cgroup_root, "/" SPECIAL_SYSTEM_SLICE);
if (!e)
e = endswith(m->cgroup_root, "/system"); /* even more legacy */
@ -1318,7 +1318,7 @@ int manager_setup_cgroup(Manager *m) {
(void) sd_event_source_set_description(m->cgroup_inotify_event_source, "cgroup-inotify");
} else if (m->running_as == MANAGER_SYSTEM) {
} else if (MANAGER_IS_SYSTEM(m)) {
/* On the legacy hierarchy we only get
* notifications via cgroup agents. (Which

View File

@ -1187,7 +1187,7 @@ static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *
if (r < 0)
return r;
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
m->exit_code = MANAGER_REBOOT;
@ -1206,7 +1206,7 @@ static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error
if (r < 0)
return r;
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
m->exit_code = MANAGER_POWEROFF;
@ -1225,7 +1225,7 @@ static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *er
if (r < 0)
return r;
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Halt is only supported for system managers.");
m->exit_code = MANAGER_HALT;
@ -1244,7 +1244,7 @@ static int method_kexec(sd_bus_message *message, void *userdata, sd_bus_error *e
if (r < 0)
return r;
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "KExec is only supported for system managers.");
m->exit_code = MANAGER_KEXEC;
@ -1265,7 +1265,7 @@ static int method_switch_root(sd_bus_message *message, void *userdata, sd_bus_er
if (r < 0)
return r;
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Root switching is only supported by system manager.");
r = sd_bus_message_read(message, "ss", &root, &init);
@ -1433,7 +1433,7 @@ static int method_set_exit_code(sd_bus_message *message, void *userdata, sd_bus_
if (r < 0)
return r;
if (m->running_as == MANAGER_SYSTEM && detect_container() <= 0)
if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "ExitCode can only be set for user service managers or in containers.");
m->return_value = code;
@ -1466,7 +1466,7 @@ static int method_list_unit_files(sd_bus_message *message, void *userdata, sd_bu
if (!h)
return -ENOMEM;
r = unit_file_get_list(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
r = unit_file_get_list(m->unit_file_scope, NULL, h);
if (r < 0)
goto fail;
@ -1498,7 +1498,6 @@ static int method_get_unit_file_state(sd_bus_message *message, void *userdata, s
Manager *m = userdata;
const char *name;
UnitFileState state;
UnitFileScope scope;
int r;
assert(message);
@ -1514,9 +1513,7 @@ static int method_get_unit_file_state(sd_bus_message *message, void *userdata, s
if (r < 0)
return r;
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = unit_file_get_state(scope, NULL, name, &state);
r = unit_file_get_state(m->unit_file_scope, NULL, name, &state);
if (r < 0)
return r;
@ -1526,7 +1523,6 @@ static int method_get_unit_file_state(sd_bus_message *message, void *userdata, s
static int method_get_default_target(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *default_target = NULL;
Manager *m = userdata;
UnitFileScope scope;
int r;
assert(message);
@ -1538,9 +1534,7 @@ static int method_get_default_target(sd_bus_message *message, void *userdata, sd
if (r < 0)
return r;
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = unit_file_get_default(scope, NULL, &default_target);
r = unit_file_get_default(m->unit_file_scope, NULL, &default_target);
if (r < 0)
return r;
@ -1624,7 +1618,6 @@ static int method_enable_unit_files_generic(
_cleanup_strv_free_ char **l = NULL;
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
UnitFileScope scope;
int runtime, force, r;
assert(message);
@ -1644,9 +1637,7 @@ static int method_enable_unit_files_generic(
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = call(scope, runtime, NULL, l, force, &changes, &n_changes);
r = call(m->unit_file_scope, runtime, NULL, l, force, &changes, &n_changes);
if (r == -ESHUTDOWN)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit file is masked.");
if (r == -EADDRNOTAVAIL)
@ -1688,7 +1679,6 @@ static int method_preset_unit_files_with_mode(sd_bus_message *message, void *use
unsigned n_changes = 0;
Manager *m = userdata;
UnitFilePresetMode mm;
UnitFileScope scope;
int runtime, force, r;
const char *mode;
@ -1717,9 +1707,7 @@ static int method_preset_unit_files_with_mode(sd_bus_message *message, void *use
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = unit_file_preset(scope, runtime, NULL, l, mm, force, &changes, &n_changes);
r = unit_file_preset(m->unit_file_scope, runtime, NULL, l, mm, force, &changes, &n_changes);
if (r < 0)
return r;
@ -1736,7 +1724,6 @@ static int method_disable_unit_files_generic(
_cleanup_strv_free_ char **l = NULL;
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
UnitFileScope scope;
int r, runtime;
assert(message);
@ -1750,15 +1737,13 @@ static int method_disable_unit_files_generic(
if (r < 0)
return r;
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = bus_verify_manage_unit_files_async(m, message, error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = call(scope, runtime, NULL, l, &changes, &n_changes);
r = call(m->unit_file_scope, runtime, NULL, l, &changes, &n_changes);
if (r < 0)
return r;
@ -1777,7 +1762,6 @@ static int method_set_default_target(sd_bus_message *message, void *userdata, sd
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
Manager *m = userdata;
UnitFileScope scope;
const char *name;
int force, r;
@ -1798,9 +1782,7 @@ static int method_set_default_target(sd_bus_message *message, void *userdata, sd
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = unit_file_set_default(scope, NULL, name, force, &changes, &n_changes);
r = unit_file_set_default(m->unit_file_scope, NULL, name, force, &changes, &n_changes);
if (r < 0)
return r;
@ -1812,7 +1794,6 @@ static int method_preset_all_unit_files(sd_bus_message *message, void *userdata,
unsigned n_changes = 0;
Manager *m = userdata;
UnitFilePresetMode mm;
UnitFileScope scope;
const char *mode;
int force, runtime, r;
@ -1841,9 +1822,7 @@ static int method_preset_all_unit_files(sd_bus_message *message, void *userdata,
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = unit_file_preset_all(scope, runtime, NULL, mm, force, &changes, &n_changes);
r = unit_file_preset_all(m->unit_file_scope, runtime, NULL, mm, force, &changes, &n_changes);
if (r < 0) {
unit_file_changes_free(changes, n_changes);
return r;
@ -1857,7 +1836,6 @@ static int method_add_dependency_unit_files(sd_bus_message *message, void *userd
Manager *m = userdata;
UnitFileChange *changes = NULL;
unsigned n_changes = 0;
UnitFileScope scope;
int runtime, force, r;
char *target;
char *type;
@ -1884,9 +1862,7 @@ static int method_add_dependency_unit_files(sd_bus_message *message, void *userd
if (dep < 0)
return -EINVAL;
scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
r = unit_file_add_dependency(scope, runtime, NULL, l, target, dep, force, &changes, &n_changes);
r = unit_file_add_dependency(m->unit_file_scope, runtime, NULL, l, target, dep, force, &changes, &n_changes);
if (r == -ESHUTDOWN)
return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit file is masked.");
if (r == -EADDRNOTAVAIL)

View File

@ -112,7 +112,7 @@ static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus
manager_notify_cgroup_empty(m, cgroup);
/* if running as system-instance, forward under our name */
if (m->running_as == MANAGER_SYSTEM && m->system_bus) {
if (MANAGER_IS_SYSTEM(m) && m->system_bus) {
r = sd_bus_message_rewind(message, 1);
if (r >= 0)
r = sd_bus_send(m->system_bus, message, NULL);
@ -690,7 +690,7 @@ static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void
return 0;
}
if (m->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(m)) {
/* When we run as system instance we get the Released
* signal via a direct connection */
@ -864,10 +864,10 @@ static int bus_init_api(Manager *m) {
return 0;
/* The API and system bus is the same if we are running in system mode */
if (m->running_as == MANAGER_SYSTEM && m->system_bus)
if (MANAGER_IS_SYSTEM(m) && m->system_bus)
bus = sd_bus_ref(m->system_bus);
else {
if (m->running_as == MANAGER_SYSTEM)
if (MANAGER_IS_SYSTEM(m))
r = sd_bus_open_system(&bus);
else
r = sd_bus_open_user(&bus);
@ -907,7 +907,7 @@ static int bus_setup_system(Manager *m, sd_bus *bus) {
assert(bus);
/* On kdbus or if we are a user instance we get the Released message via the system bus */
if (m->running_as == MANAGER_USER || m->kdbus_fd >= 0) {
if (MANAGER_IS_USER(m) || m->kdbus_fd >= 0) {
r = sd_bus_add_match(
bus,
NULL,
@ -932,7 +932,7 @@ static int bus_init_system(Manager *m) {
return 0;
/* The API and system bus is the same if we are running in system mode */
if (m->running_as == MANAGER_SYSTEM && m->api_bus) {
if (MANAGER_IS_SYSTEM(m) && m->api_bus) {
m->system_bus = sd_bus_ref(m->api_bus);
return 0;
}
@ -983,7 +983,7 @@ static int bus_init_private(Manager *m) {
if (m->kdbus_fd >= 0)
return 0;
if (m->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(m)) {
/* We want the private bus only when running as init */
if (getpid() != 1)
@ -1082,7 +1082,7 @@ static void destroy_bus(Manager *m, sd_bus **bus) {
/* Possibly flush unwritten data, but only if we are
* unprivileged, since we don't want to sync here */
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
sd_bus_flush(*bus);
/* And destroy the object */

View File

@ -265,7 +265,7 @@ static int device_add_udev_wants(Unit *u, struct udev_device *dev) {
assert(u);
assert(dev);
property = u->manager->running_as == MANAGER_USER ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
property = MANAGER_IS_USER(u->manager) ? "SYSTEMD_USER_WANTS" : "SYSTEMD_WANTS";
wants = udev_device_get_property_value(dev, property);
if (!wants)
return 0;

View File

@ -47,7 +47,7 @@ int failure_action(
if (action == FAILURE_ACTION_NONE)
return -ECANCELED;
if (m->running_as == MANAGER_USER) {
if (!MANAGER_IS_SYSTEM(m)) {
/* Downgrade all options to simply exiting if we run
* in user mode */

View File

@ -1156,7 +1156,7 @@ void job_shutdown_magic(Job *j) {
if (j->type != JOB_START)
return;
if (j->unit->manager->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(j->unit->manager))
return;
if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET))

View File

@ -2495,7 +2495,7 @@ int config_parse_syscall_filter(
/* Turn on NNP, but only if it wasn't configured explicitly
* before, and only if we are in user mode. */
if (!c->no_new_privileges_set && u->manager->running_as == MANAGER_USER)
if (!c->no_new_privileges_set && MANAGER_IS_USER(u->manager))
c->no_new_privileges = true;
return 0;

View File

@ -94,7 +94,7 @@ static enum {
ACTION_DONE
} arg_action = ACTION_RUN;
static char *arg_default_unit = NULL;
static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
static bool arg_system = false;
static bool arg_dump_core = true;
static int arg_crash_chvt = -1;
static bool arg_crash_shell = false;
@ -688,11 +688,11 @@ static int parse_config_file(void) {
const char *fn, *conf_dirs_nulstr;
fn = arg_running_as == MANAGER_SYSTEM ?
fn = arg_system ?
PKGSYSCONFDIR "/system.conf" :
PKGSYSCONFDIR "/user.conf";
conf_dirs_nulstr = arg_running_as == MANAGER_SYSTEM ?
conf_dirs_nulstr = arg_system ?
CONF_PATHS_NULSTR("systemd/system.conf.d") :
CONF_PATHS_NULSTR("systemd/user.conf.d");
@ -866,11 +866,11 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_SYSTEM:
arg_running_as = MANAGER_SYSTEM;
arg_system = true;
break;
case ARG_USER:
arg_running_as = MANAGER_USER;
arg_system = false;
break;
case ARG_TEST:
@ -1346,7 +1346,7 @@ int main(int argc, char *argv[]) {
if (getpid() == 1 && detect_container() <= 0) {
/* Running outside of a container as PID 1 */
arg_running_as = MANAGER_SYSTEM;
arg_system = true;
make_null_stdio();
log_set_target(LOG_TARGET_KMSG);
log_open();
@ -1430,7 +1430,7 @@ int main(int argc, char *argv[]) {
} else if (getpid() == 1) {
/* Running inside a container, as PID 1 */
arg_running_as = MANAGER_SYSTEM;
arg_system = true;
log_set_target(LOG_TARGET_CONSOLE);
log_close_console(); /* force reopen of /dev/console */
log_open();
@ -1443,7 +1443,7 @@ int main(int argc, char *argv[]) {
kernel_timestamp = DUAL_TIMESTAMP_NULL;
} else {
/* Running as user instance */
arg_running_as = MANAGER_USER;
arg_system = false;
log_set_target(LOG_TARGET_AUTO);
log_open();
@ -1501,7 +1501,7 @@ int main(int argc, char *argv[]) {
goto finish;
}
if (arg_running_as == MANAGER_SYSTEM) {
if (arg_system) {
r = parse_proc_cmdline(parse_proc_cmdline_item);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
@ -1522,14 +1522,14 @@ int main(int argc, char *argv[]) {
goto finish;
}
if (arg_running_as == MANAGER_USER &&
if (!arg_system &&
arg_action == ACTION_RUN &&
sd_booted() <= 0) {
log_error("Trying to run as user instance, but the system has not been booted with systemd.");
goto finish;
}
if (arg_running_as == MANAGER_SYSTEM &&
if (arg_system &&
arg_action == ACTION_RUN &&
running_in_chroot() > 0) {
log_error("Cannot be run in a chroot() environment.");
@ -1557,7 +1557,7 @@ int main(int argc, char *argv[]) {
goto finish;
}
if (arg_running_as == MANAGER_USER &&
if (!arg_system &&
!getenv("XDG_RUNTIME_DIR")) {
log_error("Trying to run as user instance, but $XDG_RUNTIME_DIR is not set.");
goto finish;
@ -1580,7 +1580,7 @@ int main(int argc, char *argv[]) {
if (arg_serialization)
assert_se(fdset_remove(fds, fileno(arg_serialization)) >= 0);
if (arg_running_as == MANAGER_SYSTEM)
if (arg_system)
/* Become a session leader if we aren't one yet. */
setsid();
@ -1589,7 +1589,7 @@ int main(int argc, char *argv[]) {
/* Reset the console, but only if this is really init and we
* are freshly booted */
if (arg_running_as == MANAGER_SYSTEM && arg_action == ACTION_RUN) {
if (arg_system && arg_action == ACTION_RUN) {
/* If we are init, we connect stdin/stdout/stderr to
* /dev/null and make sure we don't have a controlling
@ -1616,7 +1616,7 @@ int main(int argc, char *argv[]) {
goto finish;
}
if (arg_running_as == MANAGER_SYSTEM) {
if (arg_system) {
int v;
log_info(PACKAGE_STRING " running in %ssystem mode. (" SYSTEMD_FEATURES ")",
@ -1652,7 +1652,7 @@ int main(int argc, char *argv[]) {
arg_action == ACTION_TEST ? " test" : "", getuid(), t);
}
if (arg_running_as == MANAGER_SYSTEM && !skip_setup) {
if (arg_system && !skip_setup) {
if (arg_show_status > 0)
status_welcome();
@ -1664,7 +1664,7 @@ int main(int argc, char *argv[]) {
test_usr();
}
if (arg_running_as == MANAGER_SYSTEM && arg_runtime_watchdog > 0 && arg_runtime_watchdog != USEC_INFINITY)
if (arg_system && arg_runtime_watchdog > 0 && arg_runtime_watchdog != USEC_INFINITY)
watchdog_set_timeout(&arg_runtime_watchdog);
if (arg_timer_slack_nsec != NSEC_INFINITY)
@ -1694,12 +1694,12 @@ int main(int argc, char *argv[]) {
}
}
if (arg_running_as == MANAGER_USER)
if (!arg_system)
/* Become reaper of our children */
if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0)
log_warning_errno(errno, "Failed to make us a subreaper: %m");
if (arg_running_as == MANAGER_SYSTEM) {
if (arg_system) {
bump_rlimit_nofile(&saved_rlimit_nofile);
if (empty_etc) {
@ -1711,7 +1711,7 @@ int main(int argc, char *argv[]) {
}
}
r = manager_new(arg_running_as, arg_action == ACTION_TEST, &m);
r = manager_new(arg_system ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, arg_action == ACTION_TEST, &m);
if (r < 0) {
log_emergency_errno(r, "Failed to allocate manager object: %m");
error_message = "Failed to allocate manager object";
@ -1874,7 +1874,7 @@ int main(int argc, char *argv[]) {
case MANAGER_EXIT:
retval = m->return_value;
if (m->running_as == MANAGER_USER) {
if (MANAGER_IS_USER(m)) {
log_debug("Exit.");
goto finish;
}
@ -1970,7 +1970,7 @@ finish:
args[i++] = SYSTEMD_BINARY_PATH;
if (switch_root_dir)
args[i++] = "--switched-root";
args[i++] = arg_running_as == MANAGER_SYSTEM ? "--system" : "--user";
args[i++] = arg_system ? "--system" : "--user";
args[i++] = "--deserialize";
args[i++] = sfd;
args[i++] = NULL;

View File

@ -491,7 +491,7 @@ static int manager_setup_signals(Manager *m) {
if (r < 0)
return r;
if (m->running_as == MANAGER_SYSTEM)
if (MANAGER_IS_SYSTEM(m))
return enable_special_signals(m);
return 0;
@ -518,7 +518,7 @@ static void manager_clean_environment(Manager *m) {
static int manager_default_environment(Manager *m) {
assert(m);
if (m->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(m)) {
/* The system manager always starts with a clean
* environment for its children. It does not import
* the kernel or the parents exported variables.
@ -547,43 +547,36 @@ static int manager_default_environment(Manager *m) {
}
int manager_new(ManagerRunningAs running_as, bool test_run, Manager **_m) {
static const char * const unit_log_fields[_MANAGER_RUNNING_AS_MAX] = {
[MANAGER_SYSTEM] = "UNIT=",
[MANAGER_USER] = "USER_UNIT=",
};
static const char * const unit_log_format_strings[_MANAGER_RUNNING_AS_MAX] = {
[MANAGER_SYSTEM] = "UNIT=%s",
[MANAGER_USER] = "USER_UNIT=%s",
};
int manager_new(UnitFileScope scope, bool test_run, Manager **_m) {
Manager *m;
int r;
assert(_m);
assert(running_as >= 0);
assert(running_as < _MANAGER_RUNNING_AS_MAX);
assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER));
m = new0(Manager, 1);
if (!m)
return -ENOMEM;
#ifdef ENABLE_EFI
if (running_as == MANAGER_SYSTEM && detect_container() <= 0)
boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp);
#endif
m->running_as = running_as;
m->unit_file_scope = scope;
m->exit_code = _MANAGER_EXIT_CODE_INVALID;
m->default_timer_accuracy_usec = USEC_PER_MINUTE;
m->default_tasks_accounting = true;
m->default_tasks_max = UINT64_C(512);
#ifdef ENABLE_EFI
if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp);
#endif
/* Prepare log fields we can use for structured logging */
m->unit_log_field = unit_log_fields[running_as];
m->unit_log_format_string = unit_log_format_strings[running_as];
if (MANAGER_IS_SYSTEM(m)) {
m->unit_log_field = "UNIT=";
m->unit_log_format_string = "UNIT=%s";
} else {
m->unit_log_field = "USER_UNIT=";
m->unit_log_format_string = "USER_UNIT=%s";
}
m->idle_pipe[0] = m->idle_pipe[1] = m->idle_pipe[2] = m->idle_pipe[3] = -1;
@ -694,7 +687,7 @@ static int manager_setup_notify(Manager *m) {
fd_inc_rcvbuf(fd, NOTIFY_RCVBUF_SIZE);
if (m->running_as == MANAGER_SYSTEM)
if (MANAGER_IS_SYSTEM(m))
m->notify_socket = strdup("/run/systemd/notify");
else {
const char *e;
@ -756,8 +749,8 @@ static int manager_setup_kdbus(Manager *m) {
return -ESOCKTNOSUPPORT;
m->kdbus_fd = bus_kernel_create_bus(
m->running_as == MANAGER_SYSTEM ? "system" : "user",
m->running_as == MANAGER_SYSTEM, &p);
MANAGER_IS_SYSTEM(m) ? "system" : "user",
MANAGER_IS_SYSTEM(m), &p);
if (m->kdbus_fd < 0)
return log_debug_errno(m->kdbus_fd, "Failed to set up kdbus: %m");
@ -778,7 +771,7 @@ static int manager_connect_bus(Manager *m, bool reexecuting) {
try_bus_connect =
m->kdbus_fd >= 0 ||
reexecuting ||
(m->running_as == MANAGER_USER && getenv("DBUS_SESSION_BUS_ADDRESS"));
(MANAGER_IS_USER(m) && getenv("DBUS_SESSION_BUS_ADDRESS"));
/* Try to connect to the buses, if possible. */
return bus_init(m, try_bus_connect);
@ -1116,7 +1109,7 @@ int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
assert(m);
r = lookup_paths_init(&m->lookup_paths, m->running_as, true, NULL);
r = lookup_paths_init(&m->lookup_paths, m->unit_file_scope, NULL);
if (r < 0)
return r;
@ -1739,7 +1732,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
}
log_received_signal(sfsi.ssi_signo == SIGCHLD ||
(sfsi.ssi_signo == SIGTERM && m->running_as == MANAGER_USER)
(sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m))
? LOG_DEBUG : LOG_INFO,
&sfsi);
@ -1750,7 +1743,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
break;
case SIGTERM:
if (m->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(m)) {
/* This is for compatibility with the
* original sysvinit */
m->exit_code = MANAGER_REEXECUTE;
@ -1760,7 +1753,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
/* Fall through */
case SIGINT:
if (m->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(m)) {
/* If the user presses C-A-D more than
* 7 times within 2s, we reboot
@ -1786,14 +1779,14 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
break;
case SIGWINCH:
if (m->running_as == MANAGER_SYSTEM)
if (MANAGER_IS_SYSTEM(m))
manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
/* This is a nop on non-init */
break;
case SIGPWR:
if (m->running_as == MANAGER_SYSTEM)
if (MANAGER_IS_SYSTEM(m))
manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
/* This is a nop on non-init */
@ -1901,7 +1894,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
break;
case 24:
if (m->running_as == MANAGER_USER) {
if (MANAGER_IS_USER(m)) {
m->exit_code = MANAGER_EXIT;
return 0;
}
@ -2017,7 +2010,7 @@ int manager_loop(Manager *m) {
while (m->exit_code == MANAGER_OK) {
usec_t wait_usec;
if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY && m->running_as == MANAGER_SYSTEM)
if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY && MANAGER_IS_SYSTEM(m))
watchdog_ping();
if (!ratelimit_test(&rl)) {
@ -2042,7 +2035,7 @@ int manager_loop(Manager *m) {
continue;
/* Sleep for half the watchdog time */
if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY && m->running_as == MANAGER_SYSTEM) {
if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY && MANAGER_IS_SYSTEM(m)) {
wait_usec = m->runtime_watchdog / 2;
if (wait_usec <= 0)
wait_usec = 1;
@ -2113,7 +2106,7 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
const char *msg;
int audit_fd, r;
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return;
audit_fd = get_audit_fd();
@ -2159,7 +2152,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
if (m->n_reloading > 0)
return;
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return;
if (detect_container() > 0)
@ -2203,7 +2196,7 @@ int manager_open_serialization(Manager *m, FILE **_f) {
assert(_f);
path = m->running_as == MANAGER_SYSTEM ? "/run/systemd" : "/tmp";
path = MANAGER_IS_SYSTEM(m) ? "/run/systemd" : "/tmp";
fd = open_tmpfile(path, O_RDWR|O_CLOEXEC);
if (fd < 0)
return -errno;
@ -2537,7 +2530,7 @@ int manager_reload(Manager *m) {
manager_undo_generators(m);
lookup_paths_free(&m->lookup_paths);
q = lookup_paths_init(&m->lookup_paths, m->running_as, true, NULL);
q = lookup_paths_init(&m->lookup_paths, m->unit_file_scope, NULL);
if (q < 0 && r >= 0)
r = q;
@ -2616,7 +2609,7 @@ static void manager_notify_finished(Manager *m) {
if (m->test_run)
return;
if (m->running_as == MANAGER_SYSTEM && detect_container() <= 0) {
if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) {
/* Note that m->kernel_usec.monotonic is always at 0,
* and m->firmware_usec.monotonic and
@ -2733,7 +2726,7 @@ static int manager_run_generators(Manager *m) {
if (m->test_run)
return 0;
paths = generator_paths(m->running_as);
paths = generator_paths(m->unit_file_scope);
if (!paths)
return log_oom();
@ -2851,7 +2844,7 @@ void manager_recheck_journal(Manager *m) {
assert(m);
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return;
u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
@ -2875,7 +2868,7 @@ void manager_set_show_status(Manager *m, ShowStatus mode) {
assert(m);
assert(IN_SET(mode, SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES, SHOW_STATUS_TEMPORARY));
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return;
if (m->show_status != mode)
@ -2892,7 +2885,7 @@ void manager_set_show_status(Manager *m, ShowStatus mode) {
static bool manager_get_show_status(Manager *m, StatusType type) {
assert(m);
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return false;
if (m->no_console_output)
@ -2914,7 +2907,7 @@ static bool manager_get_show_status(Manager *m, StatusType type) {
void manager_set_first_boot(Manager *m, bool b) {
assert(m);
if (m->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(m))
return;
if (m->first_boot != (int) b) {
@ -2960,7 +2953,7 @@ Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) {
const char *manager_get_runtime_prefix(Manager *m) {
assert(m);
return m->running_as == MANAGER_SYSTEM ?
return MANAGER_IS_SYSTEM(m) ?
"/run" :
getenv("XDG_RUNTIME_DIR");
}

View File

@ -140,6 +140,7 @@ struct Manager {
sd_event_source *jobs_in_progress_event_source;
UnitFileScope unit_file_scope;
LookupPaths lookup_paths;
Set *unit_path_cache;
@ -224,7 +225,6 @@ struct Manager {
unsigned n_in_gc_queue;
/* Flags */
ManagerRunningAs running_as;
ManagerExitCode exit_code:5;
bool dispatching_load_queue:1;
@ -300,10 +300,13 @@ struct Manager {
const char *unit_log_field;
const char *unit_log_format_string;
int first_boot;
int first_boot; /* tri-state */
};
int manager_new(ManagerRunningAs running_as, bool test_run, Manager **m);
#define MANAGER_IS_SYSTEM(m) ((m)->unit_file_scope == UNIT_FILE_SYSTEM)
#define MANAGER_IS_USER(m) ((m)->unit_file_scope != UNIT_FILE_SYSTEM)
int manager_new(UnitFileScope scope, bool test_run, Manager **m);
Manager* manager_free(Manager *m);
void manager_enumerate(Manager *m);

View File

@ -336,8 +336,7 @@ static int mount_add_device_links(Mount *m) {
if (path_equal(m->where, "/"))
return 0;
if (mount_is_auto(p) && !mount_is_automount(p) &&
UNIT(m)->manager->running_as == MANAGER_SYSTEM)
if (mount_is_auto(p) && !mount_is_automount(p) && MANAGER_IS_SYSTEM(UNIT(m)->manager))
device_wants_mount = true;
r = unit_add_node_link(UNIT(m), p->what, device_wants_mount, m->from_fragment ? UNIT_BINDS_TO : UNIT_REQUIRES);
@ -353,7 +352,7 @@ static int mount_add_quota_links(Mount *m) {
assert(m);
if (UNIT(m)->manager->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(UNIT(m)->manager))
return 0;
p = get_mount_parameters_fragment(m);
@ -400,7 +399,7 @@ static int mount_add_default_dependencies(Mount *m) {
if (!UNIT(m)->default_dependencies)
return 0;
if (UNIT(m)->manager->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(UNIT(m)->manager))
return 0;
/* We do not add any default dependencies to /, /usr or
@ -1396,7 +1395,7 @@ static int mount_setup_unit(
goto fail;
}
if (m->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(m)) {
const char* target;
target = mount_needs_network(options, fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
@ -1424,7 +1423,7 @@ static int mount_setup_unit(
}
}
if (m->running_as == MANAGER_SYSTEM &&
if (MANAGER_IS_SYSTEM(m) &&
mount_needs_network(options, fstype)) {
/* _netdev option may have shown up late, or on a
* remount. Add remote-fs dependencies, even though

View File

@ -318,7 +318,7 @@ static int path_add_default_dependencies(Path *p) {
if (r < 0)
return r;
if (UNIT(p)->manager->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(UNIT(p)->manager)) {
r = unit_add_two_dependencies_by_name(UNIT(p), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;

View File

@ -523,7 +523,7 @@ static int service_add_default_dependencies(Service *s) {
/* Add a number of automatic dependencies useful for the
* majority of services. */
if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) {
/* First, pull in the really early boot stuff, and
* require it, so that we fail if we can't acquire
* it. */
@ -1211,7 +1211,7 @@ static int service_spawn(
if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0)
return -ENOMEM;
if (UNIT(s)->manager->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(UNIT(s)->manager))
if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0)
return -ENOMEM;

View File

@ -301,7 +301,7 @@ static int socket_add_default_dependencies(Socket *s) {
if (r < 0)
return r;
if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) {
r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;

View File

@ -198,7 +198,7 @@ static int swap_add_device_links(Swap *s) {
return 0;
if (is_device_path(s->what))
return unit_add_node_link(UNIT(s), s->what, UNIT(s)->manager->running_as == MANAGER_SYSTEM, UNIT_BINDS_TO);
return unit_add_node_link(UNIT(s), s->what, MANAGER_IS_SYSTEM(UNIT(s)->manager), UNIT_BINDS_TO);
else
/* File based swap devices need to be ordered after
* systemd-remount-fs.service, since they might need a
@ -214,7 +214,7 @@ static int swap_add_default_dependencies(Swap *s) {
if (!UNIT(s)->default_dependencies)
return 0;
if (UNIT(s)->manager->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(UNIT(s)->manager))
return 0;
if (detect_container() > 0)

View File

@ -109,7 +109,7 @@ static int timer_add_default_dependencies(Timer *t) {
if (r < 0)
return r;
if (UNIT(t)->manager->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) {
r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;
@ -135,7 +135,7 @@ static int timer_setup_persistent(Timer *t) {
if (!t->persistent)
return 0;
if (UNIT(t)->manager->running_as == MANAGER_SYSTEM) {
if (MANAGER_IS_SYSTEM(UNIT(t)->manager)) {
r = unit_require_mounts_for(UNIT(t), "/var/lib/systemd/timers");
if (r < 0)

View File

@ -140,7 +140,7 @@ static int specifier_runtime(char specifier, void *data, void *userdata, char **
assert(u);
if (u->manager->running_as == MANAGER_SYSTEM)
if (MANAGER_IS_SYSTEM(u->manager))
e = "/run";
else {
e = getenv("XDG_RUNTIME_DIR");

View File

@ -814,7 +814,7 @@ int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
return r;
}
if (u->manager->running_as != MANAGER_SYSTEM)
if (!MANAGER_IS_SYSTEM(u->manager))
return 0;
if (c->private_tmp) {
@ -2413,7 +2413,7 @@ int unit_set_default_slice(Unit *u) {
if (!escaped)
return -ENOMEM;
if (u->manager->running_as == MANAGER_SYSTEM)
if (MANAGER_IS_SYSTEM(u->manager))
b = strjoin("system-", escaped, ".slice", NULL);
else
b = strappend(escaped, ".slice");
@ -2423,7 +2423,7 @@ int unit_set_default_slice(Unit *u) {
slice_name = b;
} else
slice_name =
u->manager->running_as == MANAGER_SYSTEM && !unit_has_name(u, SPECIAL_INIT_SCOPE)
MANAGER_IS_SYSTEM(u->manager) && !unit_has_name(u, SPECIAL_INIT_SCOPE)
? SPECIAL_SYSTEM_SLICE
: SPECIAL_ROOT_SLICE;
@ -2884,7 +2884,7 @@ int unit_add_node_link(Unit *u, const char *what, bool wants, UnitDependency dep
return r;
r = unit_add_two_dependencies(u, UNIT_AFTER,
u->manager->running_as == MANAGER_SYSTEM ? dep : UNIT_WANTS,
MANAGER_IS_SYSTEM(u->manager) ? dep : UNIT_WANTS,
device, true);
if (r < 0)
return r;
@ -3158,7 +3158,7 @@ UnitFileState unit_get_unit_file_state(Unit *u) {
if (u->unit_file_state < 0 && u->fragment_path) {
r = unit_file_get_state(
u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
u->manager->unit_file_scope,
NULL,
basename(u->fragment_path),
&u->unit_file_state);
@ -3174,7 +3174,7 @@ int unit_get_unit_file_preset(Unit *u) {
if (u->unit_file_preset < 0 && u->fragment_path)
u->unit_file_preset = unit_file_query_preset(
u->manager->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
u->manager->unit_file_scope,
NULL,
basename(u->fragment_path));
@ -3225,7 +3225,7 @@ int unit_patch_contexts(Unit *u) {
return -ENOMEM;
}
if (u->manager->running_as == MANAGER_USER &&
if (MANAGER_IS_USER(u->manager) &&
!ec->working_directory) {
r = get_home_dir(&ec->working_directory);
@ -3237,7 +3237,7 @@ int unit_patch_contexts(Unit *u) {
ec->working_directory_missing_ok = true;
}
if (u->manager->running_as == MANAGER_USER &&
if (MANAGER_IS_USER(u->manager) &&
(ec->syscall_whitelist ||
!set_isempty(ec->syscall_filter) ||
!set_isempty(ec->syscall_archs) ||
@ -3318,7 +3318,7 @@ ExecRuntime *unit_get_exec_runtime(Unit *u) {
static int unit_drop_in_dir(Unit *u, UnitSetPropertiesMode mode, bool transient, char **dir) {
assert(u);
if (u->manager->running_as == MANAGER_USER) {
if (MANAGER_IS_USER(u->manager)) {
int r;
if (mode == UNIT_PERSISTENT && !transient)

View File

@ -1442,7 +1442,7 @@ int unit_file_mask(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -1493,7 +1493,7 @@ int unit_file_unmask(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -1575,7 +1575,7 @@ int unit_file_link(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -1668,7 +1668,7 @@ int unit_file_add_dependency(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -1738,7 +1738,7 @@ int unit_file_enable(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -1786,7 +1786,7 @@ int unit_file_disable(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -1863,7 +1863,7 @@ int unit_file_set_default(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -1899,7 +1899,7 @@ int unit_file_get_default(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -2001,7 +2001,7 @@ int unit_file_get_state(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -2203,7 +2203,7 @@ int unit_file_preset(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -2244,7 +2244,7 @@ int unit_file_preset_all(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;
@ -2322,7 +2322,7 @@ int unit_file_get_list(
if (r < 0)
return r;
r = lookup_paths_init_from_scope(&paths, scope, root_dir);
r = lookup_paths_init(&paths, scope, root_dir);
if (r < 0)
return r;

View File

@ -102,7 +102,7 @@ static int user_data_home_dir(char **dir, const char *suffix) {
return -ENOMEM;
*dir = res;
return 0;
return 1;
}
static char** user_dirs(
@ -223,23 +223,32 @@ static char** user_dirs(
return tmp;
}
char **generator_paths(ManagerRunningAs running_as) {
if (running_as == MANAGER_USER)
return strv_new("/run/systemd/user-generators",
"/etc/systemd/user-generators",
"/usr/local/lib/systemd/user-generators",
USER_GENERATOR_PATH,
NULL);
else
char **generator_paths(UnitFileScope scope) {
switch (scope) {
case UNIT_FILE_SYSTEM:
return strv_new("/run/systemd/system-generators",
"/etc/systemd/system-generators",
"/usr/local/lib/systemd/system-generators",
SYSTEM_GENERATOR_PATH,
NULL);
case UNIT_FILE_GLOBAL:
case UNIT_FILE_USER:
return strv_new("/run/systemd/user-generators",
"/etc/systemd/user-generators",
"/usr/local/lib/systemd/user-generators",
USER_GENERATOR_PATH,
NULL);
default:
assert_not_reached("Hmm, unexpected scope.");
}
}
static int acquire_generator_dirs(
ManagerRunningAs running_as,
UnitFileScope scope,
char **generator,
char **generator_early,
char **generator_late) {
@ -251,18 +260,28 @@ static int acquire_generator_dirs(
assert(generator_early);
assert(generator_late);
if (running_as == MANAGER_SYSTEM)
prefix = "/run/systemd/";
else {
const char *e;
switch (scope) {
assert(running_as == MANAGER_USER);
case UNIT_FILE_SYSTEM:
prefix = "/run/systemd/";
break;
case UNIT_FILE_USER: {
const char *e;
e = getenv("XDG_RUNTIME_DIR");
if (!e)
return -EINVAL;
return -ENXIO;
prefix = strjoina(e, "/systemd/", NULL);
break;
}
case UNIT_FILE_GLOBAL:
return -EOPNOTSUPP;
default:
assert_not_reached("Hmm, unexpected scope value.");
}
x = strappend(prefix, "generator");
@ -285,19 +304,26 @@ static int acquire_generator_dirs(
return 0;
}
static int acquire_config_dirs(ManagerRunningAs running_as, bool personal, char **persistent, char **runtime) {
static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **runtime) {
_cleanup_free_ char *a = NULL, *b = NULL;
int r;
assert(persistent);
assert(runtime);
if (running_as == MANAGER_SYSTEM) {
switch (scope) {
case UNIT_FILE_SYSTEM:
a = strdup(SYSTEM_CONFIG_UNIT_PATH);
b = strdup("/run/systemd/system");
} else if (personal) {
assert(running_as == MANAGER_USER);
break;
case UNIT_FILE_GLOBAL:
a = strdup(USER_CONFIG_UNIT_PATH);
b = strdup("/run/systemd/user");
break;
case UNIT_FILE_USER:
r = user_config_home(&a);
if (r < 0)
return r;
@ -310,11 +336,9 @@ static int acquire_config_dirs(ManagerRunningAs running_as, bool personal, char
a = NULL;
return 0;
} else {
assert(running_as == MANAGER_USER);
a = strdup(USER_CONFIG_UNIT_PATH);
b = strdup("/run/systemd/user");
default:
assert_not_reached("Hmm, unexpected scope value.");
}
if (!a || !b)
@ -350,8 +374,7 @@ static int patch_root_prefix(char **p, const char *root_dir) {
int lookup_paths_init(
LookupPaths *p,
ManagerRunningAs running_as,
bool personal,
UnitFileScope scope,
const char *root_dir) {
_cleanup_free_ char *generator = NULL, *generator_early = NULL, *generator_late = NULL,
@ -362,15 +385,15 @@ int lookup_paths_init(
int r;
assert(p);
assert(running_as >= 0);
assert(running_as < _MANAGER_RUNNING_AS_MAX);
assert(scope >= 0);
assert(scope < _UNIT_FILE_SCOPE_MAX);
r = acquire_config_dirs(running_as, personal, &persistent_config, &runtime_config);
r = acquire_config_dirs(scope, &persistent_config, &runtime_config);
if (r < 0)
return r;
r = acquire_generator_dirs(running_as, &generator, &generator_early, &generator_late);
if (r < 0)
r = acquire_generator_dirs(scope, &generator, &generator_early, &generator_late);
if (r < 0 && r != -EOPNOTSUPP && r != -ENXIO)
return r;
/* First priority is whatever has been passed to us via env
@ -405,46 +428,56 @@ int lookup_paths_init(
* we include /lib in the search path for the system
* stuff but avoid it for user stuff. */
if (running_as == MANAGER_USER) {
if (personal)
add = user_dirs(persistent_config, runtime_config,
generator, generator_early, generator_late);
else
add = strv_new(
switch (scope) {
case UNIT_FILE_SYSTEM:
add = strv_new(
/* If you modify this you also want to modify
* systemdsystemunitpath= in systemd.pc.in! */
STRV_IFNOTNULL(generator_early),
persistent_config,
"/etc/systemd/system",
runtime_config,
"/run/systemd/system",
STRV_IFNOTNULL(generator),
"/usr/local/lib/systemd/system",
SYSTEM_DATA_UNIT_PATH,
"/usr/lib/systemd/system",
#ifdef HAVE_SPLIT_USR
"/lib/systemd/system",
#endif
STRV_IFNOTNULL(generator_late),
NULL);
break;
case UNIT_FILE_GLOBAL:
add = strv_new(
/* If you modify this you also want to modify
* systemduserunitpath= in systemd.pc.in, and
* the arrays in user_dirs() above! */
generator_early,
STRV_IFNOTNULL(generator_early),
persistent_config,
"/etc/systemd/user",
runtime_config,
"/run/systemd/user",
generator,
STRV_IFNOTNULL(generator),
"/usr/local/lib/systemd/user",
"/usr/local/share/systemd/user",
USER_DATA_UNIT_PATH,
"/usr/lib/systemd/user",
"/usr/share/systemd/user",
generator_late,
STRV_IFNOTNULL(generator_late),
NULL);
} else
add = strv_new(
/* If you modify this you also want to modify
* systemdsystemunitpath= in systemd.pc.in! */
generator_early,
persistent_config,
"/etc/systemd/system",
runtime_config,
"/run/systemd/system",
generator,
"/usr/local/lib/systemd/system",
SYSTEM_DATA_UNIT_PATH,
"/usr/lib/systemd/system",
#ifdef HAVE_SPLIT_USR
"/lib/systemd/system",
#endif
generator_late,
NULL);
break;
case UNIT_FILE_USER:
add = user_dirs(persistent_config, runtime_config,
generator, generator_early, generator_late);
break;
default:
assert_not_reached("Hmm, unexpected scope?");
}
if (!add)
return -ENOMEM;
@ -520,19 +553,3 @@ void lookup_paths_free(LookupPaths *p) {
p->generator_early = mfree(p->generator_early);
p->generator_late = mfree(p->generator_late);
}
int lookup_paths_init_from_scope(
LookupPaths *p,
UnitFileScope scope,
const char *root_dir) {
assert(p);
assert(scope >= 0);
assert(scope < _UNIT_FILE_SCOPE_MAX);
return lookup_paths_init(
p,
scope == UNIT_FILE_SYSTEM ? MANAGER_SYSTEM : MANAGER_USER,
scope == UNIT_FILE_USER,
root_dir);
}

View File

@ -22,7 +22,6 @@
#include <stdbool.h>
typedef struct LookupPaths LookupPaths;
typedef enum ManagerRunningAs ManagerRunningAs;
#include "install.h"
#include "macro.h"
@ -40,20 +39,12 @@ struct LookupPaths {
char *generator_late;
};
enum ManagerRunningAs {
MANAGER_SYSTEM,
MANAGER_USER,
_MANAGER_RUNNING_AS_MAX,
_MANAGER_RUNNING_AS_INVALID = -1
};
int user_config_home(char **config_home);
int user_runtime_dir(char **runtime_dir);
char **generator_paths(ManagerRunningAs running_as);
char **generator_paths(UnitFileScope scope);
int lookup_paths_init(LookupPaths *p, ManagerRunningAs running_as, bool personal, const char *root_dir);
int lookup_paths_init_from_scope(LookupPaths *p, UnitFileScope scope, const char *root_dir);
int lookup_paths_init(LookupPaths *p, UnitFileScope scope, const char *root_dir);
void lookup_paths_free(LookupPaths *p);
#define _cleanup_lookup_paths_free_ _cleanup_(lookup_paths_free)

View File

@ -4810,7 +4810,7 @@ static int cat(int argc, char *argv[], void *userdata) {
return -EINVAL;
}
r = lookup_paths_init_from_scope(&lp, arg_scope, arg_root);
r = lookup_paths_init(&lp, arg_scope, arg_root);
if (r < 0)
return log_error_errno(r, "Failed to determine unit paths: %m");
@ -5241,7 +5241,7 @@ static int enable_sysv_units(const char *verb, char **args) {
/* Processes all SysV units, and reshuffles the array so that
* afterwards only the native units remain */
r = lookup_paths_init(&paths, MANAGER_SYSTEM, false, arg_root);
r = lookup_paths_init(&paths, arg_scope, arg_root);
if (r < 0)
return r;
@ -6074,7 +6074,7 @@ static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) {
assert(names);
assert(paths);
r = lookup_paths_init_from_scope(&lp, arg_scope, arg_root);
r = lookup_paths_init(&lp, arg_scope, arg_root);
if (r < 0)
return r;

View File

@ -1004,7 +1004,7 @@ int main(int argc, char *argv[]) {
umask(0022);
r = lookup_paths_init(&lp, MANAGER_SYSTEM, true, NULL);
r = lookup_paths_init(&lp, UNIT_FILE_SYSTEM, NULL);
if (r < 0) {
log_error_errno(r, "Failed to find lookup paths: %m");
goto finish;

View File

@ -33,7 +33,7 @@ static int test_cgroup_mask(void) {
/* Prepare the manager. */
assert_se(set_unit_path(TEST_DIR) >= 0);
r = manager_new(MANAGER_USER, true, &m);
r = manager_new(UNIT_FILE_USER, true, &m);
if (r == -EPERM || r == -EACCES) {
puts("manager_new: Permission denied. Skipping test.");
return EXIT_TEST_SKIP;

View File

@ -36,7 +36,7 @@ int main(int argc, char *argv[]) {
/* prepare the test */
assert_se(set_unit_path(TEST_DIR) >= 0);
r = manager_new(MANAGER_USER, true, &m);
r = manager_new(UNIT_FILE_USER, true, &m);
if (MANAGER_SKIP_TEST(r)) {
printf("Skipping test: manager_new: %s\n", strerror(-r));
return EXIT_TEST_SKIP;

View File

@ -291,14 +291,14 @@ static void test_exec_spec_interpolation(Manager *m) {
test(m, "exec-spec-interpolation.service", 0, CLD_EXITED);
}
static int run_tests(ManagerRunningAs running_as, test_function_t *tests) {
static int run_tests(UnitFileScope scope, test_function_t *tests) {
test_function_t *test = NULL;
Manager *m = NULL;
int r;
assert_se(tests);
r = manager_new(running_as, true, &m);
r = manager_new(scope, true, &m);
if (MANAGER_SKIP_TEST(r)) {
printf("Skipping test: manager_new: %s\n", strerror(-r));
return EXIT_TEST_SKIP;
@ -366,9 +366,9 @@ int main(int argc, char *argv[]) {
assert_se(unsetenv("VAR2") == 0);
assert_se(unsetenv("VAR3") == 0);
r = run_tests(MANAGER_USER, user_tests);
r = run_tests(UNIT_FILE_USER, user_tests);
if (r != 0)
return r;
return run_tests(MANAGER_SYSTEM, system_tests);
return run_tests(UNIT_FILE_SYSTEM, system_tests);
}

View File

@ -26,7 +26,7 @@
#include "string-util.h"
#include "strv.h"
static void test_paths(ManagerRunningAs running_as, bool personal) {
static void test_paths(UnitFileScope scope) {
char template[] = "/tmp/test-path-lookup.XXXXXXX";
_cleanup_lookup_paths_free_ LookupPaths lp_without_env = {};
@ -36,26 +36,26 @@ static void test_paths(ManagerRunningAs running_as, bool personal) {
assert_se(mkdtemp(template));
assert_se(unsetenv("SYSTEMD_UNIT_PATH") == 0);
assert_se(lookup_paths_init(&lp_without_env, running_as, personal, NULL) == 0);
assert_se(lookup_paths_init(&lp_without_env, scope, NULL) == 0);
assert_se(!strv_isempty(lp_without_env.search_path));
systemd_unit_path = strjoina(template, "/systemd-unit-path");
assert_se(setenv("SYSTEMD_UNIT_PATH", systemd_unit_path, 1) == 0);
assert_se(lookup_paths_init(&lp_with_env, running_as, personal, NULL) == 0);
assert_se(lookup_paths_init(&lp_with_env, scope, NULL) == 0);
assert_se(strv_length(lp_with_env.search_path) == 1);
assert_se(streq(lp_with_env.search_path[0], systemd_unit_path));
assert_se(rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
}
static void print_generator_paths(ManagerRunningAs running_as) {
static void print_generator_paths(UnitFileScope scope) {
_cleanup_strv_free_ char **paths;
char **dir;
log_info("Generators dirs (%s):", running_as == MANAGER_SYSTEM ? "system" : "user");
log_info("Generators dirs (%s):", scope == UNIT_FILE_SYSTEM ? "system" : "user");
paths = generator_paths(running_as);
paths = generator_paths(scope);
STRV_FOREACH(dir, paths)
log_info(" %s", *dir);
}
@ -65,13 +65,12 @@ int main(int argc, char **argv) {
log_parse_environment();
log_open();
test_paths(MANAGER_SYSTEM, false);
test_paths(MANAGER_SYSTEM, true);
test_paths(MANAGER_USER, false);
test_paths(MANAGER_USER, true);
test_paths(UNIT_FILE_SYSTEM);
test_paths(UNIT_FILE_USER);
test_paths(UNIT_FILE_GLOBAL);
print_generator_paths(MANAGER_SYSTEM);
print_generator_paths(MANAGER_USER);
print_generator_paths(UNIT_FILE_SYSTEM);
print_generator_paths(UNIT_FILE_USER);
return EXIT_SUCCESS;
}

View File

@ -44,7 +44,7 @@ static int setup_test(Manager **m) {
assert_se(m);
r = manager_new(MANAGER_USER, true, &tmp);
r = manager_new(UNIT_FILE_USER, true, &tmp);
if (MANAGER_SKIP_TEST(r)) {
printf("Skipping test: manager_new: %s\n", strerror(-r));
return -EXIT_TEST_SKIP;

View File

@ -33,7 +33,7 @@ int main(int argc, char *argv[]) {
/* prepare the test */
assert_se(set_unit_path(TEST_DIR) >= 0);
r = manager_new(MANAGER_USER, true, &m);
r = manager_new(UNIT_FILE_USER, true, &m);
if (MANAGER_SKIP_TEST(r)) {
printf("Skipping test: manager_new: %s\n", strerror(-r));
return EXIT_TEST_SKIP;

View File

@ -113,7 +113,7 @@ static void test_config_parse_exec(void) {
Manager *m = NULL;
Unit *u = NULL;
r = manager_new(MANAGER_USER, true, &m);
r = manager_new(UNIT_FILE_USER, true, &m);
if (MANAGER_SKIP_TEST(r)) {
printf("Skipping test: manager_new: %s\n", strerror(-r));
return;

View File

@ -209,7 +209,7 @@ static int test_unit_printf(void) {
assert_se(get_home_dir(&home) >= 0);
assert_se(get_shell(&shell) >= 0);
r = manager_new(MANAGER_USER, true, &m);
r = manager_new(UNIT_FILE_USER, true, &m);
if (r == -EPERM || r == -EACCES || r == -EADDRINUSE) {
puts("manager_new: Permission denied. Skipping test.");
return EXIT_TEST_SKIP;