Statistics
| Branch: | Tag: | Revision:

root / lib / utils / cfunc.py @ 6915fe26

History | View | Annotate | Download (2.6 kB)

1
#
2
#
3

    
4
# Copyright (C) 2009, 2010, 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21
"""Wrapper around mlockall(2).
22

23
"""
24

    
25
import os
26
import logging
27

    
28
from ganeti import errors
29

    
30
try:
31
  # pylint: disable=F0401
32
  import ctypes
33
except ImportError:
34
  ctypes = None
35

    
36

    
37
# Flags for mlockall(2) (from bits/mman.h)
38
_MCL_CURRENT = 1
39
_MCL_FUTURE = 2
40

    
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

    
71
def Mlockall(_ctypes=ctypes):
72
  """Lock current process' virtual address space into RAM.
73

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.
76

77
  @raises errors.NoCtypesError: If the C{ctypes} module is not found
78

79
  """
80
  if _ctypes is None:
81
    raise errors.NoCtypesError()
82

    
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