Revision f93f2016 lib/utils.py

b/lib/utils.py
46 46
import datetime
47 47
import calendar
48 48
import collections
49
import struct
50
import IN
49 51

  
50 52
from cStringIO import StringIO
51 53

  
......
69 71

  
70 72
_RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid"
71 73

  
74
# Structure definition for getsockopt(SOL_SOCKET, SO_PEERCRED, ...):
75
# struct ucred { pid_t pid; uid_t uid; gid_t gid; };
76
#
77
# The GNU C Library defines gid_t and uid_t to be "unsigned int" and
78
# pid_t to "int".
79
#
80
# IEEE Std 1003.1-2008:
81
# "nlink_t, uid_t, gid_t, and id_t shall be integer types"
82
# "blksize_t, pid_t, and ssize_t shall be signed integer types"
83
_STRUCT_UCRED = "iII"
84
_STRUCT_UCRED_SIZE = struct.calcsize(_STRUCT_UCRED)
85

  
72 86

  
73 87
class RunResult(object):
74 88
  """Holds the result of running external programs.
......
328 342
  return rr
329 343

  
330 344

  
345
def GetSocketCredentials(sock):
346
  """Returns the credentials of the foreign process connected to a socket.
347

  
348
  @param sock: Unix socket
349
  @rtype: tuple; (number, number, number)
350
  @return: The PID, UID and GID of the connected foreign process.
351

  
352
  """
353
  peercred = sock.getsockopt(socket.SOL_SOCKET, IN.SO_PEERCRED,
354
                             _STRUCT_UCRED_SIZE)
355
  return struct.unpack(_STRUCT_UCRED, peercred)
356

  
357

  
331 358
def RemoveFile(filename):
332 359
  """Remove a file ignoring some errors.
333 360

  

Also available in: Unified diff