]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
debian: add systemd support
authorVincent Bernat <bernat@luffy.cx>
Sun, 6 Jan 2013 23:53:01 +0000 (00:53 +0100)
committerVincent Bernat <bernat@luffy.cx>
Mon, 7 Jan 2013 20:43:06 +0000 (21:43 +0100)
Provides a lldpd.service file and let `lldpd` notify systemd when it
is ready using the notification socket.

NEWS
debian/lldpd.install [new file with mode: 0644]
debian/lldpd.service [new file with mode: 0644]
src/daemon/lldpd.c

diff --git a/NEWS b/NEWS
index 924a7b0a47e518db3c206473b0cbfc0521190869..5f4e8acc2b265c3fcd75821b5d0ea6f771f1774c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,6 @@
 lldpd (0.7.1)
   * Features
-    + Upstart support.
+    + Upstart and systemd support.
     + Remove Unix socket when there is no process listening.
 
 lldpd (0.7.0)
diff --git a/debian/lldpd.install b/debian/lldpd.install
new file mode 100644 (file)
index 0000000..9c21c90
--- /dev/null
@@ -0,0 +1 @@
+debian/lldpd.service lib/systemd/system/
diff --git a/debian/lldpd.service b/debian/lldpd.service
new file mode 100644 (file)
index 0000000..aee8287
--- /dev/null
@@ -0,0 +1,15 @@
+[Unit]
+Description=LLDP daemon
+Documentation=man:lldpd(8)
+
+[Service]
+Type=notify
+NotifyAccess=main
+EnvironmentFile=-/etc/default/lldpd
+ExecStart=/usr/sbin/lldpd $DAEMON_ARGS
+ExecStartPre=mkdir -p /var/run/lldpd/etc
+ExecStartPre=cp /etc/localtime /var/run/lldpd/etc/localtime
+Restart=on-failure
+
+[Install]
+WantedBy=multi-user.target
index 27a80a7cc367de09f9a323c8661ead05870c12d1..46ea049d15cb5f8285902ffb7a4bd8a86fb1f373 100644 (file)
@@ -1026,12 +1026,63 @@ static const struct intint filters[] = {
 static int
 lldpd_started_by_upstart()
 {
+#ifdef HOST_OS_LINUX
        const char *upstartjob = getenv("UPSTART_JOB");
        if (!(upstartjob && !strcmp(upstartjob, "lldpd")))
                return 0;
        log_debug("main", "running with upstart, don't fork but stop");
        raise(SIGSTOP);
        return 1;
+#else
+       return 0;
+#endif
+}
+
+/**
+ * Tell if we have been started by systemd.
+ */
+static int
+lldpd_started_by_systemd()
+{
+#ifdef HOST_OS_LINUX
+       int fd = -1;
+       const char *notifysocket = getenv("NOTIFY_SOCKET");
+       if (!notifysocket ||
+           !strchr("@/", notifysocket[0]) ||
+           strlen(notifysocket) < 2)
+               return 0;
+
+       log_debug("main", "running with systemd, don't fork but signal ready");
+       if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
+               log_warn("main", "unable to open systemd notification socket %s",
+                   notifysocket);
+               return 0;
+       }
+
+       struct sockaddr_un su = { .sun_family = AF_UNIX };
+       strlcpy(su.sun_path, notifysocket, sizeof(su.sun_path));
+       if (notifysocket[0] == '@') su.sun_path[0] = 0;
+
+       struct iovec iov = {
+               .iov_base = "READY=1",
+               .iov_len = strlen(iov.iov_base)
+       };
+       struct msghdr hdr = {
+               .msg_name = &su,
+               .msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(notifysocket),
+               .msg_iov = &iov,
+               .msg_iovlen = 1
+       };
+       if (sendmsg(fd, &hdr, MSG_NOSIGNAL) < 0) {
+               log_warn("main", "unable to send notification to systemd");
+               close(fd);
+               return 0;
+       }
+       close(fd);
+       return 1;
+#else
+       return 0;
+#endif
 }
 
 int
@@ -1224,7 +1275,8 @@ lldpd_main(int argc, char *argv[])
        signal(SIGPIPE, SIG_IGN);
 
        /* Daemonization, unless started by upstart or debug */
-       if (!lldpd_started_by_upstart() && !debug) {
+       if (!lldpd_started_by_upstart() && !lldpd_started_by_systemd() &&
+           !debug) {
                int pid;
                char *spid;
                log_debug("main", "daemonize");