]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getservent_r.3
twalk_r.3: New link to twalk(3) page
[thirdparty/man-pages.git] / man3 / getservent_r.3
CommitLineData
1280fbc6
MK
1.\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2.\" <mtk.manpages@gmail.com>
3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
1280fbc6
MK
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.
4b72fb64 24.\" %%%LICENSE_END
1280fbc6 25.\"
9ba01802 26.TH GETSERVENT_R 3 2019-03-06 "GNU" "Linux Programmer's Manual"
1280fbc6
MK
27.SH NAME
28getservent_r, getservbyname_r, getservbyport_r \- get
29service entry (reentrant)
30.SH SYNOPSIS
31.nf
32.B #include <netdb.h>
68e4db0a 33.PP
1280fbc6
MK
34.BI "int getservent_r(struct servent *" result_buf ", char *" buf ,
35.BI " size_t " buflen ", struct servent **" result );
68e4db0a 36.PP
1280fbc6
MK
37.BI "int getservbyname_r(const char *" name ", const char *" proto ,
38.BI " struct servent *" result_buf ", char *" buf ,
39.BI " size_t " buflen ", struct servent **" result );
68e4db0a 40.PP
1280fbc6
MK
41.BI "int getservbyport_r(int " port ", const char *" proto ,
42.BI " struct servent *" result_buf ", char *" buf ,
43.BI " size_t " buflen ", struct servent **" result );
68e4db0a 44.PP
1280fbc6
MK
45.fi
46.in -4n
47Feature Test Macro Requirements for glibc (see
48.BR feature_test_macros (7)):
49.ad l
50.in
68e4db0a 51.PP
1280fbc6
MK
52.BR getservent_r (),
53.BR getservbyname_r (),
54.BR getservbyport_r ():
51c612fb
MK
55 Since glibc 2.19:
56 _DEFAULT_SOURCE
57 Glibc 2.19 and earlier:
58 _BSD_SOURCE || _SVID_SOURCE
1280fbc6
MK
59.ad b
60.SH DESCRIPTION
61The
62.BR getservent_r (),
63.BR getservbyname_r (),
64and
65.BR getservbyport_r ()
66functions are the reentrant equivalents of, respectively,
67.BR getservent (3),
68.BR getservbyname (3),
69and
70.BR getservbyport (3).
71They differ in the way that the
72.I servent
73structure is returned,
74and in the function calling signature and return value.
75This manual page describes just the differences from
54d75d6c 76the nonreentrant functions.
847e0d88 77.PP
1280fbc6
MK
78Instead of returning a pointer to a statically allocated
79.I servent
80structure as the function result,
81these functions copy the structure into the location pointed to by
82.IR result_buf .
847e0d88 83.PP
1280fbc6
MK
84The
85.I buf
86array is used to store the string fields pointed to by the returned
87.I servent
88structure.
54d75d6c 89(The nonreentrant functions allocate these strings in static storage.)
1280fbc6
MK
90The size of this array is specified in
91.IR buflen .
92If
93.I buf
94is too small, the call fails with the error
95.BR ERANGE ,
96and the caller must try again with a larger buffer.
97(A buffer of length 1024 bytes should be sufficient for most applications.)
98.\" I can find no information on the required/recommended buffer size;
54d75d6c 99.\" the nonreentrant functions use a 1024 byte buffer -- mtk.
847e0d88 100.PP
1280fbc6
MK
101If the function call successfully obtains a service record, then
102.I *result
103is set pointing to
104.IR result_buf ;
105otherwise,
106.I *result
107is set to NULL.
47297adb 108.SH RETURN VALUE
1280fbc6 109On success, these functions return 0.
535f0df5 110On error, they return one of the positive error numbers listed in errors.
847e0d88 111.PP
1280fbc6
MK
112On error, record not found
113.RB ( getservbyname_r (),
114.BR getservbyport_r ()),
115or end of input
116.RB ( getservent_r ())
117.I result
118is set to NULL.
119.SH ERRORS
120.TP
121.B ENOENT
122.RB ( getservent_r ())
123No more records in database.
124.TP
125.B ERANGE
126.I buf
127is too small.
128Try again with a larger buffer
129(and increased
130.IR buflen ).
67e569aa
ZL
131.SH ATTRIBUTES
132For an explanation of the terms used in this section, see
133.BR attributes (7).
74714ea8 134.ad l
67e569aa
ZL
135.TS
136allbox;
137lbw18 lb lb
138l l l.
139Interface Attribute Value
140T{
141.BR getservent_r (),
142.BR getservbyname_r (),
143.BR getservbyport_r ()
144T} Thread safety MT-Safe locale
145.TE
74714ea8 146.ad
47297adb 147.SH CONFORMING TO
1280fbc6
MK
148These functions are GNU extensions.
149Functions with similar names exist on some other systems,
150though typically with different calling signatures.
151.SH EXAMPLE
152The program below uses
153.BR getservbyport_r ()
154to retrieve the service record for the port and protocol named
155in its first command-line argument.
156If a third (integer) command-line argument is supplied,
157it is used as the initial value for
158.IR buflen ;
159if
160.BR getservbyport_r ()
161fails with the error
162.BR ERANGE ,
163the program retries with larger buffer sizes.
164The following shell session shows a couple of sample runs:
e646a1ba 165.PP
1280fbc6 166.in +4n
e646a1ba 167.EX
b43a3b30 168.RB "$" " ./a.out 7 tcp 1"
1280fbc6
MK
169ERANGE! Retrying with larger buffer
170getservbyport_r() returned: 0 (success) (buflen=87)
171s_name=echo; s_proto=tcp; s_port=7; aliases=
b43a3b30 172.RB "$" " ./a.out 77777 tcp"
1280fbc6
MK
173getservbyport_r() returned: 0 (success) (buflen=1024)
174Call failed/record not found
b8302363 175.EE
1280fbc6 176.in
9c330504 177.SS Program source
d84d0300 178\&
e7d0bb47 179.EX
1280fbc6
MK
180#define _GNU_SOURCE
181#include <ctype.h>
182#include <netdb.h>
183#include <stdlib.h>
184#include <stdio.h>
185#include <errno.h>
186#include <string.h>
187
188#define MAX_BUF 10000
189
190int
191main(int argc, char *argv[])
192{
193 int buflen, erange_cnt, port, s;
194 struct servent result_buf;
195 struct servent *result;
196 char buf[MAX_BUF];
197 char *protop;
198 char **p;
199
200 if (argc < 3) {
d1a71985 201 printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
1280fbc6
MK
202 exit(EXIT_FAILURE);
203 }
204
205 port = htons(atoi(argv[1]));
206 protop = (strcmp(argv[2], "null") == 0 ||
5a6194a4 207 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
1280fbc6
MK
208
209 buflen = 1024;
210 if (argc > 3)
211 buflen = atoi(argv[3]);
212
213 if (buflen > MAX_BUF) {
d1a71985 214 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
1280fbc6
MK
215 exit(EXIT_FAILURE);
216 }
217
218 erange_cnt = 0;
219 do {
220 s = getservbyport_r(port, protop, &result_buf,
221 buf, buflen, &result);
222 if (s == ERANGE) {
223 if (erange_cnt == 0)
d1a71985 224 printf("ERANGE! Retrying with larger buffer\en");
1280fbc6
MK
225 erange_cnt++;
226
227 /* Increment a byte at a time so we can see exactly
228 what size buffer was required */
229
230 buflen++;
231
232 if (buflen > MAX_BUF) {
d1a71985 233 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
1280fbc6
MK
234 exit(EXIT_FAILURE);
235 }
236 }
237 } while (s == ERANGE);
238
d1a71985 239 printf("getservbyport_r() returned: %s (buflen=%d)\en",
1280fbc6
MK
240 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
241 strerror(s), buflen);
242
243 if (s != 0 || result == NULL) {
d1a71985 244 printf("Call failed/record not found\en");
1280fbc6
MK
245 exit(EXIT_FAILURE);
246 }
247
248 printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
249 result_buf.s_name, result_buf.s_proto,
250 ntohs(result_buf.s_port));
251 for (p = result_buf.s_aliases; *p != NULL; p++)
252 printf("%s ", *p);
d1a71985 253 printf("\en");
1280fbc6
MK
254
255 exit(EXIT_SUCCESS);
256}
e7d0bb47 257.EE
47297adb 258.SH SEE ALSO
1280fbc6 259.BR getservent (3),
1280fbc6 260.BR services (5)