Network info

endservent#

Close the services database after a walk with getservent.

endservent is the teardown half of the services-database iteration triplet. After setservent opens the database (typically /etc/services on Linux) and a sequence of getservent calls walks it entry by entry, endservent releases the underlying handle and any buffered state the C library is holding. It wraps the POSIX endservent(3) call one-to-one.

Synopsis#

endservent;
endservent();

What you get back#

Nothing useful. The function is called for its side effect; it does not return a meaningful value. Do not test the return value.

Global state it touches#

  • The system-wide services-database iterator opened by setservent or implicitly by the first getservent / getservbyname / getservbyport call. endservent closes that iterator.

  • $! is not meaningfully set by this call; the POSIX contract for endservent(3) defines no error return.

Examples#

Paired with setservent and getservent to enumerate every entry in /etc/services:

setservent 0;
while (my @e = getservent) {
    last unless @e;
    my ($name, $aliases, $port, $proto) = @e;
    print "$name/$proto = $port\n";
}
endservent;

Close a walk early. The database was opened implicitly by the first getservent; endservent is still the right way to release it:

while (my @e = getservent) {
    last if $e[0] eq 'http';
}
endservent;

Defensive cleanup around a block that may leave the iterator open. Harmless when no iteration is in progress:

eval {
    setservent 1;
    scan_services();
    1;
} or do {
    endservent;          # always release the handle
    die $@;
};
endservent;

Edge cases#

  • Calling without a preceding setservent or getservent is harmless. The underlying C call tolerates a no-op close.

  • Calling twice is harmless for the same reason. Repeated endservent does not report an error.

  • Does not affect the name and port lookup functionsgetservbyname and getservbyport open and close their own short-lived handles and are unaffected by the iterator state that endservent manages.

  • No prototype, no arguments. endservent $x is a compile-time error. Use endservent; or endservent();.

  • Not portable beyond Unix-like systems. On platforms where the C library lacks endservent(3), the call croaks with endservent not implemented.

Differences from upstream#

Fully compatible with upstream Perl 5.42.

See also#

  • setservent — open the services database and, with a non-zero STAYOPEN, keep the handle across subsequent lookups

  • getservent — read the next entry from the open services database; pair every iteration loop with endservent

  • getservbyname — one-shot lookup by service name; does not interact with the iterator endservent closes

  • getservbyport — one-shot lookup by port number; same independence from the iterator

  • endprotoent — the protocols-database analogue, same shape and same rules

  • endhostent — the hosts-database analogue