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 *restrict result_buf,
13                        char *restrict buf, size_t buflen,
14                        struct servent **restrict result);
15       int getservbyname_r(const char *restrict name,
16                        const char *restrict proto,
17                        struct servent *restrict result_buf,
18                        char *restrict buf, size_t buflen,
19                        struct servent **restrict result);
20       int getservbyport_r(int port,
21                        const char *restrict proto,
22                        struct servent *restrict result_buf,
23                        char *restrict buf, size_t buflen,
24                        struct servent **restrict result);
25
26   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
27
28       getservent_r(), getservbyname_r(), getservbyport_r():
29           Since glibc 2.19:
30               _DEFAULT_SOURCE
31           Glibc 2.19 and earlier:
32               _BSD_SOURCE || _SVID_SOURCE
33

DESCRIPTION

35       The getservent_r(), getservbyname_r(), and getservbyport_r()  functions
36       are  the  reentrant  equivalents  of, respectively, getservent(3), get‐
37       servbyname(3), and getservbyport(3).  They differ in the way  that  the
38       servent  structure  is  returned, and in the function calling signature
39       and return value.  This manual page describes just the differences from
40       the nonreentrant functions.
41
42       Instead of returning a pointer to a statically allocated servent struc‐
43       ture as the function result, these functions copy  the  structure  into
44       the location pointed to by result_buf.
45
46       The  buf array is used to store the string fields pointed to by the re‐
47       turned servent structure.  (The nonreentrant functions  allocate  these
48       strings in static storage.)  The size of this array is specified in bu‐
49       flen.  If buf is too small, the call fails with the error  ERANGE,  and
50       the  caller  must  try again with a larger buffer.  (A buffer of length
51       1024 bytes should be sufficient for most applications.)
52
53       If the function call successfully obtains a service record,  then  *re‐
54       sult is set pointing to result_buf; otherwise, *result is set to NULL.
55

RETURN VALUE

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

ERRORS

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

ATTRIBUTES

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

CONFORMING TO

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

EXAMPLES

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

SEE ALSO

181       getservent(3), services(5)
182

COLOPHON

184       This  page  is  part of release 5.12 of the Linux man-pages project.  A
185       description of the project, information about reporting bugs,  and  the
186       latest     version     of     this    page,    can    be    found    at
187       https://www.kernel.org/doc/man-pages/.
188
189
190
191GNU                               2021-03-22                   GETSERVENT_R(3)
Impressum