Introduction
libservices is a simple library, written in C, to access and edit the /etc/services file. While editing, it allows one to add, remove and change services preserving existent file proprieties (comments, blank lines, etc).
A command line utility is also provided, making /etc/services easily accessible through shell scripts.
Why?
libservices was born as a proof of concept, when working on the Linuxconf tool for Conectiva. The idea was having several auxiliary libraries which did specific configuration tasks, and the configuration tool would serve as an aggregator and user interface for such libraries. Unfortunately, interests changed and libservices is the only product of this idea.
It's still useful for its original purpose: configurators and package pre/post installation scripts.
Library interface
The content of libservices.h is presented below. Services are managed as a linked list. Using these structures and functions should be straightforward.
enum { SERV_NEW = 1, SERV_REMOVED }; struct serv_alias { struct serv_alias *prev, *next; char *alias; }; struct serv { struct serv *prev, *next; char *name; struct serv_alias *aliases; char *proto; char *port; char *comments; int flags; }; struct serv *serv_create_new(); struct serv *serv_load(const char *filename); int serv_write(const char *filename, struct serv *serv_list); int serv_add(struct serv *serv_list, struct serv *serv_new); int serv_has_alias(const struct serv *serv, const char *alias); int serv_match_any_alias(const struct serv_alias *first, const struct serv_alias *second); void serv_destroy(struct serv *serv_list); void serv_destroy_aliases(struct serv_alias *aliases);
Tool interface
The output of services --help:
Usage: services [ACTION] [OPTION]... Append, remove, and change services settings. Actions: --show show service(s) (default action) --add add service (won't replace) --remove remove service(s) --replace add service and replace if necessary Options: -n, --name=STRING service name -p, --port=NUMBER service port number -o, --proto=STRING service protocol -a, --alias=STRING service alias --comments[=STRING] display/add/replace comments --verbose gives more information --force force a dangerous action --help display this help and exit --version output version information and exit
Interactive examples
Show a service already present.
% services --show -n ssh ssh 22 tcp ssh 22 udp
Add a new service, including comment. Notice that it will be included ordered and with the correct indentation.
% services --add -n myservice -p 5000 -o tcp --comment="My test service" % grep -2 myservice /etc/services radmin-port 4899/tcp # RAdmin Port radmin-port 4899/udp myservice 5000/tcp # My test service rfe 5002/udp # Radio Free Ethernet rfe 5002/tcp
Do not allow replacing if not explicitly requested:
% services --add -n anotherservice -p 5000 -o tcp error: resources already used by myservice 5000/tcp
Download
Available files:
Author
Gustavo Niemeyer <gustavo@niemeyer.net>