2 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH getservent_r 3 (date) "Linux man-pages (unreleased)"
9 getservent_r, getservbyname_r, getservbyport_r \- get
10 service entry (reentrant)
13 .RI ( libc ", " \-lc )
18 .BI "int getservent_r(struct servent *restrict " result_buf ,
19 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
20 .BI " struct servent **restrict " result );
21 .BI "int getservbyname_r(const char *restrict " name ,
22 .BI " const char *restrict " proto ,
23 .BI " struct servent *restrict " result_buf ,
24 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
25 .BI " struct servent **restrict " result );
26 .BI "int getservbyport_r(int " port ,
27 .BI " const char *restrict " proto ,
28 .BI " struct servent *restrict " result_buf ,
29 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
30 .BI " struct servent **restrict " result );
34 Feature Test Macro Requirements for glibc (see
35 .BR feature_test_macros (7)):
39 .BR getservbyname_r (),
40 .BR getservbyport_r ():
44 glibc 2.19 and earlier:
45 _BSD_SOURCE || _SVID_SOURCE
50 .BR getservbyname_r (),
52 .BR getservbyport_r ()
53 functions are the reentrant equivalents of, respectively,
55 .BR getservbyname (3),
57 .BR getservbyport (3).
58 They differ in the way that the
60 structure is returned,
61 and in the function calling signature and return value.
62 This manual page describes just the differences from
63 the nonreentrant functions.
65 Instead of returning a pointer to a statically allocated
67 structure as the function result,
68 these functions copy the structure into the location pointed to by
73 array is used to store the string fields pointed to by the returned
76 (The nonreentrant functions allocate these strings in static storage.)
77 The size of this array is specified in
81 is too small, the call fails with the error
83 and the caller must try again with a larger buffer.
84 (A buffer of length 1024 bytes should be sufficient for most applications.)
85 .\" I can find no information on the required/recommended buffer size;
86 .\" the nonreentrant functions use a 1024 byte buffer -- mtk.
88 If the function call successfully obtains a service record, then
96 On success, these functions return 0.
97 On error, they return one of the positive error numbers listed in errors.
99 On error, record not found
100 .RB ( getservbyname_r (),
101 .BR getservbyport_r ()),
103 .RB ( getservent_r ())
109 .RB ( getservent_r ())
110 No more records in database.
115 Try again with a larger buffer
119 For an explanation of the terms used in this section, see
127 Interface Attribute Value
130 .BR getservbyname_r (),
131 .BR getservbyport_r ()
132 T} Thread safety MT-Safe locale
138 These functions are GNU extensions.
139 Functions with similar names exist on some other systems,
140 though typically with different calling signatures.
142 The program below uses
143 .BR getservbyport_r ()
144 to retrieve the service record for the port and protocol named
145 in its first command-line argument.
146 If a third (integer) command-line argument is supplied,
147 it is used as the initial value for
150 .BR getservbyport_r ()
153 the program retries with larger buffer sizes.
154 The following shell session shows a couple of sample runs:
158 .RB "$" " ./a.out 7 tcp 1"
159 ERANGE! Retrying with larger buffer
160 getservbyport_r() returned: 0 (success) (buflen=87)
161 s_name=echo; s_proto=tcp; s_port=7; aliases=
162 .RB "$" " ./a.out 77777 tcp"
163 getservbyport_r() returned: 0 (success) (buflen=1024)
164 Call failed/record not found
169 .\" SRC BEGIN (getservent_r.c)
179 #define MAX_BUF 10000
182 main(int argc, char *argv[])
184 int buflen, erange_cnt, port, s;
185 struct servent result_buf;
186 struct servent *result;
191 printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
195 port = htons(atoi(argv[1]));
196 protop = (strcmp(argv[2], "null") == 0 ||
197 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
201 buflen = atoi(argv[3]);
203 if (buflen > MAX_BUF) {
204 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
210 s = getservbyport_r(port, protop, &result_buf,
211 buf, buflen, &result);
214 printf("ERANGE! Retrying with larger buffer\en");
217 /* Increment a byte at a time so we can see exactly
218 what size buffer was required. */
222 if (buflen > MAX_BUF) {
223 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
227 } while (s == ERANGE);
229 printf("getservbyport_r() returned: %s (buflen=%d)\en",
230 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
231 strerror(s), buflen);
233 if (s != 0 || result == NULL) {
234 printf("Call failed/record not found\en");
238 printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
239 result_buf.s_name, result_buf.s_proto,
240 ntohs(result_buf.s_port));
241 for (char **p = result_buf.s_aliases; *p != NULL; p++)