Debian Codegraph @

upstream/mageia-cauldron/man3/getservent_r.3

raw
1 '\" t
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH getservent_r 3 2025-09-21 "Linux man-pages 6.17"
7 .SH NAME
8 getservent_r, getservbyname_r, getservbyport_r \- get
9 service entry (reentrant)
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .B #include <netdb.h>
16 .P
17 .BR "int getservent_r(" "size_t size;"
18 .BI " struct servent *restrict " result_buf ,
19 .BI " char " buf "[restrict " size "], size_t " size ,
20 .BI " struct servent **restrict " result );
21 .BR "int getservbyname_r(" "size_t size;"
22 .BI " const char *restrict " name ,
23 .BI " const char *restrict " proto ,
24 .BI " struct servent *restrict " result_buf ,
25 .BI " char " buf "[restrict " size "], size_t " size ,
26 .BI " struct servent **restrict " result );
27 .BR "int getservbyport_r(" "size_t size;"
28 .BI " int " port ,
29 .BI " const char *restrict " proto ,
30 .BI " struct servent *restrict " result_buf ,
31 .BI " char " buf "[restrict " size "], size_t " size ,
32 .BI " struct servent **restrict " result );
33 .P
34 .fi
35 .RS -4
36 Feature Test Macro Requirements for glibc (see
37 .BR feature_test_macros (7)):
38 .RE
39 .P
40 .BR getservent_r (),
41 .BR getservbyname_r (),
42 .BR getservbyport_r ():
43 .nf
44 Since glibc 2.19:
45 _DEFAULT_SOURCE
46 glibc 2.19 and earlier:
47 _BSD_SOURCE || _SVID_SOURCE
48 .fi
49 .SH DESCRIPTION
50 The
51 .BR getservent_r (),
52 .BR getservbyname_r (),
53 and
54 .BR getservbyport_r ()
55 functions are the reentrant equivalents of, respectively,
56 .BR getservent (3),
57 .BR getservbyname (3),
58 and
59 .BR getservbyport (3).
60 They differ in the way that the
61 .I servent
62 structure is returned,
63 and in the function calling signature and return value.
64 This manual page describes just the differences from
65 the nonreentrant functions.
66 .P
67 Instead of returning a pointer to a statically allocated
68 .I servent
69 structure as the function result,
70 these functions copy the structure into the location pointed to by
71 .IR result_buf .
72 .P
73 The
74 .I buf
75 array is used to store the string fields pointed to by the returned
76 .I servent
77 structure.
78 (The nonreentrant functions allocate these strings in static storage.)
79 The size of this array is specified in
80 .IR size .
81 If
82 .I buf
83 is too small, the call fails with the error
84 .BR ERANGE ,
85 and the caller must try again with a larger buffer.
86 (A buffer of size 1024 bytes should be sufficient for most applications.)
87 .\" I can find no information on the required/recommended buffer size;
88 .\" the nonreentrant functions use a 1024 byte buffer -- mtk.
89 .P
90 If the function call successfully obtains a service record, then
91 .I *result
92 is set pointing to
93 .IR result_buf ;
94 otherwise,
95 .I *result
96 is set to NULL.
97 .SH RETURN VALUE
98 On success, these functions return 0.
99 On error, they return one of the positive error numbers listed in errors.
100 .P
101 On error, record not found
102 .RB ( getservbyname_r (),
103 .BR getservbyport_r ()),
104 or end of input
105 .RB ( getservent_r ())
106 .I result
107 is set to NULL.
108 .SH ERRORS
109 .TP
110 .B ENOENT
111 .RB ( getservent_r ())
112 No more records in database.
113 .TP
114 .B ERANGE
115 .I buf
116 is too small.
117 Try again with a larger buffer
118 (and increased
119 .IR size ).
120 .SH ATTRIBUTES
121 For an explanation of the terms used in this section, see
122 .BR attributes (7).
123 .TS
124 allbox;
125 lbx lb lb
126 l l l.
127 Interface Attribute Value
128 T{
129 .na
130 .nh
131 .BR getservent_r (),
132 .BR getservbyname_r (),
133 .BR getservbyport_r ()
134 T} Thread safety MT-Safe locale
135 .TE
136 .SH VERSIONS
137 Functions with similar names exist on some other systems,
138 though typically with different calling signatures.
139 .SH STANDARDS
140 GNU.
141 .SH EXAMPLES
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
148 .IR size ;
149 if
150 .BR getservbyport_r ()
151 fails with the error
152 .BR ERANGE ,
153 the program retries with larger buffer sizes.
154 The following shell session shows a couple of sample runs:
155 .P
156 .in +4n
157 .EX
158 .RB "$" " ./a.out 7 tcp 1"
159 ERANGE! Retrying with larger buffer
160 getservbyport_r() returned: 0 (success) (size=87)
161 s_name=echo; s_proto=tcp; s_port=7; aliases=
162 .RB "$" " ./a.out 77777 tcp"
163 getservbyport_r() returned: 0 (success) (size=1024)
164 Call failed/record not found
165 .EE
166 .in
167 .SS Program source
168 \&
169 .\" SRC BEGIN (getservent_r.c)
170 .EX
171 #define _GNU_SOURCE
172 #include <ctype.h>
173 #include <errno.h>
174 #include <netdb.h>
175 #include <stdio.h>
176 #include <stdlib.h>
177 #include <string.h>
178 \&
179 #define MAX_BUF 10000
180 \&
181 int
182 main(int argc, char *argv[])
183 {
184 int size, erange_cnt, port, s;
185 struct servent result_buf;
186 struct servent *result;
187 char buf[MAX_BUF];
188 char *protop;
189 \&
190 if (argc < 3) {
191 printf("Usage: %s port\-num proto\-name [size]\[rs]n", argv[0]);
192 exit(EXIT_FAILURE);
193 }
194 \&
195 port = htons(atoi(argv[1]));
196 protop = (strcmp(argv[2], "null") == 0 ||
197 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
198 \&
199 size = 1024;
200 if (argc > 3)
201 size = atoi(argv[3]);
202 \&
203 if (size > MAX_BUF) {
204 printf("Exceeded buffer limit (%d)\[rs]n", MAX_BUF);
205 exit(EXIT_FAILURE);
206 }
207 \&
208 erange_cnt = 0;
209 do {
210 s = getservbyport_r(port, protop, &result_buf,
211 buf, size, &result);
212 if (s == ERANGE) {
213 if (erange_cnt == 0)
214 printf("ERANGE! Retrying with larger buffer\[rs]n");
215 erange_cnt++;
216 \&
217 /* Increment a byte at a time so we can see exactly
218 what size buffer was required. */
219 \&
220 size++;
221 \&
222 if (size > MAX_BUF) {
223 printf("Exceeded buffer limit (%d)\[rs]n", MAX_BUF);
224 exit(EXIT_FAILURE);
225 }
226 }
227 } while (s == ERANGE);
228 \&
229 printf("getservbyport_r() returned: %s (size=%d)\[rs]n",
230 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
231 strerror(s), size);
232 \&
233 if (s != 0 || result == NULL) {
234 printf("Call failed/record not found\[rs]n");
235 exit(EXIT_FAILURE);
236 }
237 \&
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++)
242 printf("%s ", *p);
243 printf("\[rs]n");
244 \&
245 exit(EXIT_SUCCESS);
246 }
247 .EE
248 .\" SRC END
249 .SH SEE ALSO
250 .BR getservent (3),
251 .BR services (5)