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 GETSERVENT_R 3 2021-03-22 "GNU" "Linux Programmer's Manual"
28 getservent_r, getservbyname_r, getservbyport_r \- get
29 service entry (reentrant)
34 .BI "int getservent_r(struct servent *restrict " result_buf ,
35 .BI " char *restrict " buf ", size_t " buflen ,
36 .BI " struct servent **restrict " result );
37 .BI "int getservbyname_r(const char *restrict " name ,
38 .BI " const char *restrict " proto ,
39 .BI " struct servent *restrict " result_buf ,
40 .BI " char *restrict " buf ", size_t " buflen ,
41 .BI " struct servent **restrict " result );
42 .BI "int getservbyport_r(int " port ,
43 .BI " const char *restrict " proto ,
44 .BI " struct servent *restrict " result_buf ,
45 .BI " char *restrict " buf ", size_t " buflen ,
46 .BI " struct servent **restrict " result );
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
55 .BR getservbyname_r (),
56 .BR getservbyport_r ():
60 Glibc 2.19 and earlier:
61 _BSD_SOURCE || _SVID_SOURCE
66 .BR getservbyname_r (),
68 .BR getservbyport_r ()
69 functions are the reentrant equivalents of, respectively,
71 .BR getservbyname (3),
73 .BR getservbyport (3).
74 They differ in the way that the
76 structure is returned,
77 and in the function calling signature and return value.
78 This manual page describes just the differences from
79 the nonreentrant functions.
81 Instead of returning a pointer to a statically allocated
83 structure as the function result,
84 these functions copy the structure into the location pointed to by
89 array is used to store the string fields pointed to by the returned
92 (The nonreentrant functions allocate these strings in static storage.)
93 The size of this array is specified in
97 is too small, the call fails with the error
99 and the caller must try again with a larger buffer.
100 (A buffer of length 1024 bytes should be sufficient for most applications.)
101 .\" I can find no information on the required/recommended buffer size;
102 .\" the nonreentrant functions use a 1024 byte buffer -- mtk.
104 If the function call successfully obtains a service record, then
112 On success, these functions return 0.
113 On error, they return one of the positive error numbers listed in errors.
115 On error, record not found
116 .RB ( getservbyname_r (),
117 .BR getservbyport_r ()),
119 .RB ( getservent_r ())
125 .RB ( getservent_r ())
126 No more records in database.
131 Try again with a larger buffer
135 For an explanation of the terms used in this section, see
143 Interface Attribute Value
146 .BR getservbyname_r (),
147 .BR getservbyport_r ()
148 T} Thread safety MT-Safe locale
154 These functions are GNU extensions.
155 Functions with similar names exist on some other systems,
156 though typically with different calling signatures.
158 The program below uses
159 .BR getservbyport_r ()
160 to retrieve the service record for the port and protocol named
161 in its first command-line argument.
162 If a third (integer) command-line argument is supplied,
163 it is used as the initial value for
166 .BR getservbyport_r ()
169 the program retries with larger buffer sizes.
170 The following shell session shows a couple of sample runs:
174 .RB "$" " ./a.out 7 tcp 1"
175 ERANGE! Retrying with larger buffer
176 getservbyport_r() returned: 0 (success) (buflen=87)
177 s_name=echo; s_proto=tcp; s_port=7; aliases=
178 .RB "$" " ./a.out 77777 tcp"
179 getservbyport_r() returned: 0 (success) (buflen=1024)
180 Call failed/record not found
194 #define MAX_BUF 10000
197 main(int argc, char *argv[])
199 int buflen, erange_cnt, port, s;
200 struct servent result_buf;
201 struct servent *result;
206 printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
210 port = htons(atoi(argv[1]));
211 protop = (strcmp(argv[2], "null") == 0 ||
212 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
216 buflen = atoi(argv[3]);
218 if (buflen > MAX_BUF) {
219 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
225 s = getservbyport_r(port, protop, &result_buf,
226 buf, buflen, &result);
229 printf("ERANGE! Retrying with larger buffer\en");
232 /* Increment a byte at a time so we can see exactly
233 what size buffer was required. */
237 if (buflen > MAX_BUF) {
238 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
242 } while (s == ERANGE);
244 printf("getservbyport_r() returned: %s (buflen=%d)\en",
245 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
246 strerror(s), buflen);
248 if (s != 0 || result == NULL) {
249 printf("Call failed/record not found\en");
253 printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
254 result_buf.s_name, result_buf.s_proto,
255 ntohs(result_buf.s_port));
256 for (char **p = result_buf.s_aliases; *p != NULL; p++)