77
88from . import version
99from .util import find_library
10+ from .errors import *
1011
1112XT_INV_PROTO = 0x40 # invert the sense of PROTO
1213
@@ -790,9 +791,6 @@ class xtables_target(ct.Union):
790791 ("v11" , _xtables_target_v11 ),
791792 ("v12" , _xtables_target_v12 )]
792793
793- class XTablesError (Exception ):
794- """Raised when an xtables call fails for some reason."""
795-
796794
797795_libc , _ = find_library ("c" )
798796_optind = ct .c_long .in_dll (_libc , "optind" )
@@ -806,13 +804,23 @@ class XTablesError(Exception):
806804_lib_xtables , xtables_version = find_library (_searchlib )
807805_xtables_libdir = os .getenv ("XTABLES_LIBDIR" )
808806if _xtables_libdir is None :
809- for xtdir in ["/lib/xtables" , "/lib64/xtables" , "/usr/lib/xtables" ,
810- "/usr/lib/iptables" , "/usr/lib64/xtables" ,
811- "/usr/lib64/iptables" , "/usr/local/lib/xtables" ,
812- "/usr/lib/x86_64-linux-gnu/xtables" ]:
813- if os .path .isdir (xtdir ):
814- _xtables_libdir = xtdir
815- break
807+ import re
808+ ldconfig_path_regex = re .compile ('^(/.*):$' )
809+ import subprocess
810+ ldconfig = subprocess .Popen (
811+ ('ldconfig' , '-N' , '-v' ),
812+ stdin = subprocess .DEVNULL , stdout = subprocess .PIPE , stderr = subprocess .DEVNULL , universal_newlines = True
813+ )
814+ ldconfig_out , ldconfig_err = ldconfig .communicate ()
815+ if ldconfig .returncode != 0 :
816+ raise XTablesError ("ldconfig failed, please set XTABLES_LIBDIR" )
817+ for ldconfig_out_line in ldconfig_out .splitlines ():
818+ ldconfig_path_regex_match = ldconfig_path_regex .match (ldconfig_out_line )
819+ if ldconfig_path_regex_match is not None :
820+ ldconfig_path = ldconfig_path_regex_match .group (1 )
821+ if os .path .isdir (ldconfig_path ):
822+ _xtables_libdir = ldconfig_path
823+ break
816824if _xtables_libdir is None :
817825 raise XTablesError ("can't find directory with extensions; "
818826 "please set XTABLES_LIBDIR" )
0 commit comments