HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/systemctl/systemctl-show.c

121,554 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 9ef3bbf51f8389fb7cac470db0bab366dffd29ad

Lines 2001–2637 of 2,637

                                _cleanup_free_ char *str = NULL;
                                const char *source, *partition, *mount_options;
                                int ignore_enoent;
 
                                r = sd_bus_message_enter_container(m, 'r', "sba(ss)");
                                if (r < 0)
                                        return bus_log_parse_error(r);
                                if (r == 0)
                                        break;
 
                                r = sd_bus_message_read(m, "sb", &source, &ignore_enoent);
                                if (r < 0)
                                        return bus_log_parse_error(r);
 
                                str = strjoin(ignore_enoent ? "-" : "", source);
                                if (!str)
                                        return log_oom();
 
                                r = sd_bus_message_enter_container(m, 'a', "(ss)");
                                if (r < 0)
                                        return bus_log_parse_error(r);
 
                                while ((r = sd_bus_message_read(m, "(ss)", &partition, &mount_options)) > 0)
                                        if (!strextend_with_separator(&str, ":", partition, mount_options))
                                                return log_oom();
                                if (r < 0)
                                        return bus_log_parse_error(r);
 
                                if (!strextend_with_separator(&paths, " ", str))
                                        return log_oom();
 
                                r = sd_bus_message_exit_container(m);
                                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);
                        }
 
                        r = sd_bus_message_exit_container(m);
                        if (r < 0)
                                return bus_log_parse_error(r);
 
                        bus_print_property_value(name, expected_value, flags, paths);
 
                        return 1;
 
                } else if (streq(name, "BPFProgram")) {
                        const char *a, *p;
 
                        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
                        if (r < 0)
                                return bus_log_parse_error(r);
 
                        while ((r = sd_bus_message_read(m, "(ss)", &a, &p)) > 0)
                                bus_print_property_valuef(name, expected_value, flags, "%s:%s", a, p);
                        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);
 
                        return 1;
                } else if (STR_IN_SET(name, "SocketBindAllow", "SocketBindDeny")) {
                        uint16_t nr_ports, port_min;
                        int32_t af, ip_protocol;
 
                        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(iiqq)");
                        if (r < 0)
                                return bus_log_parse_error(r);
                        while ((r = sd_bus_message_read(m, "(iiqq)", &af, &ip_protocol, &nr_ports, &port_min)) > 0) {
                                const char *family, *colon1, *protocol = "", *colon2 = "";
 
                                family = strempty(af_to_ipv4_ipv6(af));
                                colon1 = isempty(family) ? "" : ":";
 
                                if (ip_protocol != 0) {
                                        protocol = ip_protocol_to_tcp_udp(ip_protocol);
                                        colon2 = "";
                                }
 
                                if (nr_ports == 0)
                                        bus_print_property_valuef(name, expected_value, flags, "%s%s%s%sany",
                                                        family, colon1, protocol, colon2);
                                else if (nr_ports == 1)
                                        bus_print_property_valuef(
                                                        name, expected_value, flags, "%s%s%s%s%hu",
                                                        family, colon1, protocol, colon2, port_min);
                                else
                                        bus_print_property_valuef(
                                                        name, expected_value, flags, "%s%s%s%s%hu-%hu",
                                                        family, colon1, protocol, colon2, port_min,
                                                        (uint16_t) (port_min + nr_ports - 1));
                        }
                        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);
 
                        return 1;
                } else if (STR_IN_SET(name, "StateDirectorySymlink", "RuntimeDirectorySymlink", "CacheDirectorySymlink", "LogsDirectorySymlink")) {
                        const char *a, *p;
                        uint64_t symlink_flags;
 
                        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)");
                        if (r < 0)
                                return bus_log_parse_error(r);
 
                        while ((r = sd_bus_message_read(m, "(sst)", &a, &p, &symlink_flags)) > 0)
                                bus_print_property_valuef(name, expected_value, flags, "%s:%s", a, p);
                        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);
 
                        return 1;
                } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "OpenFile")) {
                        char *path, *fdname;
                        uint64_t offlags;
 
                        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)");
                        if (r < 0)
                                return bus_log_parse_error(r);
 
                        while ((r = sd_bus_message_read(m, "(sst)", &path, &fdname, &offlags)) > 0) {
                                _cleanup_free_ char *ofs = NULL;
 
                                r = open_file_to_string(
                                        &(OpenFile){
                                                .path = path,
                                                .fdname = fdname,
                                                .flags = offlags,
                                        },
                                        &ofs);
                                if (r < 0)
                                        return log_error_errno(
                                                        r, "Failed to convert OpenFile= value to string: %m");
 
                                bus_print_property_value(name, expected_value, flags, ofs);
                        }
                        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);
 
                        return 1;
                } else if (streq(name, "ExtraFileDescriptorNames")) {
                        _cleanup_strv_free_ char **extra_fd_names = NULL;
                        _cleanup_free_ char *joined = NULL;
 
                        r = sd_bus_message_read_strv(m, &extra_fd_names);
                        if (r < 0)
                                return bus_log_parse_error(r);
 
                        joined = strv_join(extra_fd_names, " ");
                        if (!joined)
                                return log_oom();
 
                        bus_print_property_value(name, expected_value, flags, joined);
 
                        return 1;
                }
 
                break;
        }
 
        return 0;
}
 
typedef enum SystemctlShowMode{
        SYSTEMCTL_SHOW_PROPERTIES,
        SYSTEMCTL_SHOW_STATUS,
        SYSTEMCTL_SHOW_HELP,
        _SYSTEMCTL_SHOW_MODE_MAX,
        _SYSTEMCTL_SHOW_MODE_INVALID = -EINVAL,
} SystemctlShowMode;
 
static const char* const systemctl_show_mode_table[_SYSTEMCTL_SHOW_MODE_MAX] = {
        [SYSTEMCTL_SHOW_PROPERTIES] = "show",
        [SYSTEMCTL_SHOW_STATUS]     = "status",
        [SYSTEMCTL_SHOW_HELP]       = "help",
};
 
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode);
 
static int show_one(
                sd_bus *bus,
                const char *path,
                const char *unit,
                SystemctlShowMode show_mode,
                bool *new_line,
                bool *ellipsized) {
 
        static const struct bus_properties_map property_map[] = {
                { "Id",                             "s",               NULL,           offsetof(UnitStatusInfo, id)                                },
                { "LoadState",                      "s",               NULL,           offsetof(UnitStatusInfo, load_state)                        },
                { "ActiveState",                    "s",               NULL,           offsetof(UnitStatusInfo, active_state)                      },
                { "FreezerState",                   "s",               NULL,           offsetof(UnitStatusInfo, freezer_state)                     },
                { "Documentation",                  "as",              NULL,           offsetof(UnitStatusInfo, documentation)                     },
                {}
        }, status_map[] = {
                { "Id",                             "s",               NULL,           offsetof(UnitStatusInfo, id)                                },
                { "LoadState",                      "s",               NULL,           offsetof(UnitStatusInfo, load_state)                        },
                { "ActiveState",                    "s",               NULL,           offsetof(UnitStatusInfo, active_state)                      },
                { "FreezerState",                   "s",               NULL,           offsetof(UnitStatusInfo, freezer_state)                     },
                { "SubState",                       "s",               NULL,           offsetof(UnitStatusInfo, sub_state)                         },
                { "Job",                            "(uo)",            bus_map_job_id, offsetof(UnitStatusInfo, job_id)                            },
                { "UnitFileState",                  "s",               NULL,           offsetof(UnitStatusInfo, unit_file_state)                   },
                { "UnitFilePreset",                 "s",               NULL,           offsetof(UnitStatusInfo, unit_file_preset)                  },
                { "Description",                    "s",               NULL,           offsetof(UnitStatusInfo, description)                       },
                { "Following",                      "s",               NULL,           offsetof(UnitStatusInfo, following)                         },
                { "Documentation",                  "as",              NULL,           offsetof(UnitStatusInfo, documentation)                     },
                { "FragmentPath",                   "s",               NULL,           offsetof(UnitStatusInfo, fragment_path)                     },
                { "SourcePath",                     "s",               NULL,           offsetof(UnitStatusInfo, source_path)                       },
                { "ControlGroup",                   "s",               NULL,           offsetof(UnitStatusInfo, control_group)                     },
                { "DropInPaths",                    "as",              NULL,           offsetof(UnitStatusInfo, dropin_paths)                      },
                { "LoadError",                      "(ss)",            map_load_error, offsetof(UnitStatusInfo, load_error)                        },
                { "Result",                         "s",               NULL,           offsetof(UnitStatusInfo, result)                            },
                { "TriggeredBy",                    "as",              NULL,           offsetof(UnitStatusInfo, triggered_by)                      },
                { "Triggers",                       "as",              NULL,           offsetof(UnitStatusInfo, triggers)                          },
                { "InactiveExitTimestamp",          "t",               NULL,           offsetof(UnitStatusInfo, inactive_exit_timestamp)           },
                { "InactiveExitTimestampMonotonic", "t",               NULL,           offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) },
                { "ActiveEnterTimestamp",           "t",               NULL,           offsetof(UnitStatusInfo, active_enter_timestamp)            },
                { "ActiveExitTimestamp",            "t",               NULL,           offsetof(UnitStatusInfo, active_exit_timestamp)             },
                { "InactiveEnterTimestamp",         "t",               NULL,           offsetof(UnitStatusInfo, inactive_enter_timestamp)          },
                { "RuntimeMaxUSec",                 "t",               NULL,           offsetof(UnitStatusInfo, runtime_max_sec)                   },
                { "InvocationID",                   "ay",              bus_map_id128,  offsetof(UnitStatusInfo, invocation_id)                     },
                { "NeedDaemonReload",               "b",               NULL,           offsetof(UnitStatusInfo, need_daemon_reload)                },
                { "Transient",                      "b",               NULL,           offsetof(UnitStatusInfo, transient)                         },
                { "ExecMainPID",                    "u",               NULL,           offsetof(UnitStatusInfo, main_pid)                          },
                { "MainPID",                        "u",               map_main_pid,   0                                                           },
                { "ControlPID",                     "u",               NULL,           offsetof(UnitStatusInfo, control_pid)                       },
                { "PIDFile",                        "s",               NULL,           offsetof(UnitStatusInfo, pid_file)                          },
                { "StatusText",                     "s",               NULL,           offsetof(UnitStatusInfo, status_text)                       },
                { "StatusErrno",                    "i",               NULL,           offsetof(UnitStatusInfo, status_errno)                      },
                { "StatusBusError",                 "s",               NULL,           offsetof(UnitStatusInfo, status_bus_error)                  },
                { "StatusVarlinkError",             "s",               NULL,           offsetof(UnitStatusInfo, status_varlink_error)              },
                { "FileDescriptorStoreMax",         "u",               NULL,           offsetof(UnitStatusInfo, fd_store_max)                      },
                { "NFileDescriptorStore",           "u",               NULL,           offsetof(UnitStatusInfo, n_fd_store)                        },
                { "ExecMainStartTimestamp",         "t",               NULL,           offsetof(UnitStatusInfo, start_timestamp)                   },
                { "ExecMainExitTimestamp",          "t",               NULL,           offsetof(UnitStatusInfo, exit_timestamp)                    },
                { "ExecMainCode",                   "i",               NULL,           offsetof(UnitStatusInfo, exit_code)                         },
                { "ExecMainStatus",                 "i",               NULL,           offsetof(UnitStatusInfo, exit_status)                       },
                { "LogNamespace",                   "s",               NULL,           offsetof(UnitStatusInfo, log_namespace)                     },
                { "ConditionTimestamp",             "t",               NULL,           offsetof(UnitStatusInfo, condition_timestamp)               },
                { "ConditionResult",                "b",               NULL,           offsetof(UnitStatusInfo, condition_result)                  },
                { "Conditions",                     "a(sbbsi)",        map_conditions, 0                                                           },
                { "AssertTimestamp",                "t",               NULL,           offsetof(UnitStatusInfo, assert_timestamp)                  },
                { "AssertResult",                   "b",               NULL,           offsetof(UnitStatusInfo, assert_result)                     },
                { "Asserts",                        "a(sbbsi)",        map_asserts,    0                                                           },
                { "NextElapseUSecRealtime",         "t",               NULL,           offsetof(UnitStatusInfo, next_elapse_real)                  },
                { "NextElapseUSecMonotonic",        "t",               NULL,           offsetof(UnitStatusInfo, next_elapse_monotonic)             },
                { "NAccepted",                      "u",               NULL,           offsetof(UnitStatusInfo, n_accepted)                        },
                { "NConnections",                   "u",               NULL,           offsetof(UnitStatusInfo, n_connections)                     },
                { "NRefused",                       "u",               NULL,           offsetof(UnitStatusInfo, n_refused)                         },
                { "Accept",                         "b",               NULL,           offsetof(UnitStatusInfo, accept)                            },
                { "Listen",                         "a(ss)",           map_listen,     offsetof(UnitStatusInfo, listen)                            },
                { "SysFSPath",                      "s",               NULL,           offsetof(UnitStatusInfo, sysfs_path)                        },
                { "Where",                          "s",               NULL,           offsetof(UnitStatusInfo, where)                             },
                { "What",                           "s",               NULL,           offsetof(UnitStatusInfo, what)                              },
                { "ConcurrencyHardMax",             "u",               NULL,           offsetof(UnitStatusInfo, concurrency_hard_max)              },
                { "ConcurrencySoftMax",             "u",               NULL,           offsetof(UnitStatusInfo, concurrency_soft_max)              },
                { "ActivatingConcurrencyMax",       "u",               NULL,           offsetof(UnitStatusInfo, activating_concurrency_max)        },
                { "NCurrentlyActive",               "u",               NULL,           offsetof(UnitStatusInfo, n_currently_active)                },
                { "MemoryCurrent",                  "t",               NULL,           offsetof(UnitStatusInfo, memory_current)                    },
                { "MemoryPeak",                     "t",               NULL,           offsetof(UnitStatusInfo, memory_peak)                       },
                { "MemorySwapCurrent",              "t",               NULL,           offsetof(UnitStatusInfo, memory_swap_current)               },
                { "MemorySwapPeak",                 "t",               NULL,           offsetof(UnitStatusInfo, memory_swap_peak)                  },
                { "MemoryZSwapCurrent",             "t",               NULL,           offsetof(UnitStatusInfo, memory_zswap_current)              },
                { "MemoryAvailable",                "t",               NULL,           offsetof(UnitStatusInfo, memory_available)                  },
                { "MemoryMin",                      "t",               NULL,           offsetof(UnitStatusInfo, memory_min)                        },
                { "MemoryLow",                      "t",               NULL,           offsetof(UnitStatusInfo, memory_low)                        },
                { "StartupMemoryLow",               "t",               NULL,           offsetof(UnitStatusInfo, startup_memory_low)                },
                { "MemoryHigh",                     "t",               NULL,           offsetof(UnitStatusInfo, memory_high)                       },
                { "StartupMemoryHigh",              "t",               NULL,           offsetof(UnitStatusInfo, startup_memory_high)               },
                { "MemoryMax",                      "t",               NULL,           offsetof(UnitStatusInfo, memory_max)                        },
                { "StartupMemoryMax",               "t",               NULL,           offsetof(UnitStatusInfo, startup_memory_max)                },
                { "MemorySwapMax",                  "t",               NULL,           offsetof(UnitStatusInfo, memory_swap_max)                   },
                { "StartupMemorySwapMax",           "t",               NULL,           offsetof(UnitStatusInfo, startup_memory_swap_max)           },
                { "MemoryZSwapMax",                 "t",               NULL,           offsetof(UnitStatusInfo, memory_zswap_max)                  },
                { "StartupMemoryZSwapMax",          "t",               NULL,           offsetof(UnitStatusInfo, startup_memory_zswap_max)          },
                { "MemoryLimit",                    "t",               NULL,           offsetof(UnitStatusInfo, memory_limit)                      },
                { "CPUUsageNSec",                   "t",               NULL,           offsetof(UnitStatusInfo, cpu_usage_nsec)                    },
                { "TasksCurrent",                   "t",               NULL,           offsetof(UnitStatusInfo, tasks_current)                     },
                { "TasksMax",                       "t",               NULL,           offsetof(UnitStatusInfo, tasks_max)                         },
                { "IPIngressBytes",                 "t",               NULL,           offsetof(UnitStatusInfo, ip_ingress_bytes)                  },
                { "IPEgressBytes",                  "t",               NULL,           offsetof(UnitStatusInfo, ip_egress_bytes)                   },
                { "IOReadBytes",                    "t",               NULL,           offsetof(UnitStatusInfo, io_read_bytes)                     },
                { "IOWriteBytes",                   "t",               NULL,           offsetof(UnitStatusInfo, io_write_bytes)                    },
                { "StateDirectoryQuotaUsage",       "(tt)",            map_quota,      offsetof(UnitStatusInfo, exec_directories_quota[EXEC_DIRECTORY_STATE])   },
                { "CacheDirectoryQuotaUsage",       "(tt)",            map_quota,      offsetof(UnitStatusInfo, exec_directories_quota[EXEC_DIRECTORY_CACHE])   },
                { "LogsDirectoryQuotaUsage",        "(tt)",            map_quota,      offsetof(UnitStatusInfo, exec_directories_quota[EXEC_DIRECTORY_LOGS])    },
                { "ExecCondition",                  "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecConditionEx",                "a(sasasttttuii)", map_exec,       0                                                           },
                { "ExecStartPre",                   "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecStartPreEx",                 "a(sasasttttuii)", map_exec,       0                                                           },
                { "ExecStart",                      "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecStartEx",                    "a(sasasttttuii)", map_exec,       0                                                           },
                { "ExecStartPost",                  "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecStartPostEx",                "a(sasasttttuii)", map_exec,       0                                                           },
                { "ExecReload",                     "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecReloadEx",                   "a(sasasttttuii)", map_exec,       0                                                           },
                { "ExecReloadPost",                 "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecReloadPostEx",               "a(sasasttttuii)", map_exec,       0                                                           },
                { "ExecStopPre",                    "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecStop",                       "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecStopEx",                     "a(sasasttttuii)", map_exec,       0                                                           },
                { "ExecStopPost",                   "a(sasbttttuii)",  map_exec,       0                                                           },
                { "ExecStopPostEx",                 "a(sasasttttuii)", map_exec,       0                                                           },
                {}
        };
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_set_free_ Set *found_properties = NULL;
        _cleanup_(unit_status_info_done) UnitStatusInfo info = {
                .runtime_max_sec = USEC_INFINITY,
                .memory_current = UINT64_MAX,
                .memory_high = CGROUP_LIMIT_MAX,
                .startup_memory_high = CGROUP_LIMIT_MAX,
                .memory_max = CGROUP_LIMIT_MAX,
                .startup_memory_max = CGROUP_LIMIT_MAX,
                .memory_swap_max = CGROUP_LIMIT_MAX,
                .startup_memory_swap_max = CGROUP_LIMIT_MAX,
                .memory_zswap_max = CGROUP_LIMIT_MAX,
                .startup_memory_zswap_max = CGROUP_LIMIT_MAX,
                .memory_limit = CGROUP_LIMIT_MAX,
                .memory_peak = CGROUP_LIMIT_MAX,
                .memory_swap_current = CGROUP_LIMIT_MAX,
                .memory_swap_peak = CGROUP_LIMIT_MAX,
                .memory_zswap_current = CGROUP_LIMIT_MAX,
                .memory_available = CGROUP_LIMIT_MAX,
                .cpu_usage_nsec = UINT64_MAX,
                .tasks_current = UINT64_MAX,
                .tasks_max = UINT64_MAX,
                .ip_ingress_bytes = UINT64_MAX,
                .ip_egress_bytes = UINT64_MAX,
                .io_read_bytes = UINT64_MAX,
                .io_write_bytes = UINT64_MAX,
                .concurrency_soft_max = UINT_MAX,
                .concurrency_hard_max = UINT_MAX,
                .activating_concurrency_max = UINT_MAX,
        };
        bool collect_found_properties;
        int r;
 
        assert(path);
        assert(new_line);
 
        log_debug("Showing one %s", path);
 
        r = bus_map_all_properties(
                        bus,
                        "org.freedesktop.systemd1",
                        path,
                        show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map,
                        BUS_MAP_BOOLEAN_AS_BOOL,
                        &error,
                        &reply,
                        &info);
        if (r < 0)
                return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
 
        if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) {
                log_full(show_mode == SYSTEMCTL_SHOW_PROPERTIES ? LOG_DEBUG : LOG_ERR,
                         "Unit %s could not be found.", unit);
 
                if (show_mode == SYSTEMCTL_SHOW_STATUS)
                        return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN;
                if (show_mode == SYSTEMCTL_SHOW_HELP)
                        return -ENOENT;
        }
 
        if (*new_line)
                printf("\n");
 
        *new_line = true;
 
        if (show_mode == SYSTEMCTL_SHOW_STATUS) {
                print_status_info(bus, &info, ellipsized);
 
                if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading", "refreshing"))
                        return EXIT_PROGRAM_NOT_RUNNING;
 
                return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK;
 
        } else if (show_mode == SYSTEMCTL_SHOW_HELP) {
                show_unit_help(&info);
                return 0;
        }
 
        r = sd_bus_message_rewind(reply, true);
        if (r < 0)
                return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r));
 
        /* The found properties set is expensive and only used for debug logging, so collect it only when needed. */
        collect_found_properties = DEBUG_LOGGING && !strv_isempty(arg_properties);
 
        r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_print_flags,
                                             collect_found_properties ? &found_properties : NULL);
        if (r < 0)
                return bus_log_parse_error(r);
 
        if (collect_found_properties)
                STRV_FOREACH(pp, arg_properties)
                        if (!set_contains(found_properties, *pp))
                                log_debug("Property %s does not exist.", *pp);
 
        return 0;
}
 
static int show_all(
                sd_bus *bus,
                SystemctlShowMode show_mode,
                bool *new_line,
                bool *ellipsized) {
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_free_ UnitInfo *unit_infos = NULL;
        unsigned c;
        int r, ret = 0;
 
        r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply);
        if (r < 0)
                return r;
 
        pager_open(arg_pager_flags);
 
        c = (unsigned) r;
 
        typesafe_qsort(unit_infos, c, unit_info_compare);
 
        for (const UnitInfo *u = unit_infos; u < unit_infos + c; u++) {
                _cleanup_free_ char *p = NULL;
 
                p = unit_dbus_path_from_name(u->id);
                if (!p)
                        return log_oom();
 
                r = show_one(bus, p, u->id, show_mode, new_line, ellipsized);
                if (r < 0)
                        return r;
                if (r > 0 && ret == 0)
                        ret = r;
        }
 
        return ret;
}
 
static int show_system_status(sd_bus *bus) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(machine_info_clear) struct machine_info mi = {};
        static const char prefix[] = "           ";
        _cleanup_free_ char *hn = NULL;
        const char *on, *off;
        unsigned c;
        int r;
 
        hn = gethostname_malloc();
        if (!hn)
                return log_oom();
 
        r = bus_map_all_properties(
                        bus,
                        "org.freedesktop.systemd1",
                        "/org/freedesktop/systemd1",
                        machine_info_property_map,
                        BUS_MAP_STRDUP,
                        &error,
                        NULL,
                        &mi);
        if (r < 0)
                return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r));
 
        if (streq_ptr(mi.state, "degraded")) {
                on = ansi_highlight_red();
                off = ansi_normal();
        } else if (streq_ptr(mi.state, "running")) {
                on = ansi_highlight_green();
                off = ansi_normal();
        } else {
                on = ansi_highlight_yellow();
                off = ansi_normal();
        }
 
        printf("%s%s%s %s\n", on, glyph(GLYPH_BLACK_CIRCLE), off, arg_host ?: hn);
 
        printf("    State: %s%s%s\n",
               on, strna(mi.state), off);
 
        printf("    Units: %" PRIu32 " loaded (incl. loaded aliases)\n", mi.n_names);
        printf("     Jobs: %" PRIu32 " queued\n", mi.n_jobs);
        printf("   Failed: %" PRIu32 " units\n", mi.n_failed_units);
 
        printf("    Since: %s; %s\n",
               FORMAT_TIMESTAMP_STYLE(mi.timestamp, arg_timestamp_style),
               FORMAT_TIMESTAMP_RELATIVE(mi.timestamp));
 
        printf("  systemd: %s\n", mi.version);
 
        if (!isempty(mi.tainted))
                printf("  Tainted: %s%s%s\n", ansi_highlight_yellow(), mi.tainted, ansi_normal());
 
        printf("   CGroup: %s\n", empty_to_root(mi.control_group));
 
        c = LESS_BY(columns(), strlen(prefix));
 
        r = unit_show_processes(bus, SPECIAL_ROOT_SLICE, mi.control_group, prefix, c, get_output_flags(), &error);
        if (r == -EBADR && arg_transport == BUS_TRANSPORT_LOCAL) /* Compatibility for really old systemd versions */
                show_cgroup(strempty(mi.control_group), prefix, c, get_output_flags());
        else if (r < 0)
                log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s",
                                  arg_host ?: hn, bus_error_message(&error, r));
 
        return 0;
}
 
int verb_show(int argc, char *argv[], uintptr_t _data, void *userdata) {
        bool new_line = false, ellipsized = false;
        SystemctlShowMode show_mode;
        int r, ret = 0;
        sd_bus *bus;
 
        assert(argv);
 
        show_mode = systemctl_show_mode_from_string(argv[0]);
        if (show_mode < 0)
                return log_error_errno(show_mode, "Invalid argument '%s'.", argv[0]);
 
        if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "'help' command expects one or more unit names.\n"
                                       "(Alternatively, help for systemctl itself may be shown with --help)");
 
        r = acquire_bus(BUS_MANAGER, &bus);
        if (r < 0)
                return r;
 
        pager_open(arg_pager_flags);
 
        if (argc <= 1) {
                /* If no argument or filter is specified inspect the manager itself:
                 * systemctl status → we show status of the manager
                 * systemctl status --all → status of the manager + status of all units
                 * systemctl status --state=… → status of units in listed states
                 * systemctl status --type=… → status of units of listed types
                 * systemctl status --failed → status of failed units, mirroring systemctl list-units --failed
                 */
 
                if (!arg_states && !arg_types) {
                        if (show_mode == SYSTEMCTL_SHOW_PROPERTIES)
                                /* systemctl show --all → show properties of the manager */
                                return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized);
 
                        r = show_system_status(bus);
                        if (r < 0)
                                return r;
 
                        new_line = true;
                }
 
                if (arg_all || arg_states || arg_types)
                        ret = show_all(bus, show_mode, &new_line, &ellipsized);
        } else {
                _cleanup_free_ char **patterns = NULL;
 
                STRV_FOREACH(name, strv_skip(argv, 1)) {
                        _cleanup_free_ char *path = NULL, *unit = NULL;
                        uint32_t id;
 
                        if (safe_atou32(*name, &id) < 0) {
                                if (strv_push(&patterns, *name) < 0)
                                        return log_oom();
 
                                continue;
                        } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) {
                                /* Interpret as job id */
                                if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0)
                                        return log_oom();
 
                        } else {
                                /* Interpret as PID */
                                r = lookup_unit_by_pidref(bus, (pid_t) id, &unit, &path);
                                if (r < 0) {
                                        RET_GATHER(ret, r);
                                        continue;
                                }
                        }
 
                        r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized);
                        if (r < 0)
                                return r;
                        if (r > 0 && ret == 0)
                                ret = r;
                }
 
                if (!strv_isempty(patterns)) {
                        _cleanup_strv_free_ char **names = NULL;
 
                        r = expand_unit_names(bus, patterns, NULL, &names, NULL);
                        if (r < 0)
                                return log_error_errno(r, "Failed to expand names: %m");
 
                        r = maybe_extend_with_unit_dependencies(bus, &names);
                        if (r < 0)
                                return r;
 
                        STRV_FOREACH(name, names) {
                                _cleanup_free_ char *path = NULL;
 
                                path = unit_dbus_path_from_name(*name);
                                if (!path)
                                        return log_oom();
 
                                r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized);
                                if (r < 0)
                                        return r;
                                if (r > 0 && ret == 0)
                                        ret = r;
                        }
                }
        }
 
        if (ellipsized && !arg_quiet)
                printf("Hint: Some lines were ellipsized, use -l to show in full.\n");
 
        return ret;
}