]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: add sd_notify alternative docs-develop-daem-b51ind/deployments/5813
authorFrantisek Tobias <frantisek.tobias@nic.cz>
Mon, 2 Sep 2024 12:20:30 +0000 (14:20 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 2 Dec 2024 08:41:14 +0000 (09:41 +0100)
This allows kresd to run even if libsystemd is not available.

NEWS
daemon/main.c

diff --git a/NEWS b/NEWS
index 6b86a803f8dea737dd11a266ab906f4bf3cf8916..57795d027aad7e5758507981155e20955eec7ab1 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ Knot Resolver 6.0.10 (202y-mm-dd)
 Improvements
 ------------
 - avoid multiple log lines when IPv6 isn't available (!1633)
+- manager: fix startup on Linux without libsystemd (!1608)
 
 
 Knot Resolver 6.0.9 (2024-11-11)
index e89e2b017b24baeb835822df5f00fbae263df42b..a808ba20044481629538b742b677e40f5f9aab1e 100644 (file)
@@ -38,6 +38,8 @@
 #include <uv.h>
 #if ENABLE_LIBSYSTEMD
 #include <systemd/sd-daemon.h>
+#else
+static int notify_ready(const char *state);
 #endif
 #include <libknot/error.h>
 
@@ -193,6 +195,8 @@ static int run_worker(uv_loop_t *loop, struct args *args)
        /* Notify supervisor. */
 #if ENABLE_LIBSYSTEMD
        sd_notify(0, "READY=1");
+#else
+       notify_ready("READY=1");
 #endif
        /* Run event loop */
        uv_run(loop, UV_RUN_DEFAULT);
@@ -382,6 +386,47 @@ static int start_listening(flagged_fd_array_t *fds) {
        return some_bad_ret;
 }
 
+#if !ENABLE_LIBSYSTEMD
+/* Notify supervisord about successful inicialization
+ * @note tested only on an abstract address in $NOTIFY_SOCKET*/
+static int notify_ready(const char *state)
+{
+       int sockfd;
+       struct sockaddr_un addr;
+       char *socket_path = getenv("NOTIFY_SOCKET");
+       if (!socket_path) {
+               kr_log_error(WORKER, "Failed retrieving env variable $NOTIFY_SOCKET\n");
+               return EXIT_FAILURE;
+       }
+       if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) {
+               kr_log_error(WORKER, "Failed to create unix socket at $NOTIFY_SOCKET ('%s'): %s\n",
+                               socket_path, strerror(errno));
+               return EXIT_FAILURE;
+       }
+
+       addr.sun_family = AF_UNIX;
+
+       int addrlen;
+       if (socket_path[0] == '@') {
+               addr.sun_path[0] = '\0';
+               strncpy(&addr.sun_path[1], socket_path + 1, sizeof(addr.sun_path) - 2);
+               addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path + 1) + 1;
+       } else {
+               strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
+               addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path) + 1;
+       }
+       if (sendto(sockfd, state, strlen(state), 0, &addr, addrlen) == -1) {
+               kr_log_error(WORKER, "Failed to send notify message to '%s': %s\n",
+                       socket_path, strerror(errno));
+               close(sockfd);
+               return EXIT_FAILURE;
+       }
+
+       close(sockfd);
+       return kr_ok();
+}
+#endif /* if !ENABLE_LIBSYSTEMD */
+
 /* Drop POSIX 1003.1e capabilities. */
 static void drop_capabilities(void)
 {