[libvirt] [PATCH] rpc: Fix getsockopt on Snow Leopard and lower

Since 5a468b38b6 we use SOL_LOCAL for the 2nd argument of getsockopt() however Lion added the define SOL_LOCAL set to 0, which is the value to the 2nd argument of getsockopt() for Unix sockets on Mac OS X. So instead of using the define just pass 0 so we restore compatibility with Snow Leopard and Leopard. Reported at https://github.com/mxcl/homebrew/pull/23141 --- src/rpc/virnetsocket.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index a2823ef..8651a8b 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -1160,7 +1160,13 @@ int virNetSocketGetUNIXIdentity(virNetSocketPtr sock, virObjectLock(sock); # if defined(__APPLE__) - if (getsockopt(sock->fd, SOL_LOCAL, LOCAL_PEERCRED, &cr, &cr_len) < 0) { + /* Lion (10.7) and higher define the value to be passed to getsockopt() + * as SOL_LOCAL for Unix sockets, it is defined to 0. However + * Snow Leopard and lower did not have this define so you had to + * pass 0. To support the most cases we just pass 0, this behavior + * matches PostgreSQL and CUPS source code. + */ + if (getsockopt(sock->fd, 0, LOCAL_PEERCRED, &cr, &cr_len) < 0) { # else if (getsockopt(sock->fd, SOL_SOCKET, LOCAL_PEERCRED, &cr, &cr_len) < 0) { # endif -- 1.8.1.5

On 10/10/2013 03:44 PM, Doug Goldstein wrote:
Since 5a468b38b6 we use SOL_LOCAL for the 2nd argument of getsockopt() however Lion added the define SOL_LOCAL set to 0, which is the value to the 2nd argument of getsockopt() for Unix sockets on Mac OS X. So instead of using the define just pass 0 so we restore compatibility with Snow Leopard and Leopard.
Reported at https://github.com/mxcl/homebrew/pull/23141 ---
+++ b/src/rpc/virnetsocket.c @@ -1160,7 +1160,13 @@ int virNetSocketGetUNIXIdentity(virNetSocketPtr sock, virObjectLock(sock);
# if defined(__APPLE__) - if (getsockopt(sock->fd, SOL_LOCAL, LOCAL_PEERCRED, &cr, &cr_len) < 0) { + /* Lion (10.7) and higher define the value to be passed to getsockopt() + * as SOL_LOCAL for Unix sockets, it is defined to 0. However + * Snow Leopard and lower did not have this define so you had to + * pass 0. To support the most cases we just pass 0, this behavior + * matches PostgreSQL and CUPS source code. + */ + if (getsockopt(sock->fd, 0, LOCAL_PEERCRED, &cr, &cr_len) < 0) { # else if (getsockopt(sock->fd, SOL_SOCKET, LOCAL_PEERCRED, &cr, &cr_len) < 0) { # endif
You know, I think it might be even cleaner-looking if we could hoist the ifdef outside of the function body. Something like: /* VIR_SOL_PEERCRED - the value needed to let getsockopt() work with * LOCAL_PEERCRED */ #ifdef __APPLE__ # ifdef SOL_LOCAL # define VIR_SOL_PEERCRED SOL_LOCAL # else # define VIR_SOL_PEERCRED 0 # endif #else # define VIR_SOL_PEERCRED SOL_SOCKET #endif ... { if (getsockopt(sock->fd, VIR_SOL_PEERCRED, LOCAL_PEERCRED, &cr, &cr_len) < 0) { ... -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
participants (2)
-
Doug Goldstein
-
Eric Blake