1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
4 .\" Portions extracted from /usr/include/sys/socket.h, which does not have
5 .\" any authorship information in it. It is probably available under the GPL.
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.
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.
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
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
30 .\" Copyright (c) 1983 The Regents of the University of California.
31 .\" All rights reserved.
33 .\" Redistribution and use in source and binary forms, with or without
34 .\" modification, are permitted provided that the following conditions
36 .\" 1. Redistributions of source code must retain the above copyright
37 .\" notice, this list of conditions and the following disclaimer.
38 .\" 2. Redistributions in binary form must reproduce the above copyright
39 .\" notice, this list of conditions and the following disclaimer in the
40 .\" documentation and/or other materials provided with the distribution.
41 .\" 3. All advertising materials mentioning features or use of this software
42 .\" must display the following acknowledgement:
43 .\" This product includes software developed by the University of
44 .\" California, Berkeley and its contributors.
45 .\" 4. Neither the name of the University nor the names of its contributors
46 .\" may be used to endorse or promote products derived from this software
47 .\" without specific prior written permission.
49 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
62 .\" Modified 1998 by Andi Kleen
63 .\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
64 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
66 .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
67 .\" all rights reserved.
68 .\" Translated 1996-06-25, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
69 .\" Modified 1997-11-06, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
70 .\" Modified 1999-08-02, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
71 .\" Updated 2005-02-03, Yuichi SATO <ysato444@yahoo.co.jp>
72 .\" Updated 2005-12-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
73 .\" Updated 2006-07-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.36
74 .\" Updated 2007-06-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.55
75 .\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
77 .TH BIND 2 2007-12-28 "Linux" "Linux Programmer's Manual"
80 .\"O bind \- bind a name to a socket
85 .\"O .BR "#include <sys/types.h>" " /* See NOTES */"
86 .BR "#include <sys/types.h>" " /* 「注意」参照 */"
87 .B #include <sys/socket.h>
89 .BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
90 .BI " socklen_t " addrlen );
94 .\"O When a socket is created with
96 .\"O it exists in a name space (address family) but has no address assigned to it.
98 .\"O assigns the address specified to by
100 .\"O to the socket referred to by the file descriptor
103 .\"O specifies the size, in bytes, of the address structure pointed to by
105 .\"O Traditionally, this operation is called \(lqassigning a name to a socket\(rq.
107 でソケットが作成されたとき、そのソケットは名前空間 (アドレス・ファミリー) に
108 存在するが、アドレスは割り当てられていない。
118 が指すアドレス構造体のサイズをバイト単位で指定する。
123 .\"O It is normally necessary to assign a local address using
127 .\"O socket may receive connections (see
128 .\"O .BR accept (2)).
130 ソケットが接続を受け付けられるようにするには
134 を使用してローカルアドレスを割り当てる必要がある。
136 .\"O The rules used in name binding vary between address families.
137 .\"O Consult the manual entries in Section 7 for detailed information.
142 名前付けのルールはアドレス・ファミリーごとに異なっている。詳細な情報は
191 .\"O .BR netlink (7).
197 .\"O The actual structure passed for the
199 .\"O argument will depend on the address family.
202 .\"O structure is defined as something like:
204 引き数に実際にどのような構造体が渡されるかは、
207 構造体は以下のような感じで定義されている:
212 sa_family_t sa_family;
218 .\"O The only purpose of this structure is to cast the structure
219 .\"O pointer passed in
221 .\"O in order to avoid compiler warnings.
222 .\"O See EXAMPLE below.
225 に渡される構造体へのポインタをキャストし、
226 コンパイラの警告メッセージを抑えるためだけに存在する。
228 .\"O .SH "RETURN VALUE"
230 .\"O On success, zero is returned.
231 .\"O On error, \-1 is returned, and
233 .\"O is set appropriately.
242 .\" e.g., privileged port in AF_INET domain
243 .\"O The address is protected, and the user is not the superuser.
244 そのアドレスは保護されていて、かつユーザがスーパーユーザではない。
247 .\"O The given address is already in use.
252 .\"O is not a valid descriptor.
257 .\"O The socket is already bound to an address.
258 .\"O .\" This may change in the future: see
259 .\"O .\" .I linux/unix/sock.c for details.
260 ソケットがすでにアドレスに結びつけ (bind) られている。
261 .\" これは将来変更されるかもしれない。
263 .\" .I linux/unix/sock.c
267 .\"O is a descriptor for a file, not a socket.
269 がファイルに対するディスクリプタで、ソケットに対するものではない。
271 .\"O The following errors are specific to UNIX domain
279 .\"O Search permission is denied on a component of the path prefix.
281 .\"O .BR path_resolution (7).)
282 パス名の構成要素に検索許可 (search permission) がない
283 .RB ( path_resolution (7)
287 .\"O A nonexistent interface was requested or the requested
288 .\"O address was not local.
289 存在しないインタフェースが要求されたか、要求されたアドレスが
294 .\"O points outside the user's accessible address space.
296 がユーザのアクセス可能なアドレス空間の外を指している。
301 .\"O is wrong, or the socket was not in the
310 .\"O Too many symbolic links were encountered in resolving
313 を解決する際に遭遇したシンボリック・リンクが多過ぎる。
322 .\"O The file does not exist.
326 .\"O Insufficient kernel memory was available.
327 カーネルに、利用可能なメモリーが十分にない。
330 .\"O A component of the path prefix is not a directory.
334 .\"O The socket inode would reside on a read-only file system.
335 ソケット inode が読み込み専用のファイルシステム上にある。
336 .\"O .SH "CONFORMING TO"
338 .\"O SVr4, 4.4BSD, POSIX.1-2001
340 .\"O first appeared in 4.2BSD).
341 .\"O .\" SVr4 documents an additional
343 .\"O .\" general error condition, and
348 .\"O .\" UNIX-domain error conditions.
349 SVr4, 4.4BSD, POSIX.1-2001
352 .\" SVr4 には他に、一般のエラー状態
354 .\" 及び UNIX ドメインのエラー状態
360 .\"O POSIX.1-2001 does not require the inclusion of
361 .\"O .IR <sys/types.h> ,
362 .\"O and this header file is not required on Linux.
363 .\"O However, some historical (BSD) implementations required this header
364 .\"O file, and portable applications are probably wise to include it.
368 Linux ではこのヘッダファイルは必要ではない。
369 しかし、歴史的には、いくつかの実装 (BSD 系) でこのヘッダファイルが
370 必要であり、移植性が必要なアプリケーションではこのファイルを
373 .\"O The third argument of
375 .\"O is in reality an
377 .\"O (and this is what 4.x BSD and libc4 and libc5 have).
378 .\"O Some POSIX confusion resulted in the present
380 .\"O also used by glibc.
384 の三番目の引き数は (4.x BSD や libc4, libc5 と同様に) 実際には
386 である。glibc でも使われている現在の
388 に関して、POSIX には少し混乱がある。
394 .\"O The transparent proxy options are not described.
395 透過的プロキシ (transparent proxy) オプションについて記述していない。
396 .\" FIXME What *are* transparent proxy options?
399 .\"O An example of the use of
401 .\"O with Internet domain sockets can be found in
402 .\"O .BR getaddrinfo (3).
409 .\"O The following example shows how to bind a stream socket in the UNIX
411 .\"O domain, and accept connections:
414 でストリームソケットを bind する方法を示したものである。
415 .\" listen.7 refers to this example.
416 .\" accept.7 refers to this example.
417 .\" unix.7 refers to this example.
420 #include <sys/socket.h>
426 #define MY_SOCK_PATH "/somepath"
427 #define LISTEN_BACKLOG 50
429 #define handle_error(msg) \\
430 do { perror(msg); exit(EXIT_FAILURE); } while (0)
433 main(int argc, char *argv[])
436 struct sockaddr_un my_addr, peer_addr;
437 socklen_t peer_addr_size;
439 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
441 handle_error("socket");
443 memset(&my_addr, 0, sizeof(struct sockaddr_un));
444 /* Clear structure */
445 my_addr.sun_family = AF_UNIX;
446 strncpy(my_addr.sun_path, MY_SOCK_PATH,
447 sizeof(my_addr.sun_path) \- 1);
449 if (bind(sfd, (struct sockaddr *) &my_addr,
450 sizeof(struct sockaddr_un)) == \-1)
451 handle_error("bind");
453 if (listen(sfd, LISTEN_BACKLOG) == \-1)
454 handle_error("listen");
456 /* Now we can accept incoming connections one
457 at a time using accept(2) */
459 peer_addr_size = sizeof(struct sockaddr_un);
460 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
463 handle_error("accept");
465 /* Code to deal with incoming connection(s)... */
467 /* When no longer required, the socket pathname, MY_SOCK_PATH
468 should be deleted using unlink(2) or remove(3) */
482 .BR path_resolution (7),