HP-UX Reference (11i v1 05/09) - 3 Library Functions A-M (vol 6)

g
getservent(3N) getservent(3N)
NAME
getservent(), getservbyname(), getservbyport(), setservent(), endservent() - get, set, or end service entry
SYNOPSIS
#include <netdb.h>
struct servent *getservent(void);
struct servent *getservbyname(const char *name,
const char *proto);
struct servent *getservbyport(int port, const char *proto);
int setservent(int stayopen);
int endservent(void);
_XOPEN_SOURCE_EXTENDED only
void setservent(int stayopen);
void endservent(void);
DESCRIPTION
The getservent() , getservbyname()
, and getservbyport() functions each return a pointer
to a structure of type servent containing the broken-out fields of a line in the network services data base,
/etc/services.
The members of this structure are:
s_name The ofcial name of the service.
s_aliases A null-terminated list of alternate names for the service.
s_port The port number at which the service resides.
s_proto The name of the protocol to use when contacting the service.
Functions behave as follows:
getservent() Reads the next line of the file, opening the file if necessary.
setservent() Opens and rewinds the file. If the stayopen flag is non-zero, the services
data base is not closed after each call to getservent() (either directly
or indirectly through one of the other getserv* calls).
endservent() Closes the file.
getservbyname()
getservbyport()
Each sequentially searches from the beginning of the file until a matching
service name (among either the official names or the aliases) or port
number is found, or until EOF is encountered. If a non-NULL protocol
name is also supplied (such as
tcp or udp), searches must also match the
protocol.
If the system is running the Network Information Service (NIS) or Network Information Service Plus
(NIS+), getservbyname() gets the service information from the NIS server (see ypserv(1M) and
ypfiles(4)) or from the NIS+ server (see nis+(1)), respectively.
In a multithreaded application, getservent() , getservbyaddr(), and
getservbyname() use
thread-specific storage that is re-used in each call. The return value
struct servent should be unique
for each thread and should be saved, if desired, before the thread makes the next getserv*() call.
For enumeration in multithreaded applications, the position within the enumeration is a process-wide pro-
perty shared by all threads. setservent() may be used in a multithreaded application, but resets the
enumeration position for all threads. If multiple threads interleave calls to getservent() , the threads
will enumerate disjoint subsets of the service database.
Name Service Switch-Based Operation
The library routines getservbyname(), getservbyport(), getservent() , and their reentrant
counterparts, internally call the name service switch to access the "services" database lookup policy
configured in the /etc/nsswitch.conf file (see nsswitch.conf(4)). The lookup policy defines the order
and the criteria of the supported name services used to resolve service names and ports.
Section 3378 Hewlett-Packard Company 1 HP-UX 11i Version 1: September 2005