Skip to content

Commit

Permalink
[471053] Add systemd support and services.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
stoupa-cz committed Jun 26, 2015
1 parent f3e216e commit 29731b5
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions compiling.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 9 additions & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions service/systemd/README
Original file line number Diff line number Diff line change
@@ -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).
11 changes: 11 additions & 0 deletions service/systemd/mosquitto.service.notify
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions service/systemd/mosquitto.service.simple
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ and the Eclipse Distribution License is available at
#include <signal.h>
#include <stdio.h>
#include <string.h>
#ifdef WITH_SYSTEMD
# include <systemd/sd-daemon.h>
#endif
#ifdef WITH_WRAP
#include <tcpd.h>
#endif
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 29731b5

Please sign in to comment.