]>
Commit | Line | Data |
---|---|---|
ca1a4bba MK |
1 | .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk |
2 | .\" <mtk.manpages@gmail.com> | |
3 | .\" | |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
ca1a4bba MK |
5 | .\" Permission is granted to make and distribute verbatim copies of this |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of this | |
10 | .\" manual under the conditions for verbatim copying, provided that the | |
11 | .\" entire resulting derived work is distributed under the terms of a | |
12 | .\" permission notice identical to this one. | |
13 | .\" | |
14 | .\" Since the Linux kernel and libraries are constantly changing, this | |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
16 | .\" responsibility for errors or omissions, or for damages resulting from | |
17 | .\" the use of the information contained herein. The author(s) may not | |
18 | .\" have taken the same level of care in the production of this manual, | |
19 | .\" which is licensed free of charge, as they might when working | |
20 | .\" professionally. | |
21 | .\" | |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by | |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
ca1a4bba | 25 | .\" |
5722c835 | 26 | .TH GETPROTOENT_R 3 2015-07-23 "GNU" "Linux Programmer's Manual" |
ca1a4bba MK |
27 | .SH NAME |
28 | getprotoent_r, getprotobyname_r, getprotobynumber_r \- get | |
29 | protocol entry (reentrant) | |
30 | .SH SYNOPSIS | |
31 | .nf | |
32 | .B #include <netdb.h> | |
33 | .sp | |
34 | .BI "int getprotoent_r(struct protoent *" result_buf ", char *" buf , | |
35 | .BI " size_t " buflen ", struct protoent **" result ); | |
36 | .sp | |
37 | .BI "int getprotobyname_r(const char *" name , | |
38 | .BI " struct protoent *" result_buf ", char *" buf , | |
39 | .BI " size_t " buflen ", struct protoent **" result ); | |
40 | .sp | |
41 | .BI "int getprotobynumber_r(int " proto , | |
42 | .BI " struct protoent *" result_buf ", char *" buf , | |
43 | .BI " size_t " buflen ", struct protoent **" result ); | |
44 | .sp | |
45 | .fi | |
46 | .in -4n | |
47 | Feature Test Macro Requirements for glibc (see | |
48 | .BR feature_test_macros (7)): | |
49 | .ad l | |
50 | .in | |
51 | .sp | |
52 | .BR getprotoent_r (), | |
53 | .BR getprotobyname_r (), | |
54 | .BR getprotobynumber_r (): | |
51c612fb MK |
55 | Since glibc 2.19: |
56 | _DEFAULT_SOURCE | |
57 | Glibc 2.19 and earlier: | |
58 | _BSD_SOURCE || _SVID_SOURCE | |
ca1a4bba MK |
59 | .ad b |
60 | .SH DESCRIPTION | |
61 | The | |
62 | .BR getprotoent_r (), | |
63 | .BR getprotobyname_r (), | |
64 | and | |
65 | .BR getprotobynumber_r () | |
66 | functions are the reentrant equivalents of, respectively, | |
67 | .BR getprotoent (3), | |
68 | .BR getprotobyname (3), | |
69 | and | |
70 | .BR getprotobynumber (3). | |
71 | They differ in the way that the | |
72 | .I protoent | |
73 | structure is returned, | |
74 | and in the function calling signature and return value. | |
75 | This manual page describes just the differences from | |
54d75d6c | 76 | the nonreentrant functions. |
ca1a4bba MK |
77 | |
78 | Instead of returning a pointer to a statically allocated | |
79 | .I protoent | |
80 | structure as the function result, | |
81 | these functions copy the structure into the location pointed to by | |
82 | .IR result_buf . | |
83 | ||
84 | The | |
85 | .I buf | |
86 | array is used to store the string fields pointed to by the returned | |
87 | .I protoent | |
88 | structure. | |
54d75d6c | 89 | (The nonreentrant functions allocate these strings in static storage.) |
ca1a4bba MK |
90 | The size of this array is specified in |
91 | .IR buflen . | |
92 | If | |
93 | .I buf | |
94 | is too small, the call fails with the error | |
95 | .BR ERANGE , | |
96 | and the caller must try again with a larger buffer. | |
97 | (A buffer of length 1024 bytes should be sufficient for most applications.) | |
98 | .\" I can find no information on the required/recommended buffer size; | |
54d75d6c | 99 | .\" the nonreentrant functions use a 1024 byte buffer. |
ca1a4bba MK |
100 | .\" The 1024 byte value is also what the Solaris man page suggests. -- mtk |
101 | ||
102 | If the function call successfully obtains a protocol record, then | |
103 | .I *result | |
104 | is set pointing to | |
105 | .IR result_buf ; | |
106 | otherwise, | |
107 | .I *result | |
108 | is set to NULL. | |
47297adb | 109 | .SH RETURN VALUE |
ca1a4bba | 110 | On success, these functions return 0. |
535f0df5 | 111 | On error, they return one of the positive error numbers listed in ERRORS. |
ca1a4bba MK |
112 | |
113 | On error, record not found | |
114 | .RB ( getprotobyname_r (), | |
115 | .BR getprotobynumber_r ()), | |
116 | or end of input | |
117 | .RB ( getprotoent_r ()) | |
118 | .I result | |
119 | is set to NULL. | |
120 | .SH ERRORS | |
121 | .TP | |
122 | .B ENOENT | |
123 | .RB ( getprotoent_r ()) | |
124 | No more records in database. | |
125 | .TP | |
126 | .B ERANGE | |
127 | .I buf | |
128 | is too small. | |
129 | Try again with a larger buffer | |
130 | (and increased | |
131 | .IR buflen ). | |
252c5d6b ZL |
132 | .SH ATTRIBUTES |
133 | For an explanation of the terms used in this section, see | |
134 | .BR attributes (7). | |
135 | .TS | |
136 | allbox; | |
b1c3107a | 137 | lbw20 lb lb |
252c5d6b ZL |
138 | l l l. |
139 | Interface Attribute Value | |
140 | T{ | |
141 | .BR getprotoent_r (), | |
142 | .br | |
143 | .BR getprotobyname_r (), | |
144 | .br | |
145 | .BR getprotobynumber_r () | |
146 | T} Thread safety MT-Safe locale | |
147 | .TE | |
148 | ||
47297adb | 149 | .SH CONFORMING TO |
ca1a4bba MK |
150 | These functions are GNU extensions. |
151 | Functions with similar names exist on some other systems, | |
152 | though typically with different calling signatures. | |
153 | .SH EXAMPLE | |
154 | The program below uses | |
155 | .BR getprotobyname_r () | |
156 | to retrieve the protocol record for the protocol named | |
157 | in its first command-line argument. | |
158 | If a second (integer) command-line argument is supplied, | |
159 | it is used as the initial value for | |
160 | .IR buflen ; | |
161 | if | |
162 | .BR getprotobyname_r () | |
163 | fails with the error | |
164 | .BR ERANGE , | |
165 | the program retries with larger buffer sizes. | |
166 | The following shell session shows a couple of sample runs: | |
167 | .in +4n | |
168 | .nf | |
169 | ||
b43a3b30 | 170 | .RB "$" " ./a.out tcp 1" |
ca1a4bba MK |
171 | ERANGE! Retrying with larger buffer |
172 | getprotobyname_r() returned: 0 (success) (buflen=78) | |
173 | p_name=tcp; p_proto=6; aliases=TCP | |
b43a3b30 | 174 | .RB "$" " ./a.out xxx 1" |
ca1a4bba MK |
175 | ERANGE! Retrying with larger buffer |
176 | getprotobyname_r() returned: 0 (success) (buflen=100) | |
177 | Call failed/record not found | |
ca1a4bba MK |
178 | .fi |
179 | .in | |
9c330504 | 180 | .SS Program source |
d84d0300 | 181 | \& |
ca1a4bba MK |
182 | .nf |
183 | #define _GNU_SOURCE | |
184 | #include <ctype.h> | |
185 | #include <netdb.h> | |
186 | #include <stdlib.h> | |
187 | #include <stdio.h> | |
188 | #include <errno.h> | |
189 | #include <string.h> | |
190 | ||
191 | #define MAX_BUF 10000 | |
192 | ||
193 | int | |
194 | main(int argc, char *argv[]) | |
195 | { | |
196 | int buflen, erange_cnt, s; | |
197 | struct protoent result_buf; | |
198 | struct protoent *result; | |
199 | char buf[MAX_BUF]; | |
200 | char **p; | |
201 | ||
202 | if (argc < 2) { | |
203 | printf("Usage: %s proto\-name [buflen]\\n", argv[0]); | |
204 | exit(EXIT_FAILURE); | |
205 | } | |
206 | ||
207 | buflen = 1024; | |
208 | if (argc > 2) | |
209 | buflen = atoi(argv[2]); | |
210 | ||
211 | if (buflen > MAX_BUF) { | |
212 | printf("Exceeded buffer limit (%d)\\n", MAX_BUF); | |
213 | exit(EXIT_FAILURE); | |
214 | } | |
215 | ||
216 | erange_cnt = 0; | |
217 | do { | |
218 | s = getprotobyname_r(argv[1], &result_buf, | |
219 | buf, buflen, &result); | |
220 | if (s == ERANGE) { | |
221 | if (erange_cnt == 0) | |
222 | printf("ERANGE! Retrying with larger buffer\\n"); | |
223 | erange_cnt++; | |
224 | ||
225 | /* Increment a byte at a time so we can see exactly | |
226 | what size buffer was required */ | |
227 | ||
228 | buflen++; | |
229 | ||
230 | if (buflen > MAX_BUF) { | |
231 | printf("Exceeded buffer limit (%d)\\n", MAX_BUF); | |
232 | exit(EXIT_FAILURE); | |
233 | } | |
234 | } | |
235 | } while (s == ERANGE); | |
236 | ||
237 | printf("getprotobyname_r() returned: %s (buflen=%d)\\n", | |
238 | (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" : | |
239 | strerror(s), buflen); | |
240 | ||
241 | if (s != 0 || result == NULL) { | |
242 | printf("Call failed/record not found\\n"); | |
243 | exit(EXIT_FAILURE); | |
244 | } | |
245 | ||
246 | printf("p_name=%s; p_proto=%d; aliases=", | |
247 | result_buf.p_name, result_buf.p_proto); | |
248 | for (p = result_buf.p_aliases; *p != NULL; p++) | |
249 | printf("%s ", *p); | |
250 | printf("\\n"); | |
251 | ||
252 | exit(EXIT_SUCCESS); | |
253 | } | |
254 | .fi | |
47297adb | 255 | .SH SEE ALSO |
ca1a4bba | 256 | .BR getprotoent (3), |
ca1a4bba | 257 | .BR protocols (5) |