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
125 Interface Attribute Value
130 .BR getservbyname_r (),
131 .BR getservbyport_r ()
132 T} Thread safety MT-Safe locale
135 Functions with similar names exist on some other systems,
136 though typically with different calling signatures.
140 The program below uses
141 .BR getservbyport_r ()
142 to retrieve the service record for the port and protocol named
143 in its first command-line argument.
144 If a third (integer) command-line argument is supplied,
145 it is used as the initial value for
148 .BR getservbyport_r ()
151 the program retries with larger buffer sizes.
152 The following shell session shows a couple of sample runs:
156 .RB "$" " ./a.out 7 tcp 1"
157 ERANGE! Retrying with larger buffer
158 getservbyport_r() returned: 0 (success) (buflen=87)
159 s_name=echo; s_proto=tcp; s_port=7; aliases=
160 .RB "$" " ./a.out 77777 tcp"
161 getservbyport_r() returned: 0 (success) (buflen=1024)
162 Call failed/record not found
167 .\" SRC BEGIN (getservent_r.c)
177 #define MAX_BUF 10000
180 main(int argc, char *argv[])
182 int buflen, erange_cnt, port, s;
183 struct servent result_buf;
184 struct servent *result;
189 printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
193 port = htons(atoi(argv[1]));
194 protop = (strcmp(argv[2], "null") == 0 ||
195 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
199 buflen = atoi(argv[3]);
201 if (buflen > MAX_BUF) {
202 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
208 s = getservbyport_r(port, protop, &result_buf,
209 buf, buflen, &result);
212 printf("ERANGE! Retrying with larger buffer\en");
215 /* Increment a byte at a time so we can see exactly
216 what size buffer was required. */
220 if (buflen > MAX_BUF) {
221 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
225 } while (s == ERANGE);
227 printf("getservbyport_r() returned: %s (buflen=%d)\en",
228 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
229 strerror(s), buflen);
231 if (s != 0 || result == NULL) {
232 printf("Call failed/record not found\en");
236 printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
237 result_buf.s_name, result_buf.s_proto,
238 ntohs(result_buf.s_port));
239 for (char **p = result_buf.s_aliases; *p != NULL; p++)