From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 0E191138334 for ; Sat, 3 Aug 2019 23:38:07 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 2FE2CE0848; Sat, 3 Aug 2019 23:38:06 +0000 (UTC) Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 0962DE0848 for ; Sat, 3 Aug 2019 23:38:05 +0000 (UTC) Received: from mail-io1-f48.google.com (mail-io1-f48.google.com [209.85.166.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: floppym) by smtp.gentoo.org (Postfix) with ESMTPSA id D3280349455 for ; Sat, 3 Aug 2019 23:38:04 +0000 (UTC) Received: by mail-io1-f48.google.com with SMTP id m24so160139653ioo.2 for ; Sat, 03 Aug 2019 16:38:04 -0700 (PDT) X-Gm-Message-State: APjAAAVTn+nbb6bRteMk6/n5Rp70KxT6Por0q3379TGqJcCjfCNpW6QY kv0Z6+afN6zsbNKbwOtV9BFXNR/G5Jm9aIF5NWs= X-Google-Smtp-Source: APXvYqxscY6uE1g39w3ezYZ0DoG3kHW//OjT28ZncJum7stX2q2GQVNgGNZ2ONV2fNYZFX2MneDtQiNWc1Vdh+QWNUc= X-Received: by 2002:a6b:90c3:: with SMTP id s186mr2514428iod.114.1564875482984; Sat, 03 Aug 2019 16:38:02 -0700 (PDT) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-portage-dev@lists.gentoo.org Reply-to: gentoo-portage-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 References: <20190803193010.31179-1-zmedico@gentoo.org> In-Reply-To: <20190803193010.31179-1-zmedico@gentoo.org> From: Mike Gilbert Date: Sat, 3 Aug 2019 19:37:52 -0400 X-Gmail-Original-Message-ID: Message-ID: Subject: [gentoo-portage-dev] Re: [PATCH] process: add _has_ipv6() function To: Zac Medico Cc: gentoo-portage-dev@lists.gentoo.org Content-Type: text/plain; charset="UTF-8" X-Archives-Salt: dbc25f0d-8fe9-46ea-bb67-c7b9df43a1d3 X-Archives-Hash: f2b011b1aac604e85b4cd589f71b5258 On Sat, Aug 3, 2019 at 3:30 PM Zac Medico wrote: > > Add _has_ipv6() function and use it in _configure_loopback_interface() > to decide whether to add an IPv6 address. > > Bug: https://bugs.gentoo.org/691290 > --- > lib/portage/process.py | 40 +++++++++++++++++++++++++++++++++++++--- > 1 file changed, 37 insertions(+), 3 deletions(-) > > diff --git a/lib/portage/process.py b/lib/portage/process.py > index 690421815..ca8b0c172 100644 > --- a/lib/portage/process.py > +++ b/lib/portage/process.py > @@ -339,6 +339,9 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False, > fd_pipes[1] = pw > fd_pipes[2] = pw > > + # Cache _has_ipv6() result for use in child processes. > + _has_ipv6() > + > # This caches the libc library lookup and _unshare_validator results > # in the current process, so that results are cached for use in > # child processes. > @@ -446,6 +449,38 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False, > # Everything succeeded > return 0 > > +__has_ipv6 = None > + > +def _has_ipv6(): > + """ > + Test that both userland and kernel support IPv6, by attempting > + to create a socket and listen on any unused port of the IPv6 > + ::1 loopback address. > + > + @rtype: bool > + @return: True if IPv6 is supported, False otherwise. > + """ > + global __has_ipv6 > + > + if __has_ipv6 is None: > + if socket.has_ipv6: > + sock = None > + try: > + # python2.7 sockets do not support context management protocol > + sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) > + sock.bind(('::1', 0)) > + except EnvironmentError: > + __has_ipv6 = False > + else: > + __has_ipv6 = True > + finally: > + if sock is not None: > + sock.close() > + else: > + __has_ipv6 = False > + > + return __has_ipv6 > + > def _configure_loopback_interface(): > """ > Configure the loopback interface. > @@ -478,9 +513,8 @@ def _configure_loopback_interface(): > > try: > subprocess.call(['ip', 'address', 'add', '10.0.0.1/8', 'dev', 'lo']) > - with open(os.devnull, 'wb', 0) as devnull: > - subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo'], > - stdout=devnull, stderr=devnull) > + if _has_ipv6(): > + subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo']) > except EnvironmentError as e: > writemsg("Error calling 'ip': %s\n" % e.strerror, noiselevel=-1) > > -- > 2.21.0 > This seems reasonable, though I don't have an IPv6-less system to test it on.