OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / man3 / getservent_r.3
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  2010-09-10 "GNU" "Linux Programmer's Manual"
25 .SH NAME
26 getservent_r, getservbyname_r, getservbyport_r \- get
27 service 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
45 Feature 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 .RS 4
54 _BSD_SOURCE || _SVID_SOURCE
55 .RE
56 .ad b
57 .SH DESCRIPTION
58 The
59 .BR getservent_r (),
60 .BR getservbyname_r (),
61 and
62 .BR getservbyport_r ()
63 functions are the reentrant equivalents of, respectively,
64 .BR getservent (3),
65 .BR getservbyname (3),
66 and
67 .BR getservbyport (3).
68 They differ in the way that the
69 .I servent
70 structure is returned,
71 and in the function calling signature and return value.
72 This manual page describes just the differences from
73 the nonreentrant functions.
74
75 Instead of returning a pointer to a statically allocated
76 .I servent
77 structure as the function result,
78 these functions copy the structure into the location pointed to by
79 .IR result_buf .
80
81 The
82 .I buf
83 array is used to store the string fields pointed to by the returned
84 .I servent
85 structure.
86 (The nonreentrant functions allocate these strings in static storage.)
87 The size of this array is specified in
88 .IR buflen .
89 If
90 .I buf
91 is too small, the call fails with the error
92 .BR ERANGE ,
93 and the caller must try again with a larger buffer.
94 (A buffer of length 1024 bytes should be sufficient for most applications.)
95 .\" I can find no information on the required/recommended buffer size;
96 .\" the nonreentrant functions use a 1024 byte buffer -- mtk.
97
98 If the function call successfully obtains a service record, then
99 .I *result
100 is set pointing to
101 .IR result_buf ;
102 otherwise,
103 .I *result
104 is set to NULL.
105 .SH "RETURN VALUE"
106 On success, these functions return 0.
107 On error, they return one of the positive error numbers listed in errors.
108
109 On error, record not found
110 .RB ( getservbyname_r (),
111 .BR getservbyport_r ()),
112 or end of input
113 .RB ( getservent_r ())
114 .I result
115 is set to NULL.
116 .SH ERRORS
117 .TP
118 .B ENOENT
119 .RB ( getservent_r ())
120 No more records in database.
121 .TP
122 .B ERANGE
123 .I buf
124 is too small.
125 Try again with a larger buffer
126 (and increased
127 .IR buflen ).
128 .SH "CONFORMING TO"
129 These functions are GNU extensions.
130 Functions with similar names exist on some other systems,
131 though typically with different calling signatures.
132 .SH EXAMPLE
133 The program below uses
134 .BR getservbyport_r ()
135 to retrieve the service record for the port and protocol named
136 in its first command-line argument.
137 If a third (integer) command-line argument is supplied,
138 it is used as the initial value for
139 .IR buflen ;
140 if
141 .BR getservbyport_r ()
142 fails with the error
143 .BR ERANGE ,
144 the program retries with larger buffer sizes.
145 The following shell session shows a couple of sample runs:
146 .in +4n
147 .nf
148
149 .RB "$" " ./a.out 7 tcp 1"
150 ERANGE! Retrying with larger buffer
151 getservbyport_r() returned: 0 (success)  (buflen=87)
152 s_name=echo; s_proto=tcp; s_port=7; aliases=
153 .RB "$" " ./a.out 77777 tcp"
154 getservbyport_r() returned: 0 (success)  (buflen=1024)
155 Call failed/record not found
156 .fi
157 .in
158 .SS Program source
159 \&
160 .nf
161 #define _GNU_SOURCE
162 #include <ctype.h>
163 #include <netdb.h>
164 #include <stdlib.h>
165 #include <stdio.h>
166 #include <errno.h>
167 #include <string.h>
168
169 #define MAX_BUF 10000
170
171 int
172 main(int argc, char *argv[])
173 {
174     int buflen, erange_cnt, port, s;
175     struct servent result_buf;
176     struct servent *result;
177     char buf[MAX_BUF];
178     char *protop;
179     char **p;
180
181     if (argc < 3) {
182         printf("Usage: %s port\-num proto-name [buflen]\\n", argv[0]);
183         exit(EXIT_FAILURE);
184     }
185
186     port = htons(atoi(argv[1]));
187     protop = (strcmp(argv[2], "null") == 0 ||
188               strcmp(argv[2], "NULL") == 0) ?  NULL : argv[2];
189
190     buflen = 1024;
191     if (argc > 3)
192         buflen = atoi(argv[3]);
193
194     if (buflen > MAX_BUF) {
195         printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
196         exit(EXIT_FAILURE);
197     }
198
199     erange_cnt = 0;
200     do {
201         s = getservbyport_r(port, protop, &result_buf,
202                      buf, buflen, &result);
203         if (s == ERANGE) {
204             if (erange_cnt == 0)
205                 printf("ERANGE! Retrying with larger buffer\\n");
206             erange_cnt++;
207
208             /* Increment a byte at a time so we can see exactly
209                what size buffer was required */
210
211             buflen++;
212
213             if (buflen > MAX_BUF) {
214                 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
215                 exit(EXIT_FAILURE);
216             }
217         }
218     } while (s == ERANGE);
219
220     printf("getservbyport_r() returned: %s  (buflen=%d)\\n",
221             (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
222             strerror(s), buflen);
223
224     if (s != 0 || result == NULL) {
225         printf("Call failed/record not found\\n");
226         exit(EXIT_FAILURE);
227     }
228
229     printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
230                 result_buf.s_name, result_buf.s_proto,
231                 ntohs(result_buf.s_port));
232     for (p = result_buf.s_aliases; *p != NULL; p++)
233         printf("%s ", *p);
234     printf("\\n");
235
236     exit(EXIT_SUCCESS);
237 }
238 .fi
239 .SH "SEE ALSO"
240 .BR getservent (3),
241 .BR services (5)