2 This file is part of elogind.
4 Copyright 2016 Lennart Poettering
6 elogind is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 elogind is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with elogind; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "bus-internal.h"
22 #include "bus-unit-util.h"
24 #include "cgroup-util.h"
29 #include "locale-util.h"
30 #include "parse-util.h"
31 #include "path-util.h"
32 #include "process-util.h"
33 #include "rlimit-util.h"
34 #include "signal-util.h"
35 #include "string-util.h"
36 #include "syslog-util.h"
37 #include "terminal-util.h"
41 int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u) {
47 return sd_bus_message_read(
62 int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignment) {
63 const char *eq, *field;
69 eq = strchr(assignment, '=');
71 log_error("Not an assignment: %s", assignment);
75 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
77 return bus_log_create_error(r);
79 field = strndupa(assignment, eq - assignment);
82 if (streq(field, "CPUQuota")) {
85 r = sd_bus_message_append(m, "sv", "CPUQuotaPerSecUSec", "t", USEC_INFINITY);
86 else if (endswith(eq, "%")) {
89 if (sscanf(eq, "%lf%%", &percent) != 1 || percent <= 0) {
90 log_error("CPU quota '%s' invalid.", eq);
94 r = sd_bus_message_append(m, "sv", "CPUQuotaPerSecUSec", "t", (usec_t) percent * USEC_PER_SEC / 100);
96 log_error("CPU quota needs to be in percent.");
102 } else if (streq(field, "EnvironmentFile")) {
104 r = sd_bus_message_append(m, "sv", "EnvironmentFiles", "a(sb)", 1,
105 eq[0] == '-' ? eq + 1 : eq,
109 } else if (STR_IN_SET(field, "AccuracySec", "RandomizedDelaySec", "RuntimeMaxSec")) {
113 r = parse_sec(eq, &t);
115 return log_error_errno(r, "Failed to parse %s= parameter: %s", field, eq);
118 n = newa(char, l + 2);
122 /* Change suffix Sec → USec */
123 strcpy(mempcpy(n, field, l - 3), "USec");
124 r = sd_bus_message_append(m, "sv", n, "t", t);
128 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
130 return bus_log_create_error(r);
132 rl = rlimit_from_string(field);
137 r = rlimit_parse(rl, eq, &l);
139 return log_error_errno(r, "Failed to parse resource limit: %s", eq);
141 r = sd_bus_message_append(m, "v", "t", l.rlim_max);
143 return bus_log_create_error(r);
145 r = sd_bus_message_close_container(m);
147 return bus_log_create_error(r);
149 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
151 return bus_log_create_error(r);
153 sn = strjoina(field, "Soft");
154 r = sd_bus_message_append(m, "sv", sn, "t", l.rlim_cur);
156 } else if (STR_IN_SET(field,
157 "CPUAccounting", "MemoryAccounting", "IOAccounting", "BlockIOAccounting", "TasksAccounting",
158 "SendSIGHUP", "SendSIGKILL", "WakeSystem", "DefaultDependencies",
159 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "RemainAfterExit",
160 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "NoNewPrivileges",
161 "SyslogLevelPrefix", "Delegate", "RemainAfterElapse")) {
163 r = parse_boolean(eq);
165 return log_error_errno(r, "Failed to parse boolean assignment %s.", assignment);
167 r = sd_bus_message_append(m, "v", "b", r);
169 } else if (streq(field, "MemoryLimit")) {
172 if (isempty(eq) || streq(eq, "infinity"))
173 bytes = (uint64_t) -1;
175 r = parse_size(eq, 1024, &bytes);
177 log_error("Failed to parse bytes specification %s", assignment);
182 r = sd_bus_message_append(m, "v", "t", bytes);
184 } else if (streq(field, "TasksMax")) {
187 if (isempty(eq) || streq(eq, "infinity"))
190 r = safe_atou64(eq, &n);
192 log_error("Failed to parse maximum tasks specification %s", assignment);
197 r = sd_bus_message_append(m, "v", "t", n);
199 } else if (STR_IN_SET(field, "CPUShares", "StartupCPUShares")) {
202 r = cg_cpu_shares_parse(eq, &u);
204 log_error("Failed to parse %s value %s.", field, eq);
208 r = sd_bus_message_append(m, "v", "t", u);
210 } else if (STR_IN_SET(field, "IOWeight", "StartupIOWeight")) {
213 r = cg_weight_parse(eq, &u);
215 log_error("Failed to parse %s value %s.", field, eq);
219 r = sd_bus_message_append(m, "v", "t", u);
221 } else if (STR_IN_SET(field, "BlockIOWeight", "StartupBlockIOWeight")) {
224 r = cg_cpu_shares_parse(eq, &u);
226 log_error("Failed to parse %s value %s.", field, eq);
230 r = sd_bus_message_append(m, "v", "t", u);
232 } else if (STR_IN_SET(field,
233 "User", "Group", "DevicePolicy", "KillMode",
234 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
235 "StandardInput", "StandardOutput", "StandardError",
236 "Description", "Slice", "Type", "WorkingDirectory",
237 "RootDirectory", "SyslogIdentifier", "ProtectSystem",
239 r = sd_bus_message_append(m, "v", "s", eq);
241 else if (streq(field, "SyslogLevel")) {
244 level = log_level_from_string(eq);
246 log_error("Failed to parse %s value %s.", field, eq);
250 r = sd_bus_message_append(m, "v", "i", level);
252 } else if (streq(field, "SyslogFacility")) {
255 facility = log_facility_unshifted_from_string(eq);
257 log_error("Failed to parse %s value %s.", field, eq);
261 r = sd_bus_message_append(m, "v", "i", facility);
263 } else if (streq(field, "DeviceAllow")) {
266 r = sd_bus_message_append(m, "v", "a(ss)", 0);
268 const char *path, *rwm, *e;
272 path = strndupa(eq, e - eq);
279 if (!path_startswith(path, "/dev")) {
280 log_error("%s is not a device file in /dev.", path);
284 r = sd_bus_message_append(m, "v", "a(ss)", 1, path, rwm);
287 } else if (cgroup_io_limit_type_from_string(field) >= 0 || STR_IN_SET(field, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
290 r = sd_bus_message_append(m, "v", "a(st)", 0);
292 const char *path, *bandwidth, *e;
297 path = strndupa(eq, e - eq);
300 log_error("Failed to parse %s value %s.", field, eq);
304 if (!path_startswith(path, "/dev")) {
305 log_error("%s is not a device file in /dev.", path);
309 if (streq(bandwidth, "max")) {
310 bytes = CGROUP_LIMIT_MAX;
312 r = parse_size(bandwidth, 1000, &bytes);
314 log_error("Failed to parse byte value %s.", bandwidth);
319 r = sd_bus_message_append(m, "v", "a(st)", 1, path, bytes);
322 } else if (STR_IN_SET(field, "IODeviceWeight", "BlockIODeviceWeight")) {
325 r = sd_bus_message_append(m, "v", "a(st)", 0);
327 const char *path, *weight, *e;
332 path = strndupa(eq, e - eq);
335 log_error("Failed to parse %s value %s.", field, eq);
339 if (!path_startswith(path, "/dev")) {
340 log_error("%s is not a device file in /dev.", path);
344 r = safe_atou64(weight, &u);
346 log_error("Failed to parse %s value %s.", field, weight);
349 r = sd_bus_message_append(m, "v", "a(st)", path, u);
352 } else if (streq(field, "Nice")) {
355 r = safe_atoi32(eq, &i);
357 log_error("Failed to parse %s value %s.", field, eq);
361 r = sd_bus_message_append(m, "v", "i", i);
363 } else if (STR_IN_SET(field, "Environment", "PassEnvironment")) {
366 r = sd_bus_message_open_container(m, 'v', "as");
368 return bus_log_create_error(r);
370 r = sd_bus_message_open_container(m, 'a', "s");
372 return bus_log_create_error(r);
377 _cleanup_free_ char *word = NULL;
379 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE);
381 log_error("Failed to parse Environment value %s", eq);
387 if (streq(field, "Environment")) {
388 if (!env_assignment_is_valid(word)) {
389 log_error("Invalid environment assignment: %s", word);
392 } else { /* PassEnvironment */
393 if (!env_name_is_valid(word)) {
394 log_error("Invalid environment variable name: %s", word);
399 r = sd_bus_message_append_basic(m, 's', word);
401 return bus_log_create_error(r);
404 r = sd_bus_message_close_container(m);
406 return bus_log_create_error(r);
408 r = sd_bus_message_close_container(m);
410 } else if (streq(field, "KillSignal")) {
413 sig = signal_from_string_try_harder(eq);
415 log_error("Failed to parse %s value %s.", field, eq);
419 r = sd_bus_message_append(m, "v", "i", sig);
421 } else if (streq(field, "TimerSlackNSec")) {
424 r = parse_nsec(eq, &n);
426 log_error("Failed to parse %s value %s", field, eq);
430 r = sd_bus_message_append(m, "v", "t", n);
431 } else if (streq(field, "OOMScoreAdjust")) {
434 r = safe_atoi(eq, &oa);
436 log_error("Failed to parse %s value %s", field, eq);
440 if (!oom_score_adjust_is_valid(oa)) {
441 log_error("OOM score adjust value out of range");
445 r = sd_bus_message_append(m, "v", "i", oa);
446 } else if (STR_IN_SET(field, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories")) {
449 r = sd_bus_message_open_container(m, 'v', "as");
451 return bus_log_create_error(r);
453 r = sd_bus_message_open_container(m, 'a', "s");
455 return bus_log_create_error(r);
460 _cleanup_free_ char *word = NULL;
463 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
465 log_error("Failed to parse %s value %s", field, eq);
471 if (!utf8_is_valid(word)) {
472 log_error("Failed to parse %s value %s", field, eq);
476 offset = word[0] == '-';
477 if (!path_is_absolute(word + offset)) {
478 log_error("Failed to parse %s value %s", field, eq);
482 path_kill_slashes(word + offset);
484 r = sd_bus_message_append_basic(m, 's', word);
486 return bus_log_create_error(r);
489 r = sd_bus_message_close_container(m);
491 return bus_log_create_error(r);
493 r = sd_bus_message_close_container(m);
495 } else if (streq(field, "RuntimeDirectory")) {
498 r = sd_bus_message_open_container(m, 'v', "as");
500 return bus_log_create_error(r);
502 r = sd_bus_message_open_container(m, 'a', "s");
504 return bus_log_create_error(r);
509 _cleanup_free_ char *word = NULL;
511 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
513 return log_error_errno(r, "Failed to parse %s value %s", field, eq);
518 r = sd_bus_message_append_basic(m, 's', word);
520 return bus_log_create_error(r);
523 r = sd_bus_message_close_container(m);
525 return bus_log_create_error(r);
527 r = sd_bus_message_close_container(m);
530 log_error("Unknown assignment %s.", assignment);
536 return bus_log_create_error(r);
538 r = sd_bus_message_close_container(m);
540 return bus_log_create_error(r);
545 typedef struct BusWaitForJobs {
552 sd_bus_slot *slot_job_removed;
553 sd_bus_slot *slot_disconnected;
556 static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *error) {
559 log_error("Warning! D-Bus connection terminated.");
560 sd_bus_close(sd_bus_message_get_bus(m));
565 static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
566 const char *path, *unit, *result;
567 BusWaitForJobs *d = userdata;
575 r = sd_bus_message_read(m, "uoss", &id, &path, &unit, &result);
577 bus_log_parse_error(r);
581 found = set_remove(d->jobs, (char*) path);
587 if (!isempty(result))
588 d->result = strdup(result);
591 d->name = strdup(unit);
596 void bus_wait_for_jobs_free(BusWaitForJobs *d) {
600 set_free_free(d->jobs);
602 sd_bus_slot_unref(d->slot_disconnected);
603 sd_bus_slot_unref(d->slot_job_removed);
605 sd_bus_unref(d->bus);
613 int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret) {
614 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *d = NULL;
620 d = new0(BusWaitForJobs, 1);
624 d->bus = sd_bus_ref(bus);
626 /* When we are a bus client we match by sender. Direct
627 * connections OTOH have no initialized sender field, and
628 * hence we ignore the sender then */
629 r = sd_bus_add_match(
631 &d->slot_job_removed,
634 "sender='org.freedesktop.elogind1',"
635 "interface='org.freedesktop.elogind1.Manager',"
636 "member='JobRemoved',"
637 "path='/org/freedesktop/elogind1'" :
639 "interface='org.freedesktop.elogind1.Manager',"
640 "member='JobRemoved',"
641 "path='/org/freedesktop/elogind1'",
642 match_job_removed, d);
646 r = sd_bus_add_match(
648 &d->slot_disconnected,
650 "sender='org.freedesktop.DBus.Local',"
651 "interface='org.freedesktop.DBus.Local',"
652 "member='Disconnected'",
653 match_disconnected, d);
663 static int bus_process_wait(sd_bus *bus) {
667 r = sd_bus_process(bus, NULL);
673 r = sd_bus_wait(bus, (uint64_t) -1);
679 static int bus_job_get_service_result(BusWaitForJobs *d, char **result) {
680 _cleanup_free_ char *dbus_path = NULL;
686 dbus_path = unit_dbus_path_from_name(d->name);
690 return sd_bus_get_property_string(d->bus,
691 "org.freedesktop.elogind1",
693 "org.freedesktop.elogind1.Service",
699 static const struct {
700 const char *result, *explanation;
701 } explanations [] = {
702 { "resources", "a configured resource limit was exceeded" },
703 { "timeout", "a timeout was exceeded" },
704 { "exit-code", "the control process exited with error code" },
705 { "signal", "a fatal signal was delivered to the control process" },
706 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
707 { "watchdog", "the service failed to send watchdog ping" },
708 { "start-limit", "start of the service was attempted too often" }
711 static void log_job_error_with_service_result(const char* service, const char *result, const char* const* extra_args) {
712 _cleanup_free_ char *service_shell_quoted = NULL;
713 const char *systemctl = "systemctl", *journalctl = "journalctl";
717 service_shell_quoted = shell_maybe_quote(service);
719 if (extra_args && extra_args[1]) {
720 _cleanup_free_ char *t;
722 t = strv_join((char**) extra_args, " ");
723 systemctl = strjoina("systemctl ", t ? : "<args>");
724 journalctl = strjoina("journalctl ", t ? : "<args>");
727 if (!isempty(result)) {
730 for (i = 0; i < ELEMENTSOF(explanations); ++i)
731 if (streq(result, explanations[i].result))
734 if (i < ELEMENTSOF(explanations)) {
735 log_error("Job for %s failed because %s.\n"
736 "See \"%s status %s\" and \"%s -xe\" for details.\n",
738 explanations[i].explanation,
740 service_shell_quoted ?: "<service>",
746 log_error("Job for %s failed.\n"
747 "See \"%s status %s\" and \"%s -xe\" for details.\n",
750 service_shell_quoted ?: "<service>",
754 /* For some results maybe additional explanation is required */
755 if (streq_ptr(result, "start-limit"))
756 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
757 "followed by \"%1$s start %2$s\" again.",
759 service_shell_quoted ?: "<service>");
762 static int check_wait_response(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
768 if (streq(d->result, "canceled"))
769 log_error("Job for %s canceled.", strna(d->name));
770 else if (streq(d->result, "timeout"))
771 log_error("Job for %s timed out.", strna(d->name));
772 else if (streq(d->result, "dependency"))
773 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d->name));
774 else if (streq(d->result, "invalid"))
775 log_error("%s is not active, cannot reload.", strna(d->name));
776 else if (streq(d->result, "assert"))
777 log_error("Assertion failed on job for %s.", strna(d->name));
778 else if (streq(d->result, "unsupported"))
779 log_error("Operation on or unit type of %s not supported on this system.", strna(d->name));
780 else if (!streq(d->result, "done") && !streq(d->result, "skipped")) {
783 _cleanup_free_ char *result = NULL;
785 q = bus_job_get_service_result(d, &result);
787 log_debug_errno(q, "Failed to get Result property of service %s: %m", d->name);
789 log_job_error_with_service_result(d->name, result, extra_args);
791 log_error("Job failed. See \"journalctl -xe\" for details.");
795 if (streq(d->result, "canceled"))
797 else if (streq(d->result, "timeout"))
799 else if (streq(d->result, "dependency"))
801 else if (streq(d->result, "invalid"))
803 else if (streq(d->result, "assert"))
805 else if (streq(d->result, "unsupported"))
807 else if (!streq(d->result, "done") && !streq(d->result, "skipped"))
813 int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
818 while (!set_isempty(d->jobs)) {
821 q = bus_process_wait(d->bus);
823 return log_error_errno(q, "Failed to wait for response: %m");
826 q = check_wait_response(d, quiet, extra_args);
827 /* Return the first error as it is most likely to be
832 log_debug_errno(q, "Got result %s/%m for job %s", strna(d->result), strna(d->name));
835 d->name = mfree(d->name);
836 d->result = mfree(d->result);
842 int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
847 r = set_ensure_allocated(&d->jobs, &string_hash_ops);
851 return set_put_strdup(d->jobs, path);
854 int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
857 r = bus_wait_for_jobs_add(d, path);
861 return bus_wait_for_jobs(d, quiet, NULL);
864 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, unsigned *n_changes) {
865 const char *type, *path, *source;
868 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
870 return bus_log_parse_error(r);
872 while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
873 /* We expect only "success" changes to be sent over the bus.
874 Hence, reject anything negative. */
875 UnitFileChangeType ch = unit_file_change_type_from_string(type);
878 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type, path);
882 r = unit_file_changes_add(changes, n_changes, ch, path, source);
887 return bus_log_parse_error(r);
889 r = sd_bus_message_exit_container(m);
891 return bus_log_parse_error(r);
893 unit_file_dump_changes(0, NULL, *changes, *n_changes, false);
899 bool is_const; /* If false, cgroup_path should be free()'d */
901 Hashmap *pids; /* PID → process name */
904 struct CGroupInfo *parent;
905 LIST_FIELDS(struct CGroupInfo, siblings);
906 LIST_HEAD(struct CGroupInfo, children);
910 static bool IS_ROOT(const char *p) {
911 return isempty(p) || streq(p, "/");
914 static int add_cgroup(Hashmap *cgroups, const char *path, bool is_const, struct CGroupInfo **ret) {
915 struct CGroupInfo *parent = NULL, *cg;
924 cg = hashmap_get(cgroups, path);
930 if (!IS_ROOT(path)) {
933 e = strrchr(path, '/');
937 pp = strndupa(path, e - path);
941 r = add_cgroup(cgroups, pp, false, &parent);
946 cg = new0(struct CGroupInfo, 1);
951 cg->cgroup_path = (char*) path;
953 cg->cgroup_path = strdup(path);
954 if (!cg->cgroup_path) {
960 cg->is_const = is_const;
963 r = hashmap_put(cgroups, cg->cgroup_path, cg);
966 free(cg->cgroup_path);
972 LIST_PREPEND(siblings, parent->children, cg);
973 parent->n_children++;
980 static int add_process(
986 struct CGroupInfo *cg;
993 r = add_cgroup(cgroups, path, true, &cg);
997 r = hashmap_ensure_allocated(&cg->pids, &trivial_hash_ops);
1001 return hashmap_put(cg->pids, PID_TO_PTR(pid), (void*) name);
1004 static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
1008 while (cg->children)
1009 remove_cgroup(cgroups, cg->children);
1011 hashmap_remove(cgroups, cg->cgroup_path);
1014 free(cg->cgroup_path);
1016 hashmap_free(cg->pids);
1019 LIST_REMOVE(siblings, cg->parent->children, cg);
1024 static int cgroup_info_compare_func(const void *a, const void *b) {
1025 const struct CGroupInfo *x = *(const struct CGroupInfo* const*) a, *y = *(const struct CGroupInfo* const*) b;
1030 return strcmp(x->cgroup_path, y->cgroup_path);
1033 static int dump_processes(
1035 const char *cgroup_path,
1038 OutputFlags flags) {
1040 struct CGroupInfo *cg;
1045 if (IS_ROOT(cgroup_path))
1048 cg = hashmap_get(cgroups, cgroup_path);
1052 if (!hashmap_isempty(cg->pids)) {
1060 /* Order processes by their PID */
1061 pids = newa(pid_t, hashmap_size(cg->pids));
1063 HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j)
1064 pids[n++] = PTR_TO_PID(pidp);
1066 assert(n == hashmap_size(cg->pids));
1067 qsort_safe(pids, n, sizeof(pid_t), pid_compare_func);
1069 width = DECIMAL_STR_WIDTH(pids[n-1]);
1071 for (i = 0; i < n; i++) {
1072 _cleanup_free_ char *e = NULL;
1073 const char *special;
1076 name = hashmap_get(cg->pids, PID_TO_PTR(pids[i]));
1079 if (n_columns != 0) {
1082 k = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
1084 e = ellipsize(name, k, 100);
1089 more = i+1 < n || cg->children;
1090 special = special_glyph(more ? TREE_BRANCH : TREE_RIGHT);
1092 fprintf(stdout, "%s%s%*"PID_PRI" %s\n",
1101 struct CGroupInfo **children, *child;
1104 /* Order subcgroups by their name */
1105 children = newa(struct CGroupInfo*, cg->n_children);
1106 LIST_FOREACH(siblings, child, cg->children)
1107 children[n++] = child;
1108 assert(n == cg->n_children);
1109 qsort_safe(children, n, sizeof(struct CGroupInfo*), cgroup_info_compare_func);
1111 n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
1113 for (i = 0; i < n; i++) {
1114 _cleanup_free_ char *pp = NULL;
1115 const char *name, *special;
1118 child = children[i];
1120 name = strrchr(child->cgroup_path, '/');
1126 special = special_glyph(more ? TREE_BRANCH : TREE_RIGHT);
1128 fputs(prefix, stdout);
1129 fputs(special, stdout);
1130 fputs(name, stdout);
1131 fputc('\n', stdout);
1133 special = special_glyph(more ? TREE_VERTICAL : TREE_SPACE);
1135 pp = strappend(prefix, special);
1139 r = dump_processes(cgroups, child->cgroup_path, pp, n_columns, flags);
1149 static int dump_extra_processes(
1153 OutputFlags flags) {
1155 _cleanup_free_ pid_t *pids = NULL;
1156 _cleanup_hashmap_free_ Hashmap *names = NULL;
1157 struct CGroupInfo *cg;
1158 size_t n_allocated = 0, n = 0, k;
1162 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
1163 * combined, sorted, linear list. */
1165 HASHMAP_FOREACH(cg, cgroups, i) {
1173 if (hashmap_isempty(cg->pids))
1176 r = hashmap_ensure_allocated(&names, &trivial_hash_ops);
1180 if (!GREEDY_REALLOC(pids, n_allocated, n + hashmap_size(cg->pids)))
1183 HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j) {
1184 pids[n++] = PTR_TO_PID(pidp);
1186 r = hashmap_put(names, pidp, (void*) name);
1195 qsort_safe(pids, n, sizeof(pid_t), pid_compare_func);
1196 width = DECIMAL_STR_WIDTH(pids[n-1]);
1198 for (k = 0; k < n; k++) {
1199 _cleanup_free_ char *e = NULL;
1202 name = hashmap_get(names, PID_TO_PTR(pids[k]));
1205 if (n_columns != 0) {
1208 z = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
1210 e = ellipsize(name, z, 100);
1215 fprintf(stdout, "%s%s %*" PID_PRI " %s\n",
1217 special_glyph(TRIANGULAR_BULLET),
1225 int unit_show_processes(
1228 const char *cgroup_path,
1232 sd_bus_error *error) {
1234 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1235 Hashmap *cgroups = NULL;
1236 struct CGroupInfo *cg;
1242 if (flags & OUTPUT_FULL_WIDTH)
1244 else if (n_columns <= 0)
1245 n_columns = columns();
1247 prefix = strempty(prefix);
1249 r = sd_bus_call_method(
1251 "org.freedesktop.elogind1",
1252 "/org/freedesktop/elogind1",
1253 "org.freedesktop.elogind1.Manager",
1262 cgroups = hashmap_new(&string_hash_ops);
1266 r = sd_bus_message_enter_container(reply, 'a', "(sus)");
1271 const char *path = NULL, *name = NULL;
1274 r = sd_bus_message_read(reply, "(sus)", &path, &pid, &name);
1280 r = add_process(cgroups, path, pid, name);
1285 r = sd_bus_message_exit_container(reply);
1289 r = dump_processes(cgroups, cgroup_path, prefix, n_columns, flags);
1293 r = dump_extra_processes(cgroups, prefix, n_columns, flags);
1296 while ((cg = hashmap_first(cgroups)))
1297 remove_cgroup(cgroups, cg);
1299 hashmap_free(cgroups);