2024-06-28
I wish systemd didn't require two units for each socket service
Triggered by our recent (and repeated) issue with xinetd restarts, we're considering partially or completely replacing our use of xinetd with systemd socket units on our future Ubuntu 24.04 machines. Xinetd generally works okay today, but our perception is that it's fallen out of style and may not last forever as a maintained and packaged thing in Ubuntu (it's already a 'universe' package). By contrast, systemd socket units and socket activation is definitely sticking around. However, I have a little petty gripe about systemd socket units (which also applies to systemd timer units), which is that they require you to provide two unit files, not one.
As the systemd socket unit documentation
spells out, socket units work by causing systemd to start another
unit when the socket is connected to. Depending on the setting of
Accept=
this is either a regular unit or a template unit (which will be
started with a unique name for every connection). However, in each case you
need a second unit file for the .service unit. This is in contrast
to the xinetd approach, where all you need is a single file placed
into /etc/xinetd.d. As a system administrator, my bias is that the
fewer files involved the better, because there's less chance for
things to become de-synchronized with each other.
Systemd has a coherent internal model that more or less requires there be two units involved here, because it needs to keep track of the activation status of the socket as well as the program or programs involved in handling it. After all, one of the selling points of socket units is that the socket can be active without the associated program having been started. And in the systemd world, the way you get two units is to have two files, so systemd socket activation needs you to provide both a .socket file and a .service file.
(Systemd could probably work out some way to embed the service information in the .socket unit file if it wanted to, but it's probably better not to complicate the model. I did say my gripe was a little petty one.)
PS: The good news is that although you have to install both unit files, you only have to directly activate one (the socket unit).