1GETSERVENT_R(3)            Linux Programmer's Manual           GETSERVENT_R(3)
2
3
4

NAME

6       getservent_r,  getservbyname_r,  getservbyport_r  -  get  service entry
7       (reentrant)
8

SYNOPSIS

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

DESCRIPTION

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 re‐
44       turned servent structure.  (The nonreentrant functions  allocate  these
45       strings in static storage.)  The size of this array is specified in bu‐
46       flen.  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  *re‐
51       sult is set pointing to result_buf; otherwise, *result is set to NULL.
52

RETURN VALUE

54       On success, these functions return 0.  On error, they return one of the
55       positive error numbers listed in errors.
56
57       On error, record not found (getservbyname_r(),  getservbyport_r()),  or
58       end of input (getservent_r()) result is set to NULL.
59

ERRORS

61       ENOENT (getservent_r()) No more records in database.
62
63       ERANGE buf is too small.  Try again with a larger buffer (and increased
64              buflen).
65

ATTRIBUTES

67       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
68       tributes(7).
69
70       ┌───────────────────┬───────────────┬────────────────┐
71Interface          Attribute     Value          
72       ├───────────────────┼───────────────┼────────────────┤
73getservent_r(),    │ Thread safety │ MT-Safe locale │
74getservbyname_r(), │               │                │
75getservbyport_r()  │               │                │
76       └───────────────────┴───────────────┴────────────────┘

CONFORMING TO

78       These functions are GNU extensions.  Functions with similar names exist
79       on some other systems, though typically with different  calling  signa‐
80       tures.
81

EXAMPLES

83       The program below uses getservbyport_r() to retrieve the service record
84       for the port and protocol named in its first command-line argument.  If
85       a  third (integer) command-line argument is supplied, it is used as the
86       initial value for buflen; if getservbyport_r()  fails  with  the  error
87       ERANGE,  the  program  retries with larger buffer sizes.  The following
88       shell session shows a couple of sample runs:
89
90           $ ./a.out 7 tcp 1
91           ERANGE! Retrying with larger buffer
92           getservbyport_r() returned: 0 (success)  (buflen=87)
93           s_name=echo; s_proto=tcp; s_port=7; aliases=
94           $ ./a.out 77777 tcp
95           getservbyport_r() returned: 0 (success)  (buflen=1024)
96           Call failed/record not found
97
98   Program source
99
100       #define _GNU_SOURCE
101       #include <ctype.h>
102       #include <netdb.h>
103       #include <stdlib.h>
104       #include <stdio.h>
105       #include <errno.h>
106       #include <string.h>
107
108       #define MAX_BUF 10000
109
110       int
111       main(int argc, char *argv[])
112       {
113           int buflen, erange_cnt, port, s;
114           struct servent result_buf;
115           struct servent *result;
116           char buf[MAX_BUF];
117           char *protop;
118
119           if (argc < 3) {
120               printf("Usage: %s port-num proto-name [buflen]\n", argv[0]);
121               exit(EXIT_FAILURE);
122           }
123
124           port = htons(atoi(argv[1]));
125           protop = (strcmp(argv[2], "null") == 0 ||
126                     strcmp(argv[2], "NULL") == 0) ?  NULL : argv[2];
127
128           buflen = 1024;
129           if (argc > 3)
130               buflen = atoi(argv[3]);
131
132           if (buflen > MAX_BUF) {
133               printf("Exceeded buffer limit (%d)\n", MAX_BUF);
134               exit(EXIT_FAILURE);
135           }
136
137           erange_cnt = 0;
138           do {
139               s = getservbyport_r(port, protop, &result_buf,
140                            buf, buflen, &result);
141               if (s == ERANGE) {
142                   if (erange_cnt == 0)
143                       printf("ERANGE! Retrying with larger buffer\n");
144                   erange_cnt++;
145
146                   /* Increment a byte at a time so we can see exactly
147                      what size buffer was required */
148
149                   buflen++;
150
151                   if (buflen > MAX_BUF) {
152                       printf("Exceeded buffer limit (%d)\n", MAX_BUF);
153                       exit(EXIT_FAILURE);
154                   }
155               }
156           } while (s == ERANGE);
157
158           printf("getservbyport_r() returned: %s  (buflen=%d)\n",
159                   (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
160                   strerror(s), buflen);
161
162           if (s != 0 || result == NULL) {
163               printf("Call failed/record not found\n");
164               exit(EXIT_FAILURE);
165           }
166
167           printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
168                       result_buf.s_name, result_buf.s_proto,
169                       ntohs(result_buf.s_port));
170           for (char **p = result_buf.s_aliases; *p != NULL; p++)
171               printf("%s ", *p);
172           printf("\n");
173
174           exit(EXIT_SUCCESS);
175       }
176

SEE ALSO

178       getservent(3), services(5)
179

COLOPHON

181       This page is part of release 5.10 of the Linux  man-pages  project.   A
182       description  of  the project, information about reporting bugs, and the
183       latest    version    of    this    page,    can     be     found     at
184       https://www.kernel.org/doc/man-pages/.
185
186
187
188GNU                               2020-11-01                   GETSERVENT_R(3)
Impressum