dcavalca / rpms / util-linux

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