1 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH GETSERVENT_R 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
8 getservent_r, getservbyname_r, getservbyport_r \- get
9 service entry (reentrant)
12 .RI ( libc ", " \-lc )
17 .BI "int getservent_r(struct servent *restrict " result_buf ,
18 .BI " char *restrict " buf ", size_t " buflen ,
19 .BI " struct servent **restrict " result );
20 .BI "int getservbyname_r(const char *restrict " name ,
21 .BI " const char *restrict " proto ,
22 .BI " struct servent *restrict " result_buf ,
23 .BI " char *restrict " buf ", size_t " buflen ,
24 .BI " struct servent **restrict " result );
25 .BI "int getservbyport_r(int " port ,
26 .BI " const char *restrict " proto ,
27 .BI " struct servent *restrict " result_buf ,
28 .BI " char *restrict " buf ", size_t " buflen ,
29 .BI " struct servent **restrict " result );
33 Feature Test Macro Requirements for glibc (see
34 .BR feature_test_macros (7)):
38 .BR getservbyname_r (),
39 .BR getservbyport_r ():
43 Glibc 2.19 and earlier:
44 _BSD_SOURCE || _SVID_SOURCE
49 .BR getservbyname_r (),
51 .BR getservbyport_r ()
52 functions are the reentrant equivalents of, respectively,
54 .BR getservbyname (3),
56 .BR getservbyport (3).
57 They differ in the way that the
59 structure is returned,
60 and in the function calling signature and return value.
61 This manual page describes just the differences from
62 the nonreentrant functions.
64 Instead of returning a pointer to a statically allocated
66 structure as the function result,
67 these functions copy the structure into the location pointed to by
72 array is used to store the string fields pointed to by the returned
75 (The nonreentrant functions allocate these strings in static storage.)
76 The size of this array is specified in
80 is too small, the call fails with the error
82 and the caller must try again with a larger buffer.
83 (A buffer of length 1024 bytes should be sufficient for most applications.)
84 .\" I can find no information on the required/recommended buffer size;
85 .\" the nonreentrant functions use a 1024 byte buffer -- mtk.
87 If the function call successfully obtains a service record, then
95 On success, these functions return 0.
96 On error, they return one of the positive error numbers listed in errors.
98 On error, record not found
99 .RB ( getservbyname_r (),
100 .BR getservbyport_r ()),
102 .RB ( getservent_r ())
108 .RB ( getservent_r ())
109 No more records in database.
114 Try again with a larger buffer
118 For an explanation of the terms used in this section, see
126 Interface Attribute Value
129 .BR getservbyname_r (),
130 .BR getservbyport_r ()
131 T} Thread safety MT-Safe locale
137 These functions are GNU extensions.
138 Functions with similar names exist on some other systems,
139 though typically with different calling signatures.
141 The program below uses
142 .BR getservbyport_r ()
143 to retrieve the service record for the port and protocol named
144 in its first command-line argument.
145 If a third (integer) command-line argument is supplied,
146 it is used as the initial value for
149 .BR getservbyport_r ()
152 the program retries with larger buffer sizes.
153 The following shell session shows a couple of sample runs:
157 .RB "$" " ./a.out 7 tcp 1"
158 ERANGE! Retrying with larger buffer
159 getservbyport_r() returned: 0 (success) (buflen=87)
160 s_name=echo; s_proto=tcp; s_port=7; aliases=
161 .RB "$" " ./a.out 77777 tcp"
162 getservbyport_r() returned: 0 (success) (buflen=1024)
163 Call failed/record not found
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++)