getservent(3)
๐ getservent(3) โ the girlies' guide to service entries ๐
okay so bestie, welcome to getservent(3), straight out of the Library Functions Manual. this is the whole vibe: getservent, getservbyname, getservbyport, setservent, endservent โ they all just wanna get a service entry for you. that's it. that's the job. ๐
โจ NAME โจ
getservent, getservbyname, getservbyport, setservent, endservent โ get service entry ๐ฆ
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ซถ 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);
void setservent(int stayopen);
void endservent(void);
๐ธ DESCRIPTION ๐ธ
okay so picture the services database (yes, services(5), we'll get to her). these functions are your little tour guides through it, no cap.
getservent()โ reads the next entry from the services database and hands you back aserventstructure with all the broken-out fields from that entry. if it needs to open a connection to the database, it just... does. โจgetservbyname()โ returns aserventstructure for the entry that matches the service name, using protocol proto. if proto isNULL, then literally any protocol gets matched, it's not picky. connection gets opened if necessary, obviously. ๐getservbyport()โ returns aserventstructure for the entry that matches the port port (and bestie, that port is in network byte order, don't forget), using protocol proto. if proto isNULL, again, any protocol matches. connection opened if necessary. ๐ฅsetservent()โ opens a connection to the database and rewinds you all the way back to the first entry, main character energy. if stayopen is nonzero, then that database connection does not get closed between calls to one of thegetserv*()functions. it just stays open, living its best life.endservent()โ closes the connection to the database. byeee ๐ซถ
the servent structure ๐ง
she's defined in <netdb.h> like this:
struct servent {
char *s_name; /* official service name */
char **s_aliases; /* alias list */
int s_port; /* port number */
char *s_proto; /* protocol to use */
}
and the members, one by one, because we respect her:
s_nameโ the official name of the service. the government name, if you will.s_aliasesโ aNULL-terminated list of alternative names for the service. her nicknames ๐ซs_portโ the port number for the service, given in network byte order. (saying it twice because it matters, okay?)s_protoโ the name of the protocol to use with this service.
๐ RETURN VALUE ๐
getservent(), getservbyname(), and getservbyport() return a pointer to a statically allocated servent structure. or NULL if an error occurs, or if the end of the file is reached. so like... check for NULL, bestie, please ๐ญ
๐ FILES ๐
/etc/servicesโ the services database file. that's her. that's the one. ๐
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, go see attributes(7). translating the whole table for you because I got you:
getservent()โ Thread safety:MT-Unsafe race:servent race:serventbuf localegetservbyname()โ Thread safety:MT-Unsafe race:servbyname localegetservbyport()โ Thread safety:MT-Unsafe race:servbyport localesetservent(),endservent()โ Thread safety:MT-Unsafe race:servent locale
and the note that comes with the table, in case you were wondering what that word even means:
in the table above, servent in race:servent signifies that if any of the functions setservent(), getservent(), or endservent() are used in parallel in different threads of a program, then data races could occur. so like... don't do that. not the vibe. ๐
๐ STANDARDS ๐
POSIX.1-2008. periodt.
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
POSIX.1-2001, 4.3BSD. she's been iconic for a minute.
๐ SEE ALSO ๐
bestie reminder: the servent struct you get back is statically allocated, so it's shared, it's not yours, and the next call will absolutely overwrite it. copy what you need. love you, mean it. ๐
and that's the whole page!! this came from Linux man-pages 6.20, dated 2026-02-08 ๐ โ go forth and look up your services, you absolute icon โญ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms