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