HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/bus-unit-util.c

142,933 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: f394de58800f6f44c8671d3ee9abcf76abdcad20

Lines 3001–3301 of 3,301

        [UNIT_SCOPE]     = scope_unit_properties,
        [UNIT_MOUNT]     = mount_unit_properties,
        [UNIT_AUTOMOUNT] = automount_unit_properties,
        [UNIT_TARGET]    = other_unit_properties,
        [UNIT_DEVICE]    = other_unit_properties,
        [UNIT_SWAP]      = other_unit_properties,
};
 
int bus_append_unit_property_assignment(sd_bus_message *m, UnitType t, const char *assignment) {
        _cleanup_free_ char *field = NULL;
        const char *eq;
        int r;
 
        assert(m);
        assert(assignment);
        assert(t >= 0 && t < _UNIT_TYPE_MAX);
 
        eq = strchr(assignment, '=');
        if (!eq)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Not an assignment: %s", assignment);
 
 
        field = strndup(assignment, eq - assignment);
        if (!field)
                return log_oom();
        eq++;
 
        for (const BusProperty** tables = ASSERT_PTR(unit_type_properties[t]); *tables; tables++)
                for (const BusProperty *item = *tables; item->convert; item++)
                        if (item->name) {
                                if (streq(item->name, field))
                                        return item->convert(m, field, eq);
                        } else {
                                /* If .name is not set, the function must be a "try" helper */
                                r = item->convert(m, field, eq);
                                if (r != 0)
                                        return r;
                        }
 
        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                               "Unknown assignment: %s", assignment);
}
 
int bus_append_unit_property_assignment_many(sd_bus_message *m, UnitType t, char * const *l) {
        int r;
 
        assert(m);
 
        STRV_FOREACH(i, l) {
                r = bus_append_unit_property_assignment(m, t, *i);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
void bus_dump_transient_settings(UnitType t) {
        assert(t >= 0 && t < _UNIT_TYPE_MAX);
 
        for (const BusProperty** tables = ASSERT_PTR(unit_type_properties[t]); *tables; tables++)
                for (const BusProperty *item = *tables; item->convert; item++) {
                        assert(item->name || item->dump);
 
                        /* Do not print deprecated names. All "Ex" variants are deprecated. */
                        if (item->convert == warn_deprecated)
                                continue;
                        if (item->name && endswith(item->name, "Ex"))
                                continue;
 
                        if (item->name)
                                puts(item->name);
                        else
                                item->dump();
                }
}
 
int bus_append_scope_pidref(sd_bus_message *m, const PidRef *pidref, bool allow_pidfd) {
        assert(m);
 
        if (!pidref_is_set(pidref))
                return -ESRCH;
 
        if (pidref->fd >= 0 && allow_pidfd)
                return sd_bus_message_append(
                                m, "(sv)",
                                "PIDFDs", "ah", 1, pidref->fd);
 
        return sd_bus_message_append(
                        m, "(sv)",
                        "PIDs", "au", 1, pidref->pid);
}
 
int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet) {
        const char *type, *path, *source;
        InstallChange *changes = NULL;
        size_t n_changes = 0;
        int r;
 
        CLEANUP_ARRAY(changes, n_changes, install_changes_free);
 
        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
        if (r < 0)
                return bus_log_parse_error(r);
 
        while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
                InstallChangeType t;
 
                /* We expect only "success" changes to be sent over the bus. Hence, reject anything
                 * negative. */
                t = install_change_type_from_string(type);
                if (t < 0) {
                        log_notice_errno(t, "Manager reported unknown change type \"%s\" for path \"%s\", ignoring.",
                                         type, path);
                        continue;
                }
 
                r = install_changes_add(&changes, &n_changes, t, path, source);
                if (r < 0)
                        return r;
        }
        if (r < 0)
                return bus_log_parse_error(r);
 
        r = sd_bus_message_exit_container(m);
        if (r < 0)
                return bus_log_parse_error(r);
 
        install_changes_dump(0, NULL, changes, n_changes, quiet);
 
        return 0;
}
 
int unit_load_state(sd_bus *bus, const char *name, char **ret) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_free_ char *path = NULL;
        int r;
 
        path = unit_dbus_path_from_name(name);
        if (!path)
                return log_oom();
 
        /* This function warns on its own, because otherwise it'd be awkward to pass
         * the dbus error message around. */
 
        r = sd_bus_get_property_string(
                        bus,
                        "org.freedesktop.systemd1",
                        path,
                        "org.freedesktop.systemd1.Unit",
                        "LoadState",
                        &error,
                        ret);
        if (r < 0)
                return log_error_errno(r, "Failed to get load state of %s: %s", name, bus_error_message(&error, r));
 
        return 0;
}
 
int unit_info_compare(const UnitInfo *a, const UnitInfo *b) {
        int r;
 
        /* First, order by machine */
        r = strcasecmp_ptr(a->machine, b->machine);
        if (r != 0)
                return r;
 
        /* Second, order by unit type */
        r = strcasecmp_ptr(strrchr(a->id, '.'), strrchr(b->id, '.'));
        if (r != 0)
                return r;
 
        /* Third, order by name */
        return strcasecmp(a->id, b->id);
}
 
int bus_service_manager_reload(sd_bus *bus) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        int r;
 
        assert(bus);
 
        r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "Reload");
        if (r < 0)
                return bus_log_create_error(r);
 
        /* Reloading the daemon may take long, hence set a longer timeout here */
        r = sd_bus_call(bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to reload service manager: %s", bus_error_message(&error, r));
 
        return 0;
}
 
typedef struct UnitFreezer {
        char *name;
        sd_bus *bus;
} UnitFreezer;
 
/* Wait for 60 seconds at maximum for freezer operation */
#define FREEZE_BUS_CALL_TIMEOUT (60 * USEC_PER_SEC)
 
UnitFreezer* unit_freezer_free(UnitFreezer *f) {
        if (!f)
                return NULL;
 
        free(f->name);
        sd_bus_flush_close_unref(f->bus);
 
        return mfree(f);
}
 
int unit_freezer_new(const char *name, UnitFreezer **ret) {
        _cleanup_(unit_freezer_freep) UnitFreezer *f = NULL;
        int r;
 
        assert(name);
        assert(ret);
 
        f = new(UnitFreezer, 1);
        if (!f)
                return log_oom();
 
        *f = (UnitFreezer) {
                .name = strdup(name),
        };
        if (!f->name)
                return log_oom();
 
        r = bus_connect_system_systemd(&f->bus);
        if (r < 0)
                return log_error_errno(r, "Failed to open connection to systemd: %m");
 
        (void) sd_bus_set_method_call_timeout(f->bus, FREEZE_BUS_CALL_TIMEOUT);
 
        *ret = TAKE_PTR(f);
        return 0;
}
 
static int unit_freezer_action(UnitFreezer *f, bool freeze) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        assert(f);
        assert(f->name);
        assert(f->bus);
 
        r = bus_call_method(f->bus, bus_systemd_mgr,
                            freeze ? "FreezeUnit" : "ThawUnit",
                            &error,
                            /* ret_reply= */ NULL,
                            "s",
                            f->name);
        if (r < 0) {
                if (sd_bus_error_has_names(&error,
                                           BUS_ERROR_NO_SUCH_UNIT,
                                           BUS_ERROR_UNIT_INACTIVE,
                                           SD_BUS_ERROR_NOT_SUPPORTED)) {
 
                        log_debug_errno(r, "Skipping freezer for '%s': %s", f->name, bus_error_message(&error, r));
                        return 0;
                }
 
                return log_error_errno(r, "Failed to %s unit '%s': %s",
                                       freeze ? "freeze" : "thaw", f->name, bus_error_message(&error, r));
        }
 
        log_info("Successfully %s unit '%s'.", freeze ? "froze" : "thawed", f->name);
        return 1;
}
 
int unit_freezer_freeze(UnitFreezer *f) {
        return unit_freezer_action(f, true);
}
 
int unit_freezer_thaw(UnitFreezer *f) {
        return unit_freezer_action(f, false);
}
 
ExecDirectoryFlags exec_directory_flags_from_string(const char *s) {
        if (isempty(s))
                return 0;
 
        if (streq(s, "ro"))
                return EXEC_DIRECTORY_READ_ONLY;
 
        return _EXEC_DIRECTORY_FLAGS_INVALID;
}
 
const char* exec_directory_flags_to_string(uint64_t flags) {
        if (flags == 0)
                return "";
 
        if (flags == EXEC_DIRECTORY_READ_ONLY)
                return "ro";
 
        return NULL;
}