]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getservent_r.3
getitimer.2, termios.3: Gloabl fix: s/non-canonical/noncanonical/
[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.\"
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
12.\"
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
20.\"
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\"
24.TH GETSERVENT_R 3 2008-08-19 "GNU" "Linux Programmer's Manual"
25.SH NAME
26getservent_r, getservbyname_r, getservbyport_r \- get
27service entry (reentrant)
28.SH SYNOPSIS
29.nf
30.B #include <netdb.h>
31.sp
32.BI "int getservent_r(struct servent *" result_buf ", char *" buf ,
33.BI " size_t " buflen ", struct servent **" result );
34.sp
35.BI "int getservbyname_r(const char *" name ", const char *" proto ,
36.BI " struct servent *" result_buf ", char *" buf ,
37.BI " size_t " buflen ", struct servent **" result );
38.sp
39.BI "int getservbyport_r(int " port ", const char *" proto ,
40.BI " struct servent *" result_buf ", char *" buf ,
41.BI " size_t " buflen ", struct servent **" result );
42.sp
43.fi
44.in -4n
45Feature Test Macro Requirements for glibc (see
46.BR feature_test_macros (7)):
47.ad l
48.in
49.sp
50.BR getservent_r (),
51.BR getservbyname_r (),
52.BR getservbyport_r ():
53_BSD_SOURCE || _SVID_SOURCE
54.ad b
55.SH DESCRIPTION
56The
57.BR getservent_r (),
58.BR getservbyname_r (),
59and
60.BR getservbyport_r ()
61functions are the reentrant equivalents of, respectively,
62.BR getservent (3),
63.BR getservbyname (3),
64and
65.BR getservbyport (3).
66They differ in the way that the
67.I servent
68structure is returned,
69and in the function calling signature and return value.
70This manual page describes just the differences from
71the non-reentrant functions.
72
73Instead of returning a pointer to a statically allocated
74.I servent
75structure as the function result,
76these functions copy the structure into the location pointed to by
77.IR result_buf .
78
79The
80.I buf
81array is used to store the string fields pointed to by the returned
82.I servent
83structure.
84(The non-reentrant functions allocate these strings in static storage.)
85The size of this array is specified in
86.IR buflen .
87If
88.I buf
89is too small, the call fails with the error
90.BR ERANGE ,
91and the caller must try again with a larger buffer.
92(A buffer of length 1024 bytes should be sufficient for most applications.)
93.\" I can find no information on the required/recommended buffer size;
94.\" the non-reentrant functions use a 1024 byte buffer -- mtk.
95
96If the function call successfully obtains a service record, then
97.I *result
98is set pointing to
99.IR result_buf ;
100otherwise,
101.I *result
102is set to NULL.
103.SH "RETURN VALUE"
104On success, these functions return 0.
105On error, a positive error number is returned.
106
107On error, record not found
108.RB ( getservbyname_r (),
109.BR getservbyport_r ()),
110or end of input
111.RB ( getservent_r ())
112.I result
113is set to NULL.
114.SH ERRORS
115.TP
116.B ENOENT
117.RB ( getservent_r ())
118No more records in database.
119.TP
120.B ERANGE
121.I buf
122is too small.
123Try again with a larger buffer
124(and increased
125.IR buflen ).
126.SH "CONFORMING TO"
127These functions are GNU extensions.
128Functions with similar names exist on some other systems,
129though typically with different calling signatures.
130.SH EXAMPLE
131The program below uses
132.BR getservbyport_r ()
133to retrieve the service record for the port and protocol named
134in its first command-line argument.
135If a third (integer) command-line argument is supplied,
136it is used as the initial value for
137.IR buflen ;
138if
139.BR getservbyport_r ()
140fails with the error
141.BR ERANGE ,
142the program retries with larger buffer sizes.
143The following shell session shows a couple of sample runs:
144.in +4n
145.nf
146
b43a3b30 147.RB "$" " ./a.out 7 tcp 1"
1280fbc6
MK
148ERANGE! Retrying with larger buffer
149getservbyport_r() returned: 0 (success) (buflen=87)
150s_name=echo; s_proto=tcp; s_port=7; aliases=
b43a3b30 151.RB "$" " ./a.out 77777 tcp"
1280fbc6
MK
152getservbyport_r() returned: 0 (success) (buflen=1024)
153Call failed/record not found
1280fbc6
MK
154.fi
155.in
9c330504 156.SS Program source
d84d0300 157\&
1280fbc6
MK
158.nf
159#define _GNU_SOURCE
160#include <ctype.h>
161#include <netdb.h>
162#include <stdlib.h>
163#include <stdio.h>
164#include <errno.h>
165#include <string.h>
166
167#define MAX_BUF 10000
168
169int
170main(int argc, char *argv[])
171{
172 int buflen, erange_cnt, port, s;
173 struct servent result_buf;
174 struct servent *result;
175 char buf[MAX_BUF];
176 char *protop;
177 char **p;
178
179 if (argc < 3) {
180 printf("Usage: %s port\-num proto-name [buflen]\\n", argv[0]);
181 exit(EXIT_FAILURE);
182 }
183
184 port = htons(atoi(argv[1]));
185 protop = (strcmp(argv[2], "null") == 0 ||
186 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
187
188 buflen = 1024;
189 if (argc > 3)
190 buflen = atoi(argv[3]);
191
192 if (buflen > MAX_BUF) {
193 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
194 exit(EXIT_FAILURE);
195 }
196
197 erange_cnt = 0;
198 do {
199 s = getservbyport_r(port, protop, &result_buf,
200 buf, buflen, &result);
201 if (s == ERANGE) {
202 if (erange_cnt == 0)
203 printf("ERANGE! Retrying with larger buffer\\n");
204 erange_cnt++;
205
206 /* Increment a byte at a time so we can see exactly
207 what size buffer was required */
208
209 buflen++;
210
211 if (buflen > MAX_BUF) {
212 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
213 exit(EXIT_FAILURE);
214 }
215 }
216 } while (s == ERANGE);
217
218 printf("getservbyport_r() returned: %s (buflen=%d)\\n",
219 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
220 strerror(s), buflen);
221
222 if (s != 0 || result == NULL) {
223 printf("Call failed/record not found\\n");
224 exit(EXIT_FAILURE);
225 }
226
227 printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
228 result_buf.s_name, result_buf.s_proto,
229 ntohs(result_buf.s_port));
230 for (p = result_buf.s_aliases; *p != NULL; p++)
231 printf("%s ", *p);
232 printf("\\n");
233
234 exit(EXIT_SUCCESS);
235}
236.fi
237.SH "SEE ALSO"
238.BR getservent (3),
1280fbc6 239.BR services (5)