From 74580d382b430bd9041d8da7037a7c50e66654bf Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 22 Oct 2022 13:01:25 +0200 Subject: [PATCH] Allow testing systemd integration with a test unit file --- tray/testfiles/syncthing-testinstance.service | 32 +++++++++++++++++++ widgets/settings/setupdetection.cpp | 14 +++++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tray/testfiles/syncthing-testinstance.service diff --git a/tray/testfiles/syncthing-testinstance.service b/tray/testfiles/syncthing-testinstance.service new file mode 100644 index 0000000..4917c7d --- /dev/null +++ b/tray/testfiles/syncthing-testinstance.service @@ -0,0 +1,32 @@ +# Unit file for testing systemd integration with a test instance of Syncthing +# 1. Copy to ~/.config/systemd/user/syncthing-testinstance.service for using it. +# 2. Set HOME=/tmp/syncthingtest when launching Syncthing Tray itself to be in +# accordance with this unit file. +# 3. Set SYNCTHINGTRAY_SYSTEMD_USER_UNIT=syncthing-testinstance.service so the +# wizard will make use of this unit instead of the normal one. + +[Unit] +Description=Test instance of Syncthing with HOME under /tmp/syncthingtest +Documentation=man:syncthing(1) +StartLimitIntervalSec=60 +StartLimitBurst=4 + +[Service] +ExecStart=/usr/bin/syncthing serve --no-browser --no-restart --logflags=0 +Restart=on-failure +RestartSec=1 +SuccessExitStatus=3 4 +RestartForceExitStatus=3 4 +Environment=HOME=/tmp/syncthingtest + +# Hardening +SystemCallArchitectures=native +MemoryDenyWriteExecute=true +NoNewPrivileges=true + +# Elevated permissions to sync ownership (disabled by default), +# see https://docs.syncthing.net/advanced/folder-sync-ownership +#AmbientCapabilities=CAP_CHOWN CAP_FOWNER + +[Install] +WantedBy=default.target diff --git a/widgets/settings/setupdetection.cpp b/widgets/settings/setupdetection.cpp index 46a2ee5..c74643f 100644 --- a/widgets/settings/setupdetection.cpp +++ b/widgets/settings/setupdetection.cpp @@ -1,5 +1,8 @@ #include "./setupdetection.h" +// use meta-data of syncthingtray application here +#include "resources/../../tray/resources/config.h" + #if defined(LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD) && (defined(PLATFORM_UNIX) || defined(PLATFORM_MINGW) || defined(PLATFORM_CYGWIN)) #define PLATFORM_HAS_GETLOGIN #include @@ -11,8 +14,17 @@ SetupDetection::SetupDetection(QObject *parent) : QObject(parent) { // assume default service names + const auto defaultUserUnit = +#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) + qEnvironmentVariable(PROJECT_VARNAME_UPPER "_SYSTEMD_USER_UNIT", +#endif + QStringLiteral("syncthing.service") +#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) + ) +#endif + ; #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD - userService.setUnitName(QStringLiteral("syncthing.service")); + userService.setUnitName(defaultUserUnit); systemService.setUnitName(QStringLiteral("syncthing@") % #ifdef PLATFORM_HAS_GETLOGIN QString::fromLocal8Bit(getlogin()) %