0001-Add-separate-mechanism-to-load-libc.patch 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. From e3557528d7cdcdc2c579212be8837bc9b54635a4 Mon Sep 17 00:00:00 2001
  2. From: Frank Vanbever <frank.vanbever@essensium.com>
  3. Date: Thu, 20 Feb 2020 12:14:08 +0100
  4. Subject: [PATCH] Add separate mechanism to load libc
  5. ctypes.util.find_library() always returns None for systems which do not have the
  6. tools installed to determine the location of a given shared library (i.e.
  7. ldconfig, gcc, objdump). If find_libary() fails attempt to load known libc by
  8. SONAME.
  9. Upstream: https://github.com/ldx/python-iptables/commit/e3557528d7cdcdc2c579212be8837bc9b54635a4
  10. Signed-off-by: Frank Vanbever <frank.vanbever@essensium.com>
  11. ---
  12. iptc/ip4tc.py | 4 ++--
  13. iptc/util.py | 16 ++++++++++++++++
  14. iptc/xtables.py | 4 ++--
  15. 3 files changed, 20 insertions(+), 4 deletions(-)
  16. diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py
  17. index 4c5d690..4ddd2dc 100644
  18. --- a/iptc/ip4tc.py
  19. +++ b/iptc/ip4tc.py
  20. @@ -9,7 +9,7 @@ import socket
  21. import struct
  22. import weakref
  23. -from .util import find_library, load_kernel
  24. +from .util import find_library, load_kernel, find_libc
  25. from .xtables import (XT_INV_PROTO, NFPROTO_IPV4, XTablesError, xtables,
  26. xt_align, xt_counters, xt_entry_target, xt_entry_match)
  27. @@ -26,7 +26,7 @@ if not hasattr(socket, 'IPPROTO_SCTP'):
  28. _IFNAMSIZ = 16
  29. -_libc = ct.CDLL("libc.so.6")
  30. +_libc = find_libc()
  31. _get_errno_loc = _libc.__errno_location
  32. _get_errno_loc.restype = ct.POINTER(ct.c_int)
  33. _malloc = _libc.malloc
  34. diff --git a/iptc/util.py b/iptc/util.py
  35. index ae5fb9b..e6b1649 100644
  36. --- a/iptc/util.py
  37. +++ b/iptc/util.py
  38. @@ -109,3 +109,19 @@ def find_library(*names):
  39. major = int(m.group(1))
  40. return lib, major
  41. return None, None
  42. +
  43. +
  44. +def find_libc():
  45. + lib = ctypes.util.find_library('c')
  46. + if lib is not None:
  47. + return ctypes.CDLL(lib, mode=ctypes.RTLD_GLOBAL)
  48. +
  49. + libnames = ['libc.so.6', 'libc.so.0', 'libc.so']
  50. + for name in libnames:
  51. + try:
  52. + lib = ctypes.CDLL(name, mode=ctypes.RTLD_GLOBAL)
  53. + return lib
  54. + except:
  55. + pass
  56. +
  57. + return None
  58. diff --git a/iptc/xtables.py b/iptc/xtables.py
  59. index 93bc080..cf21029 100644
  60. --- a/iptc/xtables.py
  61. +++ b/iptc/xtables.py
  62. @@ -6,7 +6,7 @@ import sys
  63. import weakref
  64. from . import version
  65. -from .util import find_library
  66. +from .util import find_library, find_libc
  67. from .errors import *
  68. XT_INV_PROTO = 0x40 # invert the sense of PROTO
  69. @@ -792,7 +792,7 @@ class xtables_target(ct.Union):
  70. ("v12", _xtables_target_v12)]
  71. -_libc, _ = find_library("c")
  72. +_libc = find_libc()
  73. _optind = ct.c_long.in_dll(_libc, "optind")
  74. _optarg = ct.c_char_p.in_dll(_libc, "optarg")
  75. --
  76. 2.20.1