diff mbox series

[2/3] package/python-iptables: try known libc instead of find_library()

Message ID 20200226142617.4170-2-frank.vanbever@essensium.com
State Superseded
Headers show
Series [1/3] package/python-iptables: add explicit dependency on dynamic libs | expand

Commit Message

Frank Vanbever Feb. 26, 2020, 2:26 p.m. UTC
ctypes.uitl.find_library() depends on gcc and friends to detect the location of
a given shared library. Given that these are not available on the target and
that python-iptables depends on this functionality we need to work around this.
The SONAMEs of the libc are well known so we try the known ones for glibc,
uClibc and musl.

Fixes: https://bugs.busybox.net/show_bug.cgi?id=12271

Signed-off-by: Frank Vanbever <frank.vanbever@essensium.com>
---
 ...-Add-separate-mechanism-to-load-libc.patch | 90 +++++++++++++++++++
 1 file changed, 90 insertions(+)
 create mode 100644 package/python-iptables/0001-Add-separate-mechanism-to-load-libc.patch

Comments

Thomas Petazzoni Feb. 26, 2020, 3:30 p.m. UTC | #1
Hello Frank,

On Wed, 26 Feb 2020 15:26:16 +0100
Frank Vanbever <frank.vanbever@essensium.com> wrote:

> ctypes.uitl.find_library() depends on gcc and friends to detect the location of

Typo: uitl -> util.

> a given shared library. Given that these are not available on the target and
> that python-iptables depends on this functionality we need to work around this.
> The SONAMEs of the libc are well known so we try the known ones for glibc,
> uClibc and musl.
> 
> Fixes: https://bugs.busybox.net/show_bug.cgi?id=12271
> 
> Signed-off-by: Frank Vanbever <frank.vanbever@essensium.com>

I think the solution is nice and good, well done. I remember thinking
about the issue a while ago, but couldn't come up with a simple way of
detecting the C library file name from the target. Testing this limited
hardcoded set of SONAMEs seems like a reasonable approach.

> +Upstream: https://github.com/ldx/python-iptables/pull/300

It has even been merged upstream, so this could even be replaced by a
link to the upstream commit now.

Thanks!

Thomas
diff mbox series

Patch

diff --git a/package/python-iptables/0001-Add-separate-mechanism-to-load-libc.patch b/package/python-iptables/0001-Add-separate-mechanism-to-load-libc.patch
new file mode 100644
index 0000000000..1a928b4235
--- /dev/null
+++ b/package/python-iptables/0001-Add-separate-mechanism-to-load-libc.patch
@@ -0,0 +1,90 @@ 
+From e3557528d7cdcdc2c579212be8837bc9b54635a4 Mon Sep 17 00:00:00 2001
+From: Frank Vanbever <frank.vanbever@essensium.com>
+Date: Thu, 20 Feb 2020 12:14:08 +0100
+Subject: [PATCH] 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.
+
+Upstream: https://github.com/ldx/python-iptables/pull/300
+
+Signed-off-by: Frank Vanbever <frank.vanbever@essensium.com>
+---
+ iptc/ip4tc.py   |  4 ++--
+ iptc/util.py    | 16 ++++++++++++++++
+ iptc/xtables.py |  4 ++--
+ 3 files changed, 20 insertions(+), 4 deletions(-)
+
+diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py
+index 4c5d690..4ddd2dc 100644
+--- a/iptc/ip4tc.py
++++ b/iptc/ip4tc.py
+@@ -9,7 +9,7 @@ import socket
+ import struct
+ import weakref
+ 
+-from .util import find_library, load_kernel
++from .util import find_library, load_kernel, find_libc
+ from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
+                       xt_align, xt_counters, xt_entry_target, xt_entry_match)
+ 
+@@ -26,7 +26,7 @@ if not hasattr(socket, 'IPPROTO_SCTP'):
+ 
+ _IFNAMSIZ = 16
+ 
+-_libc = ct.CDLL("libc.so.6")
++_libc = find_libc()
+ _get_errno_loc = _libc.__errno_location
+ _get_errno_loc.restype = ct.POINTER(ct.c_int)
+ _malloc = _libc.malloc
+diff --git a/iptc/util.py b/iptc/util.py
+index ae5fb9b..e6b1649 100644
+--- a/iptc/util.py
++++ b/iptc/util.py
+@@ -109,3 +109,19 @@ def find_library(*names):
+             major = int(m.group(1))
+         return lib, major
+     return None, None
++
++
++def find_libc():
++    lib = ctypes.util.find_library('c')
++    if lib is not None:
++        return ctypes.CDLL(lib, mode=ctypes.RTLD_GLOBAL)
++
++    libnames = ['libc.so.6', 'libc.so.0', 'libc.so']
++    for name in libnames:
++        try:
++            lib = ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL)
++            return lib
++        except:
++            pass
++
++    return None
+diff --git a/iptc/xtables.py b/iptc/xtables.py
+index 93bc080..cf21029 100644
+--- a/iptc/xtables.py
++++ b/iptc/xtables.py
+@@ -6,7 +6,7 @@ import sys
+ import weakref
+ 
+ from . import version
+-from .util import find_library
++from .util import find_library, find_libc
+ from .errors import *
+ 
+ XT_INV_PROTO = 0x40  # invert the sense of PROTO
+@@ -792,7 +792,7 @@ class xtables_target(ct.Union):
+                 ("v12", _xtables_target_v12)]
+ 
+ 
+-_libc, _ = find_library("c")
++_libc = find_libc()
+ _optind = ct.c_long.in_dll(_libc, "optind")
+ _optarg = ct.c_char_p.in_dll(_libc, "optarg")
+ 
+-- 
+2.20.1
+