Gentoo Archives: gentoo-portage-dev

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

Replies

Subject Author
[gentoo-portage-dev] Re: [PATCH] process: add _has_ipv6() function Mike Gilbert <floppym@g.o>