From 5472b903858d16a158e502f97c93236b7681ceae Mon Sep 17 00:00:00 2001 From: c0mmit Date: Thu, 10 Jun 2021 14:52:44 +0800 Subject: [PATCH] fix wrong errno --- iptc/ip4tc.py | 5 ++--- iptc/util.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 4ddd2dc..00c9c44 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -27,8 +27,6 @@ _IFNAMSIZ = 16 _libc = find_libc() -_get_errno_loc = _libc.__errno_location -_get_errno_loc.restype = ct.POINTER(ct.c_int) _malloc = _libc.malloc _malloc.restype = ct.POINTER(ct.c_ubyte) _malloc.argtypes = [ct.c_size_t] @@ -1655,7 +1653,8 @@ def builtin_chain(self, chain): def strerror(self): """Returns any pending iptables error from the previous operation.""" - errno = _get_errno_loc()[0] + errno = ct.get_errno() + ct.set_errno(0) if errno == 0: return "libiptc version error" return self._iptc.iptc_strerror(errno) diff --git a/iptc/util.py b/iptc/util.py index 04fe905..a2cbc4b 100644 --- a/iptc/util.py +++ b/iptc/util.py @@ -54,25 +54,27 @@ def load_kernel(name, exc_if_failed=False): def _do_find_library(name): if '/' in name: try: - return ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL) + return ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL, use_errno=True) except Exception: return None p = ctypes.util.find_library(name) if p: - lib = ctypes.CDLL(p, mode=ctypes.RTLD_GLOBAL) + lib = ctypes.CDLL(p, mode=ctypes.RTLD_GLOBAL, use_errno=True) return lib # probably we have been installed in a virtualenv try: lib = ctypes.CDLL(os.path.join(get_python_lib(), name), - mode=ctypes.RTLD_GLOBAL) + mode=ctypes.RTLD_GLOBAL, + use_errno=True) return lib except: pass for p in sys.path: try: - lib = ctypes.CDLL(os.path.join(p, name), mode=ctypes.RTLD_GLOBAL) + lib = ctypes.CDLL(os.path.join(p, name), mode=ctypes.RTLD_GLOBAL, + use_errno=True) return lib except: pass @@ -121,12 +123,12 @@ def find_library(*names): def find_libc(): lib = ctypes.util.find_library('c') if lib is not None: - return ctypes.CDLL(lib, mode=ctypes.RTLD_GLOBAL) + return ctypes.CDLL(lib, mode=ctypes.RTLD_GLOBAL, use_errno=True) libnames = ['libc.so.6', 'libc.so.0', 'libc.so'] for name in libnames: try: - lib = ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL) + lib = ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL, use_errno=True) return lib except: pass