From 069a4bcf5dec3ee2038dfaf4aba0e75156ec5019 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Wed, 22 May 2013 14:57:51 +0200
Subject: [PATCH] Workaround missing SO_PEERCRED

This bug happens in a few new distributions, so we workaround it by
defining the constant ourselves, if it's missing.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Thomas Thrainer <thomasth@google.com>
---
 lib/netutils.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/netutils.py b/lib/netutils.py
index 3b7658845..596f6a127 100644
--- a/lib/netutils.py
+++ b/lib/netutils.py
@@ -52,6 +52,12 @@ from ganeti import vcluster
 _STRUCT_UCRED = "iII"
 _STRUCT_UCRED_SIZE = struct.calcsize(_STRUCT_UCRED)
 
+# Workaround a bug in some linux distributions that don't define SO_PEERCRED
+try:
+  _SO_PEERCRED = IN.SO_PEERCRED
+except AttributeError:
+  _SO_PEERCRED = 17
+
 # Regexes used to find IP addresses in the output of ip.
 _IP_RE_TEXT = r"[.:a-z0-9]+"      # separate for testing purposes
 _IP_FAMILY_RE = re.compile(r"(?P<family>inet6?)\s+(?P<ip>%s)/" % _IP_RE_TEXT,
@@ -93,7 +99,7 @@ def GetSocketCredentials(sock):
   @return: The PID, UID and GID of the connected foreign process.
 
   """
-  peercred = sock.getsockopt(socket.SOL_SOCKET, IN.SO_PEERCRED,
+  peercred = sock.getsockopt(socket.SOL_SOCKET, _SO_PEERCRED,
                              _STRUCT_UCRED_SIZE)
   return struct.unpack(_STRUCT_UCRED, peercred)
 
-- 
GitLab