cgroup-show.c revision 87d2c1ff6a7375f03476767e6f59454bcc5cd04b
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include "util.h"
#include "macro.h"
#include "cgroup-util.h"
#include "cgroup-show.h"
static int compare(const void *a, const void *b) {
const pid_t *p = a, *q = b;
if (*p < *q)
return -1;
if (*p > *q)
return 1;
return 0;
}
int n = 0;
while (ul > 0) {
n++;
ul /= 10;
}
return n;
}
static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigned n_columns, bool more) {
char *fn;
FILE *f;
size_t n = 0, n_allocated = 0;
char *p;
int r;
if (n_columns <= 0)
if (!prefix)
prefix = "";
if ((r = cg_fix_path(path, &p)) < 0)
return r;
free(p);
if (r < 0)
return -ENOMEM;
if (!f)
return -errno;
while ((r = cg_read_pid(f, &pid)) > 0) {
if (n >= n_allocated) {
r = -ENOMEM;
goto finish;
}
}
assert(n < n_allocated);
}
if (r < 0)
goto finish;
if (n > 0) {
unsigned i, m;
/* Filter duplicates */
m = 0;
for (i = 0; i < n; i++) {
unsigned j;
for (j = i+1; j < n; j++)
break;
if (j >= n)
}
n = m;
/* And sort */
if (n_columns > 8)
n_columns -= 8;
else
n_columns = 20;
for (i = 0; i < n; i++) {
char *t = NULL;
printf("%s%s %*lu %s\n",
(unsigned long) pids[i],
strna(t));
free(t);
}
}
r = 0;
if (f)
fclose(f);
return r;
}
DIR *d;
bool shown_pids = false;
int r;
if (n_columns <= 0)
if (!prefix)
prefix = "";
return r;
return -errno;
}
while ((r = cg_read_subgroup(d, &gn)) > 0) {
if (!shown_pids) {
shown_pids = true;
}
if (last) {
if (!p1)
r = -ENOMEM;
goto finish;
}
}
if (r < 0) {
r = -ENOMEM;
goto finish;
}
}
if (r < 0)
goto finish;
if (!shown_pids)
if (last) {
if (!p2)
r = -ENOMEM;
goto finish;
}
}
r = 0;
closedir(d);
return r;
}
char *p;
int r;
return r;
free(p);
return r;
}