OSDN Git Service

長音記号の修正を draft に反映
[linuxjm/LDP_man-pages.git] / draft / man2 / bind.2
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\" Portions extracted from /usr/include/sys/socket.h, which does not have
3 .\" any authorship information in it.  It is probably available under the GPL.
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\"
28 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
29 .\"
30 .\" Copyright (c) 1983 The Regents of the University of California.
31 .\" All rights reserved.
32 .\"
33 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
34 .\" Redistribution and use in source and binary forms, with or without
35 .\" modification, are permitted provided that the following conditions
36 .\" are met:
37 .\" 1. Redistributions of source code must retain the above copyright
38 .\"    notice, this list of conditions and the following disclaimer.
39 .\" 2. Redistributions in binary form must reproduce the above copyright
40 .\"    notice, this list of conditions and the following disclaimer in the
41 .\"    documentation and/or other materials provided with the distribution.
42 .\" 3. All advertising materials mentioning features or use of this software
43 .\"    must display the following acknowledgement:
44 .\"     This product includes software developed by the University of
45 .\"     California, Berkeley and its contributors.
46 .\" 4. Neither the name of the University nor the names of its contributors
47 .\"    may be used to endorse or promote products derived from this software
48 .\"    without specific prior written permission.
49 .\"
50 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 .\" SUCH DAMAGE.
61 .\" %%%LICENSE_END
62 .\"
63 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
64 .\" Modified 1998 by Andi Kleen
65 .\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
66 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
67 .\"
68 .\"*******************************************************************
69 .\"
70 .\" This file was generated with po4a. Translate the source file.
71 .\"
72 .\"*******************************************************************
73 .\"
74 .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
75 .\"         all rights reserved.
76 .\" Translated 1996-06-25, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
77 .\" Modified 1997-11-06, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
78 .\" Modified 1999-08-02, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
79 .\" Updated 2005-02-03, Yuichi SATO <ysato444@yahoo.co.jp>
80 .\" Updated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
81 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
82 .\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
83 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
84 .\"
85 .TH BIND 2 2014\-08\-19 Linux "Linux Programmer's Manual"
86 .SH 名前
87 bind \- ソケットに名前をつける
88 .SH 書式
89 .nf
90 \fB#include <sys/types.h>\fP          /* 「注意」参照 */
91 \fB#include <sys/socket.h>\fP
92 .sp
93 \fBint bind(int \fP\fIsockfd\fP\fB, const struct sockaddr *\fP\fIaddr\fP\fB,\fP
94 \fB         socklen_t \fP\fIaddrlen\fP\fB);\fP
95 .fi
96 .SH 説明
97 \fBsocket\fP(2)  でソケットが作成されたとき、そのソケットは名前空間 (アドレスファミリー) に 存在するが、アドレスは割り当てられていない。
98 \fBbind\fP()  は、ファイルディスクリプター \fIsockfd\fP で参照されるソケットに \fIaddr\fP で指定されたアドレスを割り当てる。
99 \fIaddrlen\fP には \fIaddr\fP が指すアドレス構造体のサイズをバイト単位で指定する。 伝統的にこの操作は 「ソケットに名前をつける」
100 と呼ばれる。
101 .PP
102 \fBSOCK_STREAM\fP ソケットが接続を受け付けられるようにするには (\fBaccept\fP(2)  を参照)、通常その前に \fBbind\fP()
103 を使用してローカルアドレスを割り当てる必要がある。
104
105 名前付けのルールはアドレスファミリーごとに異なっている。詳細な情報は 第 7 章の各マニュアルを参照すること。 \fBAF_INET\fP は
106 \fBip\fP(7)  を、 \fBAF_INET6\fP は \fBipv6\fP(7)  を、 \fBAF_UNIX\fP は \fBunix\fP(7)  を、
107 \fBAF_APPLETALK\fP は \fBddp\fP(7)  を、 \fBAF_PACKET\fP は \fBpacket\fP(7)  を、 \fBAF_X25\fP は
108 \fBx25\fP(7)  を、 \fBAF_NETLINK\fP は \fBnetlink\fP(7)  を参照。
109
110 \fIaddr\fP 引き数に実際にどのような構造体が渡されるかは、 アドレスファミリーに依存する。 \fIsockaddr\fP
111 構造体は以下のような感じで定義されている:
112 .in +4n
113 .nf
114
115 struct sockaddr {
116     sa_family_t sa_family;
117     char        sa_data[14];
118 }
119
120 .fi
121 .in
122 この構造体は、 \fIaddr\fP に渡される構造体へのポインターをキャストし、 コンパイラの警告メッセージを抑えるためだけに存在する。
123 下記の「例」を参照。
124 .SH 返り値
125 成功した場合にはゼロが返される。エラー時には \-1 が返され、 \fIerrno\fP が適切に設定される。
126 .SH エラー
127 .TP 
128 \fBEACCES\fP
129 .\" e.g., privileged port in AF_INET domain
130 そのアドレスは保護されていて、かつユーザーがスーパーユーザーではない。
131 .TP 
132 \fBEADDRINUSE\fP
133 指定されたアドレスが既に使用中である。
134 .TP 
135 \fBEADDRINUSE\fP
136 (インターネットドメインソケットの場合) ソケットアドレス構造体でポート番号に 0 が指定されたが、 一時ポート (ephemeral port)
137 を割り当てようとした際に、 一時ポートとして使用する範囲のすべてのポート番号が使用中であった。 \fBip\fP(7) の
138 \fI/proc/sys/net/ipv4/ip_local_port_range\fP の説明を参照。
139 .TP 
140 .TP 
141 \fBEBADF\fP
142 \fIsockfd\fP が不正なディスクリプターである。
143 .TP 
144 \fBEINVAL\fP
145 .\" This may change in the future: see
146 .\" .I linux/unix/sock.c for details.
147 ソケットがすでにアドレスに結びつけ (bind) られている。
148 .TP 
149 \fBEINVAL\fP
150 \fIaddrlen\fP が間違っているか、 \fIaddr\fP がこのソケットのドメインで有効なアドレスではない。
151 .TP 
152 \fBENOTSOCK\fP
153 \fIsockfd\fP がファイルに対するディスクリプターで、ソケットに対するものではない。
154 .PP
155 以下のエラーは UNIXドメイン (\fBAF_UNIX\fP)  のソケット特有である:
156 .TP 
157 \fBEACCES\fP
158 パス名の構成要素に検索許可 (search permission) がない (\fBpath_resolution\fP(7)  も参照すること)。
159 .TP 
160 \fBEADDRNOTAVAIL\fP
161 存在しないインターフェースが要求されたか、要求されたアドレスが ローカルではなかった。
162 .TP 
163 \fBEFAULT\fP
164 \fIaddr\fP がユーザーのアクセス可能なアドレス空間の外を指している。
165 .TP 
166 \fBELOOP\fP
167 \fIaddr\fP を解決する際に遭遇したシンボリックリンクが多過ぎる。
168 .TP 
169 \fBENAMETOOLONG\fP
170 \fIaddr\fP が長過ぎる。
171 .TP 
172 \fBENOENT\fP
173 ファイルが存在しない。
174 .TP 
175 \fBENOMEM\fP
176 カーネルに、利用可能なメモリーが十分にない。
177 .TP 
178 \fBENOTDIR\fP
179 パス名の構成要素がディレクトリではない。
180 .TP 
181 \fBEROFS\fP
182 ソケット inode が読み込み専用のファイルシステム上にある。
183 .SH 準拠
184 .\" SVr4 documents an additional
185 .\" .B ENOSR
186 .\" general error condition, and
187 .\" additional
188 .\" .B EIO
189 .\" and
190 .\" .B EISDIR
191 .\" UNIX-domain error conditions.
192 SVr4, 4.4BSD, POSIX.1\-2001 (\fBbind\fP()  は 4.2BSD で最初に現われた)。
193 .SH 注意
194 POSIX.1\-2001 では \fI<sys/types.h>\fP のインクルードは必須とされておらず、 Linux
195 ではこのヘッダーファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダーファイルが
196 必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。
197
198 \fBbind\fP()  の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には \fIint\fP である。glibc
199 でも使われている現在の \fIsocklen_t\fP に関して、POSIX には少し混乱がある。 詳しくは \fBaccept\fP(2)  を参照のこと。
200 .SH バグ
201 .\" FIXME Document transparent proxy options
202 透過的プロキシ (transparent proxy) オプションについて記述していない。
203 .SH 例
204 インターネットドメインソケットでの \fBbind\fP()  の利用例が \fBgetaddrinfo\fP(3)  に記載されている。
205
206 .\" listen.7 refers to this example.
207 .\" accept.7 refers to this example.
208 .\" unix.7 refers to this example.
209 以下の例は、UNIX ドメイン (\fBAF_UNIX\fP)  でストリームソケットを bind する方法を示したものである。
210
211 .nf
212 #include <sys/socket.h>
213 #include <sys/un.h>
214 #include <stdlib.h>
215 #include <stdio.h>
216 #include <string.h>
217
218 #define MY_SOCK_PATH "/somepath"
219 #define LISTEN_BACKLOG 50
220
221 #define handle_error(msg) \e
222     do { perror(msg); exit(EXIT_FAILURE); } while (0)
223
224 int
225 main(int argc, char *argv[])
226 {
227     int sfd, cfd;
228     struct sockaddr_un my_addr, peer_addr;
229     socklen_t peer_addr_size;
230
231     sfd = socket(AF_UNIX, SOCK_STREAM, 0);
232     if (sfd == \-1)
233         handle_error("socket");
234
235     memset(&my_addr, 0, sizeof(struct sockaddr_un));
236                         /* Clear structure */
237     my_addr.sun_family = AF_UNIX;
238     strncpy(my_addr.sun_path, MY_SOCK_PATH,
239             sizeof(my_addr.sun_path) \- 1);
240
241     if (bind(sfd, (struct sockaddr *) &my_addr,
242             sizeof(struct sockaddr_un)) == \-1)
243         handle_error("bind");
244
245     if (listen(sfd, LISTEN_BACKLOG) == \-1)
246         handle_error("listen");
247
248     /* Now we can accept incoming connections one
249        at a time using accept(2) */
250
251     peer_addr_size = sizeof(struct sockaddr_un);
252     cfd = accept(sfd, (struct sockaddr *) &peer_addr,
253                  &peer_addr_size)
254     if (cfd == \-1)
255         handle_error("accept");
256
257     /* Code to deal with incoming connection(s)... */
258
259     /* When no longer required, the socket pathname, MY_SOCK_PATH
260        should be deleted using unlink(2) or remove(3) */
261 }
262 .fi
263 .SH 関連項目
264 \fBaccept\fP(2), \fBconnect\fP(2), \fBgetsockname\fP(2), \fBlisten\fP(2), \fBsocket\fP(2),
265 \fBgetaddrinfo\fP(3), \fBgetifaddrs\fP(3), \fBip\fP(7), \fBipv6\fP(7),
266 \fBpath_resolution\fP(7), \fBsocket\fP(7), \fBunix\fP(7)
267 .SH この文書について
268 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.78 の一部
269 である。プロジェクトの説明とバグ報告に関する情報は
270 http://www.kernel.org/doc/man\-pages/ に書かれている。