OSDN Git Service

info/GNU_which: Add some explanations.
[linuxjm/jm.git] / manual / LDP_man-pages / release / man2 / bind.2
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\" and Copyright 2005-2007, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Portions extracted from /usr/include/sys/socket.h, which does not have
4 .\" any authorship information in it.  It is probably available under the GPL.
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
28 .\"
29 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
30 .\"
31 .\" Copyright (c) 1983 The Regents of the University of California.
32 .\" All rights reserved.
33 .\"
34 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
35 .\" Redistribution and use in source and binary forms, with or without
36 .\" modification, are permitted provided that the following conditions
37 .\" are met:
38 .\" 1. Redistributions of source code must retain the above copyright
39 .\"    notice, this list of conditions and the following disclaimer.
40 .\" 2. Redistributions in binary form must reproduce the above copyright
41 .\"    notice, this list of conditions and the following disclaimer in the
42 .\"    documentation and/or other materials provided with the distribution.
43 .\" 3. All advertising materials mentioning features or use of this software
44 .\"    must display the following acknowledgement:
45 .\"     This product includes software developed by the University of
46 .\"     California, Berkeley and its contributors.
47 .\" 4. Neither the name of the University nor the names of its contributors
48 .\"    may be used to endorse or promote products derived from this software
49 .\"    without specific prior written permission.
50 .\"
51 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 .\" SUCH DAMAGE.
62 .\" %%%LICENSE_END
63 .\"
64 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
65 .\" Modified 1998 by Andi Kleen
66 .\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
67 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
68 .\"
69 .\"*******************************************************************
70 .\"
71 .\" This file was generated with po4a. Translate the source file.
72 .\"
73 .\"*******************************************************************
74 .\"
75 .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
76 .\"         all rights reserved.
77 .\" Translated 1996-06-25, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
78 .\" Modified 1997-11-06, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
79 .\" Modified 1999-08-02, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
80 .\" Updated 2005-02-03, Yuichi SATO <ysato444@yahoo.co.jp>
81 .\" Updated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
82 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
83 .\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
84 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
85 .\"
86 .TH BIND 2 2020\-11\-01 Linux "Linux Programmer's Manual"
87 .SH 名前
88 bind \- ソケットに名前をつける
89 .SH 書式
90 .nf
91 \fB#include <sys/types.h>\fP          /* 「注意」参照 */
92 \fB#include <sys/socket.h>\fP
93 .PP
94 \fBint bind(int \fP\fIsockfd\fP\fB, const struct sockaddr *\fP\fIaddr\fP\fB,\fP
95 \fB         socklen_t \fP\fIaddrlen\fP\fB);\fP
96 .fi
97 .SH 説明
98 \fBsocket\fP(2)  でソケットが作成されたとき、そのソケットは名前空間 (アドレスファミリー) に 存在するが、アドレスは割り当てられていない。
99 \fBbind\fP()  は、ファイルディスクリプター \fIsockfd\fP で参照されるソケットに \fIaddr\fP で指定されたアドレスを割り当てる。
100 \fIaddrlen\fP には \fIaddr\fP が指すアドレス構造体のサイズをバイト単位で指定する。 伝統的にこの操作は 「ソケットに名前をつける」
101 と呼ばれる。
102 .PP
103 \fBSOCK_STREAM\fP ソケットが接続を受け付けられるようにするには (\fBaccept\fP(2)  を参照)、通常その前に \fBbind\fP()
104 を使用してローカルアドレスを割り当てる必要がある。
105 .PP
106 名前付けのルールはアドレスファミリーごとに異なっている。詳細な情報は 第 7 章の各マニュアルを参照すること。 \fBAF_INET\fP は
107 \fBip\fP(7)  を、 \fBAF_INET6\fP は \fBipv6\fP(7)  を、 \fBAF_UNIX\fP は \fBunix\fP(7)  を、
108 \fBAF_APPLETALK\fP は \fBddp\fP(7)  を、 \fBAF_PACKET\fP は \fBpacket\fP(7)  を、 \fBAF_X25\fP は
109 \fBx25\fP(7)  を、 \fBAF_NETLINK\fP は \fBnetlink\fP(7)  を参照。
110 .PP
111 \fIaddr\fP 引数に実際にどのような構造体が渡されるかは、 アドレスファミリーに依存する。 \fIsockaddr\fP
112 構造体は以下のような感じで定義されている:
113 .PP
114 .in +4n
115 .EX
116 struct sockaddr {
117     sa_family_t sa_family;
118     char        sa_data[14];
119 }
120 .EE
121 .in
122 .PP
123 この構造体は、 \fIaddr\fP に渡される構造体へのポインターをキャストし、 コンパイラの警告メッセージを抑えるためだけに存在する。
124 下記の「例」を参照。
125 .SH 返り値
126 成功した場合にはゼロが返される。エラー時には \-1 が返され、 \fIerrno\fP が適切に設定される。
127 .SH エラー
128 .TP 
129 \fBEACCES\fP
130 .\" e.g., privileged port in AF_INET domain
131 そのアドレスは保護されていて、かつユーザーがスーパーユーザーではない。
132 .TP 
133 \fBEADDRINUSE\fP
134 指定されたアドレスが既に使用中である。
135 .TP 
136 \fBEADDRINUSE\fP
137 (インターネットドメインソケットの場合) ソケットアドレス構造体でポート番号に 0 が指定されたが、 一時ポート (ephemeral port)
138 を割り当てようとした際に、 一時ポートとして使用する範囲のすべてのポート番号が使用中であった。 \fBip\fP(7) の
139 \fI/proc/sys/net/ipv4/ip_local_port_range\fP の説明を参照。
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 A component in the directory prefix of the socket pathname does not exist.
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 \fBaccept\fP(): POSIX.1\-2001, POSIX.1\-2008, SVr4, 4.4BSD, (\fBbind\fP() は 4.2BSD
193 で初めて実装された).
194 .SH 注意
195 POSIX.1 では \fI<sys/types.h>\fP のインクルードは必須とされておらず、 Linux
196 ではこのヘッダーファイルは必要ではない。 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダーファイルが
197 必要であり、移植性が必要なアプリケーションではこのファイルを インクルードするのが賢明であろう。
198 .PP
199 For background on the \fIsocklen_t\fP type, see \fBaccept\fP(2).
200 .SH バグ
201 .\" FIXME Document transparent proxy options
202 透過的プロキシ (transparent proxy) オプションについて記述していない。
203 .SH 例
204 インターネットドメインソケットでの \fBbind\fP()  の利用例が \fBgetaddrinfo\fP(3)  に記載されている。
205 .PP
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 .PP
211 .EX
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(my_addr));
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(my_addr)) == \-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(peer_addr);
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 .EE
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 プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
269 \%https://www.kernel.org/doc/man\-pages/ に書かれている。