1 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH GETSERVENT_R 3 2008-08-19 "GNU" "Linux Programmer's Manual"
26 getservent_r, getservbyname_r, getservbyport_r \- get
27 service entry (reentrant)
32 .BI "int getservent_r(struct servent *" result_buf ", char *" buf ,
33 .BI " size_t " buflen ", struct servent **" result );
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 );
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 );
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
51 .BR getservbyname_r (),
52 .BR getservbyport_r ():
53 _BSD_SOURCE || _SVID_SOURCE
58 .BR getservbyname_r (),
60 .BR getservbyport_r ()
61 functions are the reentrant equivalents of, respectively,
63 .BR getservbyname (3),
65 .BR getservbyport (3).
66 They differ in the way that the
68 structure is returned,
69 and in the function calling signature and return value.
70 This manual page describes just the differences from
71 the nonreentrant functions.
73 Instead of returning a pointer to a statically allocated
75 structure as the function result,
76 these functions copy the structure into the location pointed to by
81 array is used to store the string fields pointed to by the returned
84 (The nonreentrant functions allocate these strings in static storage.)
85 The size of this array is specified in
89 is too small, the call fails with the error
91 and 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 nonreentrant functions use a 1024 byte buffer -- mtk.
96 If the function call successfully obtains a service record, then
104 On success, these functions return 0.
105 On error, a positive error number is returned.
107 On error, record not found
108 .RB ( getservbyname_r (),
109 .BR getservbyport_r ()),
111 .RB ( getservent_r ())
117 .RB ( getservent_r ())
118 No more records in database.
123 Try again with a larger buffer
127 These functions are GNU extensions.
128 Functions with similar names exist on some other systems,
129 though typically with different calling signatures.
131 The program below uses
132 .BR getservbyport_r ()
133 to retrieve the service record for the port and protocol named
134 in its first command-line argument.
135 If a third (integer) command-line argument is supplied,
136 it is used as the initial value for
139 .BR getservbyport_r ()
142 the program retries with larger buffer sizes.
143 The following shell session shows a couple of sample runs:
147 .RB "$" " ./a.out 7 tcp 1"
148 ERANGE! Retrying with larger buffer
149 getservbyport_r() returned: 0 (success) (buflen=87)
150 s_name=echo; s_proto=tcp; s_port=7; aliases=
151 .RB "$" " ./a.out 77777 tcp"
152 getservbyport_r() returned: 0 (success) (buflen=1024)
153 Call failed/record not found
167 #define MAX_BUF 10000
170 main(int argc, char *argv[])
172 int buflen, erange_cnt, port, s;
173 struct servent result_buf;
174 struct servent *result;
180 printf("Usage: %s port\-num proto-name [buflen]\\n", argv[0]);
184 port = htons(atoi(argv[1]));
185 protop = (strcmp(argv[2], "null") == 0 ||
186 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
190 buflen = atoi(argv[3]);
192 if (buflen > MAX_BUF) {
193 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
199 s = getservbyport_r(port, protop, &result_buf,
200 buf, buflen, &result);
203 printf("ERANGE! Retrying with larger buffer\\n");
206 /* Increment a byte at a time so we can see exactly
207 what size buffer was required */
211 if (buflen > MAX_BUF) {
212 printf("Exceeded buffer limit (%d)\\n", MAX_BUF);
216 } while (s == ERANGE);
218 printf("getservbyport_r() returned: %s (buflen=%d)\\n",
219 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
220 strerror(s), buflen);
222 if (s != 0 || result == NULL) {
223 printf("Call failed/record not found\\n");
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++)