diff --git a/CMake/FindLibsystemd.cmake b/CMake/FindLibsystemd.cmake new file mode 100644 index 0000000000..f75558a444 --- /dev/null +++ b/CMake/FindLibsystemd.cmake @@ -0,0 +1,28 @@ +include(FindPkgConfig) +pkg_check_modules(PC_SYSTEMD QUIET "libsystemd") +if (PC_SYSTEMD_FOUND) + add_definitions(${PC_SYSTEMD_CFLAGS} ${PC_SYSTEMD_CFLAGS_OTHER}) +endif(PC_SYSTEMD_FOUND) + +find_path( + SYSTEMD_INCLUDE_DIRS + NAMES systemd/sd-daemon.h + HINTS ${PC_SYSTEMD_INCLUDEDIR} ${PC_SYSTEMD_INCLUDE_DIRS} +) + +find_library( + SYSTEMD_LIBRARIES + NAMES systemd + HINTS ${PC_SYSTEMD_LIBDIR} ${PC_SYSTEMD_LIBRARY_DIRS} +) + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args( + SYSTEMD + REQUIRED_VARS SYSTEMD_LIBRARIES SYSTEMD_INCLUDE_DIRS +) +mark_as_advanced( + SYSTEMD_FOUND + SYSTEMD_LIBRARIES SYSTEMD_INCLUDE_DIRS +) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8090ff1a2b..14681c35ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -757,6 +757,14 @@ if(ENABLE_WX) endif() endif() +find_package(Libsystemd) +if(SYSTEMD_FOUND) + message(STATUS "libsystemd found, enabling traversal server watchdog support") + add_definitions(-DHAVE_LIBSYSTEMD) +else() + message(STATUS "libsystemd not found, disabling traversal server watchdog support") +endif() + ######################################## # Pre-build events: Define configuration variables and write SCM info header # diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index f4e0a4ff84..c4f153569d 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -131,6 +131,9 @@ endif() if(UNIX) # Posix networking code needs to be fixed for Windows add_executable(traversal_server TraversalServer.cpp) + if(SYSTEMD_FOUND) + target_link_libraries(traversal_server ${SYSTEMD_LIBRARIES}) + endif() if(HAIKU) target_link_libraries(traversal_server network) endif() diff --git a/Source/Core/Common/TraversalServer.cpp b/Source/Core/Common/TraversalServer.cpp index 825eee06a7..5da45a05fe 100644 --- a/Source/Core/Common/TraversalServer.cpp +++ b/Source/Core/Common/TraversalServer.cpp @@ -15,10 +15,16 @@ #include #include #include + +#ifdef HAVE_LIBSYSTEMD +#include +#endif + #include "Common/TraversalProto.h" #define DEBUG 0 #define NUMBER_OF_TRIES 5 +#define PORT 6262 static u64 currentTime; @@ -397,7 +403,7 @@ int main() addr.sin6_len = sizeof(addr); #endif addr.sin6_family = AF_INET6; - addr.sin6_port = htons(6262); + addr.sin6_port = htons(PORT); addr.sin6_flowinfo = 0; addr.sin6_addr = any; addr.sin6_scope_id = 0; @@ -419,6 +425,10 @@ int main() return 1; } +#ifdef HAVE_LIBSYSTEMD + sd_notifyf(0, "READY=1\nSTATUS=Listening on port %d", PORT); +#endif + while (true) { sockaddr_in6 raddr; @@ -450,5 +460,8 @@ int main() HandlePacket(&packet, &raddr); } ResendPackets(); +#ifdef HAVE_LIBSYSTEMD + sd_notify(0, "WATCHDOG=1"); +#endif } }