OSDN Git Service

72a82e3af4c856e4923bc6ebc2bdf04bc951c8f8
[linuxjm/LDP_man-pages.git] / release / man2 / setns.2
1 .\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
2 .\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gamil.com>
3 .\"
4 .\" %%%LICENSE_START(GPLv2_ONELINE)
5 .\" Licensed under the GPLv2
6 .\" %%%LICENSE_END
7 .\"
8 .\"*******************************************************************
9 .\"
10 .\" This file was generated with po4a. Translate the source file.
11 .\"
12 .\"*******************************************************************
13 .\"
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>
19 .\"
20 .TH SETNS 2 2014\-09\-21 Linux "Linux Programmer's Manual"
21 .SH 名前
22 setns \- スレッドに名前空間を関連付けしなおす
23 .SH 書式
24 .nf
25 \fB#define _GNU_SOURCE\fP             /* feature_test_macros(7) 参照 */
26 \fB#include <sched.h>\fP
27 .sp
28 \fBint setns(int \fP\fIfd\fP\fB, int \fP\fInstype\fP\fB);\fP
29 .fi
30 .SH 説明
31 名前空間を参照するファイルディスクリプタを指定すると、
32 呼び出したスレッドにその名前空間を関連付けしなおす。
33
34 \fIfd\fP 引き数は、 \fI/proc/[pid]/ns/\fP ディレクトリ内の名前空間エントリ
35 のいずれかを参照するファイルディスクリプタである。
36 \fI/proc/[pid]/ns/\fP の詳細は \fBnamespaces\fP(7) を参照。
37 \fInstype\fP 引き数で指定された制限の範囲内で、
38 呼び出したスレッドに \fIfd\fP に対応する名前空間を関連付けしなおす。
39
40 \fInstype\fP 引き数は、呼び出したスレッドがどのタイプの名前空間を
41 関連付けしなおすことができるかを指定する。
42 この引き数には以下のいずれかの値を指定できる。
43 .TP 
44 \fB0\fP
45 どのタイプの名前空間も関連付けることができる。
46 .TP 
47 \fBCLONE_NEWIPC\fP (Linux 3.0 以降)
48 \fIfd\fP は IPC 名前空間を参照していなければならない。
49 .TP 
50 \fBCLONE_NEWNET\fP (Linux 3.0 以降)
51 \fIfd\fP はネットワーク名前空間を参照していなければならない。
52 .TP 
53 \fBCLONE_NEWNS\fP (Linux 3.8 以降)
54 \fIfd\fP はマウント名前空間を参照していなければならない。
55 .TP 
56 \fBCLONE_NEWPID\fP (Linux 3.8 以降)
57 \fIfd\fP は PID 名前空間を参照していなければならない。
58 .TP 
59 \fBCLONE_NEWUSER\fP (Linux 3.8 以降)
60 \fIfd\fP はユーザー名前空間を参照していなければならない。
61 .TP 
62 \fBCLONE_NEWUTS\fP (Linux 3.0 以降)
63 \fIfd\fP は UTS 名前空間を参照していなければならない。
64 .PP
65 呼び出し側が \fIfd\fP がどのタイプの名前空間を参照しているかを知っている
66 (もしくは気にする必要がない) 場合には、 \fInstype\fP に 0 を指定すれば十分
67 である。呼び出し側が \fIfd\fP がどのタイプの名前空間を参照しているかを
68 知っておらず、かつ、特定のタイプの名前空間であることを保証したい場合、
69 \fInstype\fP に 0 以外の値を指定するとよい。 (ファイルディスクリプタが別の
70 プロセスによりオープンされ、例えば、UNIX ドメインソケット経由で呼び出し
71 側に渡された場合などでは、呼び出し側が \fIfd\fP がどのタイプの名前空間を
72 参照しているかを知らない可能性がある。)
73
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).
81
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).
97
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
104 mount namespaces.
105 .SH 返り値
106 成功すると \fIsetns\fP() は 0 を返す。
107 失敗すると、 \-1 が返され、 \fIerrno\fP にエラーを示す値が設定される。
108 .SH エラー
109 .TP 
110 \fBEBADF\fP
111 \fIfd\fP が有効なファイルディスクリプタではない。
112 .TP 
113 \fBEINVAL\fP
114 \fIfd\fP が \fInstype\fP で指定されたタイプと一致しない名前空間を参照している。
115 .TP 
116 \fBEINVAL\fP
117 スレッドを指定された名前空間に関連付けし直す際に問題が発生した。
118 .TP 
119 \fBEINVAL\fP
120 The caller attempted to join the user namespace in which it is already a
121 member.
122 .TP 
123 \fBEINVAL\fP
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.
127 .TP 
128 \fBEINVAL\fP
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.
131 .TP 
132 \fBENOMEM\fP
133 指定された名前空間に変更するのに必要なメモリが割り当てられない。
134 .TP 
135 \fBEPERM\fP
136 呼び出し元スレッドはこの操作を行うのに必要なケーパビリティを持っていなかった。
137 .SH バージョン
138 \fBsetns\fP() システムコールはカーネル 3.0 で Linux に初めて登場した。
139 ライブラリによるサポートは glibc バージョン 2.14 を追加された。
140 .SH 準拠
141 \fBsetns\fP() システムコールは Linux 固有である。
142 .SH 注意
143 新しいスレッドが \fBclone\fP(2) を使って作成された際に共有できる全ての属性を、
144 \fBsetns\fP() を使って変更できるわけではない。
145 .SH 例
146 以下のプログラムは 2 つ以上の引き数を取る。 最初の引き数には、 既存の \fI/proc/[pid]/ns/\fP
147 ディレクトリの名前空間ファイルのパス名を指定する。 残りの引き数は、コマンドとその引き数を指定する。 このプログラムは名前空間ファイルをオープンし、
148 \fBsetns\fP() を使って名前空間に参加し、 指定されたコマンドをその名前空間内で実行する。
149
150 以下のシェルセッションでは、 このプログラム (\fIns_exec\fP という名前のバイナリとしてコンパイルされている)を、 \fBclone\fP(2)
151 のマニュアルページの \fBCLONE_NEWUTS\fP のサンプルプログラムと組み合わせて使っている。
152
153 まず、 \fBclone\fP(2) のサンプルプログラムをバックグラウンドで実行する。 このプログラムは、 別の UTS 名前空間で子プロセスを作成する。
154 子プロセスは自分の名前空間内でホスト名を変更する。 それから、 親プロセスと子プロセスの両方でそれぞれの UTS 名前空間のホスト名を表示し、 2
155 つのホスト名が違うことが確認できる。
156
157 .nf
158 .in +4n
159 $ \fBsu\fP                   # 名前空間の操作には特権が必要
160 Password:
161 # \fB./newuts bizarro &\fP
162 [1] 3549
163 clone() returned 3550
164 uts.nodename in child:  bizarro
165 uts.nodename in parent: antero
166 # \fBuname \-n\fP             # シェルでホスト名を確認
167 antero
168 .in
169 .fi
170
171 次に、以下のプログラムを使ってシェルを実行する。
172 このシェルの中では、ホスト名が最初のプログラムで作成された子プロセスが設定したホスト名になっていることを確認できる。
173
174 .nf
175 .in +4n
176 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
177 # \fBuname \-n\fP             #  ns_exec で起動されたシェル内で実行
178 bizarro
179 .in
180 .fi
181 .SS プログラムのソース
182 .nf
183 #define _GNU_SOURCE
184 #include <fcntl.h>
185 #include <sched.h>
186 #include <unistd.h>
187 #include <stdlib.h>
188 #include <stdio.h>
189
190 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
191                         } while (0)
192
193 int
194 main(int argc, char *argv[])
195 {
196     int fd;
197
198     if (argc < 3) {
199         fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\en", argv[0]);
200         exit(EXIT_FAILURE);
201     }
202
203     fd = open(argv[1], O_RDONLY);  /* 名前空間のディスクリプタを取得 */
204     if (fd == \-1)
205         errExit("open");
206
207     if (setns(fd, 0) == \-1)        /* 名前空間に参加 */
208         errExit("setns");
209
210     execvp(argv[2], &argv[2]);     /* 名前空間内でコマンドを実行 */
211     errExit("execvp");
212 }
213 .fi
214 .SH 関連項目
215 \fBclone\fP(2), \fBfork\fP(2), \fBunshare\fP(2), \fBvfork\fP(2), \fBnamespaces\fP(7),
216 \fBunix\fP(7)
217 .SH この文書について
218 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.76 の一部
219 である。プロジェクトの説明とバグ報告に関する情報は
220 http://www.kernel.org/doc/man\-pages/ に書かれている。