dcavalca / rpms / util-linux

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