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 a servent structure with all the broken-out fields from that entry. if it needs to open a connection to the database, it just... does. โœจ
  • getservbyname() โ€” returns a servent structure for the entry that matches the service name, using protocol proto. if proto is NULL, then literally any protocol gets matched, it's not picky. connection gets opened if necessary, obviously. ๐Ÿ’…
  • getservbyport() โ€” returns a servent structure 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 is NULL, 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 the getserv*() 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 โ€” a NULL-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 locale
  • getservbyname() โ€” Thread safety: MT-Unsafe race:servbyname locale
  • getservbyport() โ€” Thread safety: MT-Unsafe race:servbyport locale
  • setservent(), 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms