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 (STR_IN_SET(field, "IOReadBandwidthMax", "IOWriteBandwidthMax",
288 "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
291 r = sd_bus_message_append(m, "v", "a(st)", 0);
293 const char *path, *bandwidth, *e;
298 path = strndupa(eq, e - eq);
301 log_error("Failed to parse %s value %s.", field, eq);
305 if (!path_startswith(path, "/dev")) {
306 log_error("%s is not a device file in /dev.", path);
310 if (streq(bandwidth, "max")) {
311 bytes = CGROUP_LIMIT_MAX;
313 r = parse_size(bandwidth, 1000, &bytes);
315 log_error("Failed to parse byte value %s.", bandwidth);
320 r = sd_bus_message_append(m, "v", "a(st)", 1, path, bytes);
323 } else if (STR_IN_SET(field, "IODeviceWeight", "BlockIODeviceWeight")) {
326 r = sd_bus_message_append(m, "v", "a(st)", 0);
328 const char *path, *weight, *e;
333 path = strndupa(eq, e - eq);
336 log_error("Failed to parse %s value %s.", field, eq);
340 if (!path_startswith(path, "/dev")) {
341 log_error("%s is not a device file in /dev.", path);
345 r = safe_atou64(weight, &u);
347 log_error("Failed to parse %s value %s.", field, weight);
350 r = sd_bus_message_append(m, "v", "a(st)", path, u);
353 } else if (streq(field, "Nice")) {
356 r = safe_atoi32(eq, &i);
358 log_error("Failed to parse %s value %s.", field, eq);
362 r = sd_bus_message_append(m, "v", "i", i);
364 } else if (STR_IN_SET(field, "Environment", "PassEnvironment")) {
367 r = sd_bus_message_open_container(m, 'v', "as");
369 return bus_log_create_error(r);
371 r = sd_bus_message_open_container(m, 'a', "s");
373 return bus_log_create_error(r);
378 _cleanup_free_ char *word = NULL;
380 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE);
382 log_error("Failed to parse Environment value %s", eq);
388 if (streq(field, "Environment")) {
389 if (!env_assignment_is_valid(word)) {
390 log_error("Invalid environment assignment: %s", word);
393 } else { /* PassEnvironment */
394 if (!env_name_is_valid(word)) {
395 log_error("Invalid environment variable name: %s", word);
400 r = sd_bus_message_append_basic(m, 's', word);
402 return bus_log_create_error(r);
405 r = sd_bus_message_close_container(m);
407 return bus_log_create_error(r);
409 r = sd_bus_message_close_container(m);
411 } else if (streq(field, "KillSignal")) {
414 sig = signal_from_string_try_harder(eq);
416 log_error("Failed to parse %s value %s.", field, eq);
420 r = sd_bus_message_append(m, "v", "i", sig);
422 } else if (streq(field, "TimerSlackNSec")) {
425 r = parse_nsec(eq, &n);
427 log_error("Failed to parse %s value %s", field, eq);
431 r = sd_bus_message_append(m, "v", "t", n);
432 } else if (streq(field, "OOMScoreAdjust")) {
435 r = safe_atoi(eq, &oa);
437 log_error("Failed to parse %s value %s", field, eq);
441 if (!oom_score_adjust_is_valid(oa)) {
442 log_error("OOM score adjust value out of range");
446 r = sd_bus_message_append(m, "v", "i", oa);
447 } else if (STR_IN_SET(field, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories")) {
450 r = sd_bus_message_open_container(m, 'v', "as");
452 return bus_log_create_error(r);
454 r = sd_bus_message_open_container(m, 'a', "s");
456 return bus_log_create_error(r);
461 _cleanup_free_ char *word = NULL;
464 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
466 log_error("Failed to parse %s value %s", field, eq);
472 if (!utf8_is_valid(word)) {
473 log_error("Failed to parse %s value %s", field, eq);
477 offset = word[0] == '-';
478 if (!path_is_absolute(word + offset)) {
479 log_error("Failed to parse %s value %s", field, eq);
483 path_kill_slashes(word + offset);
485 r = sd_bus_message_append_basic(m, 's', word);
487 return bus_log_create_error(r);
490 r = sd_bus_message_close_container(m);
492 return bus_log_create_error(r);
494 r = sd_bus_message_close_container(m);
496 } else if (streq(field, "RuntimeDirectory")) {
499 r = sd_bus_message_open_container(m, 'v', "as");
501 return bus_log_create_error(r);
503 r = sd_bus_message_open_container(m, 'a', "s");
505 return bus_log_create_error(r);
510 _cleanup_free_ char *word = NULL;
512 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
514 return log_error_errno(r, "Failed to parse %s value %s", field, eq);
519 r = sd_bus_message_append_basic(m, 's', word);
521 return bus_log_create_error(r);
524 r = sd_bus_message_close_container(m);
526 return bus_log_create_error(r);
528 r = sd_bus_message_close_container(m);
531 log_error("Unknown assignment %s.", assignment);
537 return bus_log_create_error(r);
539 r = sd_bus_message_close_container(m);
541 return bus_log_create_error(r);
546 typedef struct BusWaitForJobs {
553 sd_bus_slot *slot_job_removed;
554 sd_bus_slot *slot_disconnected;
557 static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *error) {
560 log_error("Warning! D-Bus connection terminated.");
561 sd_bus_close(sd_bus_message_get_bus(m));
566 static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
567 const char *path, *unit, *result;
568 BusWaitForJobs *d = userdata;
576 r = sd_bus_message_read(m, "uoss", &id, &path, &unit, &result);
578 bus_log_parse_error(r);
582 found = set_remove(d->jobs, (char*) path);
588 if (!isempty(result))
589 d->result = strdup(result);
592 d->name = strdup(unit);
597 void bus_wait_for_jobs_free(BusWaitForJobs *d) {
601 set_free_free(d->jobs);
603 sd_bus_slot_unref(d->slot_disconnected);
604 sd_bus_slot_unref(d->slot_job_removed);
606 sd_bus_unref(d->bus);
614 int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret) {
615 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *d = NULL;
621 d = new0(BusWaitForJobs, 1);
625 d->bus = sd_bus_ref(bus);
627 /* When we are a bus client we match by sender. Direct
628 * connections OTOH have no initialized sender field, and
629 * hence we ignore the sender then */
630 r = sd_bus_add_match(
632 &d->slot_job_removed,
635 "sender='org.freedesktop.elogind1',"
636 "interface='org.freedesktop.elogind1.Manager',"
637 "member='JobRemoved',"
638 "path='/org/freedesktop/elogind1'" :
640 "interface='org.freedesktop.elogind1.Manager',"
641 "member='JobRemoved',"
642 "path='/org/freedesktop/elogind1'",
643 match_job_removed, d);
647 r = sd_bus_add_match(
649 &d->slot_disconnected,
651 "sender='org.freedesktop.DBus.Local',"
652 "interface='org.freedesktop.DBus.Local',"
653 "member='Disconnected'",
654 match_disconnected, d);
664 static int bus_process_wait(sd_bus *bus) {
668 r = sd_bus_process(bus, NULL);
674 r = sd_bus_wait(bus, (uint64_t) -1);
680 static int bus_job_get_service_result(BusWaitForJobs *d, char **result) {
681 _cleanup_free_ char *dbus_path = NULL;
687 dbus_path = unit_dbus_path_from_name(d->name);
691 return sd_bus_get_property_string(d->bus,
692 "org.freedesktop.elogind1",
694 "org.freedesktop.elogind1.Service",
700 static const struct {
701 const char *result, *explanation;
702 } explanations [] = {
703 { "resources", "a configured resource limit was exceeded" },
704 { "timeout", "a timeout was exceeded" },
705 { "exit-code", "the control process exited with error code" },
706 { "signal", "a fatal signal was delivered to the control process" },
707 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
708 { "watchdog", "the service failed to send watchdog ping" },
709 { "start-limit", "start of the service was attempted too often" }
712 static void log_job_error_with_service_result(const char* service, const char *result, const char* const* extra_args) {
713 _cleanup_free_ char *service_shell_quoted = NULL;
714 const char *systemctl = "systemctl", *journalctl = "journalctl";
718 service_shell_quoted = shell_maybe_quote(service);
720 if (extra_args && extra_args[1]) {
721 _cleanup_free_ char *t;
723 t = strv_join((char**) extra_args, " ");
724 systemctl = strjoina("systemctl ", t ? : "<args>");
725 journalctl = strjoina("journalctl ", t ? : "<args>");
728 if (!isempty(result)) {
731 for (i = 0; i < ELEMENTSOF(explanations); ++i)
732 if (streq(result, explanations[i].result))
735 if (i < ELEMENTSOF(explanations)) {
736 log_error("Job for %s failed because %s.\n"
737 "See \"%s status %s\" and \"%s -xe\" for details.\n",
739 explanations[i].explanation,
741 service_shell_quoted ?: "<service>",
747 log_error("Job for %s failed.\n"
748 "See \"%s status %s\" and \"%s -xe\" for details.\n",
751 service_shell_quoted ?: "<service>",
755 /* For some results maybe additional explanation is required */
756 if (streq_ptr(result, "start-limit"))
757 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
758 "followed by \"%1$s start %2$s\" again.",
760 service_shell_quoted ?: "<service>");
763 static int check_wait_response(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
769 if (streq(d->result, "canceled"))
770 log_error("Job for %s canceled.", strna(d->name));
771 else if (streq(d->result, "timeout"))
772 log_error("Job for %s timed out.", strna(d->name));
773 else if (streq(d->result, "dependency"))
774 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d->name));
775 else if (streq(d->result, "invalid"))
776 log_error("%s is not active, cannot reload.", strna(d->name));
777 else if (streq(d->result, "assert"))
778 log_error("Assertion failed on job for %s.", strna(d->name));
779 else if (streq(d->result, "unsupported"))
780 log_error("Operation on or unit type of %s not supported on this system.", strna(d->name));
781 else if (!streq(d->result, "done") && !streq(d->result, "skipped")) {
784 _cleanup_free_ char *result = NULL;
786 q = bus_job_get_service_result(d, &result);
788 log_debug_errno(q, "Failed to get Result property of service %s: %m", d->name);
790 log_job_error_with_service_result(d->name, result, extra_args);
792 log_error("Job failed. See \"journalctl -xe\" for details.");
796 if (streq(d->result, "canceled"))
798 else if (streq(d->result, "timeout"))
800 else if (streq(d->result, "dependency"))
802 else if (streq(d->result, "invalid"))
804 else if (streq(d->result, "assert"))
806 else if (streq(d->result, "unsupported"))
808 else if (!streq(d->result, "done") && !streq(d->result, "skipped"))
814 int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
819 while (!set_isempty(d->jobs)) {
822 q = bus_process_wait(d->bus);
824 return log_error_errno(q, "Failed to wait for response: %m");
827 q = check_wait_response(d, quiet, extra_args);
828 /* Return the first error as it is most likely to be
833 log_debug_errno(q, "Got result %s/%m for job %s", strna(d->result), strna(d->name));
836 d->name = mfree(d->name);
837 d->result = mfree(d->result);
843 int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
848 r = set_ensure_allocated(&d->jobs, &string_hash_ops);
852 return set_put_strdup(d->jobs, path);
855 int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
858 r = bus_wait_for_jobs_add(d, path);
862 return bus_wait_for_jobs(d, quiet, NULL);
865 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, unsigned *n_changes) {
866 const char *type, *path, *source;
869 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
871 return bus_log_parse_error(r);
873 while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
874 /* We expect only "success" changes to be sent over the bus.
875 Hence, reject anything negative. */
876 UnitFileChangeType ch = unit_file_change_type_from_string(type);
879 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type, path);
883 r = unit_file_changes_add(changes, n_changes, ch, path, source);
888 return bus_log_parse_error(r);
890 r = sd_bus_message_exit_container(m);
892 return bus_log_parse_error(r);
894 unit_file_dump_changes(0, NULL, *changes, *n_changes, false);
900 bool is_const; /* If false, cgroup_path should be free()'d */
902 Hashmap *pids; /* PID → process name */
905 struct CGroupInfo *parent;
906 LIST_FIELDS(struct CGroupInfo, siblings);
907 LIST_HEAD(struct CGroupInfo, children);
911 static bool IS_ROOT(const char *p) {
912 return isempty(p) || streq(p, "/");
915 static int add_cgroup(Hashmap *cgroups, const char *path, bool is_const, struct CGroupInfo **ret) {
916 struct CGroupInfo *parent = NULL, *cg;
925 cg = hashmap_get(cgroups, path);
931 if (!IS_ROOT(path)) {
934 e = strrchr(path, '/');
938 pp = strndupa(path, e - path);
942 r = add_cgroup(cgroups, pp, false, &parent);
947 cg = new0(struct CGroupInfo, 1);
952 cg->cgroup_path = (char*) path;
954 cg->cgroup_path = strdup(path);
955 if (!cg->cgroup_path) {
961 cg->is_const = is_const;
964 r = hashmap_put(cgroups, cg->cgroup_path, cg);
967 free(cg->cgroup_path);
973 LIST_PREPEND(siblings, parent->children, cg);
974 parent->n_children++;
981 static int add_process(
987 struct CGroupInfo *cg;
994 r = add_cgroup(cgroups, path, true, &cg);
998 r = hashmap_ensure_allocated(&cg->pids, &trivial_hash_ops);
1002 return hashmap_put(cg->pids, PID_TO_PTR(pid), (void*) name);
1005 static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
1009 while (cg->children)
1010 remove_cgroup(cgroups, cg->children);
1012 hashmap_remove(cgroups, cg->cgroup_path);
1015 free(cg->cgroup_path);
1017 hashmap_free(cg->pids);
1020 LIST_REMOVE(siblings, cg->parent->children, cg);
1025 static int cgroup_info_compare_func(const void *a, const void *b) {
1026 const struct CGroupInfo *x = *(const struct CGroupInfo* const*) a, *y = *(const struct CGroupInfo* const*) b;
1031 return strcmp(x->cgroup_path, y->cgroup_path);
1034 static int dump_processes(
1036 const char *cgroup_path,
1039 OutputFlags flags) {
1041 struct CGroupInfo *cg;
1046 if (IS_ROOT(cgroup_path))
1049 cg = hashmap_get(cgroups, cgroup_path);
1053 if (!hashmap_isempty(cg->pids)) {
1061 /* Order processes by their PID */
1062 pids = newa(pid_t, hashmap_size(cg->pids));
1064 HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j)
1065 pids[n++] = PTR_TO_PID(pidp);
1067 assert(n == hashmap_size(cg->pids));
1068 qsort_safe(pids, n, sizeof(pid_t), pid_compare_func);
1070 width = DECIMAL_STR_WIDTH(pids[n-1]);
1072 for (i = 0; i < n; i++) {
1073 _cleanup_free_ char *e = NULL;
1074 const char *special;
1077 name = hashmap_get(cg->pids, PID_TO_PTR(pids[i]));
1080 if (n_columns != 0) {
1083 k = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
1085 e = ellipsize(name, k, 100);
1090 more = i+1 < n || cg->children;
1091 special = draw_special_char(more ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT);
1093 fprintf(stdout, "%s%s%*"PID_PRI" %s\n",
1102 struct CGroupInfo **children, *child;
1105 /* Order subcgroups by their name */
1106 children = newa(struct CGroupInfo*, cg->n_children);
1107 LIST_FOREACH(siblings, child, cg->children)
1108 children[n++] = child;
1109 assert(n == cg->n_children);
1110 qsort_safe(children, n, sizeof(struct CGroupInfo*), cgroup_info_compare_func);
1112 n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
1114 for (i = 0; i < n; i++) {
1115 _cleanup_free_ char *pp = NULL;
1116 const char *name, *special;
1119 child = children[i];
1121 name = strrchr(child->cgroup_path, '/');
1127 special = draw_special_char(more ? DRAW_TREE_BRANCH : DRAW_TREE_RIGHT);
1129 fputs(prefix, stdout);
1130 fputs(special, stdout);
1131 fputs(name, stdout);
1132 fputc('\n', stdout);
1134 special = draw_special_char(more ? DRAW_TREE_VERTICAL : DRAW_TREE_SPACE);
1136 pp = strappend(prefix, special);
1140 r = dump_processes(cgroups, child->cgroup_path, pp, n_columns, flags);
1150 static int dump_extra_processes(
1154 OutputFlags flags) {
1156 _cleanup_free_ pid_t *pids = NULL;
1157 _cleanup_hashmap_free_ Hashmap *names = NULL;
1158 struct CGroupInfo *cg;
1159 size_t n_allocated = 0, n = 0, k;
1163 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
1164 * combined, sorted, linear list. */
1166 HASHMAP_FOREACH(cg, cgroups, i) {
1174 if (hashmap_isempty(cg->pids))
1177 r = hashmap_ensure_allocated(&names, &trivial_hash_ops);
1181 if (!GREEDY_REALLOC(pids, n_allocated, n + hashmap_size(cg->pids)))
1184 HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j) {
1185 pids[n++] = PTR_TO_PID(pidp);
1187 r = hashmap_put(names, pidp, (void*) name);
1196 qsort_safe(pids, n, sizeof(pid_t), pid_compare_func);
1197 width = DECIMAL_STR_WIDTH(pids[n-1]);
1199 for (k = 0; k < n; k++) {
1200 _cleanup_free_ char *e = NULL;
1203 name = hashmap_get(names, PID_TO_PTR(pids[k]));
1206 if (n_columns != 0) {
1209 z = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
1211 e = ellipsize(name, z, 100);
1216 fprintf(stdout, "%s%s %*" PID_PRI " %s\n",
1218 draw_special_char(DRAW_TRIANGULAR_BULLET),
1226 int unit_show_processes(
1229 const char *cgroup_path,
1233 sd_bus_error *error) {
1235 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1236 Hashmap *cgroups = NULL;
1237 struct CGroupInfo *cg;
1243 if (flags & OUTPUT_FULL_WIDTH)
1245 else if (n_columns <= 0)
1246 n_columns = columns();
1248 prefix = strempty(prefix);
1250 r = sd_bus_call_method(
1252 "org.freedesktop.elogind1",
1253 "/org/freedesktop/elogind1",
1254 "org.freedesktop.elogind1.Manager",
1263 cgroups = hashmap_new(&string_hash_ops);
1267 r = sd_bus_message_enter_container(reply, 'a', "(sus)");
1272 const char *path = NULL, *name = NULL;
1275 r = sd_bus_message_read(reply, "(sus)", &path, &pid, &name);
1281 r = add_process(cgroups, path, pid, name);
1286 r = sd_bus_message_exit_container(reply);
1290 r = dump_processes(cgroups, cgroup_path, prefix, n_columns, flags);
1294 r = dump_extra_processes(cgroups, prefix, n_columns, flags);
1297 while ((cg = hashmap_first(cgroups)))
1298 remove_cgroup(cgroups, cg);
1300 hashmap_free(cgroups);