115 points · by vezzy-fnord · 11y ago · 77 comments
Also, daemontools suffers from the same problem as other non-systemd service managers; it can't manage processes that daemonize themselves for good reason (like nginx or Unicorn). systemd can handle it because it creates a unique cgroup for every service; if the service needs to be stopped, all processes in the cgroup are terminated.
That said, from what I know systemd doesn't necessarily handle double-forking daemons any better. That is to say, if you set the service to Type=forking, it'll still need to use a PID file (with its inherent race conditions) or employ a PID guessing heuristic, which can fail.
This is a universal problem that no hack can truly solve. The bottom line is if you're running under a service manager, you don't daemonize. You delegate to the service manager to daemonize for you. Any deviation from this will be finicky on most Unix-likes.
The purpose of cgroups here is instead process tracking, i.e. reliably killing all children. But for some services this is exactly what you don't want, and then there's nothing stopping you from allocating cgroups yourself if needed, or talk to a cgroup hierarchy management daemon like cgmanager. Ultimately, you just want some unit of isolation here, so you can use whatever your platform has, i.e. jails or contracts.
Why run something terrible (systemd) when we already have something that works really well (daemontools)?
It is terrible only because you say it is. It's been a massive help for us in adopting it, and accepting the help it offers makes custom software systems a lot easier to write in a robust way.
It's only "terrible" if you have axe to grind about the "right" way and then define that as daemontools.
deamontools provides: logging stdout, restart, keeping things running. That's pretty much all. The exact thing is already provided by systemd/upstart/supervisord, with additional benefits: resource limits, namespaces, different behaviour on multiple failures (daemontools will just keep restarting your process as fast as possible, taking 100% CPU if possible and flooding the logs), support for syslog/journal rather than just local files.
By putting daemontools on top of systemd, you're just adding simple system on top of a complex one. You're losing features, but not gaining anything instead.
How are you able to do hot-reloads without daemonizing the master process? Or have you simply chosen to sacrifice this feature?
Is there a good reason? I assume you're thinking of the ability to fork and exec a new master process, running, e.g. a new version of nginx, then handing control to that new master without dropping any connections, possibly even waiting around a bit in case the new master dies and the original master needs to take over again.
I suspect with something like s6-fdholder-daemon [1] could be used to orchestrate a similar process, though I'm not enough of an expert to know. Instead of inheriting file descriptors through forking, an entirely separate, supervised nginx-newversion could be started, get the file descriptors from fdholder, then coordinate with nginx-oldversion about who's going to accept new connections.
Certainly depending on such a service is arguably more complex then just having the functionality built-in. On the other hand, just as every service should not implement its own daemonization, one can argue each service should not implement its own hot restarting. If the technique was more common, the argument to not duplicate such code would be stronger.
[1] http://www.skarnet.org/software/s6/s6-fdholder-daemon.html
And if you don't want to use a supervisor or a fdholder, you don't even need to coordinate, and you never need to fork: simply re-exec your executable with your serving socket in a conventional place (stdin is good). Daemons should be able to take a preopened listening socket and serve on it; hot-restarting is then a simple matter of one execve(). There's really no reason to make it more complicated.
systemd has the ability to hold fds for you. You need to enable it for each service with http://www.freedesktop.org/software/systemd/man/systemd.serv... and the service needs to know hot to actually do it.
This is a nice feature of systemd. I've been using s6 quite a bit (of daemontools heritage), and often want such a feature, but part of the reason I'm using s6 is so that I can use the same run scripts in darwin and Linux, and only Linux has cgroups.
Say you want to visit www.hahaha.com but your network admin is a rotten bastard, and this does happen a lot in corporate networks, well what happens is they set up a DNS record of www.hahaha.com.myevilcorporation.local and set your default domain suffix to myevilcorporation.local.
When you hit www.hahaha.com you don't go to www.hahaha.com; you go to some internal site. The browser tends not to give you any clues about this so you just carry on without knowing.
If you visit www.hahaha.com. (note the trailing dot) then you go to www.hahaha.com. as that dot is the root of the DNS heirarchy and myevilcorporation.local never ever resolves.
Of course this throws all sorts of warnings if you are using SSL but for the average user and site, that isn't necessarily true. Hell we did it years ago and deployed an internal CA cert to all our workstations so it looks like a ton of our corporate sites are actually internet-based and SSL covered but are on internal networks only with private certs.
I suspect that this is less of an issue these days and browsers have protection against this but I wouldn't be 100% sure of it.
So in conclusion, it's really important.
It's not usually that big a deal, but every so often someone will paste the wrong thing or an automated process will misinterpret something. It's better if this happens to a nonexistent domain.
example.com is the standard FQDN to use for examples, though. http://example.com/ links to the documentation...
[1] http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/we...
[1] It's actually slightly astonishing his hosting still works at that URL given that NTL was consumed by Virgin nearly a decade ago.
With manual daemonization, the process can delay forking until it has ensured that its configuration is sound and it has the resources it needs. This way the parent process can indicate with its exit status whether the service was successfully launched.
Only once the grandchild (not the child, you must double-fork in order to start up correctly) has indicated to the initial process that it is ready to provide the service, should the initial process exit, cleanly, thereby notifying systemd that startup was successful.
See daemon(7) for the full details.
And now i ponder likening systemd with CISC...
Your app is still the one listening for connections, systemd just calls wait() until you exit, then launches you again.
There's no need for you to do a fork() or setsid() or whatever, that was a relic of the "just run all these init files as scripts" nature of sysv init.
If the job of a process "supervisor" is to launch you, wait(2), launch you again, repeat... who has the role of doing zero-downtime restarts?
I'll define a zero-downtime restart in this instance as: a new process must be launched while the old process is still running, so it can negotiate a handoff of responsibility (via migrating who owns the port and draining connections in the case of nginx or haproxy), and the old process only dies once the handoff is complete.
If you wanted this behavior with systemd/upstart/etc as a supervisor (where processes are foregrounded and monitored), the supervisor would require a special case for "restart" which would just start a new process and monitor that one instead, and not bother with killing the old one.
I have no idea if systemd can accommodate for this without switching to a non-supervised process management mode (which is definitely possible.) I don't have much familiarity with advanced systemd or upstart, although I have plenty of familiarity with mesos schedulers, which can be kind of a datacenter-level version of systemd, and in this instance we do "rolling restarts" where new instances are launched, we wait for them to pass health checks, and then we drain the old ones, while a load balancer is responsible for routing incoming connections to the healthy instances. It's an interesting notion for what a single machine's process supervisor should do in this case.
Because writing a daemon is almost always more work than writing an application that runs in the foreground. And systemd is capable of taking applications that run in the foreground and running them as daemons instead.
Put another way - if something else is doing all of the hard work for you, why would you want to write a daemon yourself, except possibly as a learning exercise?
So has been the case since IBM AIX in 1992.
Double-fork-detach was arguably only ever a hack to get around the limitations of the traditional Unix paradigm (using inetd to handle incoming connections and invoking a standard program to hand the connection off to).
http://manpages.ubuntu.com/manpages/trusty/man8/update-rc.d....
I didn't do any daemonzing like we used to do in the C days so I'm not sure that the systemd approach mentioned in the article does it better (not against the idea, maybe it's needed on non-Ubuntu systems?).
I just needed to write a simple shell script with the basics and this:
...
case "$1" in
start)
startup
;;
stop)
shutdown
;;
restart)
restartit
;;
esac
...If one is using Ubuntu Linux, then it is daft to begin with System 5 rc scripts, given that one already has upstart on version 14 and has systemd on version 15. Ironically, those "basics" are the bits that one doesn't need to write at all. See http://unix.stackexchange.com/a/200281/5132 .
Someday systemd will go beyond simple key-value substitution, and on that day I'll stop using daemonize.
[1]: http://www.opsfordevelopers.com/2015/05/startup-scripts-for-...
...
[Service]
Type=forking
EnvironmentFile=-/usr/local/myJavaConfig
ExecStart=/bin/sh -c '${JAVA_HOME} ${JAVA_OPTS}'
Or if you're after passing things in, use the other technique that lets you use systemctl myservice@your_sub.service