Skip to content

Commit 7566033

Browse files
committed
nfqws,tpws: add support for systemd readiness notifications
1 parent 8b73e2e commit 7566033

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ all: clean
1515
done \
1616
done
1717

18+
systemd: clean
19+
@mkdir -p "$(TGT)"; \
20+
for dir in $(DIRS); do \
21+
find "$$dir" -type f \( -name "*.c" -o -name "*.h" -o -name "*akefile" \) -exec chmod -x {} \; ; \
22+
$(MAKE) -C "$$dir" systemd || exit; \
23+
for exe in "$$dir/"*; do \
24+
if [ -f "$$exe" ] && [ -x "$$exe" ]; then \
25+
mv -f "$$exe" "${TGT}" ; \
26+
ln -fs "../${TGT}/$$(basename "$$exe")" "$$exe" ; \
27+
fi \
28+
done \
29+
done
30+
1831
android: clean
1932
@mkdir -p "$(TGT)"; \
2033
for dir in $(DIRS); do \

init.d/systemd/nfqws.service

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Example systemd service unit for nfqws. Adjust for your installation.
2+
3+
[Unit]
4+
After=network.target
5+
6+
[Service]
7+
Type=notify
8+
Restart=on-failure
9+
10+
ExecSearchPath=/opt/zapret/binaries/my
11+
ExecStart=nfqws @${CONFIG_FILE}
12+
Environment=CONFIG_FILE=/etc/zapret/nfqws.config
13+
14+
StateDirectory=nfqws
15+
StateDirectoryMode=0700
16+
WorkingDirectory=%S/nfqws
17+
18+
DynamicUser=true
19+
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW
20+
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW
21+
RestrictAddressFamilies=AF_NETLINK AF_UNIX AF_INET6 AF_INET
22+
23+
LockPersonality=true
24+
MemoryDenyWriteExecute=true
25+
PrivateDevices=true
26+
PrivateMounts=true
27+
PrivateTmp=true
28+
ProcSubset=pid
29+
ProtectClock=true
30+
ProtectControlGroups=true
31+
ProtectHome=true
32+
ProtectHostname=true
33+
ProtectKernelLogs=true
34+
ProtectKernelModules=true
35+
ProtectKernelTunables=true
36+
ProtectProc=invisible
37+
ProtectSystem=strict
38+
RemoveIPC=true
39+
RestrictNamespaces=true
40+
RestrictRealtime=true
41+
RestrictSUIDSGID=true
42+
SystemCallArchitectures=native
43+
SystemCallFilter=@system-service
44+
SystemCallFilter=~@resources @privileged
45+
UMask=0077
46+
47+
[Install]
48+
WantedBy=multi-user.target

ip2net/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ all: ip2net
1111
ip2net: $(SRC_FILES)
1212
$(CC) -s $(CFLAGS) -o ip2net $(SRC_FILES) $(LIBS) $(LDFLAGS)
1313

14+
systemd: ip2net
15+
1416
android: ip2net
1517

1618
bsd: $(SRC_FILES)

mdig/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ all: mdig
1212
mdig: $(SRC_FILES)
1313
$(CC) -s $(CFLAGS) -o mdig $(SRC_FILES) $(LIBS) $(LDFLAGS)
1414

15+
systemd: mdig
16+
1517
android: $(SRC_FILES)
1618
$(CC) -s $(CFLAGS) -o mdig $(SRC_FILES) $(LIBS_ANDROID) $(LDFLAGS)
1719

nfq/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
CC ?= gcc
22
CFLAGS += -std=gnu99 -Os -flto=auto
3+
CFLAGS_SYSTEMD = -DUSE_SYSTEMD
34
CFLAGS_BSD = -Wno-address-of-packed-member
45
CFLAGS_CYGWIN = -Wno-address-of-packed-member -static
56
LIBS_LINUX = -lnetfilter_queue -lnfnetlink -lz
7+
LIBS_SYSTEMD = -lsystemd
68
LIBS_BSD = -lz
79
LIBS_CYGWIN = -lz -Lwindows/windivert -Iwindows -lwlanapi -lole32 -loleaut32
810
LIBS_CYGWIN32 = -lwindivert32
@@ -16,6 +18,9 @@ all: nfqws
1618
nfqws: $(SRC_FILES)
1719
$(CC) -s $(CFLAGS) -o nfqws $(SRC_FILES) $(LIBS_LINUX) $(LDFLAGS)
1820

21+
systemd: $(SRC_FILES)
22+
$(CC) -s $(CFLAGS) $(CFLAGS_SYSTEMD) -o nfqws $(SRC_FILES) $(LIBS_LINUX) $(LIBS_SYSTEMD) $(LDFLAGS)
23+
1924
android: nfqws
2025

2126
bsd: $(SRC_FILES)

nfq/nfqws.c

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include "win.h"
3636
#endif
3737

38+
#ifdef USE_SYSTEMD
39+
#include <systemd/sd-daemon.h>
40+
#endif
41+
3842
#ifdef __linux__
3943
#include <libnetfilter_queue/libnetfilter_queue.h>
4044
#define NF_DROP 0
@@ -271,6 +275,15 @@ static bool nfq_init(struct nfq_handle **h,struct nfq_q_handle **qh)
271275
return false;
272276
}
273277

278+
static void notify_ready(void)
279+
{
280+
#ifdef USE_SYSTEMD
281+
int r = sd_notify(0, "READY=1");
282+
if (r < 0)
283+
DLOG_ERR("sd_notify: %s\n", strerror(-r));
284+
#endif
285+
}
286+
274287
static int nfq_main(void)
275288
{
276289
uint8_t buf[16384] __attribute__((aligned));
@@ -291,6 +304,8 @@ static int nfq_main(void)
291304
if (!nfq_init(&h,&qh))
292305
return 1;
293306

307+
notify_ready();
308+
294309
fd = nfq_fd(h);
295310
do
296311
{

tpws/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
CC ?= gcc
22
CFLAGS += -std=gnu99 -Os -flto=auto
3+
CFLAGS_SYSTEMD = -DUSE_SYSTEMD
34
CFLAGS_BSD = -Wno-address-of-packed-member
45
LIBS = -lz -lpthread
6+
LIBS_SYSTEMD = -lz -lsystemd
57
LIBS_ANDROID = -lz
68
SRC_FILES = *.c
79
SRC_FILES_ANDROID = $(SRC_FILES) andr/*.c
@@ -11,6 +13,9 @@ all: tpws
1113
tpws: $(SRC_FILES)
1214
$(CC) -s $(CFLAGS) -o tpws $(SRC_FILES) $(LIBS) $(LDFLAGS)
1315

16+
systemd: $(SRC_FILES)
17+
$(CC) -s $(CFLAGS) $(CFLAGS_SYSTEMD) -o tpws $(SRC_FILES) $(LIBS_SYSTEMD) $(LDFLAGS)
18+
1419
android: $(SRC_FILES)
1520
$(CC) -s $(CFLAGS) -o tpws $(SRC_FILES_ANDROID) $(LIBS_ANDROID) $(LDFLAGS)
1621

tpws/tpws.c

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
#include <ifaddrs.h>
3131
#endif
3232

33+
#ifdef USE_SYSTEMD
34+
#include <systemd/sd-daemon.h>
35+
#endif
36+
3337
#include "tpws.h"
3438

3539
#ifdef BSD
@@ -1688,6 +1692,15 @@ static const char *bindll_s[] = { "unwanted","no","prefer","force" };
16881692
#define PRINT_VER printf("self-built version %s %s\n\n", __DATE__, __TIME__)
16891693
#endif
16901694

1695+
static void notify_ready(void)
1696+
{
1697+
#ifdef USE_SYSTEMD
1698+
int r = sd_notify(0, "READY=1");
1699+
if (r < 0)
1700+
DLOG_ERR("sd_notify: %s\n", strerror(-r));
1701+
#endif
1702+
}
1703+
16911704
int main(int argc, char *argv[])
16921705
{
16931706
int i, listen_fd[MAX_BINDS], yes = 1, retval = 0, if_index, exit_v=EXIT_FAILURE;
@@ -1964,6 +1977,7 @@ int main(int argc, char *argv[])
19641977
signal(SIGHUP, onhup);
19651978
signal(SIGUSR2, onusr2);
19661979

1980+
notify_ready();
19671981
retval = event_loop(listen_fd,params.binds_last+1);
19681982
exit_v = retval < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
19691983
DLOG_CONDUP("Exiting\n");

0 commit comments

Comments
 (0)