1 |
On Sat, Aug 3, 2019 at 3:30 PM Zac Medico <zmedico@g.o> wrote: |
2 |
> |
3 |
> Add _has_ipv6() function and use it in _configure_loopback_interface() |
4 |
> to decide whether to add an IPv6 address. |
5 |
> |
6 |
> Bug: https://bugs.gentoo.org/691290 |
7 |
> --- |
8 |
> lib/portage/process.py | 40 +++++++++++++++++++++++++++++++++++++--- |
9 |
> 1 file changed, 37 insertions(+), 3 deletions(-) |
10 |
> |
11 |
> diff --git a/lib/portage/process.py b/lib/portage/process.py |
12 |
> index 690421815..ca8b0c172 100644 |
13 |
> --- a/lib/portage/process.py |
14 |
> +++ b/lib/portage/process.py |
15 |
> @@ -339,6 +339,9 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False, |
16 |
> fd_pipes[1] = pw |
17 |
> fd_pipes[2] = pw |
18 |
> |
19 |
> + # Cache _has_ipv6() result for use in child processes. |
20 |
> + _has_ipv6() |
21 |
> + |
22 |
> # This caches the libc library lookup and _unshare_validator results |
23 |
> # in the current process, so that results are cached for use in |
24 |
> # child processes. |
25 |
> @@ -446,6 +449,38 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False, |
26 |
> # Everything succeeded |
27 |
> return 0 |
28 |
> |
29 |
> +__has_ipv6 = None |
30 |
> + |
31 |
> +def _has_ipv6(): |
32 |
> + """ |
33 |
> + Test that both userland and kernel support IPv6, by attempting |
34 |
> + to create a socket and listen on any unused port of the IPv6 |
35 |
> + ::1 loopback address. |
36 |
> + |
37 |
> + @rtype: bool |
38 |
> + @return: True if IPv6 is supported, False otherwise. |
39 |
> + """ |
40 |
> + global __has_ipv6 |
41 |
> + |
42 |
> + if __has_ipv6 is None: |
43 |
> + if socket.has_ipv6: |
44 |
> + sock = None |
45 |
> + try: |
46 |
> + # python2.7 sockets do not support context management protocol |
47 |
> + sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) |
48 |
> + sock.bind(('::1', 0)) |
49 |
> + except EnvironmentError: |
50 |
> + __has_ipv6 = False |
51 |
> + else: |
52 |
> + __has_ipv6 = True |
53 |
> + finally: |
54 |
> + if sock is not None: |
55 |
> + sock.close() |
56 |
> + else: |
57 |
> + __has_ipv6 = False |
58 |
> + |
59 |
> + return __has_ipv6 |
60 |
> + |
61 |
> def _configure_loopback_interface(): |
62 |
> """ |
63 |
> Configure the loopback interface. |
64 |
> @@ -478,9 +513,8 @@ def _configure_loopback_interface(): |
65 |
> |
66 |
> try: |
67 |
> subprocess.call(['ip', 'address', 'add', '10.0.0.1/8', 'dev', 'lo']) |
68 |
> - with open(os.devnull, 'wb', 0) as devnull: |
69 |
> - subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo'], |
70 |
> - stdout=devnull, stderr=devnull) |
71 |
> + if _has_ipv6(): |
72 |
> + subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo']) |
73 |
> except EnvironmentError as e: |
74 |
> writemsg("Error calling 'ip': %s\n" % e.strerror, noiselevel=-1) |
75 |
> |
76 |
> -- |
77 |
> 2.21.0 |
78 |
> |
79 |
|
80 |
This seems reasonable, though I don't have an IPv6-less system to test it on. |