procps/lib/nsutils.c
Aristeu Rozanski de7b3b9222 pgrep: introduce support for namespaces
A PID should be specified with --ns:
	$ pgrep --ns 12345
which will only match the processes which belong to to the same 6
namespaces. It is also possible to specify which namespaces to test:
	$ pgrep --ns 12345 --nslist mnt,net,ipc
which will match processes that belong to the same mount, network and
IPC namespaces as PID 12345.

Signed-off-by: Aristeu Rozanski <arozansk@redhat.com>
2013-04-16 15:05:31 -04:00

33 lines
599 B
C

#include <errno.h>
#include <error.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "proc/readproc.h"
#include "nsutils.h"
/* we need to fill in only namespace information */
int ns_read(pid_t pid, proc_t *ns_task)
{
struct stat st;
char buff[50];
int i, rc = 0;
for (i = 0; i < NUM_NS; i++) {
snprintf(buff, sizeof(buff), "/proc/%i/ns/%s", pid,
get_ns_name(i));
if (stat(buff, &st)) {
if (errno != ENOENT)
rc = errno;
ns_task->ns[i] = 0;
continue;
}
ns_task->ns[i] = st.st_ino;
}
return rc;
}