Skip to content

Commit ee4d724

Browse files
author
Matt Pelland
committed
implement sd_notify support
1 parent 7f678f6 commit ee4d724

File tree

7 files changed

+99
-16
lines changed

7 files changed

+99
-16
lines changed

README.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,28 @@ as follows:
5050
provides the API to sysadmin
5151
- [yaml-cpp](https://github.com/jbeder/yaml-cpp) provides YAML config file support
5252

53+
Systemd Integration
54+
===================
55+
56+
sysadmin can integrate with systemd as a `notify` type service which can
57+
prevent races between other services that rely on sysadmin for configuration.
58+
In order to build sysadmin with systemd notify support you need to pass
59+
`-DSYSADMIN_USE_SD_NOTIFY` during step 3 outlined below. When building sysadmin
60+
in this configuration `libsystemd` becomes a required dependency.
61+
62+
There is an a systemd service definition which incorporates this functionality
63+
located [here](configs/prod/systemd-notify.service).
64+
5365
Development
5466
===========
5567

5668
Generally speaking, build as follows:
5769

58-
```
59-
mkdir build
60-
cd build
61-
cmake ..
62-
make check
63-
make
64-
```
70+
1. mkdir build
71+
2. cd build
72+
3. cmake ..
73+
4. make check
74+
5. make
6575

6676
`make check` runs only sysadmin's tests. If you wish to run the decibel-cpp tests, run
6777
`make decibel-check`.

cmake/FindSystemd.cmake

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#.rst:
2+
# FindSystemd
3+
# -------
4+
#
5+
# Find Systemd library
6+
#
7+
# Try to find Systemd library on UNIX systems. The following values are defined
8+
#
9+
# ::
10+
#
11+
# SYSTEMD_FOUND - True if Systemd is available
12+
# SYSTEMD_INCLUDE_DIRS - Include directories for Systemd
13+
# SYSTEMD_LIBRARIES - List of libraries for Systemd
14+
# SYSTEMD_DEFINITIONS - List of definitions for Systemd
15+
#
16+
#=============================================================================
17+
# Copyright (c) 2015 Jari Vetoniemi
18+
#
19+
# Distributed under the OSI-approved BSD License (the "License");
20+
#
21+
# This software is distributed WITHOUT ANY WARRANTY; without even the
22+
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23+
# See the License for more information.
24+
#=============================================================================
25+
26+
include(FeatureSummary)
27+
set_package_properties(Systemd PROPERTIES
28+
URL "http://freedesktop.org/wiki/Software/systemd/"
29+
DESCRIPTION "System and Service Manager")
30+
31+
find_package(PkgConfig)
32+
pkg_check_modules(PC_SYSTEMD QUIET libsystemd)
33+
find_library(SYSTEMD_LIBRARIES NAMES systemd ${PC_SYSTEMD_LIBRARY_DIRS})
34+
find_path(SYSTEMD_INCLUDE_DIRS systemd/sd-login.h HINTS ${PC_SYSTEMD_INCLUDE_DIRS})
35+
36+
set(SYSTEMD_DEFINITIONS ${PC_SYSTEMD_CFLAGS_OTHER})
37+
38+
include(FindPackageHandleStandardArgs)
39+
find_package_handle_standard_args(SYSTEMD DEFAULT_MSG SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES)
40+
mark_as_advanced(SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES SYSTEMD_DEFINITIONS)

configs/prod/sysadmin-notify.service

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Unit]
2+
Description=Sysadmin
3+
After=network.target syslog.target
4+
5+
[Service]
6+
Type=notify
7+
ExecStart=/bin/sysadmin /etc/sysadmin/config.yaml
8+
Restart=always
9+
RestartSec=30
10+
11+
[Install]
12+
WantedBy=multi-user.target

decibel-cpp/include/decibel/messaging/Reactor.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ class Reactor : public folly::Timekeeper
7070
return pPromise->getFuture();
7171
}
7272

73-
// This is kept around for posterity should anyone want to add back
74-
// folly::Executor functionality
75-
// virtual void add(folly::Func fn);
73+
virtual void add(folly::Func fn);
7674

7775
// folly::TimeKeeper
7876
virtual folly::Future<folly::Unit> after(folly::Duration duration);

decibel-cpp/src/decibel/messaging/Reactor.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,10 @@ void Reactor::CancelCall(std::shared_ptr<OneShotTimerEvent> pTimer)
152152
pTimer->Stop();
153153
}
154154

155-
// This is kept around for posterity should anyone want to add back
156-
// folly::Executor functionality
157-
// void Reactor::add(folly::Func fn)
158-
// {
159-
// CallSoon(fn);
160-
// }
155+
void Reactor::add(folly::Func fn)
156+
{
157+
CallSoon(fn);
158+
}
161159

162160
folly::Future<folly::Unit> Reactor::after(folly::Duration duration)
163161
{

src/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,15 @@ target_link_libraries(libsysadmin INTERFACE ${LIBS} decibel)
7272
target_include_directories(libsysadmin PUBLIC ${CMAKE_BINARY_DIR} ${PROJECT_SOURCE_DIR})
7373
target_link_libraries(sysadmin libsysadmin)
7474

75+
if(SYSADMIN_USE_SD_NOTIFY)
76+
include(FindSystemd)
77+
find_package(Systemd REQUIRED)
78+
79+
target_link_libraries(sysadmin ${SYSTEMD_LIBRARIES})
80+
target_include_directories(sysadmin PRIVATE ${SYSTEMD_INCLUDE_DIRS})
81+
82+
target_compile_definitions(sysadmin PRIVATE _SYSADMIN_USE_SD_NOTIFY)
83+
endif()
84+
7585
install(TARGETS sysadmin
7686
RUNTIME DESTINATION bin)

src/sysadmin.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include <iostream>
2+
3+
#ifdef _SYSADMIN_USE_SD_NOTIFY
4+
#include <systemd/sd-daemon.h>
5+
#endif
6+
27
#include <boost/filesystem.hpp>
38
#include <log4cxx/logger.h>
49
#include <log4cxx/propertyconfigurator.h>
@@ -103,6 +108,16 @@ int main(int argc, const char* argv[])
103108
protocol->Send(errResponse);
104109
});
105110

111+
#ifdef _SYSADMIN_USE_SD_NOTIFY
112+
reactor.add([]() {
113+
auto rc = sd_notify(1, "READY=1");
114+
115+
if (rc < 0) {
116+
LOG4CXX_WARN(spLogger, "Failed to notify systemd, error: " << rc);
117+
}
118+
});
119+
#endif
120+
106121
LOG4CXX_INFO(spLogger, "Starting sysadmin on port " << config.port);
107122
reactor.ServeTcp(config.host, config.port, &factory);
108123
reactor.Start();

0 commit comments

Comments
 (0)