6717ab
diff -up util-linux-2.23.2/sys-utils/Makemodule.am.kzak util-linux-2.23.2/sys-utils/Makemodule.am
6717ab
--- util-linux-2.23.2/sys-utils/Makemodule.am.kzak	2014-09-25 14:16:33.526384729 +0200
6717ab
+++ util-linux-2.23.2/sys-utils/Makemodule.am	2014-09-25 14:15:34.861825005 +0200
6717ab
@@ -290,6 +290,7 @@ usrbin_exec_PROGRAMS += unshare
6717ab
 dist_man_MANS += sys-utils/unshare.1
6717ab
 unshare_SOURCES = sys-utils/unshare.c
6717ab
 unshare_LDADD = $(LDADD) libcommon.la
6717ab
+unshare_CFLAGS = $(AM_CFLAGS) -I$(ul_libmount_incdir)
6717ab
 endif
6717ab
 
6717ab
 if BUILD_NSENTER
6717ab
diff -up util-linux-2.23.2/sys-utils/unshare.1.kzak util-linux-2.23.2/sys-utils/unshare.1
6717ab
--- util-linux-2.23.2/sys-utils/unshare.1.kzak	2014-09-25 14:14:30.194208005 +0200
6717ab
+++ util-linux-2.23.2/sys-utils/unshare.1	2014-09-25 14:15:17.617660476 +0200
6717ab
@@ -1,63 +1,82 @@
6717ab
 .\" Process this file with
6717ab
 .\" groff -man -Tascii lscpu.1
6717ab
 .\"
6717ab
-.TH UNSHARE 1 "January 2013" "util-linux" "User Commands"
6717ab
+.TH UNSHARE 1 "July 2013" "util-linux" "User Commands"
6717ab
 .SH NAME
6717ab
 unshare \- run program with some namespaces unshared from parent
6717ab
 .SH SYNOPSIS
6717ab
 .B unshare
6717ab
 .RI [ options ]
6717ab
-program
6717ab
+.I program
6717ab
 .RI [ arguments ]
6717ab
 .SH DESCRIPTION
6717ab
-Unshares specified namespaces from parent process and then executes specified
6717ab
-program. Unshareable namespaces are:
6717ab
+Unshares the indicated namespaces from the parent process and then executes
6717ab
+the specified program.  The namespaces to be unshared are indicated via
6717ab
+options.  Unshareable namespaces are:
6717ab
 .TP
6717ab
 .BR "mount namespace"
6717ab
-mounting and unmounting filesystems will not affect rest of the system
6717ab
+Mounting and unmounting filesystems will not affect the rest of the system
6717ab
 (\fBCLONE_NEWNS\fP flag), except for filesystems which are explicitly marked as
6717ab
-shared (by mount --make-shared). See /proc/self/mountinfo for the shared flags.
6717ab
+shared (with \fBmount --make-shared\fP; see \fI/proc/self/mountinfo\fP for the
6717ab
+\fBshared\fP flags).
6717ab
+
6717ab
+It's recommended to use \fBmount --make-rprivate\fP or \fBmount --make-rslave\fP
6717ab
+after \fBunshare --mount\fP to make sure that mountpoints in the new namespace
6717ab
+are really unshared from parental namespace.
6717ab
 .TP
6717ab
 .BR "UTS namespace"
6717ab
-setting hostname, domainname will not affect rest of the system
6717ab
-(\fBCLONE_NEWUTS\fP flag).
6717ab
+Setting hostname or domainname will not affect the rest of the system.
6717ab
+(\fBCLONE_NEWUTS\fP flag)
6717ab
 .TP
6717ab
 .BR "IPC namespace"
6717ab
-process will have independent namespace for System V message queues, semaphore
6717ab
-sets and shared memory segments (\fBCLONE_NEWIPC\fP flag).
6717ab
+The process will have an independent namespace for System V message queues,
6717ab
+semaphore sets and shared memory segments.  (\fBCLONE_NEWIPC\fP flag)
6717ab
 .TP
6717ab
 .BR "network namespace"
6717ab
-process will have independent IPv4 and IPv6 stacks, IP routing tables, firewall
6717ab
-rules, the \fI/proc/net\fP and \fI/sys/class/net\fP directory trees, sockets
6717ab
-etc. (\fBCLONE_NEWNET\fP flag).
6717ab
+The process will have independent IPv4 and IPv6 stacks, IP routing tables,
6717ab
+firewall rules, the \fI/proc/net\fP and \fI/sys/class/net\fP directory trees,
6717ab
+sockets, etc.  (\fBCLONE_NEWNET\fP flag)
6717ab
 .TP
6717ab
 .BR "pid namespace"
6717ab
-children will have a distinct set of pid to process mappings than their parent.
6717ab
-(\fBCLONE_NEWPID\fP flag).
6717ab
+Children will have a distinct set of PID to process mappings from their parent.
6717ab
+(\fBCLONE_NEWPID\fP flag)
6717ab
 .PP
6717ab
-See the \fBclone\fR(2) for exact semantics of the flags.
6717ab
+See \fBclone\fR(2) for the exact semantics of the flags.
6717ab
 .SH OPTIONS
6717ab
 .TP
6717ab
 .BR \-h , " \-\-help"
6717ab
-Print a help message,
6717ab
-.TP
6717ab
-.BR \-m , " \-\-mount"
6717ab
-Unshare the mount namespace,
6717ab
-.TP
6717ab
-.BR \-u , " \-\-uts"
6717ab
-Unshare the UTS namespace,
6717ab
+Display help text and exit.
6717ab
 .TP
6717ab
 .BR \-i , " \-\-ipc"
6717ab
-Unshare the IPC namespace,
6717ab
+Unshare the IPC namespace.
6717ab
+.TP
6717ab
+.BR \-m , " \-\-mount"
6717ab
+Unshare the mount namespace.
6717ab
 .TP
6717ab
 .BR \-n , " \-\-net"
6717ab
 Unshare the network namespace.
6717ab
 .TP
6717ab
 .BR \-p , " \-\-pid"
6717ab
 Unshare the pid namespace.
6717ab
+See also the \fB--fork\fP and \fB--mount-proc\fP options.
6717ab
+.TP
6717ab
+.BR \-u , " \-\-uts"
6717ab
+Unshare the UTS namespace.
6717ab
+.TP
6717ab
+.BR \-f , " \-\-fork"
6717ab
+Fork the specified \fIprogram\fR as a child process of \fBunshare\fR rather than
6717ab
+running it directly.  This is useful when creating a new pid namespace.
6717ab
+.TP
6717ab
+.BR \-\-mount-proc "[=\fImountpoint\fP]"
6717ab
+Just before running the program, mount the proc filesystem at the \fImountpoint\fP
6717ab
+(default is /proc).  This is useful when creating a new pid namespace.  It also
6717ab
+implies creating a new mount namespace since the /proc mount would otherwise
6717ab
+mess up existing programs on the system. The new proc filesystem is explicitly
6717ab
+mounted as private (by MS_PRIVATE|MS_REC).
6717ab
 .SH SEE ALSO
6717ab
 .BR unshare (2),
6717ab
-.BR clone (2)
6717ab
+.BR clone (2),
6717ab
+.BR mount (8)
6717ab
 .SH BUGS
6717ab
 None known so far.
6717ab
 .SH AUTHOR
6717ab
diff -up util-linux-2.23.2/sys-utils/unshare.c.kzak util-linux-2.23.2/sys-utils/unshare.c
6717ab
--- util-linux-2.23.2/sys-utils/unshare.c.kzak	2014-09-25 14:14:30.194208005 +0200
6717ab
+++ util-linux-2.23.2/sys-utils/unshare.c	2014-09-25 14:15:34.861825005 +0200
6717ab
@@ -24,12 +24,19 @@
6717ab
 #include <stdio.h>
6717ab
 #include <stdlib.h>
6717ab
 #include <unistd.h>
6717ab
+#include <sys/wait.h>
6717ab
+#include <sys/mount.h>
6717ab
+
6717ab
+/* we only need some defines missing in sys/mount.h, no libmount linkage */
6717ab
+#include <libmount.h>
6717ab
 
6717ab
 #include "nls.h"
6717ab
 #include "c.h"
6717ab
-#include "closestream.h"
6717ab
 #include "namespace.h"
6717ab
 #include "exec_shell.h"
6717ab
+#include "xalloc.h"
6717ab
+#include "pathnames.h"
6717ab
+
6717ab
 
6717ab
 static void usage(int status)
6717ab
 {
6717ab
@@ -40,11 +47,13 @@ static void usage(int status)
6717ab
 	      _(" %s [options] <program> [args...]\n"),	program_invocation_short_name);
6717ab
 
6717ab
 	fputs(USAGE_OPTIONS, out);
6717ab
-	fputs(_(" -m, --mount       unshare mounts namespace\n"), out);
6717ab
-	fputs(_(" -u, --uts         unshare UTS namespace (hostname etc)\n"), out);
6717ab
-	fputs(_(" -i, --ipc         unshare System V IPC namespace\n"), out);
6717ab
-	fputs(_(" -n, --net         unshare network namespace\n"), out);
6717ab
-	fputs(_(" -p, --pid         unshare pid namespace\n"), out);
6717ab
+	fputs(_(" -m, --mount               unshare mounts namespace\n"), out);
6717ab
+	fputs(_(" -u, --uts                 unshare UTS namespace (hostname etc)\n"), out);
6717ab
+	fputs(_(" -i, --ipc                 unshare System V IPC namespace\n"), out);
6717ab
+	fputs(_(" -n, --net                 unshare network namespace\n"), out);
6717ab
+	fputs(_(" -p, --pid                 unshare pid namespace\n"), out);
6717ab
+	fputs(_(" -f, --fork                fork before launching <program>\n"), out);
6717ab
+	fputs(_("     --mount-proc[=<dir>]  mount proc filesystem first (implies --mount)\n"), out);
6717ab
 
6717ab
 	fputs(USAGE_SEPARATOR, out);
6717ab
 	fputs(USAGE_HELP, out);
6717ab
@@ -56,6 +65,9 @@ static void usage(int status)
6717ab
 
6717ab
 int main(int argc, char *argv[])
6717ab
 {
6717ab
+	enum {
6717ab
+		OPT_MOUNTPROC = CHAR_MAX + 1
6717ab
+	};
6717ab
 	static const struct option longopts[] = {
6717ab
 		{ "help", no_argument, 0, 'h' },
6717ab
 		{ "version", no_argument, 0, 'V'},
6717ab
@@ -64,20 +76,24 @@ int main(int argc, char *argv[])
6717ab
 		{ "ipc", no_argument, 0, 'i' },
6717ab
 		{ "net", no_argument, 0, 'n' },
6717ab
 		{ "pid", no_argument, 0, 'p' },
6717ab
+		{ "fork", no_argument, 0, 'f' },
6717ab
+		{ "mount-proc", optional_argument, 0, OPT_MOUNTPROC },
6717ab
 		{ NULL, 0, 0, 0 }
6717ab
 	};
6717ab
 
6717ab
 	int unshare_flags = 0;
6717ab
+	int c, forkit = 0;
6717ab
+	const char *procmnt = NULL;
6717ab
 
6717ab
-	int c;
6717ab
-
6717ab
-	setlocale(LC_MESSAGES, "");
6717ab
+	setlocale(LC_ALL, "");
6717ab
 	bindtextdomain(PACKAGE, LOCALEDIR);
6717ab
 	textdomain(PACKAGE);
6717ab
-	atexit(close_stdout);
6717ab
 
6717ab
-	while ((c = getopt_long(argc, argv, "hVmuinp", longopts, NULL)) != -1) {
6717ab
+	while ((c = getopt_long(argc, argv, "+fhVmuinp", longopts, NULL)) != -1) {
6717ab
 		switch (c) {
6717ab
+		case 'f':
6717ab
+			forkit = 1;
6717ab
+			break;
6717ab
 		case 'h':
6717ab
 			usage(EXIT_SUCCESS);
6717ab
 		case 'V':
6717ab
@@ -98,6 +114,10 @@ int main(int argc, char *argv[])
6717ab
 		case 'p':
6717ab
 			unshare_flags |= CLONE_NEWPID;
6717ab
 			break;
6717ab
+		case OPT_MOUNTPROC:
6717ab
+			unshare_flags |= CLONE_NEWNS;
6717ab
+			procmnt = optarg ? optarg : "/proc";
6717ab
+			break;
6717ab
 		default:
6717ab
 			usage(EXIT_FAILURE);
6717ab
 		}
6717ab
@@ -106,6 +126,31 @@ int main(int argc, char *argv[])
6717ab
 	if (-1 == unshare(unshare_flags))
6717ab
 		err(EXIT_FAILURE, _("unshare failed"));
6717ab
 
6717ab
+	if (forkit) {
6717ab
+		int status;
6717ab
+		pid_t pid = fork();
6717ab
+
6717ab
+		switch(pid) {
6717ab
+		case -1:
6717ab
+			err(EXIT_FAILURE, _("fork failed"));
6717ab
+		case 0:	/* child */
6717ab
+			break;
6717ab
+		default: /* parent */
6717ab
+			if (waitpid(pid, &status, 0) == -1)
6717ab
+				err(EXIT_FAILURE, _("waitpid failed"));
6717ab
+			if (WIFEXITED(status))
6717ab
+				return WEXITSTATUS(status);
6717ab
+			else if (WIFSIGNALED(status))
6717ab
+				kill(getpid(), WTERMSIG(status));
6717ab
+			err(EXIT_FAILURE, _("child exit failed"));
6717ab
+		}
6717ab
+	}
6717ab
+
6717ab
+	if (procmnt &&
6717ab
+	    (mount("none", procmnt, NULL, MS_PRIVATE|MS_REC, NULL) != 0 ||
6717ab
+	     mount("proc", procmnt, "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) != 0))
6717ab
+			err(EXIT_FAILURE, _("mount %s failed"), procmnt);
6717ab
+
6717ab
 	if (optind < argc) {
6717ab
 		execvp(argv[optind], argv + optind);
6717ab
 		err(EXIT_FAILURE, _("failed to execute %s"), argv[optind]);