+ return r;
+}
+
+static int get_unit_dbus_path_by_pid(
+ sd_bus *bus,
+ uint32_t pid,
+ char **unit) {
+
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ char *u;
+ int r;
+
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "GetUnitByPID",
+ &error,
+ &reply,
+ "u", pid);
+ if (r < 0) {
+ log_error("Failed to get unit for PID %lu: %s", (unsigned long) pid, bus_error_message(&error, r));
+ return r;
+ }
+
+ r = sd_bus_message_read(reply, "o", &u);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ u = strdup(u);
+ if (!u)
+ return log_oom();
+
+ *unit = u;
+ return 0;
+}
+
+static int show_all(
+ const char* verb,
+ sd_bus *bus,
+ bool show_properties,
+ bool *new_line,
+ bool *ellipsized) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ _cleanup_free_ UnitInfo *unit_infos = NULL;
+ const UnitInfo *u;
+ unsigned c;
+ int r;
+
+ r = get_unit_list(bus, &reply, &unit_infos, NULL);
+ if (r < 0)
+ return r;
+
+ pager_open_if_enabled();
+
+ c = (unsigned) r;
+
+ qsort_safe(unit_infos, c, sizeof(UnitInfo), compare_unit_info);
+
+ for (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(verb, bus, p, show_properties, new_line, ellipsized);
+ if (r != 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int cat(sd_bus *bus, char **args) {
+ _cleanup_free_ char *unit = NULL;
+ _cleanup_strv_free_ char **names = NULL;
+ char **name;
+ bool first = true;
+ int r = 0;
+
+ assert(bus);
+ assert(args);
+
+ r = expand_names(bus, args + 1, NULL, &names);
+ if (r < 0)
+ log_error("Failed to expand names: %s", strerror(-r));
+
+ pager_open_if_enabled();
+
+ STRV_FOREACH(name, names) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_strv_free_ char **dropin_paths = NULL;
+ _cleanup_free_ char *fragment_path = NULL;
+ char **path;
+
+ unit = unit_dbus_path_from_name(*name);
+ if (!unit)
+ return log_oom();
+
+ if (need_daemon_reload(bus, *name) > 0)
+ log_warning("Unit file of %s changed on disk. Run 'systemctl%s daemon-reload'.",
+ *name, arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
+
+ r = sd_bus_get_property_string(
+ bus,
+ "org.freedesktop.systemd1",
+ unit,
+ "org.freedesktop.systemd1.Unit",
+ "FragmentPath",
+ &error,
+ &fragment_path);
+ if (r < 0) {
+ log_warning("Failed to get FragmentPath: %s", bus_error_message(&error, r));
+ continue;
+ }
+
+ r = sd_bus_get_property_strv(
+ bus,
+ "org.freedesktop.systemd1",
+ unit,
+ "org.freedesktop.systemd1.Unit",
+ "DropInPaths",
+ &error,
+ &dropin_paths);
+ if (r < 0) {
+ log_warning("Failed to get DropInPaths: %s", bus_error_message(&error, r));
+ continue;
+ }
+
+ if (first)
+ first = false;
+ else
+ puts("");
+
+ if (!isempty(fragment_path)) {
+ printf("%s# %s%s\n",
+ ansi_highlight_blue(),
+ fragment_path,
+ ansi_highlight_off());
+ fflush(stdout);
+
+ r = sendfile_full(STDOUT_FILENO, fragment_path);
+ if (r < 0) {
+ log_warning("Failed to cat %s: %s", fragment_path, strerror(-r));
+ continue;
+ }
+ }
+
+ STRV_FOREACH(path, dropin_paths) {
+ printf("%s%s# %s%s\n",
+ isempty(fragment_path) && path == dropin_paths ? "" : "\n",
+ ansi_highlight_blue(),
+ *path,
+ ansi_highlight_off());
+ fflush(stdout);
+
+ r = sendfile_full(STDOUT_FILENO, *path);
+ if (r < 0) {
+ log_warning("Failed to cat %s: %s", *path, strerror(-r));
+ continue;
+ }
+ }
+ }
+
+ return r < 0 ? r : 0;
+}
+
+static int show(sd_bus *bus, char **args) {
+ bool show_properties, show_status, new_line = false;
+ bool ellipsized = false;
+ int r, ret = 0;
+
+ assert(bus);
+ assert(args);
+
+ show_properties = streq(args[0], "show");
+ show_status = streq(args[0], "status");
+
+ if (show_properties)
+ pager_open_if_enabled();
+
+ /* If no argument is specified inspect the manager itself */
+
+ if (show_properties && strv_length(args) <= 1)
+ return show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line, &ellipsized);
+
+ if (show_status && strv_length(args) <= 1)
+ ret = show_all(args[0], bus, false, &new_line, &ellipsized);
+ else {
+ _cleanup_free_ char **patterns = NULL;
+ char **name;
+
+ STRV_FOREACH(name, args + 1) {
+ _cleanup_free_ char *unit = NULL;
+ uint32_t id;
+
+ if (safe_atou32(*name, &id) < 0) {
+ if (strv_push(&patterns, *name) < 0)
+ return log_oom();
+
+ continue;
+ } else if (show_properties) {
+ /* Interpret as job id */
+ if (asprintf(&unit, "/org/freedesktop/systemd1/job/%u", id) < 0)
+ return log_oom();
+
+ } else {
+ /* Interpret as PID */
+ r = get_unit_dbus_path_by_pid(bus, id, &unit);
+ if (r < 0) {
+ ret = r;
+ continue;
+ }
+ }
+
+ show_one(args[0], bus, unit, show_properties, &new_line, &ellipsized);
+ }
+
+ if (!strv_isempty(patterns)) {
+ _cleanup_strv_free_ char **names = NULL;
+
+ r = expand_names(bus, patterns, NULL, &names);
+ if (r < 0)
+ log_error("Failed to expand names: %s", strerror(-r));
+
+ STRV_FOREACH(name, names) {
+ _cleanup_free_ char *unit;
+
+ unit = unit_dbus_path_from_name(*name);
+ if (!unit)
+ return log_oom();
+
+ show_one(args[0], bus, unit, show_properties, &new_line, &ellipsized);
+ }
+ }
+ }
+
+ if (ellipsized && !arg_quiet)
+ printf("Hint: Some lines were ellipsized, use -l to show in full.\n");
+
+ return ret;