[libvirt] [PATCH] rpc: Retrieve peer PID via new getsockopt() for Mac

Doug Goldstein cardoe at cardoe.com
Mon Oct 21 14:59:45 UTC 2013


While LOCAL_PEERCRED on the BSDs does not return the pid information of
the peer, Mac OS X 10.8 added LOCAL_PEERPID to retrieve the pid so we
should use that when its available to get that information.
---
v3:
* Consider EOPNOTSUPP as non-fatal in case they've built on 10.8
  but run the binary on 10.6 as that's the error you'll get back
  in that case.
v2:
* Make LOCAL_PEERPID call non-fatal in case the user built the binary on
  a system that supports it but then runs it on a kernel that does not
  support it
---
 src/rpc/virnetsocket.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index b2ebefe..f73bc50 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -1197,12 +1197,36 @@ int virNetSocketGetUNIXIdentity(virNetSocketPtr sock,
         goto cleanup;
     }
 
-    /* PID and process creation time are not supported on BSDs */
+    /* PID and process creation time are not supported on BSDs by
+     * LOCAL_PEERCRED.
+     */
     *pid = -1;
     *timestamp = -1;
     *uid = cr.cr_uid;
     *gid = cr.cr_gid;
 
+# ifdef LOCAL_PEERPID
+    /* Exists on Mac OS X 10.8 for retrieving the peer's PID */
+    cr_len = sizeof(*pid);
+
+    if (getsockopt(sock->fd, VIR_SOL_PEERCRED, LOCAL_PEERPID, pid, &cr_len) < 0) {
+        /* Ensure this is set to something sane as there are no guarentees
+         * as to what its set to now.
+         */
+        *pid = -1;
+
+        /* If this was built on a system with LOCAL_PEERPID defined but
+         * the kernel doesn't support it we'll get back EOPNOTSUPP so
+         * treat all errors but EOPNOTSUPP as fatal
+         */
+        if (errno != EOPNOTSUPP) {
+            virReportSystemError(errno, "%s",
+                    _("Failed to get client socket PID"));
+            goto cleanup;
+        }
+    }
+# endif
+
     ret = 0;
 
 cleanup:
-- 
1.8.1.5




More information about the libvir-list mailing list