From 29731b5e464be6a4eb3c330a4de1bde521af2a1c Mon Sep 17 00:00:00 2001 From: Tomas Novotny Date: Mon, 22 Jun 2015 20:11:17 +0200 Subject: [PATCH] [471053] Add systemd support and services. Add possibility to notify systemd that service is fully started. Also add reference service files. Change-Id: Ib4e39c8406ab6c15e1b88f197ae8f91c3e402023 Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=471053 Signed-off-by: Tomas Novotny --- ChangeLog.txt | 1 + compiling.txt | 1 + config.mk | 9 +++++++++ service/systemd/README | 8 ++++++++ service/systemd/mosquitto.service.notify | 11 +++++++++++ service/systemd/mosquitto.service.simple | 9 +++++++++ src/CMakeLists.txt | 10 ++++++++++ src/mosquitto.c | 7 +++++++ 8 files changed, 56 insertions(+) create mode 100644 service/systemd/README create mode 100644 service/systemd/mosquitto.service.notify create mode 100644 service/systemd/mosquitto.service.simple diff --git a/ChangeLog.txt b/ChangeLog.txt index 8b9737c5..d7e32f48 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -17,6 +17,7 @@ Broker: - Add use_subject_as_username option for certificate based client authentication to use the entire certificate subject as a username, rather than just the CN. Closes #469467. +- Add systemd startup notification and services. Closes #471053. Client library: - Outgoing messages with QoS>1 are no longer retried after a timeout period. diff --git a/compiling.txt b/compiling.txt index 44af79b8..dcf7a12d 100644 --- a/compiling.txt +++ b/compiling.txt @@ -4,6 +4,7 @@ The following packages are required for mosquitto: * openssl (version 1.0.0 or greater if TLS-PSK support is needed, can be disabled) * c-ares (for DNS-SRV support, can be disabled) * libuuid (from e2fsprogs, can be disabled) +* libsystemd (optional, disabled by default) * On Windows, the Redhat pthreads library is required if threading support is to be included. diff --git a/config.mk b/config.mk index d7a732a6..9ded798b 100644 --- a/config.mk +++ b/config.mk @@ -57,6 +57,10 @@ WITH_MEMORY_TRACKING:=yes # information about the broker state. WITH_SYS_TREE:=yes +# Build with systemd support. If enabled, mosquitto will notify systemd after +# initialization. See README in service/systemd/ for more information. +WITH_SYSTEMD:=no + # Build with SRV lookup support. WITH_SRV:=yes @@ -215,6 +219,11 @@ ifeq ($(WITH_SYS_TREE),yes) BROKER_CFLAGS:=$(BROKER_CFLAGS) -DWITH_SYS_TREE endif +ifeq ($(WITH_SYSTEMD),yes) + BROKER_CFLAGS:=$(BROKER_CFLAGS) -DWITH_SYSTEMD + BROKER_LIBS:=$(BROKER_LIBS) -lsystemd +endif + ifeq ($(WITH_SRV),yes) LIB_CFLAGS:=$(LIB_CFLAGS) -DWITH_SRV LIB_LIBS:=$(LIB_LIBS) -lcares diff --git a/service/systemd/README b/service/systemd/README new file mode 100644 index 00000000..9ff087c7 --- /dev/null +++ b/service/systemd/README @@ -0,0 +1,8 @@ +Select appropriate systemd service based on your compile settings. If you +enabled WITH_SYSTEMD, use mosquitto.service.notify, otherwise use +mosquitto.service.simple. The service must be renamed to mosquitto.service +before usage. + +With WITH_SYSTEMD mosquitto will notify a complete startup after +initialization. This means that follow-up units can be started after full +initialization of mosquitto (i.e. sockets are opened). diff --git a/service/systemd/mosquitto.service.notify b/service/systemd/mosquitto.service.notify new file mode 100644 index 00000000..bced8dda --- /dev/null +++ b/service/systemd/mosquitto.service.notify @@ -0,0 +1,11 @@ +[Unit] +Description=Mosquitto MQTT v3.1/v3.1.1 Broker + +[Service] +Type=notify +NotifyAccess=main +ExecStart=/usr/sbin/mosquitto +Restart=on-failure + +[Install] +WantedBy=multi-user.target diff --git a/service/systemd/mosquitto.service.simple b/service/systemd/mosquitto.service.simple new file mode 100644 index 00000000..ed3ab95e --- /dev/null +++ b/service/systemd/mosquitto.service.simple @@ -0,0 +1,9 @@ +[Unit] +Description=Mosquitto MQTT v3.1/v3.1.1 Broker + +[Service] +ExecStart=/usr/sbin/mosquitto +Restart=on-failure + +[Install] +WantedBy=multi-user.target diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e386960d..f7bc8c21 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -67,6 +67,16 @@ if (${WITH_SYS_TREE} STREQUAL ON) add_definitions("-DWITH_SYS_TREE") endif (${WITH_SYS_TREE} STREQUAL ON) +if (CMAKE_SYSTEM_NAME STREQUAL Linux) + option(WITH_SYSTEMD + "Include systemd support?" OFF) + if (${WITH_SYSTEMD} STREQUAL ON) + add_definitions("-DWITH_SYSTEMD") + find_library(SYSTEMD_LIBRARY systemd) + set (MOSQ_LIBS ${MOSQ_LIBS} ${SYSTEMD_LIBRARY}) + endif (${WITH_SYSTEMD} STREQUAL ON) +endif (CMAKE_SYSTEM_NAME STREQUAL Linux) + option(WITH_WEBSOCKETS "Include websockets support?" OFF) if (${WITH_WEBSOCKETS} STREQUAL ON) diff --git a/src/mosquitto.c b/src/mosquitto.c index c49fc7e9..3de6ee49 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -39,6 +39,9 @@ Contributors: #include #include #include +#ifdef WITH_SYSTEMD +# include +#endif #ifdef WITH_WRAP #include #endif @@ -362,6 +365,10 @@ int main(int argc, char *argv[]) } #endif +#ifdef WITH_SYSTEMD + sd_notify(0, "READY=1"); +#endif + run = 1; rc = mosquitto_main_loop(&int_db, listensock, listensock_count, listener_max);