]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
util: abstract parsing of passed FDs into virGetListenFDs()
authorMartin Kletzander <mkletzan@redhat.com>
Tue, 15 Jul 2014 12:34:13 +0000 (14:34 +0200)
committerMartin Kletzander <mkletzan@redhat.com>
Fri, 22 Aug 2014 07:12:13 +0000 (09:12 +0200)
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@redhat.com>
src/libvirt_private.syms
src/locking/lock_daemon.c
src/util/virutil.c
src/util/virutil.h

index 08111d4bfbec3b4c1cd2f1b24bbfe659c7707cd2..7516ed3aa014970f2b5862ae7b6ecf66ee695bb8 100644 (file)
@@ -2107,6 +2107,7 @@ virGetGroupID;
 virGetGroupList;
 virGetGroupName;
 virGetHostname;
+virGetListenFDs;
 virGetSelfLastChanged;
 virGetUnprivSGIOSysfsPath;
 virGetUserCacheDirectory;
index 3379f298a4b90c706713bdfba4800fedf7d18084..e9219d5ec6f19e84c82d9400b5f96f194046b19d 100644 (file)
@@ -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'. */
index 50faf2becb0cfb8ed52416d6b52417b889ad5861..1d897d9a548dd250a16f7fffe56b61e88f5ba816 100644 (file)
@@ -2396,3 +2396,65 @@ 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;
+    size_t i = 0;
+    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);
+
+    for (i = 0; i < nfds; i++) {
+        int fd = STDERR_FILENO + i + 1;
+
+        VIR_DEBUG("Disabling inheritance of passed FD %d", fd);
+
+        if (virSetInherit(fd, false) < 0) {
+            VIR_WARN("Couldn't disable inheritance of passed FD %d", fd);
+        }
+    }
+
+    return nfds;
+}
index f93ea936c89c3f69c57d57c16ecdba0463a82984..89b79235c332230a1d7be6b5dab3ce85493133a9 100644 (file)
@@ -232,4 +232,6 @@ typedef enum {
 VIR_ENUM_DECL(virTristateBool)
 VIR_ENUM_DECL(virTristateSwitch)
 
+unsigned int virGetListenFDs(void);
+
 #endif /* __VIR_UTIL_H__ */