Skip to content

Commit e355752

Browse files
committed
Add separate mechanism to load libc
ctypes.util.find_library() always returns None for systems which do not have the tools installed to determine the location of a given shared library (i.e. ldconfig, gcc, objdump). If find_libary() fails attempt to load known libc by SONAME. Signed-off-by: Frank Vanbever <frank.vanbever@essensium.com>
1 parent e606d41 commit e355752

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

iptc/ip4tc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import struct
1010
import weakref
1111

12-
from .util import find_library, load_kernel
12+
from .util import find_library, load_kernel, find_libc
1313
from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
1414
xt_align, xt_counters, xt_entry_target, xt_entry_match)
1515

@@ -26,7 +26,7 @@
2626

2727
_IFNAMSIZ = 16
2828

29-
_libc = ct.CDLL("libc.so.6")
29+
_libc = find_libc()
3030
_get_errno_loc = _libc.__errno_location
3131
_get_errno_loc.restype = ct.POINTER(ct.c_int)
3232
_malloc = _libc.malloc

iptc/util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,19 @@ def find_library(*names):
109109
major = int(m.group(1))
110110
return lib, major
111111
return None, None
112+
113+
114+
def find_libc():
115+
lib = ctypes.util.find_library('c')
116+
if lib is not None:
117+
return ctypes.CDLL(lib, mode=ctypes.RTLD_GLOBAL)
118+
119+
libnames = ['libc.so.6', 'libc.so.0', 'libc.so']
120+
for name in libnames:
121+
try:
122+
lib = ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL)
123+
return lib
124+
except:
125+
pass
126+
127+
return None

iptc/xtables.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import weakref
77

88
from . import version
9-
from .util import find_library
9+
from .util import find_library, find_libc
1010
from .errors import *
1111

1212
XT_INV_PROTO = 0x40 # invert the sense of PROTO
@@ -792,7 +792,7 @@ class xtables_target(ct.Union):
792792
("v12", _xtables_target_v12)]
793793

794794

795-
_libc, _ = find_library("c")
795+
_libc = find_libc()
796796
_optind = ct.c_long.in_dll(_libc, "optind")
797797
_optarg = ct.c_char_p.in_dll(_libc, "optarg")
798798

0 commit comments

Comments
 (0)