Revision 6915fe26 lib/utils/cfunc.py
b/lib/utils/cfunc.py | ||
---|---|---|
34 | 34 |
ctypes = None |
35 | 35 |
|
36 | 36 |
|
37 |
# Flags for mlockall() (from bits/mman.h) |
|
37 |
# Flags for mlockall(2) (from bits/mman.h)
|
|
38 | 38 |
_MCL_CURRENT = 1 |
39 | 39 |
_MCL_FUTURE = 2 |
40 | 40 |
|
41 | 41 |
|
42 |
class _FuncWrapper: |
|
43 |
def __init__(self, ct): |
|
44 |
"""Initializes this class. |
|
45 |
|
|
46 |
""" |
|
47 |
# Make use of a dlopen(3) feature whereby giving a NULL handle returns the |
|
48 |
# main program. Functions from all previously loaded libraries can then be |
|
49 |
# used. |
|
50 |
mainprog = ct.CDLL(None) |
|
51 |
|
|
52 |
# The ctypes module before Python 2.6 does not have built-in functionality |
|
53 |
# to access the global errno global (which, depending on the libc and build |
|
54 |
# options, is per-thread), where function error codes are stored. Use GNU |
|
55 |
# libc's way to retrieve errno(3) instead. |
|
56 |
try: |
|
57 |
errno_loc = getattr(mainprog, "__errno_location") |
|
58 |
except AttributeError, err: |
|
59 |
logging.debug("Unable to find errno location: %s", err) |
|
60 |
errno_fn = None |
|
61 |
else: |
|
62 |
errno_loc.restype = ct.POINTER(ct.c_int) |
|
63 |
errno_fn = lambda: errno_loc().contents.value |
|
64 |
|
|
65 |
self.errno_fn = errno_fn |
|
66 |
|
|
67 |
# Try to get mlockall(2) |
|
68 |
self.mlockall_fn = getattr(mainprog, "mlockall", None) |
|
69 |
|
|
70 |
|
|
42 | 71 |
def Mlockall(_ctypes=ctypes): |
43 | 72 |
"""Lock current process' virtual address space into RAM. |
44 | 73 |
|
45 |
This is equivalent to the C call mlockall(MCL_CURRENT|MCL_FUTURE),
|
|
46 |
see mlock(2) for more details. This function requires ctypes module.
|
|
74 |
This is equivalent to the C call C{mlockall(MCL_CURRENT | MCL_FUTURE)}. See
|
|
75 |
mlockall(2) for more details. This function requires the C{ctypes} module.
|
|
47 | 76 |
|
48 |
@raises errors.NoCtypesError: if ctypes module is not found
|
|
77 |
@raises errors.NoCtypesError: If the C{ctypes} module is not found
|
|
49 | 78 |
|
50 | 79 |
""" |
51 | 80 |
if _ctypes is None: |
52 | 81 |
raise errors.NoCtypesError() |
53 | 82 |
|
54 |
try: |
|
55 |
libc = _ctypes.cdll.LoadLibrary("libc.so.6") |
|
56 |
except EnvironmentError, err: |
|
57 |
logging.error("Failure trying to load libc: %s", err) |
|
58 |
libc = None |
|
59 |
if libc is None: |
|
60 |
logging.error("Cannot set memory lock, ctypes cannot load libc") |
|
61 |
return |
|
62 |
|
|
63 |
# Some older version of the ctypes module don't have built-in functionality |
|
64 |
# to access the errno global variable, where function error codes are stored. |
|
65 |
# By declaring this variable as a pointer to an integer we can then access |
|
66 |
# its value correctly, should the mlockall call fail, in order to see what |
|
67 |
# the actual error code was. |
|
68 |
# pylint: disable=W0212 |
|
69 |
libc.__errno_location.restype = _ctypes.POINTER(_ctypes.c_int) |
|
70 |
|
|
71 |
if libc.mlockall(_MCL_CURRENT | _MCL_FUTURE): |
|
72 |
# pylint: disable=W0212 |
|
73 |
logging.error("Cannot set memory lock: %s", |
|
74 |
os.strerror(libc.__errno_location().contents.value)) |
|
75 |
return |
|
76 |
|
|
77 |
logging.debug("Memory lock set") |
|
83 |
funcs = _FuncWrapper(_ctypes) |
|
84 |
|
|
85 |
if funcs.mlockall_fn is None: |
|
86 |
logging.debug("libc doesn't support mlockall(2)") |
|
87 |
else: |
|
88 |
if funcs.mlockall_fn(_MCL_CURRENT | _MCL_FUTURE) == 0: |
|
89 |
logging.debug("Memory lock set") |
|
90 |
return True |
|
91 |
|
|
92 |
logging.error("Cannot set memory lock: %s", os.strerror(funcs.errno_fn())) |
|
93 |
|
|
94 |
return False |
Also available in: Unified diff