Skip to content

Commit 7626a98

Browse files
committed
systemd service improvements
Goal is to indicate service readyness after bootstrap has finished.
1 parent 58bad2e commit 7626a98

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

.github/workflows/cmake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
run: sudo apt-get update
2323

2424
- name: Setup Dependencies
25-
run: sudo apt-get install cmake libc-ares-dev libcurl4-openssl-dev libev-dev build-essential clang-tidy dnsutils python3-pip python3-venv valgrind ${{ matrix.compiler }}
25+
run: sudo apt-get install cmake libc-ares-dev libcurl4-openssl-dev libev-dev libsystemd-dev build-essential clang-tidy dnsutils python3-pip python3-venv valgrind ${{ matrix.compiler }}
2626

2727
- name: Setup Python Virtual Environment
2828
run: python3 -m venv ${{github.workspace}}/venv

CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.7)
22
project(HttpsDnsProxy C)
33

4+
include(CheckIncludeFile)
5+
46
# FUNCTIONS
57

68
# source: https://stackoverflow.com/a/27990434
@@ -35,6 +37,9 @@ if (((CMAKE_C_COMPILER_ID MATCHES GNU AND CMAKE_C_COMPILER_VERSION VERSION_GRE
3537
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-gnu-zero-variadic-macro-arguments -Wno-gnu-folding-constant")
3638
endif()
3739

40+
set(SERVICE_EXTRA_OPTIONS "")
41+
set(SERVICE_TYPE "simple")
42+
3843
# VERSION
3944
# It is possible to define external default value like: cmake -DSW_VERSION=1.2-custom
4045

@@ -82,6 +87,15 @@ include_directories(
8287
${LIBCARES_INCLUDE_DIR} ${LIBCURL_INCLUDE_DIR}
8388
${LIBEV_INCLUDE_DIR} src)
8489

90+
check_include_file("systemd/sd-daemon.h" HAVE_SD_DAEMON_H)
91+
92+
if(HAVE_SD_DAEMON_H)
93+
message(STATUS "Using libsystemd")
94+
add_definitions(-DHAS_LIBSYSTEMD=1)
95+
set(LIBS ${LIBS} systemd)
96+
set(SERVICE_TYPE "notify")
97+
endif()
98+
8599
# CLANG TIDY
86100

87101
option(USE_CLANG_TIDY "Use clang-tidy during compilation" ON)
@@ -133,7 +147,6 @@ endif()
133147

134148
install(TARGETS ${TARGET_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
135149

136-
set(SERVICE_EXTRA_OPTIONS "")
137150
if(IS_DIRECTORY "/etc/munin/plugins" AND
138151
IS_DIRECTORY "/etc/munin/plugin-conf.d")
139152
set(SERVICE_EXTRA_OPTIONS "-s 300")

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Depends on `c-ares (>=1.11.0)`, `libcurl (>=7.66.0)`, `libev (>=4.25)`.
4848
On Debian-derived systems those are libc-ares-dev,
4949
libcurl4-{openssl,nss,gnutls}-dev and libev-dev respectively.
5050
On Redhat-derived systems those are c-ares-devel, libcurl-devel and libev-devel.
51+
On systems with systemd it is recommended to have libsystemd development package installed.
5152

5253
On MacOS, you may run into issues with curl headers. Others have had success when first installing curl with brew.
5354
```
@@ -57,7 +58,7 @@ brew link curl --force
5758

5859
On Ubuntu
5960
```
60-
apt-get install cmake libc-ares-dev libcurl4-openssl-dev libev-dev build-essential
61+
apt-get install cmake libc-ares-dev libcurl4-openssl-dev libev-dev libsystemd-dev build-essential
6162
```
6263

6364
If all pre-requisites are met, you should be able to build with:

https_dns_proxy.service.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ Before=nss-lookup.target
66
After=network.target
77

88
[Service]
9-
Type=simple
9+
Type=${SERVICE_TYPE}
1010
DynamicUser=yes
11-
Restart=on-failure
1211
ExecStart=${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/https_dns_proxy \
1312
-v -v ${SERVICE_EXTRA_OPTIONS}
13+
Restart=on-failure
14+
RestartSec=5
15+
TimeoutStartSec=20
1416
TimeoutStopSec=10
1517

1618
[Install]

src/main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <sys/types.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
1010
#include <unistd.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
1111

12+
#if HAS_LIBSYSTEMD == 1
13+
#include <systemd/sd-daemon.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
14+
#endif
15+
1216
#include "dns_poller.h"
1317
#include "dns_server.h"
1418
#include "dns_server_tcp.h"
@@ -150,6 +154,27 @@ static void dns_server_cb(void *dns_server, uint8_t is_tcp, void *data,
150154
req->dns_req, dns_req_len, app->resolv, req->tx_id, https_resp_cb, req);
151155
}
152156

157+
static void systemd_notify_ready(void) {
158+
#if HAS_LIBSYSTEMD == 1
159+
static uint8_t called_once = 0;
160+
if (called_once != 0) {
161+
DLOG("Systemd notify already called once!");
162+
return;
163+
}
164+
called_once = 1;
165+
const int result = sd_notify(0, "READY=1");
166+
if (result > 0) {
167+
DLOG("Systemd notify succeeded, service is ready!");
168+
} else if (result == 0) {
169+
WLOG("Systemd notify called, but NOTIFY_SOCKET not set. Running manually?");
170+
} else {
171+
ELOG("Systemd notify failed with: %s", strerror(result));
172+
}
173+
#else
174+
DLOG("Systemd notify skipped, not compiled with libsystemd!");
175+
#endif
176+
}
177+
153178
static int addr_list_reduced(const char* full_list, const char* list) {
154179
const char *pos = list;
155180
const char *end = list + strlen(list);
@@ -184,6 +209,9 @@ static void dns_poll_cb(const char* hostname, void *data,
184209
abort(); // must be impossible
185210
}
186211
(void)snprintf(buf + ip_start, sizeof(buf) - 1 - (uint32_t)ip_start, "%s", addr_list); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
212+
if (app->resolv == NULL) {
213+
systemd_notify_ready();
214+
}
187215
if (app->resolv && app->resolv->data) {
188216
char * old_addr_list = strstr(app->resolv->data, ":443:");
189217
if (old_addr_list) {
@@ -394,6 +422,8 @@ int main(int argc, char *argv[]) {
394422
} else {
395423
ILOG("Resolver prefix '%s' doesn't appear to contain a "
396424
"hostname. DNS polling disabled.", opt.resolver_url);
425+
426+
systemd_notify_ready();
397427
}
398428
}
399429

0 commit comments

Comments
 (0)