1 .\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
2 .\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gamil.com>
4 .\" %%%LICENSE_START(GPLv2_ONELINE)
5 .\" Licensed under the GPLv2
8 .\"*******************************************************************
10 .\" This file was generated with po4a. Translate the source file.
12 .\"*******************************************************************
14 .\" Japanese Version Copyright (c) 2012-2013 Akihiro MOTOKI
15 .\" all rights reserved.
16 .\" Translated 2012-06-04, Akihiro MOTOKI <amotoki@gmail.com>
17 .\" Updated 2013-05-01, Akihiro MOTOKI <amotoki@gmail.com>
18 .\" Updated 2013-05-06, Akihiro MOTOKI <amotoki@gmail.com>
20 .TH SETNS 2 2014\-09\-21 Linux "Linux Programmer's Manual"
22 setns \- スレッドに名前空間を関連付けしなおす
25 \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */
26 \fB#include <sched.h>\fP
28 \fBint setns(int \fP\fIfd\fP\fB, int \fP\fInstype\fP\fB);\fP
31 名前空間を参照するファイルディスクリプタを指定すると、
32 呼び出したスレッドにその名前空間を関連付けしなおす。
34 \fIfd\fP 引き数は、 \fI/proc/[pid]/ns/\fP ディレクトリ内の名前空間エントリ
35 のいずれかを参照するファイルディスクリプタである。
36 \fI/proc/[pid]/ns/\fP の詳細は \fBnamespaces\fP(7) を参照。
37 \fInstype\fP 引き数で指定された制限の範囲内で、
38 呼び出したスレッドに \fIfd\fP に対応する名前空間を関連付けしなおす。
40 \fInstype\fP 引き数は、呼び出したスレッドがどのタイプの名前空間を
42 この引き数には以下のいずれかの値を指定できる。
45 どのタイプの名前空間も関連付けることができる。
47 \fBCLONE_NEWIPC\fP (Linux 3.0 以降)
48 \fIfd\fP は IPC 名前空間を参照していなければならない。
50 \fBCLONE_NEWNET\fP (Linux 3.0 以降)
51 \fIfd\fP はネットワーク名前空間を参照していなければならない。
53 \fBCLONE_NEWNS\fP (Linux 3.8 以降)
54 \fIfd\fP はマウント名前空間を参照していなければならない。
56 \fBCLONE_NEWPID\fP (Linux 3.8 以降)
57 \fIfd\fP は PID 名前空間を参照していなければならない。
59 \fBCLONE_NEWUSER\fP (Linux 3.8 以降)
60 \fIfd\fP はユーザー名前空間を参照していなければならない。
62 \fBCLONE_NEWUTS\fP (Linux 3.0 以降)
63 \fIfd\fP は UTS 名前空間を参照していなければならない。
65 呼び出し側が \fIfd\fP がどのタイプの名前空間を参照しているかを知っている
66 (もしくは気にする必要がない) 場合には、 \fInstype\fP に 0 を指定すれば十分
67 である。呼び出し側が \fIfd\fP がどのタイプの名前空間を参照しているかを
68 知っておらず、かつ、特定のタイプの名前空間であることを保証したい場合、
69 \fInstype\fP に 0 以外の値を指定するとよい。 (ファイルディスクリプタが別の
70 プロセスによりオープンされ、例えば、UNIX ドメインソケット経由で呼び出し
71 側に渡された場合などでは、呼び出し側が \fIfd\fP がどのタイプの名前空間を
74 \fBCLONE_NEWPID\fP behaves somewhat differently from the other \fInstype\fP
75 values: reassociating the calling thread with a PID namespace only changes
76 the PID namespace that child processes of the caller will be created in; it
77 does not change the PID namespace of the caller itself. Reassociating with
78 a PID namespace is only allowed if the PID namespace specified by \fIfd\fP is a
79 descendant (child, grandchild, etc.) of the PID namespace of the caller.
80 For further details on PID namespaces, see \fBpid_namespaces\fP(7).
82 .\" See kernel/user_namespace.c:userns_install() [3.8 source]
83 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
84 .\" https://lwn.net/Articles/543273/
85 A process reassociating itself with a user namespace must have the
86 \fBCAP_SYS_ADMIN\fP capability in the target user namespace. Upon successfully
87 joining a user namespace, a process is granted all capabilities in that
88 namespace, regardless of its user and group IDs. A multithreaded process
89 may not change user namespace with \fBsetns\fP(). It is not permitted to use
90 \fBsetns\fP() to reenter the caller's current user namespace. This prevents a
91 caller that has dropped capabilities from regaining those capabilities via a
92 call to \fBsetns\fP(). For security reasons, a process can't join a new user
93 namespace if it is sharing filesystem\-related attributes (the attributes
94 whose sharing is controlled by the \fBclone\fP(2) \fBCLONE_FS\fP flag) with
95 another process. For further details on user namespaces, see
96 \fBuser_namespaces\fP(7).
98 .\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
99 A process may not be reassociated with a new mount namespace if it is
100 multithreaded. Changing the mount namespace requires that the caller
101 possess both \fBCAP_SYS_CHROOT\fP and \fBCAP_SYS_ADMIN\fP capabilities in its own
102 user namespace and \fBCAP_SYS_ADMIN\fP in the target mount namespace. See
103 \fBuser_namespaces\fP(7) for details on the interaction of user namespaces and
106 成功すると \fIsetns\fP() は 0 を返す。
107 失敗すると、 \-1 が返され、 \fIerrno\fP にエラーを示す値が設定される。
111 \fIfd\fP が有効なファイルディスクリプタではない。
114 \fIfd\fP が \fInstype\fP で指定されたタイプと一致しない名前空間を参照している。
117 スレッドを指定された名前空間に関連付けし直す際に問題が発生した。
120 The caller attempted to join the user namespace in which it is already a
124 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
125 The caller shares filesystem (\fBCLONE_FS\fP) state (in particular, the root
126 directory) with other processes and tried to join a new user namespace.
129 .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
130 The caller is multithreaded and tried to join a new user namespace.
133 指定された名前空間に変更するのに必要なメモリが割り当てられない。
136 呼び出し元スレッドはこの操作を行うのに必要なケーパビリティを持っていなかった。
138 \fBsetns\fP() システムコールはカーネル 3.0 で Linux に初めて登場した。
139 ライブラリによるサポートは glibc バージョン 2.14 を追加された。
141 \fBsetns\fP() システムコールは Linux 固有である。
143 新しいスレッドが \fBclone\fP(2) を使って作成された際に共有できる全ての属性を、
144 \fBsetns\fP() を使って変更できるわけではない。
146 以下のプログラムは 2 つ以上の引き数を取る。 最初の引き数には、 既存の \fI/proc/[pid]/ns/\fP
147 ディレクトリの名前空間ファイルのパス名を指定する。 残りの引き数は、コマンドとその引き数を指定する。 このプログラムは名前空間ファイルをオープンし、
148 \fBsetns\fP() を使って名前空間に参加し、 指定されたコマンドをその名前空間内で実行する。
150 以下のシェルセッションでは、 このプログラム (\fIns_exec\fP という名前のバイナリとしてコンパイルされている)を、 \fBclone\fP(2)
151 のマニュアルページの \fBCLONE_NEWUTS\fP のサンプルプログラムと組み合わせて使っている。
153 まず、 \fBclone\fP(2) のサンプルプログラムをバックグラウンドで実行する。 このプログラムは、 別の UTS 名前空間で子プロセスを作成する。
154 子プロセスは自分の名前空間内でホスト名を変更する。 それから、 親プロセスと子プロセスの両方でそれぞれの UTS 名前空間のホスト名を表示し、 2
159 $ \fBsu\fP # 名前空間の操作には特権が必要
161 # \fB./newuts bizarro &\fP
163 clone() returned 3550
164 uts.nodename in child: bizarro
165 uts.nodename in parent: antero
166 # \fBuname \-n\fP # シェルでホスト名を確認
171 次に、以下のプログラムを使ってシェルを実行する。
172 このシェルの中では、ホスト名が最初のプログラムで作成された子プロセスが設定したホスト名になっていることを確認できる。
176 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
177 # \fBuname \-n\fP # ns_exec で起動されたシェル内で実行
190 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
194 main(int argc, char *argv[])
199 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\en", argv[0]);
203 fd = open(argv[1], O_RDONLY); /* 名前空間のディスクリプタを取得 */
207 if (setns(fd, 0) == \-1) /* 名前空間に参加 */
210 execvp(argv[2], &argv[2]); /* 名前空間内でコマンドを実行 */
215 \fBclone\fP(2), \fBfork\fP(2), \fBunshare\fP(2), \fBvfork\fP(2), \fBnamespaces\fP(7),
218 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.76 の一部
219 である。プロジェクトの説明とバグ報告に関する情報は
220 http://www.kernel.org/doc/man\-pages/ に書かれている。