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
81 bind \- ¥½¥±¥Ã¥È¤Ë̾Á°¤ò¤Ä¤±¤ë
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 ¸ºß¤¹¤ë¤¬¡¢¥¢¥É¥ì¥¹¤Ï³ä¤êÅö¤Æ¤é¤ì¤Æ¤¤¤Ê¤¤¡£
110 ¤Ï¡¢¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿
112 ¤Ç»²¾È¤µ¤ì¤ë¥½¥±¥Ã¥È¤Ë
114 ¤Ç»ØÄꤵ¤ì¤¿¥¢¥É¥ì¥¹¤ò³ä¤êÅö¤Æ¤ë¡£
118 ¤¬»Ø¤¹¥¢¥É¥ì¥¹¹½Â¤ÂΤΥµ¥¤¥º¤ò¥Ð¥¤¥Èñ°Ì¤Ç»ØÄꤹ¤ë¡£
120 ¡Ö¥½¥±¥Ã¥È¤Ë̾Á°¤ò¤Ä¤±¤ë¡×
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 ¥½¥±¥Ã¥È¤¬Àܳ¤ò¼õ¤±ÉÕ¤±¤é¤ì¤ë¤è¤¦¤Ë¤¹¤ë¤Ë¤Ï
132 ¤ò»²¾È)¡¢Ä̾綠¤ÎÁ°¤Ë
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 ̾Á°ÉÕ¤±¤Î¥ë¡¼¥ë¤Ï¥¢¥É¥ì¥¹¡¦¥Õ¥¡¥ß¥ê¡¼¤´¤È¤Ë°Û¤Ê¤Ã¤Æ¤¤¤ë¡£¾ÜºÙ¤Ê¾ðÊó¤Ï
143 Âè 7 ¾Ï¤Î³Æ¥Þ¥Ë¥å¥¢¥ë¤ò»²¾È¤¹¤ë¤³¤È¡£
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 °ú¤¿ô¤Ë¼ÂºÝ¤Ë¤É¤Î¤è¤¦¤Ê¹½Â¤ÂΤ¬ÅϤµ¤ì¤ë¤«¤Ï¡¢
205 ¥¢¥É¥ì¥¹¡¦¥Õ¥¡¥ß¥ê¡¼¤Ë°Í¸¤¹¤ë¡£
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.
234 À®¸ù¤¹¤ë¤È 0 ¤òÊÖ¤¹¡£
235 ¼ºÇÔ¤¹¤ë¤È \-1 ¤òÊÖ¤·¡¢
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.
248 »ØÄꤵ¤ì¤¿¥¢¥É¥ì¥¹¤¬´û¤Ë»ÈÍÑÃæ¤Ç¤¢¤ë¡£
252 .\"O is not a valid descriptor.
254 ¤¬ÉÔÀµ¤Ê¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤¢¤ë¡£
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
274 °Ê²¼¤Î¥¨¥é¡¼¤Ï Unix¥É¥á¥¤¥ó
276 ¤Î¥½¥±¥Ã¥ÈÆÃͤǤ¢¤ë:
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 ¸ºß¤·¤Ê¤¤¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤¬Í׵ᤵ¤ì¤¿¤«¡¢Í׵ᤵ¤ì¤¿¥¢¥É¥ì¥¹¤¬
290 ¥í¡¼¥«¥ë¤Ç¤Ï¤Ê¤«¤Ã¤¿¡£
294 .\"O points outside the user's accessible address space.
296 ¤¬¥æ¡¼¥¶¤Î¥¢¥¯¥»¥¹²Äǽ¤Ê¥¢¥É¥ì¥¹¶õ´Ö¤Î³°¤ò»Ø¤·¤Æ¤¤¤ë¡£
301 .\"O is wrong, or the socket was not in the
305 ¤¬ÉÔÀµ¤Ç¤¢¤ë¤«¡¢¥½¥±¥Ã¥È¤¬
310 .\"O Too many symbolic links were encountered in resolving
313 ¤ò²ò·è¤¹¤ëºÝ¤ËÁø¶ø¤·¤¿¥·¥ó¥Ü¥ê¥Ã¥¯¡¦¥ê¥ó¥¯¤¬Â¿²á¤®¤ë¡£
322 .\"O The file does not exist.
323 ¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤·¤Ê¤¤¡£
326 .\"O Insufficient kernel memory was available.
327 ¥«¡¼¥Í¥ë¤Ë¡¢ÍøÍѲÄǽ¤Ê¥á¥â¥ê¡¼¤¬½½Ê¬¤Ë¤Ê¤¤¡£
330 .\"O A component of the path prefix is not a directory.
331 ¥Ñ¥¹Ì¾¤Î¹½À®Í×ÁǤ¬¥Ç¥£¥ì¥¯¥È¥ê¤Ç¤Ï¤Ê¤¤¡£
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
351 ¤Ï 4.2BSD ¤ÇºÇ½é¤Ë¸½¤ï¤ì¤¿)¡£
352 .\" SVr4 ¤Ë¤Ï¾¤Ë¡¢°ìÈ̤Υ¨¥é¡¼¾õÂÖ
354 .\" µÚ¤Ó Unix ¥É¥á¥¤¥ó¤Î¥¨¥é¡¼¾õÂÖ
357 .\" ¤Ë¤Ä¤¤¤Æ¤Îµ½Ò¤¬¤¢¤ë¡£
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.
367 ¤Î¥¤¥ó¥¯¥ë¡¼¥É¤Ïɬ¿Ü¤È¤µ¤ì¤Æ¤ª¤é¤º¡¢
368 Linux ¤Ç¤Ï¤³¤Î¥Ø¥Ã¥À¥Õ¥¡¥¤¥ë¤ÏɬÍפǤϤʤ¤¡£
369 ¤·¤«¤·¡¢Îò»ËŪ¤Ë¤Ï¡¢¤¤¤¯¤Ä¤«¤Î¼ÂÁõ (BSD ·Ï) ¤Ç¤³¤Î¥Ø¥Ã¥À¥Õ¥¡¥¤¥ë¤¬
370 ɬÍפǤ¢¤ê¡¢°Ü¿¢À¤¬É¬Íפʥ¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤Ï¤³¤Î¥Õ¥¡¥¤¥ë¤ò
371 ¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ë¤Î¤¬¸ÌÀ¤Ç¤¢¤í¤¦¡£
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).
403 ¥¤¥ó¥¿¡¼¥Í¥Ã¥È¡¦¥É¥á¥¤¥ó¡¦¥½¥±¥Ã¥È¤Ç¤Î
409 .\"O The following example shows how to bind a stream socket in the Unix
411 .\"O domain, and accept connections:
412 °Ê²¼¤ÎÎã¤Ï¡¢Unix ¥É¥á¥¤¥ó
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),