1 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH GETPROTOENT_R 3 2021-03-22 "GNU" "Linux Programmer's Manual"
28 getprotoent_r, getprotobyname_r, getprotobynumber_r \- get
29 protocol entry (reentrant)
34 .BI "int getprotoent_r(struct protoent *restrict " result_buf ,
35 .BI " char *restrict " buf ", size_t " buflen ,
36 .BI " struct protoent **restrict " result );
37 .BI "int getprotobyname_r(const char *restrict " name ,
38 .BI " struct protoent *restrict " result_buf ,
39 .BI " char *restrict " buf ", size_t " buflen ,
40 .BI " struct protoent **restrict " result );
41 .BI "int getprotobynumber_r(int " proto ,
42 .BI " struct protoent *restrict " result_buf ,
43 .BI " char *restrict " buf ", size_t " buflen ,
44 .BI " struct protoent **restrict " result );
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
53 .BR getprotobyname_r (),
54 .BR getprotobynumber_r ():
58 Glibc 2.19 and earlier:
59 _BSD_SOURCE || _SVID_SOURCE
64 .BR getprotobyname_r (),
66 .BR getprotobynumber_r ()
67 functions are the reentrant equivalents of, respectively,
69 .BR getprotobyname (3),
71 .BR getprotobynumber (3).
72 They differ in the way that the
74 structure is returned,
75 and in the function calling signature and return value.
76 This manual page describes just the differences from
77 the nonreentrant functions.
79 Instead of returning a pointer to a statically allocated
81 structure as the function result,
82 these functions copy the structure into the location pointed to by
87 array is used to store the string fields pointed to by the returned
90 (The nonreentrant functions allocate these strings in static storage.)
91 The size of this array is specified in
95 is too small, the call fails with the error
97 and the caller must try again with a larger buffer.
98 (A buffer of length 1024 bytes should be sufficient for most applications.)
99 .\" I can find no information on the required/recommended buffer size;
100 .\" the nonreentrant functions use a 1024 byte buffer.
101 .\" The 1024 byte value is also what the Solaris man page suggests. -- mtk
103 If the function call successfully obtains a protocol record, then
111 On success, these functions return 0.
112 On error, they return one of the positive error numbers listed in ERRORS.
114 On error, record not found
115 .RB ( getprotobyname_r (),
116 .BR getprotobynumber_r ()),
118 .RB ( getprotoent_r ())
124 .RB ( getprotoent_r ())
125 No more records in database.
130 Try again with a larger buffer
134 For an explanation of the terms used in this section, see
142 Interface Attribute Value
144 .BR getprotoent_r (),
145 .BR getprotobyname_r (),
146 .BR getprotobynumber_r ()
147 T} Thread safety MT-Safe locale
153 These functions are GNU extensions.
154 Functions with similar names exist on some other systems,
155 though typically with different calling signatures.
157 The program below uses
158 .BR getprotobyname_r ()
159 to retrieve the protocol record for the protocol named
160 in its first command-line argument.
161 If a second (integer) command-line argument is supplied,
162 it is used as the initial value for
165 .BR getprotobyname_r ()
168 the program retries with larger buffer sizes.
169 The following shell session shows a couple of sample runs:
173 .RB "$" " ./a.out tcp 1"
174 ERANGE! Retrying with larger buffer
175 getprotobyname_r() returned: 0 (success) (buflen=78)
176 p_name=tcp; p_proto=6; aliases=TCP
177 .RB "$" " ./a.out xxx 1"
178 ERANGE! Retrying with larger buffer
179 getprotobyname_r() returned: 0 (success) (buflen=100)
180 Call failed/record not found
194 #define MAX_BUF 10000
197 main(int argc, char *argv[])
199 int buflen, erange_cnt, s;
200 struct protoent result_buf;
201 struct protoent *result;
205 printf("Usage: %s proto\-name [buflen]\en", argv[0]);
211 buflen = atoi(argv[2]);
213 if (buflen > MAX_BUF) {
214 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
220 s = getprotobyname_r(argv[1], &result_buf,
221 buf, buflen, &result);
224 printf("ERANGE! Retrying with larger buffer\en");
227 /* Increment a byte at a time so we can see exactly
228 what size buffer was required. */
232 if (buflen > MAX_BUF) {
233 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
237 } while (s == ERANGE);
239 printf("getprotobyname_r() returned: %s (buflen=%d)\en",
240 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
241 strerror(s), buflen);
243 if (s != 0 || result == NULL) {
244 printf("Call failed/record not found\en");
248 printf("p_name=%s; p_proto=%d; aliases=",
249 result_buf.p_name, result_buf.p_proto);
250 for (char **p = result_buf.p_aliases; *p != NULL; p++)