dcavalca / rpms / util-linux

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