diff --git a/lorax.spec b/lorax.spec index aa7b7343..3c92d5bb 100644 --- a/lorax.spec +++ b/lorax.spec @@ -169,12 +169,15 @@ getent passwd weldr >/dev/null 2>&1 || useradd -r -g weldr -d / -s /sbin/nologin %post composer %systemd_post lorax-composer.service +%systemd_post lorax-composer.socket %preun composer %systemd_preun lorax-composer.service +%systemd_preun lorax-composer.socket %postun composer %systemd_postun_with_restart lorax-composer.service +%systemd_postun_with_restart lorax-composer.socket %files %defattr(-,root,root,-) @@ -206,8 +209,10 @@ getent passwd weldr >/dev/null 2>&1 || useradd -r -g weldr -d / -s /sbin/nologin %{python3_sitelib}/pylorax/api/* %{_sbindir}/lorax-composer %{_unitdir}/lorax-composer.service +%{_unitdir}/lorax-composer.socket %dir %{_datadir}/lorax/composer %{_datadir}/lorax/composer/* +%{_tmpfilesdir}/lorax-composer.conf %files -n composer-cli %{_bindir}/composer-cli diff --git a/setup.py b/setup.py index f8ee058d..266c54d5 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,9 @@ import sys # config file data_files = [("/etc/lorax", ["etc/lorax.conf"]), ("/etc/lorax", ["etc/composer.conf"]), - ("/usr/lib/systemd/system", ["systemd/lorax-composer.service"])] + ("/usr/lib/systemd/system", ["systemd/lorax-composer.service", + "systemd/lorax-composer.socket"]), + ("/usr/lib/tmpfiles.d/", ["systemd/lorax-composer.conf"])] # shared files for root, dnames, fnames in os.walk("share"): diff --git a/src/sbin/lorax-composer b/src/sbin/lorax-composer index 251837e3..26c0b021 100755 --- a/src/sbin/lorax-composer +++ b/src/sbin/lorax-composer @@ -209,14 +209,27 @@ if __name__ == '__main__': log.error(e) sys.exit(1) - # Setup the Unix Domain Socket, remove old one, set ownership and permissions - if os.path.exists(opts.socket): - os.unlink(opts.socket) - listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - listener.bind(opts.socket) - os.chmod(opts.socket, 0o660) - os.chown(opts.socket, 0, gid) - listener.listen(1) + # Did systemd pass any extra fds (for socket activation)? + try: + fds = int(os.environ['LISTEN_FDS']) + except (ValueError, KeyError): + fds = 0 + + if fds == 1: + # Inherit the fd passed by systemd + listener = socket.fromfd(3, socket.AF_UNIX, socket.SOCK_STREAM) + elif fds > 1: + log.error("lorax-composer only supports inheriting 1 fd from systemd.") + sys.exit(1) + else: + # Setup the Unix Domain Socket, remove old one, set ownership and permissions + if os.path.exists(opts.socket): + os.unlink(opts.socket) + listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + listener.bind(opts.socket) + os.chmod(opts.socket, 0o660) + os.chown(opts.socket, 0, gid) + listener.listen(1) start_queue_monitor(server.config["COMPOSER_CFG"], uid, gid) diff --git a/systemd/lorax-composer.conf b/systemd/lorax-composer.conf new file mode 100644 index 00000000..a2107d9c --- /dev/null +++ b/systemd/lorax-composer.conf @@ -0,0 +1 @@ +d /run/weldr 750 root weldr diff --git a/systemd/lorax-composer.socket b/systemd/lorax-composer.socket new file mode 100644 index 00000000..f56bb0d2 --- /dev/null +++ b/systemd/lorax-composer.socket @@ -0,0 +1,12 @@ +[Unit] +Description=lorax-composer socket activation + +[Socket] +ListenStream=/run/weldr/api.socket +SocketUser=root +SocketGroup=weldr +SocketMode=0660 +DirectoryMode=0750 + +[Install] +WantedBy=sockets.target