memfd_secret.2: SEE ALSO: add memfd_create(2)
[man-pages.git] / man3 / bindresvport.3
blob36d49234d1edd5399287810cdacd629b5322e67e
1 .\" Copyright (C) 2007, Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
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 .\" 2007-05-31, mtk: Rewrite and substantial additional text.
28 .\" 2008-12-03, mtk: Rewrote some pieces and fixed some errors
29 .\"
30 .TH BINDRESVPORT 3  2021-03-22 "" "Linux Programmer's Manual"
31 .SH NAME
32 bindresvport \- bind a socket to a privileged IP port
33 .SH SYNOPSIS
34 .nf
35 .B #include <sys/types.h>
36 .B #include <netinet/in.h>
37 .PP
38 .BI "int bindresvport(int " sockfd ", struct sockaddr_in *" sin );
39 .fi
40 .SH DESCRIPTION
41 .BR bindresvport ()
42 is used to bind the socket referred to by the
43 file descriptor
44 .I sockfd
45 to a privileged anonymous IP port,
46 that is, a port number arbitrarily selected from the range 512 to 1023.
47 .\" Glibc actually starts searching with a port # in the range 600 to 1023
48 .PP
49 If the
50 .BR bind (2)
51 performed by
52 .BR bindresvport ()
53 is successful, and
54 .I sin
55 is not NULL, then
56 .I sin\->sin_port
57 returns the port number actually allocated.
58 .PP
59 .I sin
60 can be NULL, in which case
61 .I sin\->sin_family
62 is implicitly taken to be
63 .BR AF_INET .
64 However, in this case,
65 .BR bindresvport ()
66 has no way to return the port number actually allocated.
67 (This information can later be obtained using
68 .BR getsockname (2).)
69 .SH RETURN VALUE
70 .BR bindresvport ()
71 returns 0 on success; otherwise \-1 is returned and
72 .I errno
73 is set to indicate the error.
74 .SH ERRORS
75 .BR bindresvport ()
76 can fail for any of the same reasons as
77 .BR bind (2).
78 In addition, the following errors may occur:
79 .TP
80 .BR EACCES
81 The calling process was not privileged
82 (on Linux: the calling process did not have the
83 .B CAP_NET_BIND_SERVICE
84 capability in the user namespace governing its network namespace).
85 .TP
86 .B EADDRINUSE
87 All privileged ports are in use.
88 .TP
89 .BR EAFNOSUPPORT " (" EPFNOSUPPORT " in glibc 2.7 and earlier)"
90 .I sin
91 is not NULL and
92 .I sin\->sin_family
93 is not
94 .BR AF_INET .
95 .SH ATTRIBUTES
96 For an explanation of the terms used in this section, see
97 .BR attributes (7).
98 .ad l
99 .nh
101 allbox;
102 lb lb lbx
103 l l l.
104 Interface       Attribute       Value
106 .BR bindresvport ()
107 T}      Thread safety   T{
108 glibc\ >=\ 2.17: MT-Safe;
109 .\" commit f6da27e53695ad1cc0e2a9490358decbbfdff5e5
110 glibc\ <\ 2.17: MT-Unsafe
115 .sp 1
118 .BR bindresvport ()
119 function uses a static variable that was not protected by a lock
120 before glibc 2.17, rendering the function MT-Unsafe.
121 .SH CONFORMING TO
122 Not in POSIX.1.
123 Present on the BSDs, Solaris, and many other systems.
124 .SH NOTES
125 Unlike some
126 .BR bindresvport ()
127 implementations,
128 the glibc implementation ignores any value that the caller supplies in
129 .IR sin\->sin_port .
130 .SH SEE ALSO
131 .BR bind (2),
132 .BR getsockname (2)