mount_setattr.2: Minor tweaks to Christian's patch
[man-pages.git] / man3 / getservent_r.3
blob66fbfaafca74da97050604a1994c74f879c9c6df
1 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH GETSERVENT_R 3  2021-03-22 "GNU" "Linux Programmer's Manual"
27 .SH NAME
28 getservent_r, getservbyname_r, getservbyport_r \- get
29 service entry (reentrant)
30 .SH SYNOPSIS
31 .nf
32 .B #include <netdb.h>
33 .PP
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 );
47 .PP
48 .fi
49 .RS -4
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
52 .RE
53 .PP
54 .BR getservent_r (),
55 .BR getservbyname_r (),
56 .BR getservbyport_r ():
57 .nf
58     Since glibc 2.19:
59         _DEFAULT_SOURCE
60     Glibc 2.19 and earlier:
61         _BSD_SOURCE || _SVID_SOURCE
62 .fi
63 .SH DESCRIPTION
64 The
65 .BR getservent_r (),
66 .BR getservbyname_r (),
67 and
68 .BR getservbyport_r ()
69 functions are the reentrant equivalents of, respectively,
70 .BR getservent (3),
71 .BR getservbyname (3),
72 and
73 .BR getservbyport (3).
74 They differ in the way that the
75 .I servent
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.
80 .PP
81 Instead of returning a pointer to a statically allocated
82 .I servent
83 structure as the function result,
84 these functions copy the structure into the location pointed to by
85 .IR result_buf .
86 .PP
87 The
88 .I buf
89 array is used to store the string fields pointed to by the returned
90 .I servent
91 structure.
92 (The nonreentrant functions allocate these strings in static storage.)
93 The size of this array is specified in
94 .IR buflen .
96 .I buf
97 is too small, the call fails with the error
98 .BR ERANGE ,
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
105 .I *result
106 is set pointing to
107 .IR result_buf ;
108 otherwise,
109 .I *result
110 is set to NULL.
111 .SH RETURN VALUE
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 ()),
118 or end of input
119 .RB ( getservent_r ())
120 .I result
121 is set to NULL.
122 .SH ERRORS
124 .B ENOENT
125 .RB ( getservent_r ())
126 No more records in database.
128 .B ERANGE
129 .I buf
130 is too small.
131 Try again with a larger buffer
132 (and increased
133 .IR buflen ).
134 .SH ATTRIBUTES
135 For an explanation of the terms used in this section, see
136 .BR attributes (7).
137 .ad l
140 allbox;
141 lbx lb lb
142 l l l.
143 Interface       Attribute       Value
145 .BR getservent_r (),
146 .BR getservbyname_r (),
147 .BR getservbyport_r ()
148 T}      Thread safety   MT-Safe locale
152 .sp 1
153 .SH CONFORMING TO
154 These functions are GNU extensions.
155 Functions with similar names exist on some other systems,
156 though typically with different calling signatures.
157 .SH EXAMPLES
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
164 .IR buflen ;
166 .BR getservbyport_r ()
167 fails with the error
168 .BR ERANGE ,
169 the program retries with larger buffer sizes.
170 The following shell session shows a couple of sample runs:
172 .in +4n
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
183 .SS Program source
186 #define _GNU_SOURCE
187 #include <ctype.h>
188 #include <netdb.h>
189 #include <stdlib.h>
190 #include <stdio.h>
191 #include <errno.h>
192 #include <string.h>
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;
202     char buf[MAX_BUF];
203     char *protop;
205     if (argc < 3) {
206         printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
207         exit(EXIT_FAILURE);
208     }
210     port = htons(atoi(argv[1]));
211     protop = (strcmp(argv[2], "null") == 0 ||
212               strcmp(argv[2], "NULL") == 0) ?  NULL : argv[2];
214     buflen = 1024;
215     if (argc > 3)
216         buflen = atoi(argv[3]);
218     if (buflen > MAX_BUF) {
219         printf("Exceeded buffer limit (%d)\en", MAX_BUF);
220         exit(EXIT_FAILURE);
221     }
223     erange_cnt = 0;
224     do {
225         s = getservbyport_r(port, protop, &result_buf,
226                      buf, buflen, &result);
227         if (s == ERANGE) {
228             if (erange_cnt == 0)
229                 printf("ERANGE! Retrying with larger buffer\en");
230             erange_cnt++;
232             /* Increment a byte at a time so we can see exactly
233                what size buffer was required. */
235             buflen++;
237             if (buflen > MAX_BUF) {
238                 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
239                 exit(EXIT_FAILURE);
240             }
241         }
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");
250         exit(EXIT_FAILURE);
251     }
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++)
257         printf("%s ", *p);
258     printf("\en");
260     exit(EXIT_SUCCESS);
263 .SH SEE ALSO
264 .BR getservent (3),
265 .BR services (5)