Bug 15980 - lighttpd: better systemd service unit
Summary: lighttpd: better systemd service unit
Status: RESOLVED FIXED
Alias: None
Product: Mageia
Classification: Unclassified
Component: RPM Packages (show other bugs)
Version: 4
Hardware: All Linux
Priority: Normal enhancement
Target Milestone: ---
Assignee: Mageia Bug Squad
QA Contact:
URL: http://lists.freedesktop.org/archives...
Whiteboard:
Keywords:
Depends on: 16555
Blocks:
  Show dependency treegraph
 
Reported: 2015-05-19 09:43 CEST by Luca Olivetti
Modified: 2015-09-08 17:26 CEST (History)
2 users (show)

See Also:
Source RPM: lighttpd-1.4.33-4.1.mga4.src.rpm
CVE:
Status comment:


Attachments
lighttpd service unit file (385 bytes, text/plain)
2015-05-19 09:43 CEST, Luca Olivetti
Details

Description Luca Olivetti 2015-05-19 09:43:08 CEST
The systemd service file provided with lighttpd doesn't implement a graceful reload of the configuration files.
I propose a replacement service unit based on what's described in the thread here

http://lists.freedesktop.org/archives/systemd-devel/2011-January/001067.html

that uses lighttpd-angel instead of lighttpd.
As an added bonus, the reload command will check the configuration, and if there is an error lighttpd keeps running with the old one.

Also, the variable defined in the environment file (LIGHTTPD_CONF_PATH) is not used anywhere, so I removed the EnvironmentFile definition (and the corresponding /etc/sysconfig/lighttpd file should also be removed), it's just a remnant of the sysv init file (i.e. "daemon $lighttpd -f $LIGHTTPD_CONF_PATH").

This should *not* be interpreted as a fix to bug #15948 since there's no need to reload the configuration when you just want to reopen the log files, so the "killall -HUP lighttpd" seems to me is the correct solution to that bug.

I wonder why no distribution is using lighttpd-angel, I just tried here on a couple of servers and it is working fine.

Reproducible: 

Steps to Reproduce:
Comment 1 Luca Olivetti 2015-05-19 09:43:49 CEST
Created attachment 6575 [details]
lighttpd service unit file
Comment 2 Samuel Verschelde 2015-05-19 09:52:28 CEST
You set version as Mageia 4. Isn't this valid also in Cauldron?
Comment 3 Luca Olivetti 2015-05-19 10:00:28 CEST
Yes, sorry, I put that because that's what I'm using, but cauldron has the same systemd unit.
Samuel Verschelde 2015-05-19 10:13:18 CEST

Version: 4 => Cauldron
Whiteboard: (none) => MGA4TOO MGA5TOO

Comment 4 David Walser 2015-05-19 17:54:15 CEST
OK, committed in SVN with minor changes.  No freeze push requested yet.

Since we removed PIDFile from the service, does it still use the /run/lighttpd directory when it's running (for a PID file or anything else)?  If not, we can drop the tmpfiles config too.

Index: SOURCES/lighttpd.service
===================================================================
--- SOURCES/lighttpd.service    (revision 822335)
+++ SOURCES/lighttpd.service    (working copy)
@@ -3,10 +3,9 @@
 After=syslog.target network.target

 [Service]
-Type=forking
-PIDFile=/run/lighttpd/lighttpd.pid
-EnvironmentFile=-/etc/sysconfig/lighttpd
-ExecStart=/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
+ExecStartPre=/usr/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf
+ExecStart=/usr/sbin/lighttpd-angel -D -f /etc/lighttpd/lighttpd.conf
+ExecReload=/usr/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf && /bin/kill -HUP $MAINPID

 [Install]
 WantedBy=multi-user.target
Index: SPECS/lighttpd.spec
===================================================================
--- SPECS/lighttpd.spec (revision 822339)
+++ SPECS/lighttpd.spec (working copy)
@@ -4,7 +4,7 @@

 Name:          lighttpd
 Version:       1.4.35
-Release:       %mkrel 4
+Release:       %mkrel 5
 Summary:       A fast webserver with minimal memory-footprint
 License:       BSD
 Group:         System/Servers
@@ -186,10 +186,6 @@
 %install
 %makeinstall_std

-install -d -m 755 %{buildroot}%{_sysconfdir}/sysconfig
-install -m 644 doc/initscripts/sysconfig.lighttpd \
-    %{buildroot}%{_sysconfdir}/sysconfig/lighttpd
-
 install -d -m 755 %{buildroot}%{_unitdir}
 install -m 644 %{SOURCE2} %{buildroot}%{_unitdir}

@@ -281,7 +277,6 @@
 %doc doc/config/lighttpd.conf README INSTALL NEWS COPYING AUTHORS
 %{_unitdir}/lighttpd.service
 %{_prefix}/lib/tmpfiles.d/lighttpd.conf
-%config(noreplace) %{_sysconfdir}/sysconfig/lighttpd
 %dir %{_sysconfdir}/lighttpd/
 %dir %{_sysconfdir}/lighttpd/conf.d/
 %config(noreplace) %{_sysconfdir}/lighttpd/*.conf

CC: (none) => luigiwalser

Comment 5 Luca Olivetti 2015-05-19 18:46:58 CEST
Yes, it still creates a /run/lighttpd/lighttpd.pid (unless you remove server.pid-file from lighttpd.conf), but since it isn't forking anymore (-D) systemd can determine the pid by itself.

BTW, I checked with your modified ExecReload and it doesn't work:

# systemctl reload lighttpd
Job for lighttpd.service failed. See 'systemctl status lighttpd.service' and 'journalctl -xn' for details.

# systemctl status lighttpd
lighttpd.service - Lightning Fast Webserver With Light System Requirements
   Loaded: loaded (/etc/systemd/system/lighttpd.service; enabled)
   Active: active (running) (Result: exit-code) since dt 2015-05-19 18:41:10 CEST; 2min 34s ago
  Process: 1959 ExecReload=/usr/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf && /bin/kill -HUP $MAINPID (code=exited, status=255)
 Main PID: 1879 (lighttpd-angel)
   CGroup: /system.slice/lighttpd.service
           ââ1879 /usr/sbin/lighttpd-angel -D -f /etc/lighttpd/lighttpd.conf
           ââ1937 /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf


It only works correctly if the two commands are separated by ';' and not by '&&' (and, yes, I tested the case with a configuration error, in that case systemd won't execute the kill -HUP).
Comment 6 David Walser 2015-05-19 18:55:51 CEST
Ahh, so systemd doesn't support && I guess.  That's unintuitive.

So maybe the angel isn't needed at all?  Maybe this would have worked with a regular lighttpd for the start.

ExecReload=/bin/kill -INT $MAINPID ; /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
Comment 7 David Walser 2015-05-19 18:57:50 CEST
Oops, you still need to not daemonize it.

ExecStartPre=/usr/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf
ExecStart=/usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
ExecReload=/bin/kill -INT $MAINPID ; /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
Comment 8 Luca Olivetti 2015-05-19 18:59:18 CEST
man systemd.service says that multiple commands must be either separated by ';' or put in multiple directives, that it doesn't interpret shell metacharacters and 

"If more than one command is specified, the commands are invoked sequentially in the order they appear in the unit file. If one of the commands fails (and is not prefixed with "-"), other lines are not executed, and the unit is considered failed."
Comment 9 Luca Olivetti 2015-05-19 19:00:38 CEST
The angel is needed because it will keep trying to spawn a new lighttpd until it succeeds (it will fail randomly if the previous instance is still using the socket).
Comment 10 David Walser 2015-05-19 19:01:10 CEST
Yes thanks, poor design on their part.  Learn something new every day :o)
Comment 11 David Walser 2015-05-19 19:03:20 CEST
(In reply to Luca Olivetti from comment #9)
> The angel is needed because it will keep trying to spawn a new lighttpd
> until it succeeds (it will fail randomly if the previous instance is still
> using the socket).

I guess that makes sense.  It does make me wonder how init scripts I've found that do the kill -INT followed by $0 start don't have that problem.

OK, I've fixed the && to be a ;.  Asking for a freeze push.
Comment 12 Luca Olivetti 2015-05-19 19:15:10 CEST
Well, let's say that lighttpd development is strange: the current version is 1.4.x, there was a 1.5.x in the works but it never materialized (or at least I couldn't find it), now the next version will be 2.x but that's not really progressing.
The documentation reflects this "strangeness", and I just discovered it right after migrating our internal webserver to lighttpd in search of something simpler and lighter weight than apache....
Comment 13 David Walser 2015-05-19 23:50:28 CEST
lighttpd-1.4.35-5.mga5 uploaded for Cauldron.

Version: Cauldron => 4
Whiteboard: MGA4TOO MGA5TOO => (none)

Comment 14 Nicolas Lécureuil 2015-05-23 01:05:30 CEST
so what about this bureport then ? we can close it ?

CC: (none) => mageia

Comment 15 David Walser 2015-05-23 03:23:59 CEST
We can backport this change for Mageia 4 and also fix Bug 15948.
David Walser 2015-09-02 17:20:38 CEST

Depends on: (none) => 16555

Comment 16 David Walser 2015-09-08 17:26:41 CEST
Fixed in:
http://advisories.mageia.org/MGASA-2015-0338.html

Status: NEW => RESOLVED
Resolution: (none) => FIXED


Note You need to log in before you can comment on or make changes to this bug.