2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2010-07-08 21:01:42 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <stddef.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdio.h>
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <stdlib.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <string.h>
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2017-02-01 19:25:27 +01:00
|
|
|
#include "bus-error.h"
|
|
|
|
#include "bus-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "cgroup-show.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "cgroup-util.h"
|
2018-11-30 22:08:41 +01:00
|
|
|
#include "env-file.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-26 23:01:30 +01:00
|
|
|
#include "locale-util.h"
|
2010-07-08 06:08:32 +02:00
|
|
|
#include "macro.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "output-mode.h"
|
2012-05-07 21:36:12 +02:00
|
|
|
#include "path-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "process-util.h"
|
2019-03-13 12:14:47 +01:00
|
|
|
#include "sort-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-04-10 23:15:59 +02:00
|
|
|
#include "terminal-util.h"
|
2017-02-01 19:25:27 +01:00
|
|
|
#include "unit-name.h"
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
static void show_pid_array(
|
|
|
|
pid_t pids[],
|
|
|
|
unsigned n_pids,
|
|
|
|
const char *prefix,
|
|
|
|
unsigned n_columns,
|
|
|
|
bool extra,
|
|
|
|
bool more,
|
|
|
|
OutputFlags flags) {
|
|
|
|
|
2015-01-21 05:09:58 +01:00
|
|
|
unsigned i, j, pid_width;
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2015-01-23 02:03:58 +01:00
|
|
|
if (n_pids == 0)
|
2015-01-22 18:54:48 +01:00
|
|
|
return;
|
|
|
|
|
2018-09-18 01:39:24 +02:00
|
|
|
typesafe_qsort(pids, n_pids, pid_compare_func);
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2015-01-21 05:09:58 +01:00
|
|
|
/* Filter duplicates */
|
|
|
|
for (j = 0, i = 1; i < n_pids; i++) {
|
|
|
|
if (pids[i] == pids[j])
|
|
|
|
continue;
|
|
|
|
pids[++j] = pids[i];
|
2012-04-16 18:56:18 +02:00
|
|
|
}
|
2015-01-21 05:09:58 +01:00
|
|
|
n_pids = j + 1;
|
|
|
|
pid_width = DECIMAL_STR_WIDTH(pids[j]);
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2013-10-12 18:15:49 +02:00
|
|
|
if (flags & OUTPUT_FULL_WIDTH)
|
2013-01-14 18:16:50 +01:00
|
|
|
n_columns = 0;
|
|
|
|
else {
|
|
|
|
if (n_columns > pid_width+2)
|
|
|
|
n_columns -= pid_width+2;
|
|
|
|
else
|
|
|
|
n_columns = 20;
|
|
|
|
}
|
2012-04-16 18:56:18 +02:00
|
|
|
for (i = 0; i < n_pids; i++) {
|
2013-10-12 18:15:49 +02:00
|
|
|
_cleanup_free_ char *t = NULL;
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2017-01-31 17:39:04 +01:00
|
|
|
(void) get_process_cmdline(pids[i], n_columns, true, &t);
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2014-04-23 19:05:46 +02:00
|
|
|
if (extra)
|
2018-12-13 22:11:32 +01:00
|
|
|
printf("%s%s ", prefix, special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
|
2014-04-23 19:05:46 +02:00
|
|
|
else
|
2018-12-13 22:11:32 +01:00
|
|
|
printf("%s%s", prefix, special_glyph(((more || i < n_pids-1) ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT)));
|
2014-04-23 19:05:46 +02:00
|
|
|
|
2015-01-21 05:09:58 +01:00
|
|
|
printf("%*"PID_PRI" %s\n", pid_width, pids[i], strna(t));
|
2012-04-16 18:56:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 15:28:28 +02:00
|
|
|
static int show_cgroup_one_by_path(
|
|
|
|
const char *path,
|
|
|
|
const char *prefix,
|
|
|
|
unsigned n_columns,
|
|
|
|
bool more,
|
|
|
|
OutputFlags flags) {
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2010-07-08 06:08:32 +02:00
|
|
|
char *fn;
|
2013-04-16 04:36:06 +02:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2010-07-08 06:08:32 +02:00
|
|
|
size_t n = 0, n_allocated = 0;
|
2013-04-16 04:36:06 +02:00
|
|
|
_cleanup_free_ pid_t *pids = NULL;
|
2013-10-12 18:15:49 +02:00
|
|
|
_cleanup_free_ char *p = NULL;
|
2012-04-16 18:56:18 +02:00
|
|
|
pid_t pid;
|
2010-07-08 21:01:42 +02:00
|
|
|
int r;
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
r = cg_mangle_path(path, &p);
|
2012-04-16 18:56:18 +02:00
|
|
|
if (r < 0)
|
2010-07-12 18:16:44 +02:00
|
|
|
return r;
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2015-02-03 02:05:59 +01:00
|
|
|
fn = strjoina(p, "/cgroup.procs");
|
2010-07-08 21:01:42 +02:00
|
|
|
f = fopen(fn, "re");
|
2010-07-08 06:08:32 +02:00
|
|
|
if (!f)
|
2010-07-08 21:01:42 +02:00
|
|
|
return -errno;
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2010-07-11 00:50:49 +02:00
|
|
|
while ((r = cg_read_pid(f, &pid)) > 0) {
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
if (!(flags & OUTPUT_KERNEL_THREADS) && is_kernel_thread(pid) > 0)
|
2012-01-22 18:18:51 +01:00
|
|
|
continue;
|
|
|
|
|
2015-01-21 05:09:58 +01:00
|
|
|
if (!GREEDY_REALLOC(pids, n_allocated, n + 1))
|
|
|
|
return -ENOMEM;
|
2010-07-08 06:08:32 +02:00
|
|
|
|
|
|
|
assert(n < n_allocated);
|
2010-07-11 00:50:49 +02:00
|
|
|
pids[n++] = pid;
|
2010-07-08 06:08:32 +02:00
|
|
|
}
|
|
|
|
|
2010-07-11 00:50:49 +02:00
|
|
|
if (r < 0)
|
2013-04-16 04:36:06 +02:00
|
|
|
return r;
|
2010-07-11 00:50:49 +02:00
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
show_pid_array(pids, n, prefix, n_columns, false, more, flags);
|
2010-07-08 06:08:32 +02:00
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
return 0;
|
2010-07-08 21:01:42 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 15:28:28 +02:00
|
|
|
int show_cgroup_by_path(
|
|
|
|
const char *path,
|
|
|
|
const char *prefix,
|
|
|
|
unsigned n_columns,
|
|
|
|
OutputFlags flags) {
|
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
_cleanup_free_ char *fn = NULL, *p1 = NULL, *last = NULL, *p2 = NULL;
|
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
|
|
|
char *gn = NULL;
|
2010-07-08 21:01:42 +02:00
|
|
|
bool shown_pids = false;
|
|
|
|
int r;
|
|
|
|
|
2012-04-16 18:56:18 +02:00
|
|
|
assert(path);
|
|
|
|
|
2010-07-08 21:01:42 +02:00
|
|
|
if (n_columns <= 0)
|
|
|
|
n_columns = columns();
|
|
|
|
|
2016-04-20 15:28:28 +02:00
|
|
|
prefix = strempty(prefix);
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
r = cg_mangle_path(path, &fn);
|
2012-04-16 17:35:58 +02:00
|
|
|
if (r < 0)
|
2010-07-12 18:16:44 +02:00
|
|
|
return r;
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
d = opendir(fn);
|
2013-04-16 04:36:06 +02:00
|
|
|
if (!d)
|
2010-07-08 21:01:42 +02:00
|
|
|
return -errno;
|
|
|
|
|
2010-07-12 18:16:44 +02:00
|
|
|
while ((r = cg_read_subgroup(d, &gn)) > 0) {
|
2013-04-16 04:36:06 +02:00
|
|
|
_cleanup_free_ char *k = NULL;
|
2012-04-16 17:35:58 +02:00
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
k = strjoin(fn, "/", gn);
|
2012-04-16 17:35:58 +02:00
|
|
|
free(gn);
|
2013-04-16 04:36:06 +02:00
|
|
|
if (!k)
|
|
|
|
return -ENOMEM;
|
2012-04-16 17:35:58 +02:00
|
|
|
|
2015-09-01 18:32:07 +02:00
|
|
|
if (!(flags & OUTPUT_SHOW_ALL) && cg_is_empty_recursive(NULL, k) > 0)
|
2012-04-16 17:35:58 +02:00
|
|
|
continue;
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
if (!shown_pids) {
|
2016-04-20 16:06:58 +02:00
|
|
|
show_cgroup_one_by_path(path, prefix, n_columns, true, flags);
|
2010-07-08 21:01:42 +02:00
|
|
|
shown_pids = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last) {
|
2018-12-13 22:11:32 +01:00
|
|
|
printf("%s%s%s\n", prefix, special_glyph(SPECIAL_GLYPH_TREE_BRANCH), cg_unescape(basename(last)));
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
if (!p1) {
|
2018-12-13 22:11:32 +01:00
|
|
|
p1 = strappend(prefix, special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
|
2013-04-16 04:36:06 +02:00
|
|
|
if (!p1)
|
|
|
|
return -ENOMEM;
|
2012-04-16 17:35:58 +02:00
|
|
|
}
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
show_cgroup_by_path(last, p1, n_columns-2, flags);
|
2010-07-08 21:01:42 +02:00
|
|
|
free(last);
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
last = TAKE_PTR(k);
|
2010-07-08 21:01:42 +02:00
|
|
|
}
|
|
|
|
|
2010-07-12 18:16:44 +02:00
|
|
|
if (r < 0)
|
2013-04-16 04:36:06 +02:00
|
|
|
return r;
|
2010-07-12 18:16:44 +02:00
|
|
|
|
2010-07-08 21:01:42 +02:00
|
|
|
if (!shown_pids)
|
2016-04-20 16:06:58 +02:00
|
|
|
show_cgroup_one_by_path(path, prefix, n_columns, !!last, flags);
|
2010-07-08 21:01:42 +02:00
|
|
|
|
|
|
|
if (last) {
|
2018-12-13 22:11:32 +01:00
|
|
|
printf("%s%s%s\n", prefix, special_glyph(SPECIAL_GLYPH_TREE_RIGHT), cg_unescape(basename(last)));
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2012-04-16 17:35:58 +02:00
|
|
|
if (!p2) {
|
|
|
|
p2 = strappend(prefix, " ");
|
2013-04-16 04:36:06 +02:00
|
|
|
if (!p2)
|
|
|
|
return -ENOMEM;
|
2012-04-16 17:35:58 +02:00
|
|
|
}
|
2010-07-08 21:01:42 +02:00
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
show_cgroup_by_path(last, p2, n_columns-2, flags);
|
2010-07-08 21:01:42 +02:00
|
|
|
}
|
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
return 0;
|
2010-07-08 06:08:32 +02:00
|
|
|
}
|
2010-07-12 18:16:44 +02:00
|
|
|
|
2016-04-20 15:28:28 +02:00
|
|
|
int show_cgroup(const char *controller,
|
|
|
|
const char *path,
|
|
|
|
const char *prefix,
|
|
|
|
unsigned n_columns,
|
|
|
|
OutputFlags flags) {
|
2013-04-16 04:36:06 +02:00
|
|
|
_cleanup_free_ char *p = NULL;
|
2010-07-12 18:16:44 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(path);
|
|
|
|
|
2012-01-22 18:18:51 +01:00
|
|
|
r = cg_get_path(controller, path, NULL, &p);
|
|
|
|
if (r < 0)
|
2010-07-12 18:16:44 +02:00
|
|
|
return r;
|
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
return show_cgroup_by_path(p, prefix, n_columns, flags);
|
2010-07-12 18:16:44 +02:00
|
|
|
}
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2016-04-20 15:28:28 +02:00
|
|
|
static int show_extra_pids(
|
|
|
|
const char *controller,
|
|
|
|
const char *path,
|
|
|
|
const char *prefix,
|
|
|
|
unsigned n_columns,
|
|
|
|
const pid_t pids[],
|
|
|
|
unsigned n_pids,
|
|
|
|
OutputFlags flags) {
|
|
|
|
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ pid_t *copy = NULL;
|
2012-04-16 18:56:18 +02:00
|
|
|
unsigned i, j;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(path);
|
|
|
|
|
|
|
|
if (n_pids <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (n_columns <= 0)
|
|
|
|
n_columns = columns();
|
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
prefix = strempty(prefix);
|
2012-04-16 18:56:18 +02:00
|
|
|
|
|
|
|
copy = new(pid_t, n_pids);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < n_pids; i++) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ char *k = NULL;
|
2012-04-16 18:56:18 +02:00
|
|
|
|
2013-04-16 04:36:06 +02:00
|
|
|
r = cg_pid_get_path(controller, pids[i], &k);
|
2013-04-02 05:11:54 +02:00
|
|
|
if (r < 0)
|
2012-04-16 18:56:18 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
if (path_startswith(k, path))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
copy[j++] = pids[i];
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
show_pid_array(copy, j, prefix, n_columns, true, false, flags);
|
2012-04-16 18:56:18 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-20 15:28:28 +02:00
|
|
|
int show_cgroup_and_extra(
|
|
|
|
const char *controller,
|
|
|
|
const char *path,
|
|
|
|
const char *prefix,
|
|
|
|
unsigned n_columns,
|
|
|
|
const pid_t extra_pids[],
|
|
|
|
unsigned n_extra_pids,
|
|
|
|
OutputFlags flags) {
|
|
|
|
|
2012-04-16 18:56:18 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(path);
|
|
|
|
|
2016-04-20 16:06:58 +02:00
|
|
|
r = show_cgroup(controller, path, prefix, n_columns, flags);
|
2012-04-16 18:56:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-01-14 18:16:50 +01:00
|
|
|
return show_extra_pids(controller, path, prefix, n_columns, extra_pids, n_extra_pids, flags);
|
2012-04-16 18:56:18 +02:00
|
|
|
}
|
|
|
|
|
2017-02-01 20:30:57 +01:00
|
|
|
int show_cgroup_get_unit_path_and_warn(
|
|
|
|
sd_bus *bus,
|
|
|
|
const char *unit,
|
|
|
|
char **ret) {
|
|
|
|
|
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
|
|
|
_cleanup_free_ char *path = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
path = unit_dbus_path_from_name(unit);
|
|
|
|
if (!path)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = sd_bus_get_property_string(
|
|
|
|
bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
path,
|
|
|
|
unit_dbus_interface_from_name(unit),
|
|
|
|
"ControlGroup",
|
|
|
|
&error,
|
|
|
|
ret);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to query unit control group path: %s",
|
|
|
|
bus_error_message(&error, r));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-02 02:26:58 +01:00
|
|
|
int show_cgroup_get_path_and_warn(
|
2017-02-01 19:25:27 +01:00
|
|
|
const char *machine,
|
2017-02-02 02:26:58 +01:00
|
|
|
const char *prefix,
|
2017-02-01 19:25:27 +01:00
|
|
|
char **ret) {
|
|
|
|
|
|
|
|
int r;
|
2017-02-02 02:26:58 +01:00
|
|
|
_cleanup_free_ char *root = NULL;
|
|
|
|
|
|
|
|
if (machine) {
|
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
2017-02-01 20:30:57 +01:00
|
|
|
_cleanup_free_ char *unit = NULL;
|
2017-02-02 02:26:58 +01:00
|
|
|
const char *m;
|
|
|
|
|
|
|
|
m = strjoina("/run/systemd/machines/", machine);
|
2018-11-12 14:18:03 +01:00
|
|
|
r = parse_env_file(NULL, m, "SCOPE", &unit);
|
2017-02-02 02:26:58 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to load machine data: %m");
|
2017-02-01 19:25:27 +01:00
|
|
|
|
2017-02-02 02:26:58 +01:00
|
|
|
r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, false, &bus);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to create bus connection: %m");
|
|
|
|
|
2017-02-01 20:30:57 +01:00
|
|
|
r = show_cgroup_get_unit_path_and_warn(bus, unit, &root);
|
2017-02-02 02:26:58 +01:00
|
|
|
if (r < 0)
|
2017-02-01 20:30:57 +01:00
|
|
|
return r;
|
2017-02-02 02:26:58 +01:00
|
|
|
} else {
|
|
|
|
r = cg_get_root_path(&root);
|
2017-02-01 19:25:27 +01:00
|
|
|
if (r == -ENOMEDIUM)
|
|
|
|
return log_error_errno(r, "Failed to get root control group path.\n"
|
|
|
|
"No cgroup filesystem mounted on /sys/fs/cgroup");
|
|
|
|
else if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get root control group path: %m");
|
|
|
|
}
|
|
|
|
|
2017-02-02 02:26:58 +01:00
|
|
|
if (prefix) {
|
|
|
|
char *t;
|
2017-02-01 19:25:27 +01:00
|
|
|
|
2017-02-02 02:26:58 +01:00
|
|
|
t = strjoin(root, prefix);
|
|
|
|
if (!t)
|
|
|
|
return log_oom();
|
2017-02-01 19:25:27 +01:00
|
|
|
|
2017-02-02 02:26:58 +01:00
|
|
|
*ret = t;
|
2018-04-05 07:26:26 +02:00
|
|
|
} else
|
|
|
|
*ret = TAKE_PTR(root);
|
2017-02-01 19:25:27 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|