2 This file is part of systemd.
4 Copyright 2016 Lennart Poettering
6 systemd 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 systemd 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 systemd; 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
);
87 r
= parse_percent(eq
);
89 log_error_errno(r
, "CPU quota '%s' invalid.", eq
);
93 r
= sd_bus_message_append(m
, "sv", "CPUQuotaPerSecUSec", "t", (usec_t
) r
* USEC_PER_SEC
/ 100U);
98 } else if (streq(field
, "EnvironmentFile")) {
100 r
= sd_bus_message_append(m
, "sv", "EnvironmentFiles", "a(sb)", 1,
101 eq
[0] == '-' ? eq
+ 1 : eq
,
105 } else if (STR_IN_SET(field
, "AccuracySec", "RandomizedDelaySec", "RuntimeMaxSec")) {
110 r
= parse_sec(eq
, &t
);
112 return log_error_errno(r
, "Failed to parse %s= parameter: %s", field
, eq
);
115 n
= newa(char, l
+ 2);
119 /* Change suffix Sec → USec */
120 strcpy(mempcpy(n
, field
, l
- 3), "USec");
121 r
= sd_bus_message_append(m
, "sv", n
, "t", t
);
125 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
127 return bus_log_create_error(r
);
129 rl
= rlimit_from_string(field
);
134 r
= rlimit_parse(rl
, eq
, &l
);
136 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
);
138 r
= sd_bus_message_append(m
, "v", "t", l
.rlim_max
);
140 return bus_log_create_error(r
);
142 r
= sd_bus_message_close_container(m
);
144 return bus_log_create_error(r
);
146 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
148 return bus_log_create_error(r
);
150 sn
= strjoina(field
, "Soft");
151 r
= sd_bus_message_append(m
, "sv", sn
, "t", l
.rlim_cur
);
153 } else if (STR_IN_SET(field
,
154 "CPUAccounting", "MemoryAccounting", "IOAccounting", "BlockIOAccounting", "TasksAccounting",
155 "SendSIGHUP", "SendSIGKILL", "WakeSystem", "DefaultDependencies",
156 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "RemainAfterExit",
157 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "NoNewPrivileges",
158 "SyslogLevelPrefix", "Delegate", "RemainAfterElapse", "MemoryDenyWriteExecute")) {
160 r
= parse_boolean(eq
);
162 return log_error_errno(r
, "Failed to parse boolean assignment %s.", assignment
);
164 r
= sd_bus_message_append(m
, "v", "b", r
);
166 } else if (STR_IN_SET(field
, "MemoryLow", "MemoryHigh", "MemoryMax", "MemoryLimit")) {
169 if (isempty(eq
) || streq(eq
, "infinity"))
170 bytes
= CGROUP_LIMIT_MAX
;
172 r
= parse_size(eq
, 1024, &bytes
);
174 log_error("Failed to parse bytes specification %s", assignment
);
179 r
= sd_bus_message_append(m
, "v", "t", bytes
);
181 } else if (streq(field
, "TasksMax")) {
184 if (isempty(eq
) || streq(eq
, "infinity"))
187 r
= safe_atou64(eq
, &n
);
189 log_error("Failed to parse maximum tasks specification %s", assignment
);
194 r
= sd_bus_message_append(m
, "v", "t", n
);
196 } else if (STR_IN_SET(field
, "CPUShares", "StartupCPUShares")) {
199 r
= cg_cpu_shares_parse(eq
, &u
);
201 log_error("Failed to parse %s value %s.", field
, eq
);
205 r
= sd_bus_message_append(m
, "v", "t", u
);
207 } else if (STR_IN_SET(field
, "IOWeight", "StartupIOWeight")) {
210 r
= cg_weight_parse(eq
, &u
);
212 log_error("Failed to parse %s value %s.", field
, eq
);
216 r
= sd_bus_message_append(m
, "v", "t", u
);
218 } else if (STR_IN_SET(field
, "BlockIOWeight", "StartupBlockIOWeight")) {
221 r
= cg_blkio_weight_parse(eq
, &u
);
223 log_error("Failed to parse %s value %s.", field
, eq
);
227 r
= sd_bus_message_append(m
, "v", "t", u
);
229 } else if (STR_IN_SET(field
,
230 "User", "Group", "DevicePolicy", "KillMode",
231 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
232 "StandardInput", "StandardOutput", "StandardError",
233 "Description", "Slice", "Type", "WorkingDirectory",
234 "RootDirectory", "SyslogIdentifier", "ProtectSystem",
235 "ProtectHome", "SELinuxContext"))
236 r
= sd_bus_message_append(m
, "v", "s", eq
);
238 else if (streq(field
, "SyslogLevel")) {
241 level
= log_level_from_string(eq
);
243 log_error("Failed to parse %s value %s.", field
, eq
);
247 r
= sd_bus_message_append(m
, "v", "i", level
);
249 } else if (streq(field
, "SyslogFacility")) {
252 facility
= log_facility_unshifted_from_string(eq
);
254 log_error("Failed to parse %s value %s.", field
, eq
);
258 r
= sd_bus_message_append(m
, "v", "i", facility
);
260 } else if (streq(field
, "DeviceAllow")) {
263 r
= sd_bus_message_append(m
, "v", "a(ss)", 0);
265 const char *path
, *rwm
, *e
;
269 path
= strndupa(eq
, e
- eq
);
276 if (!path_startswith(path
, "/dev")) {
277 log_error("%s is not a device file in /dev.", path
);
281 r
= sd_bus_message_append(m
, "v", "a(ss)", 1, path
, rwm
);
284 } else if (cgroup_io_limit_type_from_string(field
) >= 0 || STR_IN_SET(field
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
287 r
= sd_bus_message_append(m
, "v", "a(st)", 0);
289 const char *path
, *bandwidth
, *e
;
294 path
= strndupa(eq
, e
- eq
);
297 log_error("Failed to parse %s value %s.", field
, eq
);
301 if (!path_startswith(path
, "/dev")) {
302 log_error("%s is not a device file in /dev.", path
);
306 if (streq(bandwidth
, "infinity")) {
307 bytes
= CGROUP_LIMIT_MAX
;
309 r
= parse_size(bandwidth
, 1000, &bytes
);
311 log_error("Failed to parse byte value %s.", bandwidth
);
316 r
= sd_bus_message_append(m
, "v", "a(st)", 1, path
, bytes
);
319 } else if (STR_IN_SET(field
, "IODeviceWeight", "BlockIODeviceWeight")) {
322 r
= sd_bus_message_append(m
, "v", "a(st)", 0);
324 const char *path
, *weight
, *e
;
329 path
= strndupa(eq
, e
- eq
);
332 log_error("Failed to parse %s value %s.", field
, eq
);
336 if (!path_startswith(path
, "/dev")) {
337 log_error("%s is not a device file in /dev.", path
);
341 r
= safe_atou64(weight
, &u
);
343 log_error("Failed to parse %s value %s.", field
, weight
);
346 r
= sd_bus_message_append(m
, "v", "a(st)", 1, path
, u
);
349 } else if (streq(field
, "Nice")) {
352 r
= safe_atoi32(eq
, &i
);
354 log_error("Failed to parse %s value %s.", field
, eq
);
358 r
= sd_bus_message_append(m
, "v", "i", i
);
360 } else if (STR_IN_SET(field
, "Environment", "PassEnvironment")) {
363 r
= sd_bus_message_open_container(m
, 'v', "as");
365 return bus_log_create_error(r
);
367 r
= sd_bus_message_open_container(m
, 'a', "s");
369 return bus_log_create_error(r
);
374 _cleanup_free_
char *word
= NULL
;
376 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
378 log_error("Failed to parse Environment value %s", eq
);
384 if (streq(field
, "Environment")) {
385 if (!env_assignment_is_valid(word
)) {
386 log_error("Invalid environment assignment: %s", word
);
389 } else { /* PassEnvironment */
390 if (!env_name_is_valid(word
)) {
391 log_error("Invalid environment variable name: %s", word
);
396 r
= sd_bus_message_append_basic(m
, 's', word
);
398 return bus_log_create_error(r
);
401 r
= sd_bus_message_close_container(m
);
403 return bus_log_create_error(r
);
405 r
= sd_bus_message_close_container(m
);
407 } else if (streq(field
, "KillSignal")) {
410 sig
= signal_from_string_try_harder(eq
);
412 log_error("Failed to parse %s value %s.", field
, eq
);
416 r
= sd_bus_message_append(m
, "v", "i", sig
);
418 } else if (streq(field
, "TimerSlackNSec")) {
421 r
= parse_nsec(eq
, &n
);
423 log_error("Failed to parse %s value %s", field
, eq
);
427 r
= sd_bus_message_append(m
, "v", "t", n
);
428 } else if (streq(field
, "OOMScoreAdjust")) {
431 r
= safe_atoi(eq
, &oa
);
433 log_error("Failed to parse %s value %s", field
, eq
);
437 if (!oom_score_adjust_is_valid(oa
)) {
438 log_error("OOM score adjust value out of range");
442 r
= sd_bus_message_append(m
, "v", "i", oa
);
443 } else if (STR_IN_SET(field
, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories")) {
446 r
= sd_bus_message_open_container(m
, 'v', "as");
448 return bus_log_create_error(r
);
450 r
= sd_bus_message_open_container(m
, 'a', "s");
452 return bus_log_create_error(r
);
457 _cleanup_free_
char *word
= NULL
;
460 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
462 log_error("Failed to parse %s value %s", field
, eq
);
468 if (!utf8_is_valid(word
)) {
469 log_error("Failed to parse %s value %s", field
, eq
);
473 offset
= word
[0] == '-';
474 if (!path_is_absolute(word
+ offset
)) {
475 log_error("Failed to parse %s value %s", field
, eq
);
479 path_kill_slashes(word
+ offset
);
481 r
= sd_bus_message_append_basic(m
, 's', word
);
483 return bus_log_create_error(r
);
486 r
= sd_bus_message_close_container(m
);
488 return bus_log_create_error(r
);
490 r
= sd_bus_message_close_container(m
);
492 } else if (streq(field
, "RuntimeDirectory")) {
495 r
= sd_bus_message_open_container(m
, 'v', "as");
497 return bus_log_create_error(r
);
499 r
= sd_bus_message_open_container(m
, 'a', "s");
501 return bus_log_create_error(r
);
506 _cleanup_free_
char *word
= NULL
;
508 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
510 return log_error_errno(r
, "Failed to parse %s value %s", field
, eq
);
515 r
= sd_bus_message_append_basic(m
, 's', word
);
517 return bus_log_create_error(r
);
520 r
= sd_bus_message_close_container(m
);
522 return bus_log_create_error(r
);
524 r
= sd_bus_message_close_container(m
);
527 log_error("Unknown assignment %s.", assignment
);
533 return bus_log_create_error(r
);
535 r
= sd_bus_message_close_container(m
);
537 return bus_log_create_error(r
);
542 typedef struct BusWaitForJobs
{
549 sd_bus_slot
*slot_job_removed
;
550 sd_bus_slot
*slot_disconnected
;
553 static int match_disconnected(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
556 log_error("Warning! D-Bus connection terminated.");
557 sd_bus_close(sd_bus_message_get_bus(m
));
562 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
563 const char *path
, *unit
, *result
;
564 BusWaitForJobs
*d
= userdata
;
572 r
= sd_bus_message_read(m
, "uoss", &id
, &path
, &unit
, &result
);
574 bus_log_parse_error(r
);
578 found
= set_remove(d
->jobs
, (char*) path
);
584 if (!isempty(result
))
585 d
->result
= strdup(result
);
588 d
->name
= strdup(unit
);
593 void bus_wait_for_jobs_free(BusWaitForJobs
*d
) {
597 set_free_free(d
->jobs
);
599 sd_bus_slot_unref(d
->slot_disconnected
);
600 sd_bus_slot_unref(d
->slot_job_removed
);
602 sd_bus_unref(d
->bus
);
610 int bus_wait_for_jobs_new(sd_bus
*bus
, BusWaitForJobs
**ret
) {
611 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*d
= NULL
;
617 d
= new0(BusWaitForJobs
, 1);
621 d
->bus
= sd_bus_ref(bus
);
623 /* When we are a bus client we match by sender. Direct
624 * connections OTOH have no initialized sender field, and
625 * hence we ignore the sender then */
626 r
= sd_bus_add_match(
628 &d
->slot_job_removed
,
631 "sender='org.freedesktop.systemd1',"
632 "interface='org.freedesktop.systemd1.Manager',"
633 "member='JobRemoved',"
634 "path='/org/freedesktop/systemd1'" :
636 "interface='org.freedesktop.systemd1.Manager',"
637 "member='JobRemoved',"
638 "path='/org/freedesktop/systemd1'",
639 match_job_removed
, d
);
643 r
= sd_bus_add_match(
645 &d
->slot_disconnected
,
647 "sender='org.freedesktop.DBus.Local',"
648 "interface='org.freedesktop.DBus.Local',"
649 "member='Disconnected'",
650 match_disconnected
, d
);
660 static int bus_process_wait(sd_bus
*bus
) {
664 r
= sd_bus_process(bus
, NULL
);
670 r
= sd_bus_wait(bus
, (uint64_t) -1);
676 static int bus_job_get_service_result(BusWaitForJobs
*d
, char **result
) {
677 _cleanup_free_
char *dbus_path
= NULL
;
683 dbus_path
= unit_dbus_path_from_name(d
->name
);
687 return sd_bus_get_property_string(d
->bus
,
688 "org.freedesktop.systemd1",
690 "org.freedesktop.systemd1.Service",
696 static const struct {
697 const char *result
, *explanation
;
698 } explanations
[] = {
699 { "resources", "of unavailable resources or another system error" },
700 { "timeout", "a timeout was exceeded" },
701 { "exit-code", "the control process exited with error code" },
702 { "signal", "a fatal signal was delivered to the control process" },
703 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
704 { "watchdog", "the service failed to send watchdog ping" },
705 { "start-limit", "start of the service was attempted too often" }
708 static void log_job_error_with_service_result(const char* service
, const char *result
, const char* const* extra_args
) {
709 _cleanup_free_
char *service_shell_quoted
= NULL
;
710 const char *systemctl
= "systemctl", *journalctl
= "journalctl";
714 service_shell_quoted
= shell_maybe_quote(service
);
716 if (extra_args
&& extra_args
[1]) {
717 _cleanup_free_
char *t
;
719 t
= strv_join((char**) extra_args
, " ");
720 systemctl
= strjoina("systemctl ", t
? : "<args>");
721 journalctl
= strjoina("journalctl ", t
? : "<args>");
724 if (!isempty(result
)) {
727 for (i
= 0; i
< ELEMENTSOF(explanations
); ++i
)
728 if (streq(result
, explanations
[i
].result
))
731 if (i
< ELEMENTSOF(explanations
)) {
732 log_error("Job for %s failed because %s.\n"
733 "See \"%s status %s\" and \"%s -xe\" for details.\n",
735 explanations
[i
].explanation
,
737 service_shell_quoted
?: "<service>",
743 log_error("Job for %s failed.\n"
744 "See \"%s status %s\" and \"%s -xe\" for details.\n",
747 service_shell_quoted
?: "<service>",
751 /* For some results maybe additional explanation is required */
752 if (streq_ptr(result
, "start-limit"))
753 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
754 "followed by \"%1$s start %2$s\" again.",
756 service_shell_quoted
?: "<service>");
759 static int check_wait_response(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
765 if (streq(d
->result
, "canceled"))
766 log_error("Job for %s canceled.", strna(d
->name
));
767 else if (streq(d
->result
, "timeout"))
768 log_error("Job for %s timed out.", strna(d
->name
));
769 else if (streq(d
->result
, "dependency"))
770 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d
->name
));
771 else if (streq(d
->result
, "invalid"))
772 log_error("%s is not active, cannot reload.", strna(d
->name
));
773 else if (streq(d
->result
, "assert"))
774 log_error("Assertion failed on job for %s.", strna(d
->name
));
775 else if (streq(d
->result
, "unsupported"))
776 log_error("Operation on or unit type of %s not supported on this system.", strna(d
->name
));
777 else if (!streq(d
->result
, "done") && !streq(d
->result
, "skipped")) {
780 _cleanup_free_
char *result
= NULL
;
782 q
= bus_job_get_service_result(d
, &result
);
784 log_debug_errno(q
, "Failed to get Result property of service %s: %m", d
->name
);
786 log_job_error_with_service_result(d
->name
, result
, extra_args
);
788 log_error("Job failed. See \"journalctl -xe\" for details.");
792 if (streq(d
->result
, "canceled"))
794 else if (streq(d
->result
, "timeout"))
796 else if (streq(d
->result
, "dependency"))
798 else if (streq(d
->result
, "invalid"))
800 else if (streq(d
->result
, "assert"))
802 else if (streq(d
->result
, "unsupported"))
804 else if (!streq(d
->result
, "done") && !streq(d
->result
, "skipped"))
810 int bus_wait_for_jobs(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
815 while (!set_isempty(d
->jobs
)) {
818 q
= bus_process_wait(d
->bus
);
820 return log_error_errno(q
, "Failed to wait for response: %m");
823 q
= check_wait_response(d
, quiet
, extra_args
);
824 /* Return the first error as it is most likely to be
829 log_debug_errno(q
, "Got result %s/%m for job %s", strna(d
->result
), strna(d
->name
));
832 d
->name
= mfree(d
->name
);
833 d
->result
= mfree(d
->result
);
839 int bus_wait_for_jobs_add(BusWaitForJobs
*d
, const char *path
) {
844 r
= set_ensure_allocated(&d
->jobs
, &string_hash_ops
);
848 return set_put_strdup(d
->jobs
, path
);
851 int bus_wait_for_jobs_one(BusWaitForJobs
*d
, const char *path
, bool quiet
) {
854 r
= bus_wait_for_jobs_add(d
, path
);
858 return bus_wait_for_jobs(d
, quiet
, NULL
);
861 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message
*m
, bool quiet
, UnitFileChange
**changes
, unsigned *n_changes
) {
862 const char *type
, *path
, *source
;
865 /* changes is dereferenced when calling unit_file_dump_changes() later,
866 * so we have to make sure this is not NULL. */
870 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sss)");
872 return bus_log_parse_error(r
);
874 while ((r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
)) > 0) {
875 /* We expect only "success" changes to be sent over the bus.
876 Hence, reject anything negative. */
877 UnitFileChangeType ch
= unit_file_change_type_from_string(type
);
880 log_notice("Manager reported unknown change type \"%s\" for path \"%s\", ignoring.", type
, path
);
884 r
= unit_file_changes_add(changes
, n_changes
, ch
, path
, source
);
889 return bus_log_parse_error(r
);
891 r
= sd_bus_message_exit_container(m
);
893 return bus_log_parse_error(r
);
895 unit_file_dump_changes(0, NULL
, *changes
, *n_changes
, false);
901 bool is_const
; /* If false, cgroup_path should be free()'d */
903 Hashmap
*pids
; /* PID → process name */
906 struct CGroupInfo
*parent
;
907 LIST_FIELDS(struct CGroupInfo
, siblings
);
908 LIST_HEAD(struct CGroupInfo
, children
);
912 static bool IS_ROOT(const char *p
) {
913 return isempty(p
) || streq(p
, "/");
916 static int add_cgroup(Hashmap
*cgroups
, const char *path
, bool is_const
, struct CGroupInfo
**ret
) {
917 struct CGroupInfo
*parent
= NULL
, *cg
;
926 cg
= hashmap_get(cgroups
, path
);
932 if (!IS_ROOT(path
)) {
935 e
= strrchr(path
, '/');
939 pp
= strndupa(path
, e
- path
);
943 r
= add_cgroup(cgroups
, pp
, false, &parent
);
948 cg
= new0(struct CGroupInfo
, 1);
953 cg
->cgroup_path
= (char*) path
;
955 cg
->cgroup_path
= strdup(path
);
956 if (!cg
->cgroup_path
) {
962 cg
->is_const
= is_const
;
965 r
= hashmap_put(cgroups
, cg
->cgroup_path
, cg
);
968 free(cg
->cgroup_path
);
974 LIST_PREPEND(siblings
, parent
->children
, cg
);
975 parent
->n_children
++;
982 static int add_process(
988 struct CGroupInfo
*cg
;
995 r
= add_cgroup(cgroups
, path
, true, &cg
);
999 r
= hashmap_ensure_allocated(&cg
->pids
, &trivial_hash_ops
);
1003 return hashmap_put(cg
->pids
, PID_TO_PTR(pid
), (void*) name
);
1006 static void remove_cgroup(Hashmap
*cgroups
, struct CGroupInfo
*cg
) {
1010 while (cg
->children
)
1011 remove_cgroup(cgroups
, cg
->children
);
1013 hashmap_remove(cgroups
, cg
->cgroup_path
);
1016 free(cg
->cgroup_path
);
1018 hashmap_free(cg
->pids
);
1021 LIST_REMOVE(siblings
, cg
->parent
->children
, cg
);
1026 static int cgroup_info_compare_func(const void *a
, const void *b
) {
1027 const struct CGroupInfo
*x
= *(const struct CGroupInfo
* const*) a
, *y
= *(const struct CGroupInfo
* const*) b
;
1032 return strcmp(x
->cgroup_path
, y
->cgroup_path
);
1035 static int dump_processes(
1037 const char *cgroup_path
,
1040 OutputFlags flags
) {
1042 struct CGroupInfo
*cg
;
1047 if (IS_ROOT(cgroup_path
))
1050 cg
= hashmap_get(cgroups
, cgroup_path
);
1054 if (!hashmap_isempty(cg
->pids
)) {
1062 /* Order processes by their PID */
1063 pids
= newa(pid_t
, hashmap_size(cg
->pids
));
1065 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
)
1066 pids
[n
++] = PTR_TO_PID(pidp
);
1068 assert(n
== hashmap_size(cg
->pids
));
1069 qsort_safe(pids
, n
, sizeof(pid_t
), pid_compare_func
);
1071 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
1073 for (i
= 0; i
< n
; i
++) {
1074 _cleanup_free_
char *e
= NULL
;
1075 const char *special
;
1078 name
= hashmap_get(cg
->pids
, PID_TO_PTR(pids
[i
]));
1081 if (n_columns
!= 0) {
1084 k
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
1086 e
= ellipsize(name
, k
, 100);
1091 more
= i
+1 < n
|| cg
->children
;
1092 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
1094 fprintf(stdout
, "%s%s%*"PID_PRI
" %s\n",
1103 struct CGroupInfo
**children
, *child
;
1106 /* Order subcgroups by their name */
1107 children
= newa(struct CGroupInfo
*, cg
->n_children
);
1108 LIST_FOREACH(siblings
, child
, cg
->children
)
1109 children
[n
++] = child
;
1110 assert(n
== cg
->n_children
);
1111 qsort_safe(children
, n
, sizeof(struct CGroupInfo
*), cgroup_info_compare_func
);
1113 n_columns
= MAX(LESS_BY(n_columns
, 2U), 20U);
1115 for (i
= 0; i
< n
; i
++) {
1116 _cleanup_free_
char *pp
= NULL
;
1117 const char *name
, *special
;
1120 child
= children
[i
];
1122 name
= strrchr(child
->cgroup_path
, '/');
1128 special
= special_glyph(more
? TREE_BRANCH
: TREE_RIGHT
);
1130 fputs(prefix
, stdout
);
1131 fputs(special
, stdout
);
1132 fputs(name
, stdout
);
1133 fputc('\n', stdout
);
1135 special
= special_glyph(more
? TREE_VERTICAL
: TREE_SPACE
);
1137 pp
= strappend(prefix
, special
);
1141 r
= dump_processes(cgroups
, child
->cgroup_path
, pp
, n_columns
, flags
);
1151 static int dump_extra_processes(
1155 OutputFlags flags
) {
1157 _cleanup_free_ pid_t
*pids
= NULL
;
1158 _cleanup_hashmap_free_ Hashmap
*names
= NULL
;
1159 struct CGroupInfo
*cg
;
1160 size_t n_allocated
= 0, n
= 0, k
;
1164 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
1165 * combined, sorted, linear list. */
1167 HASHMAP_FOREACH(cg
, cgroups
, i
) {
1175 if (hashmap_isempty(cg
->pids
))
1178 r
= hashmap_ensure_allocated(&names
, &trivial_hash_ops
);
1182 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ hashmap_size(cg
->pids
)))
1185 HASHMAP_FOREACH_KEY(name
, pidp
, cg
->pids
, j
) {
1186 pids
[n
++] = PTR_TO_PID(pidp
);
1188 r
= hashmap_put(names
, pidp
, (void*) name
);
1197 qsort_safe(pids
, n
, sizeof(pid_t
), pid_compare_func
);
1198 width
= DECIMAL_STR_WIDTH(pids
[n
-1]);
1200 for (k
= 0; k
< n
; k
++) {
1201 _cleanup_free_
char *e
= NULL
;
1204 name
= hashmap_get(names
, PID_TO_PTR(pids
[k
]));
1207 if (n_columns
!= 0) {
1210 z
= MAX(LESS_BY(n_columns
, 2U + width
+ 1U), 20U);
1212 e
= ellipsize(name
, z
, 100);
1217 fprintf(stdout
, "%s%s %*" PID_PRI
" %s\n",
1219 special_glyph(TRIANGULAR_BULLET
),
1227 int unit_show_processes(
1230 const char *cgroup_path
,
1234 sd_bus_error
*error
) {
1236 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1237 Hashmap
*cgroups
= NULL
;
1238 struct CGroupInfo
*cg
;
1244 if (flags
& OUTPUT_FULL_WIDTH
)
1246 else if (n_columns
<= 0)
1247 n_columns
= columns();
1249 prefix
= strempty(prefix
);
1251 r
= sd_bus_call_method(
1253 "org.freedesktop.systemd1",
1254 "/org/freedesktop/systemd1",
1255 "org.freedesktop.systemd1.Manager",
1264 cgroups
= hashmap_new(&string_hash_ops
);
1268 r
= sd_bus_message_enter_container(reply
, 'a', "(sus)");
1273 const char *path
= NULL
, *name
= NULL
;
1276 r
= sd_bus_message_read(reply
, "(sus)", &path
, &pid
, &name
);
1282 r
= add_process(cgroups
, path
, pid
, name
);
1287 r
= sd_bus_message_exit_container(reply
);
1291 r
= dump_processes(cgroups
, cgroup_path
, prefix
, n_columns
, flags
);
1295 r
= dump_extra_processes(cgroups
, prefix
, n_columns
, flags
);
1298 while ((cg
= hashmap_first(cgroups
)))
1299 remove_cgroup(cgroups
, cg
);
1301 hashmap_free(cgroups
);