[libvirt] [PATCH v2 1/8] util: abstract parsing of passed FDs into virGetListenFDs()

Martin Kletzander mkletzan at redhat.com
Wed Jul 16 18:29:55 UTC 2014


Since not only systemd can do this (we'll be doing it as well few
patches later), change 'systemd' to 'caller' and fix LISTEN_FDS to
LISTEN_PID where applicable.

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/libvirt_private.syms  |  1 +
 src/locking/lock_daemon.c | 45 ++++-------------------------------------
 src/util/virutil.c        | 51 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virutil.h        |  2 ++
 4 files changed, 58 insertions(+), 41 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8d3671c..138a9fa 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2123,6 +2123,7 @@ virGetGroupID;
 virGetGroupList;
 virGetGroupName;
 virGetHostname;
+virGetListenFDs;
 virGetSelfLastChanged;
 virGetUnprivSGIOSysfsPath;
 virGetUserCacheDirectory;
diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index 3379f29..e9219d5 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -600,50 +600,13 @@ static int
 virLockDaemonSetupNetworkingSystemD(virNetServerPtr srv)
 {
     virNetServerServicePtr svc;
-    const char *pidstr;
-    const char *fdstr;
-    unsigned long long procid;
     unsigned int nfds;

-    VIR_DEBUG("Setting up networking from systemd");
-
-    if (!(pidstr = virGetEnvAllowSUID("LISTEN_PID"))) {
-        VIR_DEBUG("No LISTEN_FDS from systemd");
-        return 0;
-    }
-
-    if (virStrToLong_ull(pidstr, NULL, 10, &procid) < 0) {
-        VIR_DEBUG("Malformed LISTEN_PID from systemd %s", pidstr);
-        return 0;
-    }
-
-    if ((pid_t)procid != getpid()) {
-        VIR_DEBUG("LISTEN_PID %s is not for us %llu",
-                  pidstr, (unsigned long long)getpid());
-        return 0;
-    }
-
-    if (!(fdstr = virGetEnvAllowSUID("LISTEN_FDS"))) {
-        VIR_DEBUG("No LISTEN_FDS from systemd");
-        return 0;
-    }
-
-    if (virStrToLong_ui(fdstr, NULL, 10, &nfds) < 0) {
-        VIR_DEBUG("Malformed LISTEN_FDS from systemd %s", fdstr);
-        return 0;
-    }
-
-    if (nfds > 1) {
-        VIR_DEBUG("Too many (%d) file descriptors from systemd",
-                  nfds);
-        nfds = 1;
-    }
-
-    unsetenv("LISTEN_PID");
-    unsetenv("LISTEN_FDS");
-
-    if (nfds == 0)
+    if ((nfds = virGetListenFDs()) == 0)
         return 0;
+    if (nfds > 1)
+        VIR_DEBUG("Too many (%d) file descriptors from systemd", nfds);
+    nfds = 1;

     /* Systemd passes FDs, starting immediately after stderr,
      * so the first FD we'll get is '3'. */
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 95d1ff9..6f3f411 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -2227,3 +2227,54 @@ void virUpdateSelfLastChanged(const char *path)
         selfLastChanged = sb.st_ctime;
     }
 }
+
+/*
+ * virGetListenFDs:
+ *
+ * Parse LISTEN_PID and LISTEN_FDS passed from caller.
+ *
+ * Returns number of passed FDs.
+ */
+unsigned int
+virGetListenFDs(void)
+{
+    const char *pidstr;
+    const char *fdstr;
+    unsigned long long procid;
+    unsigned int nfds;
+
+    VIR_DEBUG("Setting up networking from caller");
+
+    if (!(pidstr = virGetEnvAllowSUID("LISTEN_PID"))) {
+        VIR_DEBUG("No LISTEN_PID from caller");
+        return 0;
+    }
+
+    if (virStrToLong_ull(pidstr, NULL, 10, &procid) < 0) {
+        VIR_DEBUG("Malformed LISTEN_PID from caller %s", pidstr);
+        return 0;
+    }
+
+    if ((pid_t)procid != getpid()) {
+        VIR_DEBUG("LISTEN_PID %s is not for us %llu",
+                  pidstr, (unsigned long long)getpid());
+        return 0;
+    }
+
+    if (!(fdstr = virGetEnvAllowSUID("LISTEN_FDS"))) {
+        VIR_DEBUG("No LISTEN_FDS from caller");
+        return 0;
+    }
+
+    if (virStrToLong_ui(fdstr, NULL, 10, &nfds) < 0) {
+        VIR_DEBUG("Malformed LISTEN_FDS from caller %s", fdstr);
+        return 0;
+    }
+
+    unsetenv("LISTEN_PID");
+    unsetenv("LISTEN_FDS");
+
+    VIR_DEBUG("Got %u file descriptors", nfds);
+
+    return nfds;
+}
diff --git a/src/util/virutil.h b/src/util/virutil.h
index 2bb74e2..76c1ef3 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -203,4 +203,6 @@ bool virIsSUID(void);
 time_t virGetSelfLastChanged(void);
 void virUpdateSelfLastChanged(const char *path);

+unsigned int virGetListenFDs(void);
+
 #endif /* __VIR_UTIL_H__ */
-- 
2.0.0




More information about the libvir-list mailing list