|
|
531551 |
From 03f539c766780a083010636cc67f96fcb2bab30f Mon Sep 17 00:00:00 2001
|
|
|
531551 |
From: Karel Zak <kzak@redhat.com>
|
|
|
531551 |
Date: Fri, 24 Jun 2016 12:16:24 +0200
|
|
|
531551 |
Subject: [PATCH 76/84] lsns: backport new command
|
|
|
531551 |
|
|
|
531551 |
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1332084
|
|
|
531551 |
Signed-off-by: Karel Zak <kzak@redhat.com>
|
|
|
531551 |
---
|
|
|
531551 |
bash-completion/lsns | 55 ++++
|
|
|
531551 |
configure.ac | 6 +
|
|
|
531551 |
include/Makemodule.am | 1 +
|
|
|
531551 |
include/c.h | 8 +
|
|
|
531551 |
include/debug.h | 2 +-
|
|
|
531551 |
include/idcache.h | 28 ++
|
|
|
531551 |
include/procutils.h | 2 +
|
|
|
531551 |
lib/Makemodule.am | 1 +
|
|
|
531551 |
lib/idcache.c | 117 ++++++++
|
|
|
531551 |
lib/procutils.c | 43 +++
|
|
|
531551 |
sys-utils/Makemodule.am | 7 +
|
|
|
531551 |
sys-utils/lsns.8 | 78 +++++
|
|
|
531551 |
sys-utils/lsns.c | 748 ++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
531551 |
13 files changed, 1095 insertions(+), 1 deletion(-)
|
|
|
531551 |
create mode 100644 bash-completion/lsns
|
|
|
531551 |
create mode 100644 include/idcache.h
|
|
|
531551 |
create mode 100644 lib/idcache.c
|
|
|
531551 |
create mode 100644 sys-utils/lsns.8
|
|
|
531551 |
create mode 100644 sys-utils/lsns.c
|
|
|
531551 |
|
|
|
531551 |
diff --git a/bash-completion/lsns b/bash-completion/lsns
|
|
|
531551 |
new file mode 100644
|
|
|
531551 |
index 0000000..d02df3b
|
|
|
531551 |
--- /dev/null
|
|
|
531551 |
+++ b/bash-completion/lsns
|
|
|
531551 |
@@ -0,0 +1,55 @@
|
|
|
531551 |
+_lsns_module()
|
|
|
531551 |
+{
|
|
|
531551 |
+ local cur prev OPTS LSNS_COLS_ALL
|
|
|
531551 |
+ COMPREPLY=()
|
|
|
531551 |
+ cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
531551 |
+ prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
531551 |
+ LSNS_COLS_ALL="
|
|
|
531551 |
+ NS TYPE PATH NPROCS PID PPID COMMAND UID USER
|
|
|
531551 |
+ "
|
|
|
531551 |
+ case $prev in
|
|
|
531551 |
+ '-o'|'--output')
|
|
|
531551 |
+ local prefix realcur LSNS_COLS
|
|
|
531551 |
+ realcur="${cur##*,}"
|
|
|
531551 |
+ prefix="${cur%$realcur}"
|
|
|
531551 |
+ for WORD in $LSNS_COLS_ALL; do
|
|
|
531551 |
+ if ! [[ $prefix == *"$WORD"* ]]; then
|
|
|
531551 |
+ LSNS_COLS="$WORD $LSNS_COLS"
|
|
|
531551 |
+ fi
|
|
|
531551 |
+ done
|
|
|
531551 |
+ compopt -o nospace
|
|
|
531551 |
+ COMPREPLY=( $(compgen -P "$prefix" -W "$LSNS_COLS" -S ',' -- $realcur) )
|
|
|
531551 |
+ return 0
|
|
|
531551 |
+ ;;
|
|
|
531551 |
+ '-p'|'--task')
|
|
|
531551 |
+ COMPREPLY=( $(compgen -W "$(cd /proc && echo [0-9]*)" -- $cur) )
|
|
|
531551 |
+ return 0
|
|
|
531551 |
+ ;;
|
|
|
531551 |
+ '-t'|'--type')
|
|
|
531551 |
+ COMPREPLY=( $(compgen -W "mnt net ipc user pid uts" -- $cur) )
|
|
|
531551 |
+ return 0
|
|
|
531551 |
+ ;;
|
|
|
531551 |
+ '-h'|'--help'|'-V'|'--version')
|
|
|
531551 |
+ return 0
|
|
|
531551 |
+ ;;
|
|
|
531551 |
+ esac
|
|
|
531551 |
+ case $cur in
|
|
|
531551 |
+ -*)
|
|
|
531551 |
+ COMPREPLY=( $(compgen -W "
|
|
|
531551 |
+ --list
|
|
|
531551 |
+ --noheadings
|
|
|
531551 |
+ --output
|
|
|
531551 |
+ --task
|
|
|
531551 |
+ --raw
|
|
|
531551 |
+ --notruncate
|
|
|
531551 |
+ --type
|
|
|
531551 |
+ --help
|
|
|
531551 |
+ --version
|
|
|
531551 |
+ " -- $cur) )
|
|
|
531551 |
+ return 0
|
|
|
531551 |
+ ;;
|
|
|
531551 |
+ esac
|
|
|
531551 |
+ COMPREPLY=( $(compgen -W "mnt net pid uts ipc user" -- $cur ) )
|
|
|
531551 |
+ return 0
|
|
|
531551 |
+}
|
|
|
531551 |
+complete -F _lsns_module lsns
|
|
|
531551 |
diff --git a/configure.ac b/configure.ac
|
|
|
531551 |
index f3c7214..5d9ea39 100644
|
|
|
531551 |
--- a/configure.ac
|
|
|
531551 |
+++ b/configure.ac
|
|
|
531551 |
@@ -1032,6 +1032,12 @@ UL_REQUIRES_BUILD([lslogins], [libsmartcols])
|
|
|
531551 |
AM_CONDITIONAL([BUILD_LSLOGINS], [test "x$build_lslogins" = xyes])
|
|
|
531551 |
|
|
|
531551 |
|
|
|
531551 |
+UL_BUILD_INIT([lsns], [check])
|
|
|
531551 |
+UL_REQUIRES_LINUX([lsns])
|
|
|
531551 |
+UL_REQUIRES_BUILD([lsns], [libsmartcols])
|
|
|
531551 |
+AM_CONDITIONAL([BUILD_LSNS], [test "x$build_lsns" = xyes])
|
|
|
531551 |
+
|
|
|
531551 |
+
|
|
|
531551 |
UL_BUILD_INIT([chcpu], [check])
|
|
|
531551 |
UL_REQUIRES_LINUX([chcpu])
|
|
|
531551 |
UL_REQUIRES_HAVE([chcpu], [cpu_set_t], [cpu_set_t type])
|
|
|
531551 |
diff --git a/include/Makemodule.am b/include/Makemodule.am
|
|
|
531551 |
index 7b53244..757f317 100644
|
|
|
531551 |
--- a/include/Makemodule.am
|
|
|
531551 |
+++ b/include/Makemodule.am
|
|
|
531551 |
@@ -15,6 +15,7 @@ dist_noinst_HEADERS += \
|
|
|
531551 |
include/exec_shell.h \
|
|
|
531551 |
include/exitcodes.h \
|
|
|
531551 |
include/fileutils.h \
|
|
|
531551 |
+ include/idcache.h \
|
|
|
531551 |
include/ismounted.h \
|
|
|
531551 |
include/linux_reboot.h \
|
|
|
531551 |
include/linux_version.h \
|
|
|
531551 |
diff --git a/include/c.h b/include/c.h
|
|
|
531551 |
index 7b59ce8..a2779a5 100644
|
|
|
531551 |
--- a/include/c.h
|
|
|
531551 |
+++ b/include/c.h
|
|
|
531551 |
@@ -110,6 +110,14 @@
|
|
|
531551 |
_max1 > _max2 ? _max1 : _max2; })
|
|
|
531551 |
#endif
|
|
|
531551 |
|
|
|
531551 |
+#ifndef cmp_numbers
|
|
|
531551 |
+# define cmp_numbers(x, y) __extension__ ({ \
|
|
|
531551 |
+ __typeof__(x) _a = (x); \
|
|
|
531551 |
+ __typeof__(y) _b = (y); \
|
|
|
531551 |
+ (void) (&_a == &_b); \
|
|
|
531551 |
+ _a == _b ? 0 : _a > _b ? 1 : -1; })
|
|
|
531551 |
+#endif
|
|
|
531551 |
+
|
|
|
531551 |
#ifndef offsetof
|
|
|
531551 |
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
|
|
|
531551 |
#endif
|
|
|
531551 |
diff --git a/include/debug.h b/include/debug.h
|
|
|
531551 |
index 25045aa..848e474 100644
|
|
|
531551 |
--- a/include/debug.h
|
|
|
531551 |
+++ b/include/debug.h
|
|
|
531551 |
@@ -15,7 +15,7 @@ struct dbg_mask { char *mname; int val; };
|
|
|
531551 |
|
|
|
531551 |
#define UL_DEBUG_DEFINE_MASK(m) int m ## _debug_mask
|
|
|
531551 |
#define UL_DEBUG_DECLARE_MASK(m) extern UL_DEBUG_DEFINE_MASK(m)
|
|
|
531551 |
-#define UL_DEBUG_DEFINE_MASKANEMS(m) static const struct dbg_mask m ## _masknames[]
|
|
|
531551 |
+#define UL_DEBUG_DEFINE_MASKNAMES(m) static const struct dbg_mask m ## _masknames[]
|
|
|
531551 |
|
|
|
531551 |
/* p - flag prefix, m - flag postfix */
|
|
|
531551 |
#define UL_DEBUG_DEFINE_FLAG(p, m) p ## m
|
|
|
531551 |
diff --git a/include/idcache.h b/include/idcache.h
|
|
|
531551 |
new file mode 100644
|
|
|
531551 |
index 0000000..912edd5
|
|
|
531551 |
--- /dev/null
|
|
|
531551 |
+++ b/include/idcache.h
|
|
|
531551 |
@@ -0,0 +1,28 @@
|
|
|
531551 |
+#ifndef UTIL_LINUX_IDCACHE_H
|
|
|
531551 |
+#define UTIL_LINUX_IDCACHE_H
|
|
|
531551 |
+
|
|
|
531551 |
+#include <sys/types.h>
|
|
|
531551 |
+#include <pwd.h>
|
|
|
531551 |
+
|
|
|
531551 |
+#define IDCACHE_FLAGS_NAMELEN (1 << 1)
|
|
|
531551 |
+
|
|
|
531551 |
+struct identry {
|
|
|
531551 |
+ unsigned long int id;
|
|
|
531551 |
+ char *name;
|
|
|
531551 |
+ struct identry *next;
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+struct idcache {
|
|
|
531551 |
+ struct identry *ent; /* first entry */
|
|
|
531551 |
+ int width; /* name width */
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+
|
|
|
531551 |
+extern struct idcache *new_idcache(void);
|
|
|
531551 |
+extern void add_gid(struct idcache *cache, unsigned long int id);
|
|
|
531551 |
+extern void add_uid(struct idcache *cache, unsigned long int id);
|
|
|
531551 |
+
|
|
|
531551 |
+extern void free_idcache(struct idcache *ic);
|
|
|
531551 |
+extern struct identry *get_id(struct idcache *ic, unsigned long int id);
|
|
|
531551 |
+
|
|
|
531551 |
+#endif /* UTIL_LINUX_IDCACHE_H */
|
|
|
531551 |
diff --git a/include/procutils.h b/include/procutils.h
|
|
|
531551 |
index 14b766c..9f8dd76 100644
|
|
|
531551 |
--- a/include/procutils.h
|
|
|
531551 |
+++ b/include/procutils.h
|
|
|
531551 |
@@ -28,5 +28,7 @@ extern void proc_processes_filter_by_name(struct proc_processes *ps, const char
|
|
|
531551 |
extern void proc_processes_filter_by_uid(struct proc_processes *ps, uid_t uid);
|
|
|
531551 |
extern int proc_next_pid(struct proc_processes *ps, pid_t *pid);
|
|
|
531551 |
|
|
|
531551 |
+extern char *proc_get_command(pid_t pid);
|
|
|
531551 |
+extern char *proc_get_command_name(pid_t pid);
|
|
|
531551 |
|
|
|
531551 |
#endif /* UTIL_LINUX_PROCUTILS */
|
|
|
531551 |
diff --git a/lib/Makemodule.am b/lib/Makemodule.am
|
|
|
531551 |
index eed31f1..73280f9 100644
|
|
|
531551 |
--- a/lib/Makemodule.am
|
|
|
531551 |
+++ b/lib/Makemodule.am
|
|
|
531551 |
@@ -8,6 +8,7 @@ libcommon_la_SOURCES = \
|
|
|
531551 |
lib/colors.c \
|
|
|
531551 |
lib/crc32.c \
|
|
|
531551 |
lib/env.c \
|
|
|
531551 |
+ lib/idcache.c \
|
|
|
531551 |
lib/fileutils.c \
|
|
|
531551 |
lib/ismounted.c \
|
|
|
531551 |
lib/mangle.c \
|
|
|
531551 |
diff --git a/lib/idcache.c b/lib/idcache.c
|
|
|
531551 |
new file mode 100644
|
|
|
531551 |
index 0000000..3c358b8
|
|
|
531551 |
--- /dev/null
|
|
|
531551 |
+++ b/lib/idcache.c
|
|
|
531551 |
@@ -0,0 +1,117 @@
|
|
|
531551 |
+
|
|
|
531551 |
+#include <wchar.h>
|
|
|
531551 |
+#include <pwd.h>
|
|
|
531551 |
+#include <grp.h>
|
|
|
531551 |
+#include <sys/types.h>
|
|
|
531551 |
+
|
|
|
531551 |
+#include "c.h"
|
|
|
531551 |
+#include "idcache.h"
|
|
|
531551 |
+
|
|
|
531551 |
+#ifndef LOGIN_NAME_MAX
|
|
|
531551 |
+#define LOGIN_NAME_MAX 256
|
|
|
531551 |
+#endif
|
|
|
531551 |
+
|
|
|
531551 |
+struct identry *get_id(struct idcache *ic, unsigned long int id)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct identry *ent;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ic)
|
|
|
531551 |
+ return NULL;
|
|
|
531551 |
+
|
|
|
531551 |
+ for (ent = ic->ent; ent; ent = ent->next) {
|
|
|
531551 |
+ if (ent->id == id)
|
|
|
531551 |
+ return ent;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ return NULL;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+struct idcache *new_idcache()
|
|
|
531551 |
+{
|
|
|
531551 |
+ return calloc(1, sizeof(struct idcache));
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+void free_idcache(struct idcache *ic)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct identry *ent = ic->ent;
|
|
|
531551 |
+
|
|
|
531551 |
+ while (ent) {
|
|
|
531551 |
+ struct identry *next = ent->next;
|
|
|
531551 |
+ free(ent->name);
|
|
|
531551 |
+ free(ent);
|
|
|
531551 |
+ ent = next;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ free(ic);
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static void add_id(struct idcache *ic, char *name, unsigned long int id)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct identry *ent, *x;
|
|
|
531551 |
+ int w = 0;
|
|
|
531551 |
+
|
|
|
531551 |
+ ent = calloc(1, sizeof(struct identry));
|
|
|
531551 |
+ if (!ent)
|
|
|
531551 |
+ return;
|
|
|
531551 |
+ ent->id = id;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (name) {
|
|
|
531551 |
+#ifdef HAVE_WIDECHAR
|
|
|
531551 |
+ wchar_t wc[LOGIN_NAME_MAX + 1];
|
|
|
531551 |
+
|
|
|
531551 |
+ if (mbstowcs(wc, name, LOGIN_NAME_MAX) > 0) {
|
|
|
531551 |
+ wc[LOGIN_NAME_MAX] = '\0';
|
|
|
531551 |
+ w = wcswidth(wc, LOGIN_NAME_MAX);
|
|
|
531551 |
+ }
|
|
|
531551 |
+ else
|
|
|
531551 |
+#endif
|
|
|
531551 |
+ w = strlen(name);
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ /* note, we ignore names with non-printable widechars */
|
|
|
531551 |
+ if (w > 0) {
|
|
|
531551 |
+ ent->name = strdup(name);
|
|
|
531551 |
+ if (!ent->name) {
|
|
|
531551 |
+ free(ent);
|
|
|
531551 |
+ return;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ } else {
|
|
|
531551 |
+ if (asprintf(&ent->name, "%lu", id) < 0) {
|
|
|
531551 |
+ free(ent);
|
|
|
531551 |
+ return;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ for (x = ic->ent; x && x->next; x = x->next);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (x)
|
|
|
531551 |
+ x->next = ent;
|
|
|
531551 |
+ else
|
|
|
531551 |
+ ic->ent = ent;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (w <= 0)
|
|
|
531551 |
+ w = ent->name ? strlen(ent->name) : 0;
|
|
|
531551 |
+ ic->width = ic->width < w ? w : ic->width;
|
|
|
531551 |
+ return;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+void add_uid(struct idcache *cache, unsigned long int id)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct identry *ent= get_id(cache, id);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ent) {
|
|
|
531551 |
+ struct passwd *pw = getpwuid((uid_t) id);
|
|
|
531551 |
+ add_id(cache, pw ? pw->pw_name : NULL, id);
|
|
|
531551 |
+ }
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+void add_gid(struct idcache *cache, unsigned long int id)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct identry *ent = get_id(cache, id);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ent) {
|
|
|
531551 |
+ struct group *gr = getgrgid((gid_t) id);
|
|
|
531551 |
+ add_id(cache, gr ? gr->gr_name : NULL, id);
|
|
|
531551 |
+ }
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
diff --git a/lib/procutils.c b/lib/procutils.c
|
|
|
531551 |
index d633261..8dfdec9 100644
|
|
|
531551 |
--- a/lib/procutils.c
|
|
|
531551 |
+++ b/lib/procutils.c
|
|
|
531551 |
@@ -25,6 +25,7 @@
|
|
|
531551 |
#include "procutils.h"
|
|
|
531551 |
#include "at.h"
|
|
|
531551 |
#include "c.h"
|
|
|
531551 |
+#include "all-io.h"
|
|
|
531551 |
|
|
|
531551 |
/*
|
|
|
531551 |
* @pid: process ID for which we want to obtain the threads group
|
|
|
531551 |
@@ -193,6 +194,48 @@ int proc_next_pid(struct proc_processes *ps, pid_t *pid)
|
|
|
531551 |
return 0;
|
|
|
531551 |
}
|
|
|
531551 |
|
|
|
531551 |
+/* returns process command path, use free() for result */
|
|
|
531551 |
+static char *proc_file_strdup(pid_t pid, const char *name)
|
|
|
531551 |
+{
|
|
|
531551 |
+ char buf[BUFSIZ], *res = NULL;
|
|
|
531551 |
+ ssize_t sz = 0;
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+ int fd;
|
|
|
531551 |
+
|
|
|
531551 |
+ snprintf(buf, sizeof(buf), "/proc/%d/%s", (int) pid, name);
|
|
|
531551 |
+ fd = open(buf, O_RDONLY);
|
|
|
531551 |
+ if (fd < 0)
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+
|
|
|
531551 |
+ sz = read_all(fd, buf, sizeof(buf));
|
|
|
531551 |
+ if (sz <= 0)
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < (size_t) sz; i++) {
|
|
|
531551 |
+
|
|
|
531551 |
+ if (buf[i] == '\0')
|
|
|
531551 |
+ buf[i] = ' ';
|
|
|
531551 |
+ }
|
|
|
531551 |
+ buf[sz - 1] = '\0';
|
|
|
531551 |
+ res = strdup(buf);
|
|
|
531551 |
+done:
|
|
|
531551 |
+ if (fd >= 0)
|
|
|
531551 |
+ close(fd);
|
|
|
531551 |
+ return res;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+/* returns process command path, use free() for result */
|
|
|
531551 |
+char *proc_get_command(pid_t pid)
|
|
|
531551 |
+{
|
|
|
531551 |
+ return proc_file_strdup(pid, "cmdline");
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+/* returns process command name, use free() for result */
|
|
|
531551 |
+char *proc_get_command_name(pid_t pid)
|
|
|
531551 |
+{
|
|
|
531551 |
+ return proc_file_strdup(pid, "comm");
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
#ifdef TEST_PROGRAM
|
|
|
531551 |
|
|
|
531551 |
static int test_tasks(int argc, char *argv[])
|
|
|
531551 |
diff --git a/sys-utils/Makemodule.am b/sys-utils/Makemodule.am
|
|
|
531551 |
index c6c561c..9baf5a3 100644
|
|
|
531551 |
--- a/sys-utils/Makemodule.am
|
|
|
531551 |
+++ b/sys-utils/Makemodule.am
|
|
|
531551 |
@@ -182,6 +182,13 @@ prlimit_SOURCES = sys-utils/prlimit.c
|
|
|
531551 |
prlimit_LDADD = $(LDADD) libcommon.la
|
|
|
531551 |
endif
|
|
|
531551 |
|
|
|
531551 |
+if BUILD_LSNS
|
|
|
531551 |
+usrbin_exec_PROGRAMS += lsns
|
|
|
531551 |
+dist_man_MANS += sys-utils/lsns.8
|
|
|
531551 |
+lsns_SOURCES = sys-utils/lsns.c
|
|
|
531551 |
+lsns_LDADD = $(LDADD) libcommon.la libsmartcols.la
|
|
|
531551 |
+lsns_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
|
|
|
531551 |
+endif
|
|
|
531551 |
|
|
|
531551 |
if BUILD_MOUNT
|
|
|
531551 |
#
|
|
|
531551 |
diff --git a/sys-utils/lsns.8 b/sys-utils/lsns.8
|
|
|
531551 |
new file mode 100644
|
|
|
531551 |
index 0000000..328df47
|
|
|
531551 |
--- /dev/null
|
|
|
531551 |
+++ b/sys-utils/lsns.8
|
|
|
531551 |
@@ -0,0 +1,78 @@
|
|
|
531551 |
+.\" Man page for the lsns command.
|
|
|
531551 |
+.\" Copyright 2015 Karel Zak <kzak@redhat.com>
|
|
|
531551 |
+.\" May be distributed under the GNU General Public License
|
|
|
531551 |
+
|
|
|
531551 |
+.TH LSNS 8 "December 2015" "util-linux" "System Administration"
|
|
|
531551 |
+.SH NAME
|
|
|
531551 |
+lsns \- list namespaces
|
|
|
531551 |
+.SH SYNOPSIS
|
|
|
531551 |
+.B lsns
|
|
|
531551 |
+[options]
|
|
|
531551 |
+.RI [ namespace ]
|
|
|
531551 |
+
|
|
|
531551 |
+.SH DESCRIPTION
|
|
|
531551 |
+.B lsns
|
|
|
531551 |
+lists information about all the currently accessible namespaces or about the
|
|
|
531551 |
+given \fInamespace\fP. The \fInamespace\fP identifier is an inode number.
|
|
|
531551 |
+
|
|
|
531551 |
+The default output is subject to change. So whenever possible, you should
|
|
|
531551 |
+avoid using default outputs in your scripts. Always explicitly define expected
|
|
|
531551 |
+columns by using the \fB\-\-output\fR option together with a columns list in
|
|
|
531551 |
+environments where a stable output is required.
|
|
|
531551 |
+
|
|
|
531551 |
+Note that \fBlsns\fR reads information directly from the /proc filesystem and
|
|
|
531551 |
+for non-root users it may return incomplete information. The current /proc
|
|
|
531551 |
+filesystem may be unshared and affected by a PID namespace
|
|
|
531551 |
+(see \fBunshare \-\-mount\-proc\fP for more details).
|
|
|
531551 |
+.B lsns
|
|
|
531551 |
+is not able to see persistent namespaces without processes where the namespace
|
|
|
531551 |
+instance is held by a bind mount to /proc/\fIpid\fR/ns/\fItype\fR.
|
|
|
531551 |
+
|
|
|
531551 |
+.SH OPTIONS
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-l , " \-\-list"
|
|
|
531551 |
+Use list output format.
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-n , " \-\-noheadings"
|
|
|
531551 |
+Do not print a header line.
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-o , " \-\-output " \fIlist\fP
|
|
|
531551 |
+Specify which output columns to print. Use \fB\-\-help\fR
|
|
|
531551 |
+to get a list of all supported columns.
|
|
|
531551 |
+
|
|
|
531551 |
+The default list of columns may be extended if \fIlist\fP is
|
|
|
531551 |
+specified in the format \fB+\fIlist\fP (e.g. \fBlsns \-o +PATH\fP).
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-p , " \-\-task " \fIpid\fP
|
|
|
531551 |
+Display only the namespaces held by the process with this \fIpid\fR.
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-r , " \-\-raw"
|
|
|
531551 |
+Use the raw output format.
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-t , " \-\-type " \fItype\fP
|
|
|
531551 |
+Display the specified \fItype\fP of namespaces only. The supported types are
|
|
|
531551 |
+\fBmnt\fP, \fBnet\fP, \fBipc\fP, \fBuser\fP, \fBpid\fP and \fButs\fP. This
|
|
|
531551 |
+option may be given more than once.
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-u , " \-\-notruncate"
|
|
|
531551 |
+Do not truncate text in columns.
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-V , " \-\-version"
|
|
|
531551 |
+Display version information and exit.
|
|
|
531551 |
+.TP
|
|
|
531551 |
+.BR \-h , " \-\-help"
|
|
|
531551 |
+Display help text and exit.
|
|
|
531551 |
+
|
|
|
531551 |
+.SH AUTHORS
|
|
|
531551 |
+.nf
|
|
|
531551 |
+Karel Zak <kzak@redhat.com>
|
|
|
531551 |
+.fi
|
|
|
531551 |
+
|
|
|
531551 |
+.SH "SEE ALSO"
|
|
|
531551 |
+.BR unshare (1),
|
|
|
531551 |
+.BR nsenter (1),
|
|
|
531551 |
+.BR clone (2)
|
|
|
531551 |
+
|
|
|
531551 |
+.SH AVAILABILITY
|
|
|
531551 |
+The lsns command is part of the util-linux package and is available from
|
|
|
531551 |
+ftp://ftp.kernel.org/pub/linux/utils/util-linux/.
|
|
|
531551 |
diff --git a/sys-utils/lsns.c b/sys-utils/lsns.c
|
|
|
531551 |
new file mode 100644
|
|
|
531551 |
index 0000000..5ee2981
|
|
|
531551 |
--- /dev/null
|
|
|
531551 |
+++ b/sys-utils/lsns.c
|
|
|
531551 |
@@ -0,0 +1,748 @@
|
|
|
531551 |
+/*
|
|
|
531551 |
+ * lsns(8) - list system namespaces
|
|
|
531551 |
+ *
|
|
|
531551 |
+ * Copyright (C) 2015 Karel Zak <kzak@redhat.com>
|
|
|
531551 |
+ *
|
|
|
531551 |
+ * This program is free software; you can redistribute it and/or modify
|
|
|
531551 |
+ * it under the terms of the GNU General Public License as published by
|
|
|
531551 |
+ * the Free Software Foundation; either version 2 of the License, or
|
|
|
531551 |
+ * (at your option) any later version.
|
|
|
531551 |
+ *
|
|
|
531551 |
+ * This program is distributed in the hope that it would be useful,
|
|
|
531551 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
531551 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
531551 |
+ * GNU General Public License for more details.
|
|
|
531551 |
+ *
|
|
|
531551 |
+ * You should have received a copy of the GNU General Public License
|
|
|
531551 |
+ * along with this program; if not, write to the Free Software Foundation,
|
|
|
531551 |
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
531551 |
+ */
|
|
|
531551 |
+#include <stdio.h>
|
|
|
531551 |
+#include <string.h>
|
|
|
531551 |
+#include <getopt.h>
|
|
|
531551 |
+#include <stdlib.h>
|
|
|
531551 |
+#include <assert.h>
|
|
|
531551 |
+#include <dirent.h>
|
|
|
531551 |
+#include <unistd.h>
|
|
|
531551 |
+#include <sys/stat.h>
|
|
|
531551 |
+#include <sys/types.h>
|
|
|
531551 |
+#include <wchar.h>
|
|
|
531551 |
+#include <libsmartcols.h>
|
|
|
531551 |
+
|
|
|
531551 |
+#include "pathnames.h"
|
|
|
531551 |
+#include "nls.h"
|
|
|
531551 |
+#include "xalloc.h"
|
|
|
531551 |
+#include "c.h"
|
|
|
531551 |
+#include "list.h"
|
|
|
531551 |
+#include "closestream.h"
|
|
|
531551 |
+#include "optutils.h"
|
|
|
531551 |
+#include "procutils.h"
|
|
|
531551 |
+#include "strutils.h"
|
|
|
531551 |
+#include "namespace.h"
|
|
|
531551 |
+#include "path.h"
|
|
|
531551 |
+#include "idcache.h"
|
|
|
531551 |
+
|
|
|
531551 |
+#include "debug.h"
|
|
|
531551 |
+
|
|
|
531551 |
+UL_DEBUG_DEFINE_MASK(lsns);
|
|
|
531551 |
+UL_DEBUG_DEFINE_MASKNAMES(lsns) = UL_DEBUG_EMPTY_MASKNAMES;
|
|
|
531551 |
+
|
|
|
531551 |
+#define LSNS_DEBUG_INIT (1 << 1)
|
|
|
531551 |
+#define LSNS_DEBUG_PROC (1 << 2)
|
|
|
531551 |
+#define LSNS_DEBUG_NS (1 << 3)
|
|
|
531551 |
+#define LSNS_DEBUG_ALL 0xFFFF
|
|
|
531551 |
+
|
|
|
531551 |
+#define DBG(m, x) __UL_DBG(lsns, LSNS_DEBUG_, m, x)
|
|
|
531551 |
+#define ON_DBG(m, x) __UL_DBG_CALL(lsns, LSNS_DEBUG_, m, x)
|
|
|
531551 |
+
|
|
|
531551 |
+struct idcache *uid_cache = NULL;
|
|
|
531551 |
+
|
|
|
531551 |
+/* column IDs */
|
|
|
531551 |
+enum {
|
|
|
531551 |
+ COL_NS = 0,
|
|
|
531551 |
+ COL_TYPE,
|
|
|
531551 |
+ COL_PATH,
|
|
|
531551 |
+ COL_NPROCS,
|
|
|
531551 |
+ COL_PID,
|
|
|
531551 |
+ COL_PPID,
|
|
|
531551 |
+ COL_COMMAND,
|
|
|
531551 |
+ COL_UID,
|
|
|
531551 |
+ COL_USER
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+/* column names */
|
|
|
531551 |
+struct colinfo {
|
|
|
531551 |
+ const char *name; /* header */
|
|
|
531551 |
+ double whint; /* width hint (N < 1 is in percent of termwidth) */
|
|
|
531551 |
+ int flags; /* SCOLS_FL_* */
|
|
|
531551 |
+ const char *help;
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+/* columns descriptions */
|
|
|
531551 |
+static const struct colinfo infos[] = {
|
|
|
531551 |
+ [COL_NS] = { "NS", 10, SCOLS_FL_RIGHT, N_("namespace identifier (inode number)") },
|
|
|
531551 |
+ [COL_TYPE] = { "TYPE", 5, 0, N_("kind of namespace") },
|
|
|
531551 |
+ [COL_PATH] = { "PATH", 0, 0, N_("path to the namespace")},
|
|
|
531551 |
+ [COL_NPROCS] = { "NPROCS", 5, SCOLS_FL_RIGHT, N_("number of processes in the namespace") },
|
|
|
531551 |
+ [COL_PID] = { "PID", 5, SCOLS_FL_RIGHT, N_("lowest PID in the namespace") },
|
|
|
531551 |
+ [COL_PPID] = { "PPID", 5, SCOLS_FL_RIGHT, N_("PPID of the PID") },
|
|
|
531551 |
+ [COL_COMMAND] = { "COMMAND", 0, SCOLS_FL_TRUNC, N_("command line of the PID")},
|
|
|
531551 |
+ [COL_UID] = { "UID", 0, SCOLS_FL_RIGHT, N_("UID of the PID")},
|
|
|
531551 |
+ [COL_USER] = { "USER", 0, 0, N_("username of the PID")}
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+static int columns[ARRAY_SIZE(infos) * 2];
|
|
|
531551 |
+static size_t ncolumns;
|
|
|
531551 |
+
|
|
|
531551 |
+enum {
|
|
|
531551 |
+ LSNS_ID_MNT = 0,
|
|
|
531551 |
+ LSNS_ID_NET,
|
|
|
531551 |
+ LSNS_ID_PID,
|
|
|
531551 |
+ LSNS_ID_UTS,
|
|
|
531551 |
+ LSNS_ID_IPC,
|
|
|
531551 |
+ LSNS_ID_USER
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+static char *ns_names[] = {
|
|
|
531551 |
+ [LSNS_ID_MNT] = "mnt",
|
|
|
531551 |
+ [LSNS_ID_NET] = "net",
|
|
|
531551 |
+ [LSNS_ID_PID] = "pid",
|
|
|
531551 |
+ [LSNS_ID_UTS] = "uts",
|
|
|
531551 |
+ [LSNS_ID_IPC] = "ipc",
|
|
|
531551 |
+ [LSNS_ID_USER] = "user"
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+struct lsns_namespace {
|
|
|
531551 |
+ ino_t id;
|
|
|
531551 |
+ int type; /* LSNS_* */
|
|
|
531551 |
+ int nprocs;
|
|
|
531551 |
+
|
|
|
531551 |
+ struct lsns_process *proc;
|
|
|
531551 |
+
|
|
|
531551 |
+ struct list_head namespaces; /* lsns->processes member */
|
|
|
531551 |
+ struct list_head processes; /* head of lsns_process *siblings */
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+struct lsns_process {
|
|
|
531551 |
+ pid_t pid; /* process PID */
|
|
|
531551 |
+ pid_t ppid; /* parent's PID */
|
|
|
531551 |
+ pid_t tpid; /* thread group */
|
|
|
531551 |
+ char state;
|
|
|
531551 |
+ uid_t uid;
|
|
|
531551 |
+
|
|
|
531551 |
+ ino_t ns_ids[ARRAY_SIZE(ns_names)];
|
|
|
531551 |
+ struct list_head ns_siblings[ARRAY_SIZE(ns_names)];
|
|
|
531551 |
+
|
|
|
531551 |
+ struct list_head processes; /* list of processes */
|
|
|
531551 |
+
|
|
|
531551 |
+ struct libscols_line *outline;
|
|
|
531551 |
+ struct lsns_process *parent;
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+struct lsns {
|
|
|
531551 |
+ struct list_head processes;
|
|
|
531551 |
+ struct list_head namespaces;
|
|
|
531551 |
+
|
|
|
531551 |
+ pid_t fltr_pid; /* filter out by PID */
|
|
|
531551 |
+ ino_t fltr_ns; /* filter out by namespace */
|
|
|
531551 |
+ int fltr_types[ARRAY_SIZE(ns_names)];
|
|
|
531551 |
+ int fltr_ntypes;
|
|
|
531551 |
+
|
|
|
531551 |
+ unsigned int raw : 1,
|
|
|
531551 |
+ tree : 1,
|
|
|
531551 |
+ list : 1,
|
|
|
531551 |
+ notrunc : 1,
|
|
|
531551 |
+ no_headings: 1;
|
|
|
531551 |
+};
|
|
|
531551 |
+
|
|
|
531551 |
+static void lsns_init_debug(void)
|
|
|
531551 |
+{
|
|
|
531551 |
+ __UL_INIT_DEBUG(lsns, LSNS_DEBUG_, 0, LSNS_DEBUG);
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int ns_name2type(const char *name)
|
|
|
531551 |
+{
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < ARRAY_SIZE(ns_names); i++) {
|
|
|
531551 |
+ if (strcmp(ns_names[i], name) == 0)
|
|
|
531551 |
+ return i;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ return -1;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int column_name_to_id(const char *name, size_t namesz)
|
|
|
531551 |
+{
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+
|
|
|
531551 |
+ assert(name);
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < ARRAY_SIZE(infos); i++) {
|
|
|
531551 |
+ const char *cn = infos[i].name;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
|
|
|
531551 |
+ return i;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ warnx(_("unknown column: %s"), name);
|
|
|
531551 |
+ return -1;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static inline int get_column_id(int num)
|
|
|
531551 |
+{
|
|
|
531551 |
+ assert(num >= 0);
|
|
|
531551 |
+ assert((size_t) num < ncolumns);
|
|
|
531551 |
+ assert(columns[num] < (int) ARRAY_SIZE(infos));
|
|
|
531551 |
+
|
|
|
531551 |
+ return columns[num];
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static inline const struct colinfo *get_column_info(unsigned num)
|
|
|
531551 |
+{
|
|
|
531551 |
+ return &infos[ get_column_id(num) ];
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static ino_t get_ns_ino(int dir, const char *nsname, ino_t *ino)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct stat st;
|
|
|
531551 |
+ char path[16];
|
|
|
531551 |
+
|
|
|
531551 |
+ snprintf(path, sizeof(path), "ns/%s", nsname);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (fstatat(dir, path, &st, 0) != 0)
|
|
|
531551 |
+ return -errno;
|
|
|
531551 |
+ *ino = st.st_ino;
|
|
|
531551 |
+ return 0;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+
|
|
|
531551 |
+static int read_process(struct lsns *ls, pid_t pid)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct lsns_process *p = NULL;
|
|
|
531551 |
+ char buf[BUFSIZ];
|
|
|
531551 |
+ DIR *dir;
|
|
|
531551 |
+ int rc = 0, fd;
|
|
|
531551 |
+ FILE *f = NULL;
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+ struct stat st;
|
|
|
531551 |
+
|
|
|
531551 |
+ DBG(PROC, ul_debug("reading %d", (int) pid));
|
|
|
531551 |
+
|
|
|
531551 |
+ snprintf(buf, sizeof(buf), "/proc/%d", pid);
|
|
|
531551 |
+ dir = opendir(buf);
|
|
|
531551 |
+ if (!dir)
|
|
|
531551 |
+ return -errno;
|
|
|
531551 |
+
|
|
|
531551 |
+ p = xcalloc(1, sizeof(*p));
|
|
|
531551 |
+ if (!p) {
|
|
|
531551 |
+ rc = -ENOMEM;
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ if (fstat(dirfd(dir), &st) == 0) {
|
|
|
531551 |
+ p->uid = st.st_uid;
|
|
|
531551 |
+ add_uid(uid_cache, st.st_uid);
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ fd = openat(dirfd(dir), "stat", O_RDONLY);
|
|
|
531551 |
+ if (fd < 0) {
|
|
|
531551 |
+ rc = -errno;
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ if (!(f = fdopen(fd, "r"))) {
|
|
|
531551 |
+ rc = -errno;
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ rc = fscanf(f, "%d %*s %c %d*[^\n]", &p->pid, &p->state, &p->ppid);
|
|
|
531551 |
+ if (rc != 3) {
|
|
|
531551 |
+ rc = rc < 0 ? -errno : -EINVAL;
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ rc = 0;
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < ARRAY_SIZE(p->ns_ids); i++) {
|
|
|
531551 |
+ INIT_LIST_HEAD(&p->ns_siblings[i]);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ls->fltr_types[i])
|
|
|
531551 |
+ continue;
|
|
|
531551 |
+
|
|
|
531551 |
+ rc = get_ns_ino(dirfd(dir), ns_names[i], &p->ns_ids[i]);
|
|
|
531551 |
+ if (rc && rc != -EACCES)
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+ rc = 0;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ INIT_LIST_HEAD(&p->processes);
|
|
|
531551 |
+
|
|
|
531551 |
+ DBG(PROC, ul_debugobj(p, "new pid=%d", p->pid));
|
|
|
531551 |
+ list_add_tail(&p->processes, &ls->processes);
|
|
|
531551 |
+done:
|
|
|
531551 |
+ if (f)
|
|
|
531551 |
+ fclose(f);
|
|
|
531551 |
+ closedir(dir);
|
|
|
531551 |
+ if (rc)
|
|
|
531551 |
+ free(p);
|
|
|
531551 |
+ return rc;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int read_processes(struct lsns *ls)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct proc_processes *proc = NULL;
|
|
|
531551 |
+ pid_t pid;
|
|
|
531551 |
+ int rc = 0;
|
|
|
531551 |
+
|
|
|
531551 |
+ DBG(PROC, ul_debug("opening /proc"));
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!(proc = proc_open_processes())) {
|
|
|
531551 |
+ rc = -errno;
|
|
|
531551 |
+ goto done;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ while (proc_next_pid(proc, &pid) == 0) {
|
|
|
531551 |
+ rc = read_process(ls, pid);
|
|
|
531551 |
+ if (rc && rc != -EACCES && rc != -ENOENT)
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ rc = 0;
|
|
|
531551 |
+ }
|
|
|
531551 |
+done:
|
|
|
531551 |
+ DBG(PROC, ul_debug("closing /proc"));
|
|
|
531551 |
+ proc_close_processes(proc);
|
|
|
531551 |
+ return rc;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static struct lsns_namespace *get_namespace(struct lsns *ls, ino_t ino)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct list_head *p;
|
|
|
531551 |
+
|
|
|
531551 |
+ list_for_each(p, &ls->namespaces) {
|
|
|
531551 |
+ struct lsns_namespace *ns = list_entry(p, struct lsns_namespace, namespaces);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (ns->id == ino)
|
|
|
531551 |
+ return ns;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ return NULL;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int namespace_has_process(struct lsns_namespace *ns, pid_t pid)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct list_head *p;
|
|
|
531551 |
+
|
|
|
531551 |
+ list_for_each(p, &ns->processes) {
|
|
|
531551 |
+ struct lsns_process *proc = list_entry(p, struct lsns_process, ns_siblings[ns->type]);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (proc->pid == pid)
|
|
|
531551 |
+ return 1;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ return 0;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static struct lsns_namespace *add_namespace(struct lsns *ls, int type, ino_t ino)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct lsns_namespace *ns = xcalloc(1, sizeof(*ns));
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ns)
|
|
|
531551 |
+ return NULL;
|
|
|
531551 |
+
|
|
|
531551 |
+ DBG(NS, ul_debugobj(ns, "new %s[%ju]", ns_names[type], (uintmax_t)ino));
|
|
|
531551 |
+
|
|
|
531551 |
+ INIT_LIST_HEAD(&ns->processes);
|
|
|
531551 |
+ INIT_LIST_HEAD(&ns->namespaces);
|
|
|
531551 |
+
|
|
|
531551 |
+ ns->type = type;
|
|
|
531551 |
+ ns->id = ino;
|
|
|
531551 |
+
|
|
|
531551 |
+ list_add_tail(&ns->namespaces, &ls->namespaces);
|
|
|
531551 |
+ return ns;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int add_process_to_namespace(struct lsns *ls, struct lsns_namespace *ns, struct lsns_process *proc)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct list_head *p;
|
|
|
531551 |
+
|
|
|
531551 |
+ DBG(NS, ul_debugobj(ns, "add process [%p] pid=%d to %s[%ju]",
|
|
|
531551 |
+ proc, proc->pid, ns_names[ns->type], (uintmax_t)ns->id));
|
|
|
531551 |
+
|
|
|
531551 |
+ list_for_each(p, &ls->processes) {
|
|
|
531551 |
+ struct lsns_process *xproc = list_entry(p, struct lsns_process, processes);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (xproc->pid == proc->ppid) /* my parent */
|
|
|
531551 |
+ proc->parent = xproc;
|
|
|
531551 |
+ else if (xproc->ppid == proc->pid) /* my child */
|
|
|
531551 |
+ xproc->parent = proc;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ list_add_tail(&proc->ns_siblings[ns->type], &ns->processes);
|
|
|
531551 |
+ ns->nprocs++;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ns->proc || ns->proc->pid > proc->pid)
|
|
|
531551 |
+ ns->proc = proc;
|
|
|
531551 |
+
|
|
|
531551 |
+ return 0;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int cmp_namespaces(struct list_head *a, struct list_head *b,
|
|
|
531551 |
+ __attribute__((__unused__)) void *data)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct lsns_namespace *xa = list_entry(a, struct lsns_namespace, namespaces),
|
|
|
531551 |
+ *xb = list_entry(b, struct lsns_namespace, namespaces);
|
|
|
531551 |
+
|
|
|
531551 |
+ return cmp_numbers(xa->id, xb->id);
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int read_namespaces(struct lsns *ls)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct list_head *p;
|
|
|
531551 |
+
|
|
|
531551 |
+ DBG(NS, ul_debug("reading namespace"));
|
|
|
531551 |
+
|
|
|
531551 |
+ list_for_each(p, &ls->processes) {
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+ struct lsns_namespace *ns;
|
|
|
531551 |
+ struct lsns_process *proc = list_entry(p, struct lsns_process, processes);
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < ARRAY_SIZE(proc->ns_ids); i++) {
|
|
|
531551 |
+ if (proc->ns_ids[i] == 0)
|
|
|
531551 |
+ continue;
|
|
|
531551 |
+ if (!(ns = get_namespace(ls, proc->ns_ids[i]))) {
|
|
|
531551 |
+ ns = add_namespace(ls, i, proc->ns_ids[i]);
|
|
|
531551 |
+ if (!ns)
|
|
|
531551 |
+ return -ENOMEM;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ add_process_to_namespace(ls, ns, proc);
|
|
|
531551 |
+ }
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ list_sort(&ls->namespaces, cmp_namespaces, NULL);
|
|
|
531551 |
+
|
|
|
531551 |
+ return 0;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static void add_scols_line(struct lsns *ls, struct libscols_table *table,
|
|
|
531551 |
+ struct lsns_namespace *ns, struct lsns_process *proc)
|
|
|
531551 |
+{
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+ struct libscols_line *line;
|
|
|
531551 |
+
|
|
|
531551 |
+ assert(ns);
|
|
|
531551 |
+ assert(table);
|
|
|
531551 |
+
|
|
|
531551 |
+ line = scols_table_new_line(table,
|
|
|
531551 |
+ ls->tree && proc->parent ? proc->parent->outline : NULL);
|
|
|
531551 |
+ if (!line) {
|
|
|
531551 |
+ warn(_("failed to add line to output"));
|
|
|
531551 |
+ return;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < ncolumns; i++) {
|
|
|
531551 |
+ char *str = NULL;
|
|
|
531551 |
+
|
|
|
531551 |
+ switch (get_column_id(i)) {
|
|
|
531551 |
+ case COL_NS:
|
|
|
531551 |
+ xasprintf(&str, "%ju", (uintmax_t)ns->id);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_PID:
|
|
|
531551 |
+ xasprintf(&str, "%d", (int) proc->pid);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_PPID:
|
|
|
531551 |
+ xasprintf(&str, "%d", (int) proc->ppid);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_TYPE:
|
|
|
531551 |
+ xasprintf(&str, "%s", ns_names[ns->type]);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_NPROCS:
|
|
|
531551 |
+ xasprintf(&str, "%d", ns->nprocs);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_COMMAND:
|
|
|
531551 |
+ str = proc_get_command(proc->pid);
|
|
|
531551 |
+ if (!str)
|
|
|
531551 |
+ str = proc_get_command_name(proc->pid);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_PATH:
|
|
|
531551 |
+ xasprintf(&str, "/proc/%d/ns/%s", (int) proc->pid, ns_names[ns->type]);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_UID:
|
|
|
531551 |
+ xasprintf(&str, "%d", (int) proc->uid);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case COL_USER:
|
|
|
531551 |
+ xasprintf(&str, "%s", get_id(uid_cache, proc->uid)->name);
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ default:
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ if (str)
|
|
|
531551 |
+ scols_line_set_data(line, i, str);
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ proc->outline = line;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static struct libscols_table *init_scols_table(struct lsns *ls)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct libscols_table *tab;
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+
|
|
|
531551 |
+ tab = scols_new_table();
|
|
|
531551 |
+ if (!tab) {
|
|
|
531551 |
+ warn(_("failed to initialize output table"));
|
|
|
531551 |
+ return NULL;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ scols_table_enable_raw(tab, ls->raw);
|
|
|
531551 |
+ scols_table_enable_noheadings(tab, ls->no_headings);
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < ncolumns; i++) {
|
|
|
531551 |
+ const struct colinfo *col = get_column_info(i);
|
|
|
531551 |
+ int flags = col->flags;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (ls->notrunc)
|
|
|
531551 |
+ flags &= ~SCOLS_FL_TRUNC;
|
|
|
531551 |
+ if (ls->tree && get_column_id(i) == COL_COMMAND)
|
|
|
531551 |
+ flags |= SCOLS_FL_TREE;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!scols_table_new_column(tab, col->name, col->whint, flags)) {
|
|
|
531551 |
+ warnx(_("failed to initialize output column"));
|
|
|
531551 |
+ goto err;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ return tab;
|
|
|
531551 |
+err:
|
|
|
531551 |
+ scols_unref_table(tab);
|
|
|
531551 |
+ return NULL;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static int show_namespaces(struct lsns *ls)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct libscols_table *tab;
|
|
|
531551 |
+ struct list_head *p;
|
|
|
531551 |
+ int rc = 0;
|
|
|
531551 |
+
|
|
|
531551 |
+ tab = init_scols_table(ls);
|
|
|
531551 |
+ if (!tab)
|
|
|
531551 |
+ return -ENOMEM;
|
|
|
531551 |
+
|
|
|
531551 |
+ list_for_each(p, &ls->namespaces) {
|
|
|
531551 |
+ struct lsns_namespace *ns = list_entry(p, struct lsns_namespace, namespaces);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (ls->fltr_pid != 0 && !namespace_has_process(ns, ls->fltr_pid))
|
|
|
531551 |
+ continue;
|
|
|
531551 |
+
|
|
|
531551 |
+ add_scols_line(ls, tab, ns, ns->proc);
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ scols_print_table(tab);
|
|
|
531551 |
+ scols_unref_table(tab);
|
|
|
531551 |
+ return rc;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static void show_process(struct lsns *ls, struct libscols_table *tab,
|
|
|
531551 |
+ struct lsns_process *proc, struct lsns_namespace *ns)
|
|
|
531551 |
+{
|
|
|
531551 |
+ /*
|
|
|
531551 |
+ * create a tree from parent->child relation, but only if the parent is
|
|
|
531551 |
+ * within the same namespace
|
|
|
531551 |
+ */
|
|
|
531551 |
+ if (ls->tree
|
|
|
531551 |
+ && proc->parent
|
|
|
531551 |
+ && !proc->parent->outline
|
|
|
531551 |
+ && proc->parent->ns_ids[ns->type] == proc->ns_ids[ns->type])
|
|
|
531551 |
+ show_process(ls, tab, proc->parent, ns);
|
|
|
531551 |
+
|
|
|
531551 |
+ add_scols_line(ls, tab, ns, proc);
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+
|
|
|
531551 |
+static int show_namespace_processes(struct lsns *ls, struct lsns_namespace *ns)
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct libscols_table *tab;
|
|
|
531551 |
+ struct list_head *p;
|
|
|
531551 |
+
|
|
|
531551 |
+ tab = init_scols_table(ls);
|
|
|
531551 |
+ if (!tab)
|
|
|
531551 |
+ return -ENOMEM;
|
|
|
531551 |
+
|
|
|
531551 |
+ list_for_each(p, &ns->processes) {
|
|
|
531551 |
+ struct lsns_process *proc = list_entry(p, struct lsns_process, ns_siblings[ns->type]);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!proc->outline)
|
|
|
531551 |
+ show_process(ls, tab, proc, ns);
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+
|
|
|
531551 |
+ scols_print_table(tab);
|
|
|
531551 |
+ scols_unref_table(tab);
|
|
|
531551 |
+ return 0;
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+static void __attribute__ ((__noreturn__)) usage(FILE * out)
|
|
|
531551 |
+{
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+
|
|
|
531551 |
+ fputs(USAGE_HEADER, out);
|
|
|
531551 |
+
|
|
|
531551 |
+ fprintf(out,
|
|
|
531551 |
+ _(" %s [options] [<namespace>]\n"), program_invocation_short_name);
|
|
|
531551 |
+
|
|
|
531551 |
+ fputs(USAGE_SEPARATOR, out);
|
|
|
531551 |
+ fputs(_("List system namespaces.\n"), out);
|
|
|
531551 |
+
|
|
|
531551 |
+ fputs(USAGE_OPTIONS, out);
|
|
|
531551 |
+ fputs(_(" -l, --list use list format output\n"), out);
|
|
|
531551 |
+ fputs(_(" -n, --noheadings don't print headings\n"), out);
|
|
|
531551 |
+ fputs(_(" -o, --output <list> define which output columns to use\n"), out);
|
|
|
531551 |
+ fputs(_(" -p, --task <pid> print process namespaces\n"), out);
|
|
|
531551 |
+ fputs(_(" -r, --raw use the raw output format\n"), out);
|
|
|
531551 |
+ fputs(_(" -u, --notruncate don't truncate text in columns\n"), out);
|
|
|
531551 |
+ fputs(_(" -t, --type <name> namespace type (mnt, net, ipc, user, pid, uts)\n"), out);
|
|
|
531551 |
+
|
|
|
531551 |
+ fputs(USAGE_SEPARATOR, out);
|
|
|
531551 |
+ fputs(USAGE_HELP, out);
|
|
|
531551 |
+ fputs(USAGE_VERSION, out);
|
|
|
531551 |
+
|
|
|
531551 |
+ fputs(_("\nAvailable columns (for --output):\n"), out);
|
|
|
531551 |
+
|
|
|
531551 |
+ for (i = 0; i < ARRAY_SIZE(infos); i++)
|
|
|
531551 |
+ fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
|
|
|
531551 |
+
|
|
|
531551 |
+ fprintf(out, USAGE_MAN_TAIL("lsns(8)"));
|
|
|
531551 |
+
|
|
|
531551 |
+ exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
531551 |
+}
|
|
|
531551 |
+
|
|
|
531551 |
+
|
|
|
531551 |
+int main(int argc, char *argv[])
|
|
|
531551 |
+{
|
|
|
531551 |
+ struct lsns ls;
|
|
|
531551 |
+ int c;
|
|
|
531551 |
+ int r = 0;
|
|
|
531551 |
+ char *outarg = NULL;
|
|
|
531551 |
+ static const struct option long_opts[] = {
|
|
|
531551 |
+ { "task", required_argument, NULL, 'p' },
|
|
|
531551 |
+ { "help", no_argument, NULL, 'h' },
|
|
|
531551 |
+ { "output", required_argument, NULL, 'o' },
|
|
|
531551 |
+ { "notruncate", no_argument, NULL, 'u' },
|
|
|
531551 |
+ { "version", no_argument, NULL, 'V' },
|
|
|
531551 |
+ { "noheadings", no_argument, NULL, 'n' },
|
|
|
531551 |
+ { "list", no_argument, NULL, 'l' },
|
|
|
531551 |
+ { "raw", no_argument, NULL, 'r' },
|
|
|
531551 |
+ { "type", required_argument, NULL, 't' },
|
|
|
531551 |
+ { NULL, 0, NULL, 0 }
|
|
|
531551 |
+ };
|
|
|
531551 |
+
|
|
|
531551 |
+ static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
|
|
|
531551 |
+ { 'J','r' },
|
|
|
531551 |
+ { 0 }
|
|
|
531551 |
+ };
|
|
|
531551 |
+ int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
|
|
|
531551 |
+
|
|
|
531551 |
+ setlocale(LC_ALL, "");
|
|
|
531551 |
+ bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
531551 |
+ textdomain(PACKAGE);
|
|
|
531551 |
+ atexit(close_stdout);
|
|
|
531551 |
+
|
|
|
531551 |
+ lsns_init_debug();
|
|
|
531551 |
+ memset(&ls, 0, sizeof(ls));
|
|
|
531551 |
+
|
|
|
531551 |
+ INIT_LIST_HEAD(&ls.processes);
|
|
|
531551 |
+ INIT_LIST_HEAD(&ls.namespaces);
|
|
|
531551 |
+
|
|
|
531551 |
+ while ((c = getopt_long(argc, argv,
|
|
|
531551 |
+ "lp:o:nruhVt:", long_opts, NULL)) != -1) {
|
|
|
531551 |
+
|
|
|
531551 |
+ err_exclusive_options(c, long_opts, excl, excl_st);
|
|
|
531551 |
+
|
|
|
531551 |
+ switch(c) {
|
|
|
531551 |
+ case 'l':
|
|
|
531551 |
+ ls.list = 1;
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case 'o':
|
|
|
531551 |
+ outarg = optarg;
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case 'V':
|
|
|
531551 |
+ printf(UTIL_LINUX_VERSION);
|
|
|
531551 |
+ return EXIT_SUCCESS;
|
|
|
531551 |
+ case 'p':
|
|
|
531551 |
+ ls.fltr_pid = strtos32_or_err(optarg, _("invalid PID argument"));
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case 'h':
|
|
|
531551 |
+ usage(stdout);
|
|
|
531551 |
+ case 'n':
|
|
|
531551 |
+ ls.no_headings = 1;
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case 'r':
|
|
|
531551 |
+ ls.raw = 1;
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case 'u':
|
|
|
531551 |
+ ls.notrunc = 1;
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ case 't':
|
|
|
531551 |
+ {
|
|
|
531551 |
+ int type = ns_name2type(optarg);
|
|
|
531551 |
+ if (type < 0)
|
|
|
531551 |
+ errx(EXIT_FAILURE, _("unknown namespace type: %s"), optarg);
|
|
|
531551 |
+ ls.fltr_types[type] = 1;
|
|
|
531551 |
+ ls.fltr_ntypes++;
|
|
|
531551 |
+ break;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ case '?':
|
|
|
531551 |
+ default:
|
|
|
531551 |
+ usage(stderr);
|
|
|
531551 |
+ }
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ls.fltr_ntypes) {
|
|
|
531551 |
+ size_t i;
|
|
|
531551 |
+ for (i = 0; i < ARRAY_SIZE(ns_names); i++)
|
|
|
531551 |
+ ls.fltr_types[i] = 1;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ if (optind < argc) {
|
|
|
531551 |
+ if (ls.fltr_pid)
|
|
|
531551 |
+ errx(EXIT_FAILURE, _("--task is mutually exclusive with <namespace>"));
|
|
|
531551 |
+ ls.fltr_ns = strtou64_or_err(argv[optind], _("invalid namespace argument"));
|
|
|
531551 |
+ ls.tree = ls.list ? 0 : 1;
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ncolumns) {
|
|
|
531551 |
+ columns[ncolumns++] = COL_PID;
|
|
|
531551 |
+ columns[ncolumns++] = COL_PPID;
|
|
|
531551 |
+ columns[ncolumns++] = COL_USER;
|
|
|
531551 |
+ columns[ncolumns++] = COL_COMMAND;
|
|
|
531551 |
+ }
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ncolumns) {
|
|
|
531551 |
+ columns[ncolumns++] = COL_NS;
|
|
|
531551 |
+ columns[ncolumns++] = COL_TYPE;
|
|
|
531551 |
+ columns[ncolumns++] = COL_NPROCS;
|
|
|
531551 |
+ columns[ncolumns++] = COL_PID;
|
|
|
531551 |
+ columns[ncolumns++] = COL_USER;
|
|
|
531551 |
+ columns[ncolumns++] = COL_COMMAND;
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
|
|
|
531551 |
+ (int *) &ncolumns, column_name_to_id) < 0)
|
|
|
531551 |
+ return EXIT_FAILURE;
|
|
|
531551 |
+
|
|
|
531551 |
+ scols_init_debug(0);
|
|
|
531551 |
+
|
|
|
531551 |
+ uid_cache = new_idcache();
|
|
|
531551 |
+ if (!uid_cache)
|
|
|
531551 |
+ err(EXIT_FAILURE, _("failed to allocate UID cache"));
|
|
|
531551 |
+
|
|
|
531551 |
+ r = read_processes(&ls);
|
|
|
531551 |
+ if (!r)
|
|
|
531551 |
+ r = read_namespaces(&ls);
|
|
|
531551 |
+ if (!r) {
|
|
|
531551 |
+ if (ls.fltr_ns) {
|
|
|
531551 |
+ struct lsns_namespace *ns = get_namespace(&ls, ls.fltr_ns);
|
|
|
531551 |
+
|
|
|
531551 |
+ if (!ns)
|
|
|
531551 |
+ errx(EXIT_FAILURE, _("not found namespace: %ju"), (uintmax_t) ls.fltr_ns);
|
|
|
531551 |
+ r = show_namespace_processes(&ls, ns);
|
|
|
531551 |
+ } else
|
|
|
531551 |
+ r = show_namespaces(&ls);
|
|
|
531551 |
+ }
|
|
|
531551 |
+
|
|
|
531551 |
+ free_idcache(uid_cache);
|
|
|
531551 |
+ return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
531551 |
+}
|
|
|
531551 |
--
|
|
|
531551 |
2.7.4
|
|
|
531551 |
|