HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/systemctl/systemctl-list-units.c

39,998 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 9a8cd89295b5c498d40e843ae70f8fb395867c07

Lines 1001–1221 of 1,221

                FOREACH_ARRAY(u, unit_infos, n) {
                        r = automount_info_add(bus, u, &automounts, &n_automounts);
                        if (r < 0)
                                return r;
                }
 
        }
 
        typesafe_qsort(automounts, n_automounts, automount_info_compare);
        output_automounts_list(automounts, n_automounts);
 
        return 0;
}
 
typedef struct PathInfo {
        const char *machine;
        const char *id;
 
        char *path;
        char *condition;
 
        /* Note: triggered is a list here, although it almost certainly will always be one
         * unit. Nevertheless, dbus API allows for multiple values, so let's follow that. */
        char** triggered;
} PathInfo;
 
static int path_info_compare(const PathInfo *a, const PathInfo *b) {
        int r;
 
        assert(a);
        assert(b);
 
        r = strcasecmp_ptr(a->machine, b->machine);
        if (r != 0)
                return r;
 
        r = path_compare(a->path, b->path);
        if (r != 0)
                return r;
 
        r = strcmp(a->condition, b->condition);
        if (r != 0)
                return r;
 
        return strcasecmp_ptr(a->id, b->id);
}
 
static void path_info_array_free(PathInfo *paths, size_t n_paths) {
        assert(paths || n_paths == 0);
 
        FOREACH_ARRAY(p, paths, n_paths) {
                free(p->condition);
                free(p->path);
                strv_free(p->triggered);
        }
 
        free(paths);
}
 
static int path_info_add(
                sd_bus *bus,
                const struct UnitInfo *u,
                PathInfo **paths,
                size_t *n_paths) {
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_strv_free_ char **triggered = NULL;
        const char *condition, *path;
        int r;
 
        assert(bus);
        assert(u);
        assert(paths);
        assert(n_paths);
 
        if (!endswith(u->id, ".path"))
                return 0;
 
        r = get_triggered_units(bus, u->unit_path, &triggered);
        if (r < 0)
                return r;
 
        r = sd_bus_get_property(bus,
                                "org.freedesktop.systemd1",
                                u->unit_path,
                                "org.freedesktop.systemd1.Path",
                                "Paths",
                                &error,
                                &reply,
                                "a(ss)");
        if (r < 0)
                return log_error_errno(r, "Failed to get paths: %s", bus_error_message(&error, r));
 
        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
        if (r < 0)
                return bus_log_parse_error(r);
 
        while ((r = sd_bus_message_read(reply, "(ss)", &condition, &path)) > 0) {
                _cleanup_free_ char *condition_dup = NULL, *path_dup = NULL;
                _cleanup_strv_free_ char **triggered_dup = NULL;
 
                condition_dup = strdup(condition);
                if (!condition_dup)
                        return log_oom();
 
                path_dup = strdup(path);
                if (!path_dup)
                        return log_oom();
 
                triggered_dup = strv_copy(triggered);
                if (!triggered_dup)
                        return log_oom();
 
                if (!GREEDY_REALLOC(*paths, *n_paths + 1))
                        return log_oom();
 
                (*paths)[(*n_paths)++] = (PathInfo) {
                        .machine = u->machine,
                        .id = u->id,
                        .condition = TAKE_PTR(condition_dup),
                        .path = TAKE_PTR(path_dup),
                        .triggered = TAKE_PTR(triggered_dup),
                };
        }
        if (r < 0)
                return bus_log_parse_error(r);
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return bus_log_parse_error(r);
 
        return 0;
}
 
static int output_paths_list(const PathInfo *paths, size_t n_paths) {
        _cleanup_(table_unrefp) Table *table = NULL;
        int r;
 
        assert(paths || n_paths == 0);
 
        table = table_new("path", "condition", "unit", "activates");
        if (!table)
                return log_oom();
 
        table_set_header(table, arg_legend != 0);
        if (arg_full)
                table_set_width(table, 0);
 
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
 
        FOREACH_ARRAY(p, paths, n_paths) {
                _cleanup_free_ char *unit = NULL;
 
                unit = format_unit_id(p->id, p->machine);
                if (!unit)
                        return log_oom();
 
                r = table_add_many(table,
                                   TABLE_STRING, p->path,
                                   TABLE_STRING, p->condition,
                                   TABLE_STRING, unit);
                if (r < 0)
                        return table_log_add_error(r);
 
                r = table_add_triggered(table, p->triggered);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        r = output_table(table);
        if (r < 0)
                return r;
 
        if (arg_legend != 0)
                output_legend("path", n_paths);
 
        return 0;
}
 
int verb_list_paths(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_set_free_ Set *replies = NULL;
        _cleanup_strv_free_ char **units = NULL;
        _cleanup_free_ UnitInfo *unit_infos = NULL;
        PathInfo *paths = NULL;
        size_t n_paths = 0;
        sd_bus *bus;
        int r;
 
        CLEANUP_ARRAY(paths, n_paths, path_info_array_free);
 
        r = acquire_bus(BUS_MANAGER, &bus);
        if (r < 0)
                return r;
 
        pager_open(arg_pager_flags);
 
        r = expand_unit_names(bus, strv_skip(argv, 1), ".path", &units, NULL);
        if (r < 0)
                return r;
 
        if (argc == 1 || units) {
                int n;
 
                n = get_unit_list_recursive(bus, units, &unit_infos, &replies);
                if (n < 0)
                        return n;
 
                FOREACH_ARRAY(u, unit_infos, n) {
                        r = path_info_add(bus, u, &paths, &n_paths);
                        if (r < 0)
                                return r;
                }
        }
 
        typesafe_qsort(paths, n_paths, path_info_compare);
        output_paths_list(paths, n_paths);
 
        return 0;
}