| 1 |
'\" t |
| 2 |
.\" Copyright 1993, David Metcalfe <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH getservent 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
getservent, getservbyname, getservbyport, setservent, endservent \- |
| 10 |
get service entry |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <netdb.h> |
| 17 |
.P |
| 18 |
.B struct servent *getservent(void); |
| 19 |
.P |
| 20 |
.BI "struct servent *getservbyname(const char *" name ", const char *" proto ); |
| 21 |
.BI "struct servent *getservbyport(int " port ", const char *" proto ); |
| 22 |
.P |
| 23 |
.BI "void setservent(int " stayopen ); |
| 24 |
.B void endservent(void); |
| 25 |
.fi |
| 26 |
.SH DESCRIPTION |
| 27 |
The |
| 28 |
.BR getservent () |
| 29 |
function reads the next entry from the services database (see |
| 30 |
.BR services (5)) |
| 31 |
and returns a |
| 32 |
.I servent |
| 33 |
structure containing |
| 34 |
the broken-out fields from the entry. |
| 35 |
A connection is opened to the database if necessary. |
| 36 |
.P |
| 37 |
The |
| 38 |
.BR getservbyname () |
| 39 |
function returns a |
| 40 |
.I servent |
| 41 |
structure |
| 42 |
for the entry from the database |
| 43 |
that matches the service |
| 44 |
.I name |
| 45 |
using protocol |
| 46 |
.IR proto . |
| 47 |
If |
| 48 |
.I proto |
| 49 |
is NULL, any protocol will be matched. |
| 50 |
A connection is opened to the database if necessary. |
| 51 |
.P |
| 52 |
The |
| 53 |
.BR getservbyport () |
| 54 |
function returns a |
| 55 |
.I servent |
| 56 |
structure |
| 57 |
for the entry from the database |
| 58 |
that matches the port |
| 59 |
.I port |
| 60 |
(given in network byte order) |
| 61 |
using protocol |
| 62 |
.IR proto . |
| 63 |
If |
| 64 |
.I proto |
| 65 |
is NULL, any protocol will be matched. |
| 66 |
A connection is opened to the database if necessary. |
| 67 |
.P |
| 68 |
The |
| 69 |
.BR setservent () |
| 70 |
function opens a connection to the database, |
| 71 |
and sets the next entry to the first entry. |
| 72 |
If |
| 73 |
.I stayopen |
| 74 |
is nonzero, |
| 75 |
then the connection to the database |
| 76 |
will not be closed between calls to one of the |
| 77 |
.BR getserv* () |
| 78 |
functions. |
| 79 |
.P |
| 80 |
The |
| 81 |
.BR endservent () |
| 82 |
function closes the connection to the database. |
| 83 |
.P |
| 84 |
The |
| 85 |
.I servent |
| 86 |
structure is defined in |
| 87 |
.I <netdb.h> |
| 88 |
as follows: |
| 89 |
.P |
| 90 |
.in +4n |
| 91 |
.EX |
| 92 |
struct servent { |
| 93 |
char *s_name; /* official service name */ |
| 94 |
char **s_aliases; /* alias list */ |
| 95 |
int s_port; /* port number */ |
| 96 |
char *s_proto; /* protocol to use */ |
| 97 |
} |
| 98 |
.EE |
| 99 |
.in |
| 100 |
.P |
| 101 |
The members of the |
| 102 |
.I servent |
| 103 |
structure are: |
| 104 |
.TP |
| 105 |
.I s_name |
| 106 |
The official name of the service. |
| 107 |
.TP |
| 108 |
.I s_aliases |
| 109 |
A NULL-terminated list of alternative names for the service. |
| 110 |
.TP |
| 111 |
.I s_port |
| 112 |
The port number for the service given in network byte order. |
| 113 |
.TP |
| 114 |
.I s_proto |
| 115 |
The name of the protocol to use with this service. |
| 116 |
.SH RETURN VALUE |
| 117 |
The |
| 118 |
.BR getservent (), |
| 119 |
.BR getservbyname (), |
| 120 |
and |
| 121 |
.BR getservbyport () |
| 122 |
functions return a pointer to a |
| 123 |
statically allocated |
| 124 |
.I servent |
| 125 |
structure, or NULL if an |
| 126 |
error occurs or the end of the file is reached. |
| 127 |
.SH FILES |
| 128 |
.TP |
| 129 |
.I /etc/services |
| 130 |
services database file |
| 131 |
.SH ATTRIBUTES |
| 132 |
For an explanation of the terms used in this section, see |
| 133 |
.BR attributes (7). |
| 134 |
.TS |
| 135 |
allbox; |
| 136 |
lb lb lbx |
| 137 |
l l l. |
| 138 |
Interface Attribute Value |
| 139 |
T{ |
| 140 |
.na |
| 141 |
.nh |
| 142 |
.BR getservent () |
| 143 |
T} Thread safety T{ |
| 144 |
.na |
| 145 |
.nh |
| 146 |
MT-Unsafe race:servent |
| 147 |
race:serventbuf locale |
| 148 |
T} |
| 149 |
T{ |
| 150 |
.na |
| 151 |
.nh |
| 152 |
.BR getservbyname () |
| 153 |
T} Thread safety T{ |
| 154 |
.na |
| 155 |
.nh |
| 156 |
MT-Unsafe race:servbyname |
| 157 |
locale |
| 158 |
T} |
| 159 |
T{ |
| 160 |
.na |
| 161 |
.nh |
| 162 |
.BR getservbyport () |
| 163 |
T} Thread safety T{ |
| 164 |
.na |
| 165 |
.nh |
| 166 |
MT-Unsafe race:servbyport |
| 167 |
locale |
| 168 |
T} |
| 169 |
T{ |
| 170 |
.na |
| 171 |
.nh |
| 172 |
.BR setservent (), |
| 173 |
.BR endservent () |
| 174 |
T} Thread safety T{ |
| 175 |
.na |
| 176 |
.nh |
| 177 |
MT-Unsafe race:servent |
| 178 |
locale |
| 179 |
T} |
| 180 |
.TE |
| 181 |
.P |
| 182 |
In the above table, |
| 183 |
.I servent |
| 184 |
in |
| 185 |
.I race:servent |
| 186 |
signifies that if any of the functions |
| 187 |
.BR setservent (), |
| 188 |
.BR getservent (), |
| 189 |
or |
| 190 |
.BR endservent () |
| 191 |
are used in parallel in different threads of a program, |
| 192 |
then data races could occur. |
| 193 |
.SH STANDARDS |
| 194 |
POSIX.1-2008. |
| 195 |
.SH HISTORY |
| 196 |
POSIX.1-2001, 4.3BSD. |
| 197 |
.SH SEE ALSO |
| 198 |
.BR getnetent (3), |
| 199 |
.BR getprotoent (3), |
| 200 |
.BR getservent_r (3), |
| 201 |
.BR services (5) |