OSDN Git Service

Update draft based on LDP v3.75 po4a files
[linuxjm/LDP_man-pages.git] / draft / man7 / unix.7
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" and Copyright (C) 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
5 .\" and Copyright (C) 2008, 2012 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" Permission is granted to distribute possibly modified copies
7 .\" of this page provided the header is included verbatim,
8 .\" and in case of nontrivial modification author and date
9 .\" of the modification is added to the header.
10 .\" %%%LICENSE_END
11 .\"
12 .\" Modified, 2003-12-02, Michael Kerrisk, <mtk.manpages@gmail.com>
13 .\" Modified, 2003-09-23, Adam Langley
14 .\" Modified, 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com>
15 .\"     Added SOCK_SEQPACKET
16 .\" 2008-05-27, mtk, Provide a clear description of the three types of
17 .\"     address that can appear in the sockaddr_un structure: pathname,
18 .\"     unnamed, and abstract.
19 .\"
20 .\"*******************************************************************
21 .\"
22 .\" This file was generated with po4a. Translate the source file.
23 .\"
24 .\"*******************************************************************
25 .\"
26 .\" Japanese Version Copyright (c) 1999 Shouichi Saito and
27 .\"     NAKANO Takeo all rights reserved.
28 .\" Translated 1999-12-06, NAKANO Takeo <nakano@apm.seikei.ac.jp>
29 .\"     based on the work by Shouichi Saito <ss236rx@ymg.urban.ne.jp>
30 .\" Updated 2003-01-07, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
31 .\" Updated 2005-02-21, Akihiro MOTOKI
32 .\" Updated 2005-12-26, Akihiro MOTOKI
33 .\" Updated 2008-08-08, Akihiro MOTOKI, LDP v3.05
34 .\"
35 .TH UNIX 7 2014\-10\-15 Linux "Linux Programmer's Manual"
36 .SH 名前
37 unix \- ローカルな プロセス間通信用のソケット
38 .SH 書式
39 \fB#include <sys/socket.h>\fP
40 .br
41 \fB#include <sys/un.h>\fP
42
43 \fIunix_socket\fP\fB = socket(AF_UNIX, type, 0);\fP
44 .br
45 \fIerror\fP\fB = socketpair(AF_UNIX, type, 0, int *\fP\fIsv\fP\fB);\fP
46 .SH 説明
47 \fBAF_UNIX\fP (\fBAF_LOCAL\fP とも言われる) ソケットファミリーは、同じマシン上で
48 プロセス同士が 効率的に通信するために用いられる。伝統的に、UNIX ドメイン
49 ソケットは、名前なしにもできるし、 (ソケット型であると印のついた) ファイル
50 システムのパス名に 結び付けることもできる。さらに Linux では、ファイル
51 システムに依存しない抽象名前空間 (abstract namespace) もサポートしている。
52
53 UNIX ドメインに指定できるソケットタイプは以下の通りである。 \fBSOCK_STREAM\fP は、 ストリーム指向のソケットで有効である。
54 \fBSOCK_DGRAM\fP は、 メッセージ境界を保存するデータグラム指向のソケットで有効である (ほとんどの UNIX の実装では、 UNIX
55 ドメイン・データグラム・ソケットは常に信頼でき、 データグラムの並び替えは行わない)。 \fBSOCK_SEQPACKET\fP は、
56 メッセージ境界を保存し送信された順序でメッセージを届ける接続指向ソケットで有効である (Linux 2.6.4 以降で利用できる)。
57
58 UNIX ドメインソケットでは、補助データを使って ファイルディスクリプタや
59 プロセスの信任状 (credential) を 送受信することもできる。
60 .SS アドレスのフォーマット
61 UNIX ドメインソケットのアドレスは以下の構造体で表現される。
62 .in +4n
63 .nf
64
65 #define UNIX_PATH_MAX    108
66
67 struct sockaddr_un {
68     sa_family_t sun_family;               /* AF_UNIX */
69     char        sun_path[UNIX_PATH_MAX];  /* pathname */
70 };
71 .fi
72 .in
73 .PP
74 \fIsun_family\fP フィールドには必ず \fBAF_UNIX\fP が入っている。
75
76 Various systems calls (for example, \fBbind\fP(2), \fBconnect\fP(2), and
77 \fBsendto\fP(2))  take a \fIsockaddr_un\fP argument as input.  Some other system
78 calls (for example, \fBgetsockname\fP(2), \fBgetpeername\fP(2), \fBrecvfrom\fP(2),
79 and \fBaccept\fP(2))  return an argument of this type.
80
81 \fIsockaddr_un\fP 構造体では 3 種類のアドレスが区別される。
82 .IP * 3
83 \fIpathname (パス名)\fP: \fBbind\fP(2) を使って、UNIX ドメインソケットを、
84 ヌル終端されたファイルシステム上のパス名に結び付けることができる。 (上述のいずれかのシステムコールにより) ソケットのアドレスが返される際、
85 その長さは
86
87     offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
88
89 であり、 \fIsun_path\fP にはヌル終端されたパス名が格納される。 (Linux では、上記の \fBoffsetof\fP() 式は
90 \fIsizeof(sa_family_t)\fP の値と同じだが、 他の実装では \fIsun_path\fP の前に他のフィールドが含まれる場合もある。
91 そのため、 \fBoffsetof\fP() 式を使う方がより移植性のある方法でアドレス構造体のサイズを知ることができる。)
92 .IP
93 For further details of pathname sockets, see below.
94 .IP *
95 .\" There is quite some variation across implementations: FreeBSD
96 .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
97 \fIunnamed (名前なし)\fP: \fBbind\fP(2)  を使ってパス名に結び付けることができないストリーム型のソケットは 名前を持たない。同様に、
98 \fBsocketpair\fP(2)  で作成される 2 つのソケットも名前を持たない。 名前なしのソケットのアドレスを返す際には、 その長さは
99 \fIsizeof(sa_family_t)\fP であり、 \fIsun_path\fP は検査すべきではない。
100 .IP *
101 \fIabstract (抽象)\fP: 抽象ソケットアドレスは、 \fIsun_path[0]\fP がヌルバイト (\(aq\e0\(aq) であることから
102 (パス名ソケットから) 区別できる。 この名前空間におけるソケットのアドレスは、 \fIsun_path\fP の残りのバイトの、
103 アドレス構造体の指定された長さの範囲で表される (名前中のヌルバイトには特別な意味はない)。 この名前はファイルシステムのパス名とは何の関係もない。
104 抽象ソケットのアドレスを返される際には、 返される \fIaddrlen\fP は \fIsizeof(sa_family_t)\fP より大きく (つまり 2
105 より大きく)、 ソケットの名前は \fIsun_path\fP の最初の \fI(addrlen \- sizeof(sa_family_t))\fP
106 バイトに格納される。 ソケットの抽象名前空間は Linux による拡張であり、移植性はない。
107 .SS パス名ソケット
108 When binding a socket to a pathname, a few rules should be observed for
109 maximum portability and ease of coding:
110 .IP * 3
111 \fIsun_path\fP のパス名はヌル終端すべきである。
112 .IP *
113 終端のヌルバイトを含めたパス名の長さは \fIsun_path\fP の大きさを超えないようにすべきである。
114 .IP *
115 The \fIaddrlen\fP argument that describes the enclosing \fIsockaddr_un\fP
116 structure should have a value of at least:
117
118 .nf
119     offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
120 .fi
121 .IP
122 or, more simply, \fIaddrlen\fP can be specified as \fIsizeof(struct
123 sockaddr_un)\fP.
124 .PP
125 .\" Linux does this, including for the case where the supplied path
126 .\" is 108 bytes
127 There is some variation in how implementations handle UNIX domain socket
128 addresses that do not follow the above rules.  For example, some (but not
129 all) implementations append a null terminator if none is present in the
130 supplied \fIsun_path\fP.
131
132 .\" HP-UX
133 .\" Modern BSDs generally have 104, Tru64 and AIX have 104,
134 .\" Solaris and Irix have 108
135 When coding portable applications, keep in mind that some implementations
136 have \fIsun_path\fP as short as 92 bytes.
137
138 Various system calls (\fBaccept\fP(2), \fBrecvfrom\fP(2), \fBgetsockname\fP(2),
139 \fBgetpeername\fP(2))  return socket address structures.  When applied to UNIX
140 domain sockets, the value\-result \fIaddrlen\fP argument supplied to the call
141 should be initialized as above.  Upon return, the argument is set to
142 indicate the \fIactual\fP size of the address structure.  The caller should
143 check the value returned in this argument: if the output value exceeds the
144 input value, then there is no guarantee that a null terminator is present in
145 \fIsun_path\fP.  (See BUGS.)
146 .SS ソケットオプション
147 歴史的な理由により、これらのオプションは たとえ \fBAF_UNIX\fP 固有のオプションであっても \fBSOL_SOCKET\fP 型で指定する。
148 ソケットファミリーとして \fBSOL_SOCKET\fP を指定すると、 \fBsetsockopt\fP(2)  でオプションが設定でき、
149 \fBgetsockopt\fP(2)  で取得ができる。
150 .TP 
151 \fBSO_PASSCRED\fP
152 送信プロセスの補助メッセージで信任状を受信できるようにする。このオプションが
153 セットされていて、まだソケットが接続されていないと、抽象名前空間に他と重なら
154 ない名前が自動的に生成される。ブール整数値のフラグを取る。
155 .SS "自動バインド (autobind) 機能"
156 .\" i.e., sizeof(short)
157 \fBbind\fP(2) 呼び出しで \fIsizeof(sa_family_t)\fP として \fIaddrlen\fP を指定するか、
158 アドレスに明示的にバインドされていないソケットに対して
159 \fBSO_PASSCRED\fP ソケットオプションが指定されていた場合、
160 そのソケットは抽象アドレスに自動的にバインドされる。
161 このアドレスは、1 個のヌルバイトの後に、文字集合 \fI[0\-9a\-f]\fP のバイトが
162 5 個続く形式である。したがって、自動的にバインドされるアドレス数には
163 2^20 個という上限が存在する。
164 (Linux 2.1.15 以降で、自動バインド機能が追加されたときには、
165 8 バイトが使われており、自動バインドアドレス数の上限は 2^32 であった。
166 Linux 2.3.15 で 5 バイトに変更された。)
167 .SS "ソケット API"
168 この節では、Linux の UNIX ドメインソケットでの、ドメイン固有の詳細仕様と
169 ソケット API でサポートされていない機能について説明する。
170
171 UNIX ドメインソケットでは、帯域外データ (out\-of\-band data) の 送信
172 (\fBsend\fP(2) と \fBrecv\fP(2) の \fBMSG_OOB\fP フラグ) はサポートされていない。
173
174 \fBsend\fP(2) \fBMSG_MORE\fP フラグは UNIX ドメインソケットではサポートされていない。
175
176 \fBrecv\fP(2) の \fIflags\fP 引き数での \fBMSG_TRUNC\fP の使用は UNIX ドメイン
177 ソケットではサポートされていない。
178
179 \fBSO_SNDBUF\fP ソケットオプションは UNIX ドメインソケットで効果を持つが、
180 \fBSO_RCVBUF\fP は効果がない。 データグラム・ソケットでは、 \fBSO_SNDBUF\fP の値が
181 出力データグラムの上限サイズとなる。 実際の上限値は、 \fBSO_SNDBUF\fP オプション
182 として設定された値の 2倍 (\fBsocket\fP(7) 参照) からオーバヘッドとして使用される
183 32 バイトを引いた値となる。
184 .SS 補助メッセージ
185 補助データを送受するには、 \fBsendmsg\fP(2)  や \fBrecvmsg\fP(2)  を使用する。
186 歴史的な理由により、以下に示す補助メッセージの型は たとえ \fBAF_UNIX\fP 固有のものであっても \fBSOL_SOCKET\fP 型で指定する。
187 これらを送るには、構造体 \fIcmsghdr\fP の \fIcmsg_level\fP フィールドに \fBSOL_SOCKET\fP をセットし、
188 \fIcmsg_type\fP フィールドにタイプをセットする。 詳細は \fBcmsg\fP(3)  を見よ。
189 .TP 
190 \fBSCM_RIGHTS\fP
191 他のプロセスでオープンされたファイルディスクリプタのセットを送受信する。 データ部分にファイルディスクリプタの整数配列が入っている。
192 渡されたファイルディスクリプタは、あたかも \fBdup\fP(2)  で生成されたかのように振る舞う。
193 .TP 
194 \fBSCM_CREDENTIALS\fP
195 UNIX 信任状を送受信する。これは認証に用いることができる。
196 信任状は \fIstruct ucred\fP の補助メッセージとして渡される。
197 この構造体は \fI<sys/socket.h>\fP で以下のように定義されている。
198
199 .in +4n
200 .nf
201 struct ucred {
202     pid_t pid;    /* process ID of the sending process */
203     uid_t uid;    /* user ID of the sending process */
204     gid_t gid;    /* group ID of the sending process */
205 };
206 .fi
207 .in
208
209 glibc 2.8 以降では、この構造体の定義を得るためには
210 (\fIどの\fPヘッダファイルをインクルードするよりも前に)
211 機能検査マクロ \fB_GNU_SOURCE\fP を定義しなければならない。
212
213 送信側が指定した信任状は、カーネルがチェックする。 実効ユーザー ID が 0 のプロセスには、 自分自身以外の値を指定する事が許される。
214 送信側は以下の 3 つを指定しなければならない。 1) 自分自身のプロセス ID (\fBCAP_SYS_ADMIN\fP 権限を持っていない場合)、 2)
215 自分自身のユーザー ID あるいは実効ユーザー ID か保存 set\-user\-ID (\fBCAP_SETUID\fP 権限を持っていない場合)、 3)
216 自分自身のグループ ID あるいは実行グループ ID か保存 set\-group\-ID (\fBCAP_SETGID\fP を持っていない場合)。
217 \fIstruct ucred\fP メッセージを受信するためには、ソケットに対し \fBSO_PASSCRED\fP オプションを有効にしなくてはならない。
218 .SS ioctl
219 以下の \fBioctl\fP(2) 呼び出しは \fIvalue\fP に情報を入れて返す。
220 正しい書式は以下の通り。
221 .PP
222 .RS
223 .nf
224 \fBint\fP\fI value\fP\fB;\fP
225 \fIerror\fP\fB = ioctl(\fP\fIunix_socket\fP\fB, \fP\fIioctl_type\fP\fB, &\fP\fIvalue\fP\fB);\fP
226 .fi
227 .RE
228 .PP
229 \fIioctl_type\fP には以下を指定できる:
230 .TP 
231 \fBSIOCINQ\fP
232 .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
233 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
234 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
235 .\" quite what userland might expect. It seems to return the number
236 .\" of bytes allocated for buffers containing pending output.
237 .\" That number is normally larger than the number of bytes of pending
238 .\" output. Since this info is, from userland's point of view, imprecise,
239 .\" and it may well change, probably best not to document this now.
240 受信バッファのキューにある、まだ読んでいないデータの量を返す。ソケットは
241 LISTEN 状態にあってはならず、さもないとエラー (\fBEINVAL\fP) が返る。
242 \fBSIOCINQ\fP は \fI<linux/sockios.h>\fP で定義されている。
243 代わりに、\fI<sys/ioctl.h>\fP で定義されている、同義語の \fBFIONREAD\fP
244 を使うこともできる。
245 .SH エラー
246 .TP 
247 \fBEADDRINUSE\fP
248 指定したローカルアドレスが既に使用されているか、ファイルシステムの
249 ソケットオブジェクトが既に存在している。
250 .TP 
251 \fBECONNREFUSED\fP
252 \fBconnect\fP(2) により指定されたリモートアドレスが接続待ちソケットではなかった。
253 このエラーはターゲットのパス名がソケットでなかった場合にも発生する。
254 .TP 
255 \fBECONNRESET\fP
256 リモートソケットが予期しないかたちでクローズされた。
257 .TP 
258 \fBEFAULT\fP
259 ユーザーメモリアドレスが不正。
260 .TP 
261 \fBEINVAL\fP
262 渡した引数が不正。よくある原因としては、渡したアドレスの \fIsun_type\fP フィール
263 ドに \fBAF_UNIX\fP が指定されていなかった、行おうとした操作に対してソケットが有
264 効な状態ではなかった、など。
265 .TP 
266 \fBEISCONN\fP
267 既に接続されているソケットに対して \fBconnect\fP(2)  が呼ばれた。または、指定したターゲットアドレスが 既に接続済みのソケットだった。
268 .TP 
269 \fBENOENT\fP
270 \fBconnect\fP(2) に指定されたリモートアドレスのパス名が存在しなかった。
271 .TP 
272 \fBENOMEM\fP
273 メモリが足りない。
274 .TP 
275 \fBENOTCONN\fP
276 ソケット操作にターゲットアドレスが必要だが、 このソケットは接続されていない。
277 .TP 
278 \fBEOPNOTSUPP\fP
279 ストリーム指向でないソケットに対してストリーム操作が呼び出された。 または帯域外データオプションを用いようとした。
280 .TP 
281 \fBEPERM\fP
282 送信者が \fIstruct ucred\fP に不正な信任状を渡した。
283 .TP 
284 \fBEPIPE\fP
285 リモートソケットがストリームソケット上でクローズされた。 可能な場合は \fBSIGPIPE\fP も同時に送られる。これを避けるには
286 \fBMSG_NOSIGNAL\fP フラグを \fBsendmsg\fP(2)  や \fBrecvmsg\fP(2)  に渡す。
287 .TP 
288 \fBEPROTONOSUPPORT\fP
289 渡されたプロトコルが \fBAF_UNIX\fP でない。
290 .TP 
291 \fBEPROTOTYPE\fP
292 リモートソケットとローカルソケットのタイプが一致していなかった (\fBSOCK_DGRAM\fP と \fBSOCK_STREAM\fP)。
293 .TP 
294 \fBESOCKTNOSUPPORT\fP
295 未知のソケットタイプ。
296 .PP
297 他にも汎用のソケット層でエラーが起こったり、 ファイルシステム上にソケットオブジェクトを作ろうとした場合に ファイルシステムのエラーが起こることがある。
298 それぞれの詳細は適切な man ページを参照すること。
299 .SH バージョン
300 \fBSCM_CREDENTIALS\fP と抽象名前空間は、Linux 2.2 で導入された。 移植性が必要なプログラムでは使うべきではない。 (BSD
301 由来のシステムの中にも信任状の送受信をサポートしているものがあるが、 その実装の詳細はシステムによって異なる)
302 .SH 注意
303 Linux の実装では、 ファイルシステム上から見えるソケットは、 それらが置かれているディレクトリのパーミッションに従う。 ソケットの所有者、
304 グループ、 パーミッションは変更できる。 新しいソケットを作るとき、 作ろうとするディレクトリに対して プロセスが書き込みと検索 (実行)
305 権限を持っていなければ、 作成に失敗する。 ソケットオブジェクトに接続するには、 read/write 権限が必要である。 この動作は、 多くの BSD
306 由来のシステムとは異なっている (BSD では UNIX ドメインソケットに対してはパーミッションを無視する)。
307 移植性の必要なプログラムでは、セキュリティをこの仕様に依存してはならない。
308
309 ファイル名を指定してソケットにバインドすると、ファイルシステムにソケットが
310 生成される。これは必要なくなったときに呼びだしたユーザーが削除しなければ
311 ならない (\fBunlink\fP(2) を用いる)。 UNIX で通常使われる「背後で閉じる方式」
312 が適用される。ソケットはいつでも unlink することができ、最後の参照が
313 クローズされたときにファイルシステムから削除される。
314
315 \fBSOCK_STREAM\fP 上でファイルディスクリプタや信任状を渡すためには、同じ \fBsendmsg\fP(2)  や \fBrecvmsg\fP(2)
316 コールで補助データ以外のデータを少なくとも 1 バイト送信/受信する必要がある。
317
318 .\"
319 UNIX ドメインのストリーム・ソケットでは、 帯域外データの概念はサポートされない。
320 .SH バグ
321 .\" The behavior on Solaris is quite similar.
322 When binding a socket to an address, Linux is one of the implementations
323 that appends a null terminator if none is supplied in \fIsun_path\fP.  In most
324 cases this is unproblematic: when the socket address is retrieved, it will
325 be one byte longer than that supplied when the socket was bound.  However,
326 there is one case where confusing behavior can result: if 108 non\-null bytes
327 are supplied when a socket is bound, then the addition of the null
328 terminator takes the length of the pathname beyond \fIsizeof(sun_path)\fP.
329 Consequently, when retrieving the socket address (for example, via
330 \fBaccept\fP(2)), if the input \fIaddrlen\fP argument for the retrieving call is
331 specified as \fIsizeof(struct sockaddr_un)\fP, then the returned address
332 structure \fIwon't\fP have a null terminator in \fIsun_path\fP.
333
334 .\" i.e., traditional BSD
335 In addition, some implementations don't require a null terminator when
336 binding a socket (the \fIaddrlen\fP argument is used to determine the length of
337 \fIsun_path\fP)  and when the socket address is retrieved on these
338 implementations, there is no null terminator in \fIsun_path\fP.
339
340 Applications that retrieve socket addresses can (portably) code to handle
341 the possibility that there is no null terminator in \fIsun_path\fP by
342 respecting the fact that the number of valid bytes in the pathname is:
343
344 .\" The following patch to amend kernel behavior was rejected:
345 .\" http://thread.gmane.org/gmane.linux.kernel.api/2437
346 .\" Subject: [patch] Fix handling of overlength pathname in AF_UNIX sun_path
347 .\" 2012-04-17
348 .\" And there was a related discussion in the Austin list:
349 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735
350 .\" Subject: Having a sun_path with no null terminator
351 .\" 2012-04-18
352 .\"
353 .\" FIXME . Track http://austingroupbugs.net/view.php?id=561
354     strnlen(addr.sun_path, addrlen \- offsetof(sockaddr_un, sun_path))
355
356 Alternatively, an application can retrieve the socket address by allocating
357 a buffer of size \fIsizeof(struct sockaddr_un)+1\fP that is zeroed out before
358 the retrieval.  The retrieving call can specify \fIaddrlen\fP as
359 \fIsizeof(struct sockaddr_un)\fP, and the extra zero byte ensures that there
360 will be a null terminator for the string returned in \fIsun_path\fP:
361
362 .nf
363 .in +3
364 void *addrp;
365
366 addrlen = sizeof(struct sockaddr_un);
367 addrp = malloc(addrlen + 1);
368 if (addrp == NULL)
369     /* Handle error */ ;
370 memset(addrp, 0, addrlen + 1);
371
372 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) != \-1)
373     /* handle error */ ;
374
375 printf("sun_path = %s\en", ((struct sockaddr_un *) addrp)\->sun_path);
376 .in
377 .fi
378
379 This sort of messiness can be avoided if it is guaranteed that the
380 applications that \fIcreate\fP pathname sockets follow the rules outlined above
381 under \fIPathname sockets\fP.
382 .SH 例
383 \fBbind\fP(2)  参照。
384
385 \fBSCM_RIGHTS\fP の使用例については \fBcmsg\fP(3) を参照。
386 .SH 関連項目
387 \fBrecvmsg\fP(2), \fBsendmsg\fP(2), \fBsocket\fP(2), \fBsocketpair\fP(2), \fBcmsg\fP(3),
388 \fBcapabilities\fP(7), \fBcredentials\fP(7), \fBsocket\fP(7)
389 .SH この文書について
390 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.75 の一部
391 である。プロジェクトの説明とバグ報告に関する情報は
392 http://www.kernel.org/doc/man\-pages/ に書かれている。