1GETSERVENT_R(3) Linux Programmer's Manual GETSERVENT_R(3)
2
3
4
6 getservent_r, getservbyname_r, getservbyport_r - get service entry
7 (reentrant)
8
10 #include <netdb.h>
11
12 int getservent_r(struct servent *result_buf, char *buf,
13 size_t buflen, struct servent **result);
14
15 int getservbyname_r(const char *name, const char *proto,
16 struct servent *result_buf, char *buf,
17 size_t buflen, struct servent **result);
18
19 int getservbyport_r(int port, const char *proto,
20 struct servent *result_buf, char *buf,
21 size_t buflen, struct servent **result);
22
23 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
24
25 getservent_r(), getservbyname_r(), getservbyport_r():
26 Since glibc 2.19:
27 _DEFAULT_SOURCE
28 Glibc 2.19 and earlier:
29 _BSD_SOURCE || _SVID_SOURCE
30
32 The getservent_r(), getservbyname_r(), and getservbyport_r() functions
33 are the reentrant equivalents of, respectively, getservent(3), get‐
34 servbyname(3), and getservbyport(3). They differ in the way that the
35 servent structure is returned, and in the function calling signature
36 and return value. This manual page describes just the differences from
37 the nonreentrant functions.
38
39 Instead of returning a pointer to a statically allocated servent struc‐
40 ture as the function result, these functions copy the structure into
41 the location pointed to by result_buf.
42
43 The buf array is used to store the string fields pointed to by the
44 returned servent structure. (The nonreentrant functions allocate these
45 strings in static storage.) The size of this array is specified in
46 buflen. If buf is too small, the call fails with the error ERANGE, and
47 the caller must try again with a larger buffer. (A buffer of length
48 1024 bytes should be sufficient for most applications.)
49
50 If the function call successfully obtains a service record, then
51 *result is set pointing to result_buf; otherwise, *result is set to
52 NULL.
53
55 On success, these functions return 0. On error, they return one of the
56 positive error numbers listed in errors.
57
58 On error, record not found (getservbyname_r(), getservbyport_r()), or
59 end of input (getservent_r()) result is set to NULL.
60
62 ENOENT (getservent_r()) No more records in database.
63
64 ERANGE buf is too small. Try again with a larger buffer (and increased
65 buflen).
66
68 For an explanation of the terms used in this section, see
69 attributes(7).
70
71 ┌───────────────────┬───────────────┬────────────────┐
72 │Interface │ Attribute │ Value │
73 ├───────────────────┼───────────────┼────────────────┤
74 │getservent_r(), │ Thread safety │ MT-Safe locale │
75 │getservbyname_r(), │ │ │
76 │getservbyport_r() │ │ │
77 └───────────────────┴───────────────┴────────────────┘
79 These functions are GNU extensions. Functions with similar names exist
80 on some other systems, though typically with different calling signa‐
81 tures.
82
84 The program below uses getservbyport_r() to retrieve the service record
85 for the port and protocol named in its first command-line argument. If
86 a third (integer) command-line argument is supplied, it is used as the
87 initial value for buflen; if getservbyport_r() fails with the error
88 ERANGE, the program retries with larger buffer sizes. The following
89 shell session shows a couple of sample runs:
90
91 $ ./a.out 7 tcp 1
92 ERANGE! Retrying with larger buffer
93 getservbyport_r() returned: 0 (success) (buflen=87)
94 s_name=echo; s_proto=tcp; s_port=7; aliases=
95 $ ./a.out 77777 tcp
96 getservbyport_r() returned: 0 (success) (buflen=1024)
97 Call failed/record not found
98
99 Program source
100
101 #define _GNU_SOURCE
102 #include <ctype.h>
103 #include <netdb.h>
104 #include <stdlib.h>
105 #include <stdio.h>
106 #include <errno.h>
107 #include <string.h>
108
109 #define MAX_BUF 10000
110
111 int
112 main(int argc, char *argv[])
113 {
114 int buflen, erange_cnt, port, s;
115 struct servent result_buf;
116 struct servent *result;
117 char buf[MAX_BUF];
118 char *protop;
119 char **p;
120
121 if (argc < 3) {
122 printf("Usage: %s port-num proto-name [buflen]\n", argv[0]);
123 exit(EXIT_FAILURE);
124 }
125
126 port = htons(atoi(argv[1]));
127 protop = (strcmp(argv[2], "null") == 0 ||
128 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
129
130 buflen = 1024;
131 if (argc > 3)
132 buflen = atoi(argv[3]);
133
134 if (buflen > MAX_BUF) {
135 printf("Exceeded buffer limit (%d)\n", MAX_BUF);
136 exit(EXIT_FAILURE);
137 }
138
139 erange_cnt = 0;
140 do {
141 s = getservbyport_r(port, protop, &result_buf,
142 buf, buflen, &result);
143 if (s == ERANGE) {
144 if (erange_cnt == 0)
145 printf("ERANGE! Retrying with larger buffer\n");
146 erange_cnt++;
147
148 /* Increment a byte at a time so we can see exactly
149 what size buffer was required */
150
151 buflen++;
152
153 if (buflen > MAX_BUF) {
154 printf("Exceeded buffer limit (%d)\n", MAX_BUF);
155 exit(EXIT_FAILURE);
156 }
157 }
158 } while (s == ERANGE);
159
160 printf("getservbyport_r() returned: %s (buflen=%d)\n",
161 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
162 strerror(s), buflen);
163
164 if (s != 0 || result == NULL) {
165 printf("Call failed/record not found\n");
166 exit(EXIT_FAILURE);
167 }
168
169 printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
170 result_buf.s_name, result_buf.s_proto,
171 ntohs(result_buf.s_port));
172 for (p = result_buf.s_aliases; *p != NULL; p++)
173 printf("%s ", *p);
174 printf("\n");
175
176 exit(EXIT_SUCCESS);
177 }
178
180 getservent(3), services(5)
181
183 This page is part of release 5.07 of the Linux man-pages project. A
184 description of the project, information about reporting bugs, and the
185 latest version of this page, can be found at
186 https://www.kernel.org/doc/man-pages/.
187
188
189
190GNU 2020-06-09 GETSERVENT_R(3)