.\" Copyright (C) 2006, Janak Desai .\" and Copyright (C) 2006, 2012 Michael Kerrisk .\" .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE) .\" Licensed under the GPL .\" %%%LICENSE_END .\" .\" Patch Justification: .\" unshare system call is needed to implement, using PAM, .\" per-security_context and/or per-user namespace to provide .\" polyinstantiated directories. Using unshare and bind mounts, a .\" PAM module can create private namespace with appropriate .\" directories(based on user's security context) bind mounted on .\" public directories such as /tmp, thus providing an instance of .\" /tmp that is based on user's security context. Without the .\" unshare system call, namespace separation can only be achieved .\" by clone, which would require porting and maintaining all commands .\" such as login, and su, that establish a user session. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2006 Yuichi SATO .\" all rights reserved. .\" Translated 2006-08-10 by Yuichi SATO , LDP v2.36 .\" Updated 2012-05-08, Akihiro MOTOKI .\" Updated 2013-03-26, Akihiro MOTOKI .\" Updated 2013-05-06, Akihiro MOTOKI .\" .TH UNSHARE 2 2014\-09\-21 Linux "Linux Programmer's Manual" .SH 名前 unshare \- プロセス実行コンテキストの一部を分離する .SH 書式 .nf \fB#include \fP .sp \fBint unshare(int \fP\fIflags\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .sp \fBunshare\fP(): .ad l .RS 4 .PD 0 .TP 4 glibc 2.14 以降: _GNU_SOURCE .TP 4 .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749 glibc 2.14 より前: _BSD_SOURCE || _SVID_SOURCE /* _GNU_SOURCE も定義される */ .PD .RE .ad b .SH 説明 \fBunshare\fP() を使うと、プロセスは他のプロセスと現在共有している 実行コンテキストの一部を分離することができる。 実行コンテキストの一部、たとえばマウント名前空間 (mount namespace) などは、新しいプロセスを \fBfork\fP(2) または \fBvfork\fP(2) を使って生成したときに、暗黙のうちに共有される。 一方、仮想メモリなどは、 \fBclone\fP(2) を使ってプロセスを生成するときに、明示的に共有することを要求できる。 \fBunshare\fP() の主な利用法は、プロセスが新しいプロセスを生成することなく、 共有実行コンテキストを制御することである。 \fIflags\fP 引き数はビットマスクであり、 実行コンテキストのどの部分の共有を解除するかを表す。 この引き数は、以下の定数の 0 個以上の OR で指定する: .TP \fBCLONE_FILES\fP \fBclone\fP(2) \fBCLONE_FILES\fP フラグの効果を取り消す。 ファイルディスクリプタテーブルを共有させず、 呼び出し元プロセスは他のプロセスとファイルディスクリプタを共有しなくなる。 .TP \fBCLONE_FS\fP \fBclone\fP(2) \fBCLONE_FS\fP フラグの効果を取り消す。ファイルシステム属性を共有させず、呼び出し元プロセスは、ルートディレクトリ (\fBchroot\fP(2))、カレントディレクトリ (\fBchdir\fP(2))、 umask (\fBumask\fP(2)) を他のプロセスと共有しなくなる。 .TP \fBCLONE_NEWIPC\fP (Linux 2.6.19 以降) このフラグは \fBclone\fP(2) \fBCLONE_NEWIPC\fP フラグと同じ効果を持つ。 System\ V IPC 名前空間を共有せず、呼び出し元プロセスは 他のプロセスとは 共有しない固有の System\ V IPC 名前空間のコピーを持つ。 このフラグを指定 すると、 \fBCLONE_SYSVSEM\fP も暗黙のうちに指定される。 \fBCLONE_NEWIPC\fP を 使用するには \fBCAP_SYS_ADMIN\fP ケーパビリティが必要である。 .TP \fBCLONE_NEWNET\fP (Linux 2.6.24 以降) このフラグは \fBclone\fP(2) \fBCLONE_NEWNET\fP フラグと同じ効果を持つ。ネット ワーク名前空間を共有せず、呼び出し元プロセスは他のプロセスとは共有しな い固有のネットワーク名前空間のコピーを持つ。\fBCLONE_NEWNET\fP を使用する には \fBCAP_SYS_ADMIN\fP ケーパビリティが必要である。 .TP \fBCLONE_NEWNS\fP .\" These flag name are inconsistent: .\" CLONE_NEWNS does the same thing in clone(), but CLONE_VM, .\" CLONE_FS, and CLONE_FILES reverse the action of the clone() .\" flags of the same name. このフラグは \fBclone\fP(2) \fBCLONE_NEWNS\fP フラグと同じ効果を持つ。 マウン ト名前空間を共有せず、呼び出し元プロセスは 他のプロセスとは共有しない固 有の名前空間のコピーを持つ。 このフラグを指定すると、 \fBCLONE_FS\fP も暗 黙のうちに指定される。 \fBCLONE_NEWNS\fP を使用するには \fBCAP_SYS_ADMIN\fP ケーパビリティが必要である。 .TP \fBCLONE_NEWPID\fP (Linux 3.8 以降) This flag has the same effect as the \fBclone\fP(2) \fBCLONE_NEWPID\fP flag. Unshare the PID namespace, so that the calling process has a new PID namespace for its children which is not shared with any previously existing process. The calling process is \fInot\fP moved into the new namespace. The first child created by the calling process will have the process ID 1 and will assume the role of \fBinit\fP(1) in the new namespace. \fBCLONE_NEWPID\fP automatically implies \fBCLONE_THREAD\fP as well. Use of \fBCLONE_NEWPID\fP requires the \fBCAP_SYS_ADMIN\fP capability. For further information, see \fBpid_namespaces\fP(7). .TP \fBCLONE_NEWUSER\fP (Linux 3.8 以降) This flag has the same effect as the \fBclone\fP(2) \fBCLONE_NEWUSER\fP flag. Unshare the user namespace, so that the calling process is moved into a new user namespace which is not shared with any previously existing process. As with the child process created by \fBclone\fP(2) with the \fBCLONE_NEWUSER\fP flag, the caller obtains a full set of capabilities in the new namespace. .IP .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71 .\" https://lwn.net/Articles/543273/ \fBCLONE_NEWUSER\fP requires that the calling process is not threaded; specifying \fBCLONE_NEWUSER\fP automatically implies \fBCLONE_THREAD\fP. Since Linux 3.9, \fBCLONE_NEWUSER\fP also automatically implies \fBCLONE_FS\fP. \fBCLONE_NEWUSER\fP requires that the user ID and group ID of the calling process are mapped to user IDs and group IDs in the user namespace of the calling process at the time of the call. For further information on user namespaces, see \fBuser_namespaces\fP(7). .TP \fBCLONE_NEWUTS\fP (Linux 2.6.19 以降) このフラグは \fBclone\fP(2) \fBCLONE_NEWUTS\fP フラグと同じ効果を持つ。 UTS IPC 名前空間を共有せず、呼び出し元プロセスは他のプロセスとは共有しない 固有の UTS IPC 名前空間のコピーを持つ。 このフラグを指定すると、 \fBCLONE_FS\fP も暗黙のうちに指定される。\fBCLONE_NEWUTS\fP を使用するには \fBCAP_SYS_ADMIN\fP ケーパビリティが必要である。 .TP \fBCLONE_SYSVSEM\fP (Linux 2.6.26 以降) .\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7 .\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared .\" (i.e., current->signal->count > 1), force CLONE_THREAD. This flag reverses the effect of the \fBclone\fP(2) \fBCLONE_SYSVSEM\fP flag. Unshare System\ V semaphore adjustment (\fIsemadj\fP) values, so that the calling process has a new empty \fIsemadj\fP list that is not shared with any other process. If this is the last process that has a reference to the process's current \fIsemadj\fP list, then the adjustments in that list are applied to the corresponding semaphores, as described in \fBsemop\fP(2). .PP .\" As at 3.9, the following forced implications also apply, .\" although the relevant flags are not yet implemented. .\" If CLONE_THREAD is set force CLONE_VM. .\" If CLONE_VM is set, force CLONE_SIGHAND. .\" .\" See kernel/fork.c::check_unshare_flags() In addition, \fBCLONE_THREAD\fP, \fBCLONE_SIGHAND\fP, and \fBCLONE_VM\fP can be specified in \fIflags\fP if the caller is single threaded (i.e., it is not sharing its address space with another process or thread). In this case, these flags have no effect. (Note also that specifying \fBCLONE_THREAD\fP automatically implies \fBCLONE_VM\fP, and specifying \fBCLONE_VM\fP automatically implies \fBCLONE_SIGHAND\fP.) If the process is multithreaded, then the use of these flags results in an error. .PP \fIflags\fP に 0 が指定された場合、 \fBunshare\fP() は何も行わないので、 呼び出し元プロセスの実行コンテキストは、何も変更されない。 .SH 返り値 成功した場合は 0 が返される。 失敗した場合は \-1 が返されて、 \fIerrno\fP にはエラーを示す値が設定される。 .SH エラー .TP \fBEINVAL\fP \fIflags\fP に不正なビットが指定された。 .TP \fBEINVAL\fP \fBCLONE_THREAD\fP, \fBCLONE_SIGHAND\fP, or \fBCLONE_VM\fP was specified in \fIflags\fP, and the caller is multithreaded. .TP \fBENOMEM\fP 呼び出し元のコンテキストのうち共有を解除する必要がある部分をコピーするために、 十分なメモリが確保できなかった。 .TP \fBEPERM\fP 呼び出し元プロセスはこの操作を行うのに必要な特権を持っていなかった。 .TP \fBEPERM\fP \fBCLONE_NEWUSER\fP was specified in \fIflags\fP, but either the effective user ID or the effective group ID of the caller does not have a mapping in the parent namespace (see \fBuser_namespaces\fP(7)). .TP \fBEPERM\fP (Linux 3.9 以降) .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d .\" FIXME What is the rationale for this restriction? \fBCLONE_NEWUSER was specified in\fP \fIflags\fP and the caller is in a chroot environment (i.e., the caller's root directory does not match the root directory of the mount namespace in which it resides). .TP \fBEUSERS\fP (Linux 3.11 以降) \fBCLONE_NEWUSER\fP was specified in \fIflags\fP, and the call would cause the limit on the number of nested user namespaces to be exceeded. See \fBuser_namespaces\fP(7). .SH バージョン \fBunshare\fP() システムコールは Linux カーネル 2.6.16 で追加された。 .SH 準拠 \fBunshare\fP() システムコールは Linux 固有である。 .SH 注意 .\" FIXME all of the following needs to be reviewed for the current kernel .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND .\" was not specified when doing clone(); i.e., unsharing .\" signal handlers is permitted if we are not actually .\" sharing signal handlers. mtk .\" However, we can do unshare(CLONE_VM) if CLONE_VM .\" was not specified when doing clone(); i.e., unsharing .\" virtual memory is permitted if we are not actually .\" sharing virtual memory. mtk .\" .\"9) Future Work .\"-------------- .\"The current implementation of unshare does not allow unsharing of .\"signals and signal handlers. Signals are complex to begin with and .\"to unshare signals and/or signal handlers of a currently running .\"process is even more complex. If in the future there is a specific .\"need to allow unsharing of signals and/or signal handlers, it can .\"be incrementally added to unshare without affecting legacy .\"applications using unshare. .\" \fBclone\fP(2) で新しいプロセスを生成したときに共有される全てのプロセス属性を、 \fBunshare\fP() によって共有の解除ができるわけではない。 特に、カーネル 3.8 時点では、 \fBunshare\fP() に \fBCLONE_SIGHAND\fP, \fBCLONE_THREAD\fP, \fBCLONE_VM\fP の効果を取り消すためのフラグが実装されていない。 これらの機能は、必要であれば将来追加されるかもしれない。 .SH 例 The program below provides a simple implementation of the \fBunshare\fP(1) command, which unshares one or more namespaces and executes the command supplied in its command\-line arguments. Here's an example of the use of this program, running a shell in a new mount namespace, and verifying that the original shell and the new shell are in separate mount namespaces: .in +4n .nf $ \fBreadlink /proc/$$/ns/mnt\fP mnt:[4026531840] $ \fBsudo ./unshare \-m /bin/bash\fP [sudo] password for cecilia: # \fBreadlink /proc/$$/ns/mnt\fP mnt:[4026532325] .fi .in The differing output of the two \fBreadlink\fP(1) commands shows that the two shells are in different mount namespaces. .SS プログラムのソース \& .nf /* unshare.c A simple implementation of the unshare(1) command: unshare namespaces and execute a command. */ #define _GNU_SOURCE #include #include #include #include /* A simple error\-handling function: print an error message based on the value in \(aqerrno\(aq and terminate the calling process */ #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e } while (0) static void usage(char *pname) { fprintf(stderr, "Usage: %s [options] program [arg...]\en", pname); fprintf(stderr, "Options can be:\en"); fprintf(stderr, " \-i unshare IPC namespace\en"); fprintf(stderr, " \-m unshare mount namespace\en"); fprintf(stderr, " \-n unshare network namespace\en"); fprintf(stderr, " \-p unshare PID namespace\en"); fprintf(stderr, " \-u unshare UTS namespace\en"); fprintf(stderr, " \-U unshare user namespace\en"); exit(EXIT_FAILURE); } int main(int argc, char *argv[]) { int flags, opt; flags = 0; while ((opt = getopt(argc, argv, "imnpuU")) != \-1) { switch (opt) { case \(aqi\(aq: flags |= CLONE_NEWIPC; break; case \(aqm\(aq: flags |= CLONE_NEWNS; break; case \(aqn\(aq: flags |= CLONE_NEWNET; break; case \(aqp\(aq: flags |= CLONE_NEWPID; break; case \(aqu\(aq: flags |= CLONE_NEWUTS; break; case \(aqU\(aq: flags |= CLONE_NEWUSER; break; default: usage(argv[0]); } } if (optind >= argc) usage(argv[0]); if (unshare(flags) == \-1) errExit("unshare"); execvp(argv[optind], &argv[optind]); errExit("execvp"); } .fi .SH 関連項目 \fBunshare\fP(1), \fBclone\fP(2), \fBfork\fP(2), \fBkcmp\fP(2), \fBsetns\fP(2), \fBvfork\fP(2), \fBnamespaces\fP(7) Linux カーネルソース内の \fIDocumentation/unshare.txt\fP .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.76 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。