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