}
static int method_start_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_start_unit_generic(message, userdata, JOB_START, false, error);
+ return method_start_unit_generic(message, userdata, JOB_START, /* reload_if_possible = */ false, error);
}
static int method_stop_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_start_unit_generic(message, userdata, JOB_STOP, false, error);
+ return method_start_unit_generic(message, userdata, JOB_STOP, /* reload_if_possible = */ false, error);
}
static int method_reload_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_start_unit_generic(message, userdata, JOB_RELOAD, false, error);
+ return method_start_unit_generic(message, userdata, JOB_RELOAD, /* reload_if_possible = */ false, error);
}
static int method_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_start_unit_generic(message, userdata, JOB_RESTART, false, error);
+ return method_start_unit_generic(message, userdata, JOB_RESTART, /* reload_if_possible = */ false, error);
}
static int method_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, false, error);
+ return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, /* reload_if_possible = */ false, error);
}
static int method_reload_or_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_start_unit_generic(message, userdata, JOB_RESTART, true, error);
+ return method_start_unit_generic(message, userdata, JOB_RESTART, /* reload_if_possible = */ true, error);
}
static int method_reload_or_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, true, error);
+ return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, /* reload_if_possible = */ true, error);
}
typedef enum GenericUnitOperationFlags {
if (!u->job || u->job->type != JOB_START)
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
- return method_start_unit_generic(message, m, JOB_START, false, error);
+ return method_start_unit_generic(message, m, JOB_START, /* reload_if_possible = */ false, error);
}
static int method_kill_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
if (!unit_is_pristine(u))
return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
- "Unit %s already exists.", name);
+ "Unit %s was already loaded or has a fragment file.", name);
/* OK, the unit failed to load and is unreferenced, now let's
* fill in the transient data instead */
}
static int reply_dump_by_fd(sd_bus_message *message, char *dump) {
- _cleanup_close_ int fd = -1;
+ _cleanup_close_ int fd = -EBADF;
fd = acquire_data_fd(dump, strlen(dump), 0);
if (fd < 0)
return 0;
}
+static void log_caller(sd_bus_message *message, Manager *manager, const char *method) {
+ _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
+ const char *comm = NULL;
+ Unit *caller;
+ pid_t pid;
+
+ assert(message);
+ assert(manager);
+ assert(method);
+
+ if (sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID|SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_COMM, &creds) < 0)
+ return;
+
+ /* We need at least the PID, otherwise there's nothing to log, the rest is optional */
+ if (sd_bus_creds_get_pid(creds, &pid) < 0)
+ return;
+
+ (void) sd_bus_creds_get_comm(creds, &comm);
+ caller = manager_get_unit_by_pid(manager, pid);
+
+ log_info("%s requested from client PID " PID_FMT " ('%s') (from unit '%s')...",
+ method, pid, strna(comm), strna(caller ? caller->id : NULL));
+}
+
static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
int r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
+ /* Write a log message noting the unit or process who requested the Reload() */
+ log_caller(message, m, "Reloading");
+
+ /* Check the rate limit after the authorization succeeds, to avoid denial-of-service issues. */
+ if (!ratelimit_below(&m->reload_ratelimit)) {
+ log_warning("Reloading request rejected due to rate limit.");
+ return sd_bus_error_setf(error,
+ SD_BUS_ERROR_LIMITS_EXCEEDED,
+ "Reload() request rejected due to rate limit.");
+ }
+
/* Instead of sending the reply back right away, we just
* remember that we need to and then send it after the reload
* is finished. That way the caller knows when the reload
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
+ /* Write a log message noting the unit or process who requested the Reexecute() */
+ log_caller(message, m, "Reexecuting");
+
/* We don't send a reply back here, the client should
* just wait for us disconnecting. */
/* Create an error reply, using the error information from changes[]
* if possible, and fall back to generating an error from error code c.
* The error message only describes the first error.
- *
- * Coordinate with install_changes_dump() in install.c.
*/
static int install_error(
sd_bus_error *error,
for (size_t i = 0; i < n_changes; i++)
- switch (changes[i].change_or_errno) {
+ /* When making changes here, make sure to also change install_changes_dump() in install.c. */
- case 0 ... _INSTALL_CHANGE_MAX: /* not errors */
- continue;
+ switch (changes[i].type) {
+ case 0 ... _INSTALL_CHANGE_TYPE_MAX: /* not errors */
+ break;
case -EEXIST:
if (changes[i].source)
"Unit %s is transient or generated.", changes[i].path);
goto found;
+ case -ETXTBSY:
+ r = sd_bus_error_setf(error, BUS_ERROR_UNIT_BAD_PATH,
+ "File %s is under the systemd unit hierarchy already.", changes[i].path);
+ goto found;
+
+ case -EBADSLT:
+ r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
+ "Invalid specifier in %s.", changes[i].path);
+ goto found;
+
+ case -EIDRM:
+ r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
+ "Destination unit %s is a non-template unit.", changes[i].path);
+ goto found;
+
case -EUCLEAN:
r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
"\"%s\" is not a valid unit name.",
changes[i].path);
goto found;
+ case -EXDEV:
+ if (changes[i].source)
+ r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
+ "Cannot alias %s as %s.",
+ changes[i].source, changes[i].path);
+ else
+ r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
+ "Invalid unit reference %s.", changes[i].path);
+ goto found;
+
case -ENOENT:
r = sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT,
"Unit file %s does not exist.", changes[i].path);
goto found;
+ case -EUNATCH:
+ r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
+ "Cannot resolve specifiers in %s.", changes[i].path);
+ goto found;
+
default:
- assert(changes[i].change_or_errno < 0); /* other errors */
- r = sd_bus_error_set_errnof(error, changes[i].change_or_errno, "File %s: %m", changes[i].path);
+ assert(changes[i].type < 0); /* other errors */
+ r = sd_bus_error_set_errnof(error, changes[i].type, "File %s: %m", changes[i].path);
goto found;
}
for (size_t i = 0; i < n_changes; i++) {
- if (changes[i].change_or_errno < 0) {
+ if (changes[i].type < 0) {
bad = true;
continue;
}
r = sd_bus_message_append(
reply, "(sss)",
- install_change_to_string(changes[i].change_or_errno),
+ install_change_type_to_string(changes[i].type),
changes[i].path,
changes[i].source);
if (r < 0)
}
static int method_enable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_enable_unit_files_generic(message, userdata, unit_file_enable, true, error);
+ return method_enable_unit_files_generic(message, userdata, unit_file_enable, /* carries_install_info = */ true, error);
}
static int method_enable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_enable_unit_files_generic(message, userdata, unit_file_enable, true, error);
+ return method_enable_unit_files_generic(message, userdata, unit_file_enable, /* carries_install_info = */ true, error);
}
static int method_reenable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_enable_unit_files_generic(message, userdata, unit_file_reenable, true, error);
+ return method_enable_unit_files_generic(message, userdata, unit_file_reenable, /* carries_install_info = */ true, error);
}
static int method_link_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_enable_unit_files_generic(message, userdata, unit_file_link, false, error);
+ return method_enable_unit_files_generic(message, userdata, unit_file_link, /* carries_install_info = */ false, error);
}
static int unit_file_preset_without_mode(LookupScope scope, UnitFileFlags flags, const char *root_dir, char **files, InstallChange **changes, size_t *n_changes) {
}
static int method_preset_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_enable_unit_files_generic(message, userdata, unit_file_preset_without_mode, true, error);
+ return method_enable_unit_files_generic(message, userdata, unit_file_preset_without_mode, /* carries_install_info = */ true, error);
}
static int method_mask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_enable_unit_files_generic(message, userdata, unit_file_mask, false, error);
+ return method_enable_unit_files_generic(message, userdata, unit_file_mask, /* carries_install_info = */ false, error);
}
static int method_preset_unit_files_with_mode(sd_bus_message *message, void *userdata, sd_bus_error *error) {
sd_bus_message *message,
Manager *m,
int (*call)(LookupScope scope, UnitFileFlags flags, const char *root_dir, char *files[], InstallChange **changes, size_t *n_changes),
+ bool carries_install_info,
sd_bus_error *error) {
_cleanup_strv_free_ char **l = NULL;
if (r < 0)
return r;
- if (sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlags")) {
+ if (sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlags") ||
+ sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlagsAndInstallInfo")) {
uint64_t raw_flags;
r = sd_bus_message_read(message, "t", &raw_flags);
if (r < 0)
return install_error(error, r, changes, n_changes);
- return reply_install_changes_and_free(m, message, -1, changes, n_changes, error);
+ return reply_install_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes, error);
}
static int method_disable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_disable_unit_files_generic(message, userdata, unit_file_disable, error);
+ return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info = */ false, error);
+}
+
+static int method_disable_unit_files_with_flags_and_install_info(sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info = */ true, error);
}
static int method_disable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_disable_unit_files_generic(message, userdata, unit_file_disable, error);
+ return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info = */ false, error);
}
static int method_unmask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
- return method_disable_unit_files_generic(message, userdata, unit_file_unmask, error);
+ return method_disable_unit_files_generic(message, userdata, unit_file_unmask, /* carries_install_info = */ false, error);
}
static int method_revert_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_get_unit_file_links(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
+ Manager *m = ASSERT_PTR(userdata);
InstallChange *changes = NULL;
size_t n_changes = 0, i;
- UnitFileFlags flags;
const char *name;
- char **p;
int runtime, r;
r = sd_bus_message_read(message, "sb", &name, &runtime);
if (r < 0)
return r;
- p = STRV_MAKE(name);
- flags = UNIT_FILE_DRY_RUN |
- (runtime ? UNIT_FILE_RUNTIME : 0);
-
- r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, flags, NULL, p, &changes, &n_changes);
- if (r < 0)
- return log_error_errno(r, "Failed to get file links for %s: %m", name);
+ r = unit_file_disable(m->unit_file_scope,
+ UNIT_FILE_DRY_RUN | (runtime ? UNIT_FILE_RUNTIME : 0),
+ NULL, STRV_MAKE(name), &changes, &n_changes);
+ if (r < 0) {
+ log_error_errno(r, "Failed to get file links for %s: %m", name);
+ goto finish;
+ }
for (i = 0; i < n_changes; i++)
- if (changes[i].change_or_errno == INSTALL_CHANGE_UNLINK) {
+ if (changes[i].type == INSTALL_CHANGE_UNLINK) {
r = sd_bus_message_append(reply, "s", changes[i].path);
if (r < 0)
- return r;
+ goto finish;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
- return r;
+ goto finish;
- return sd_bus_send(NULL, reply, NULL);
+ r = sd_bus_send(NULL, reply, NULL);
+
+finish:
+ install_changes_free(changes, n_changes);
+ return r;
}
static int method_get_job_waiting(sd_bus_message *message, void *userdata, sd_bus_error *error) {
SD_BUS_RESULT("a(sss)", changes),
method_disable_unit_files_with_flags,
SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD_WITH_ARGS("DisableUnitFilesWithFlagsAndInstallInfo",
+ SD_BUS_ARGS("as", files, "t", flags),
+ SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes),
+ method_disable_unit_files_with_flags_and_install_info,
+ SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ReenableUnitFiles",
SD_BUS_ARGS("as", files, "b", runtime, "b", force),
SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes),