From: Vincent Bernat Date: Sat, 7 Dec 2024 11:24:26 +0000 (+0100) Subject: daemon: use code from sd_notify(3) for handling notify socket X-Git-Tag: 1.0.19~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=50d0c2234774ed4b0e4c43f58ff5145641cffc93;p=thirdparty%2Flldpd.git daemon: use code from sd_notify(3) for handling notify socket This also fixes a compilation error about connect(). --- diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index c3b67c6d..259b5b05 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -1464,28 +1464,34 @@ lldpd_started_by_systemd() int fd = -1; int ret = 0; size_t path_length; - struct sockaddr_un sun = { - .sun_family = AF_UNIX, + union sockaddr_union { + struct sockaddr sa; + struct sockaddr_un sun; + } socket_addr = { + .sun.sun_family = AF_UNIX, }; - const char *notifysocket = getenv("NOTIFY_SOCKET"); - if (!notifysocket || !strchr("@/", notifysocket[0]) || - (path_length = strlen(notifysocket)) < 2) + const char *socket_path = getenv("NOTIFY_SOCKET"); + if (!socket_path || (socket_path[0] != '/' && socket_path[0] != '@') || + (path_length = strlen(socket_path)) < 2) goto done; - if (path_length >= sizeof(sun.sun_path)) { + if (path_length >= sizeof(socket_addr.sun.sun_path)) { log_warnx("main", "systemd notification socket is too long"); goto done; } - strlcpy(sun.sun_path, notifysocket, sizeof(sun.sun_path)); - if (notifysocket[0] == '@') sun.sun_path[0] = 0; + memcpy(socket_addr.sun.sun_path, socket_path, path_length); + + /* Support for abstract socket */ + if (socket_addr.sun.sun_path[0] == '@') socket_addr.sun.sun_path[0] = 0; log_debug("main", "running with systemd, don't fork but signal ready"); - if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { + if ((fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0) { log_warn("main", "unable to open systemd notification socket %s", - notifysocket); + socket_path); goto done; } - if (connect(fd, &sun, sizeof(sun)) != 0) { + if (connect(fd, &socket_addr.sa, + offsetof(struct sockaddr_un, sun_path) + path_length) != 0) { log_warn("main", "unable to connect to systemd notification socket"); goto done; }