procps: add support for linux namespaces
Each process in Linux has a /proc/<pid>/ns directory which contains symbolic links to pipes that identify which namespaces that process belongs to. This patch adds support for ps to display that information optionally. Signed-off-by: Aristeu Rozanski <arozansk@redhat.com>
This commit is contained in:
@ -457,6 +457,51 @@ static void oomadj2proc(const char* S, proc_t *restrict P)
|
||||
#endif
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
static ino_t _ns2proc(unsigned pid, const char *ns)
|
||||
{
|
||||
struct stat s;
|
||||
char filename[40];
|
||||
|
||||
snprintf(filename, sizeof(filename), "/proc/%i/ns/%s", pid, ns);
|
||||
|
||||
if (stat(filename, &s) == -1)
|
||||
return 0;
|
||||
|
||||
return s.st_ino;
|
||||
}
|
||||
|
||||
static const char *ns_names[] = {
|
||||
[IPCNS] = "ipc",
|
||||
[MNTNS] = "mnt",
|
||||
[NETNS] = "net",
|
||||
[PIDNS] = "pid",
|
||||
[USERNS] = "user",
|
||||
[UTSNS] = "uts",
|
||||
};
|
||||
|
||||
const char *get_ns_name(int id) {
|
||||
if (id >= NUM_NS)
|
||||
return NULL;
|
||||
return ns_names[id];
|
||||
}
|
||||
|
||||
int get_ns_id(const char *name) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_NS; i++)
|
||||
if (!strcmp(ns_names[i], name))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void ns2proc(proc_t *restrict P) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_NS; i++)
|
||||
P->ns[i] = _ns2proc(P->tgid, ns_names[i]);
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// Reads /proc/*/stat files, being careful not to trip over processes with
|
||||
// names like ":-) 1 2 3 4 5 6".
|
||||
@ -758,6 +803,7 @@ static proc_t* simple_readproc(PROCTAB *restrict const PT, proc_t *restrict cons
|
||||
static struct stat sb; // stat() buffer
|
||||
char *restrict const path = PT->path;
|
||||
unsigned flags = PT->flags;
|
||||
int i;
|
||||
|
||||
if (unlikely(stat(path, &sb) == -1)) /* no such dirent (anymore) */
|
||||
goto next_proc;
|
||||
@ -845,6 +891,12 @@ static proc_t* simple_readproc(PROCTAB *restrict const PT, proc_t *restrict cons
|
||||
}
|
||||
#endif
|
||||
|
||||
if (unlikely(flags & PROC_FILLNS)) // read /proc/#/ns/*
|
||||
ns2proc(p);
|
||||
else
|
||||
for (i = 0; i < NUM_NS; i++)
|
||||
p->ns[i] = 0;
|
||||
|
||||
return p;
|
||||
next_proc:
|
||||
return NULL;
|
||||
@ -863,6 +915,7 @@ static proc_t* simple_readtask(PROCTAB *restrict const PT, const proc_t *restric
|
||||
static struct utlbuf_s ub = { NULL, 0 }; // buf for stat,statm,status
|
||||
static struct stat sb; // stat() buffer
|
||||
unsigned flags = PT->flags;
|
||||
int i;
|
||||
|
||||
if (unlikely(stat(path, &sb) == -1)) /* no such dirent (anymore) */
|
||||
goto next_task;
|
||||
@ -975,6 +1028,11 @@ static proc_t* simple_readtask(PROCTAB *restrict const PT, const proc_t *restric
|
||||
oomadj2proc(ub.buf, t);
|
||||
}
|
||||
#endif
|
||||
if (unlikely(flags & PROC_FILLNS))
|
||||
ns2proc(t);
|
||||
else
|
||||
for (i = 0; i < NUM_NS; i++)
|
||||
t->ns[i] = 0;
|
||||
|
||||
return t;
|
||||
next_task:
|
||||
|
Reference in New Issue
Block a user