Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-portage-dev] [PATCH v2] process: Unshare UTS namespace, and set hostname to 'localhost'
Date: Sat, 28 Mar 2020 07:14:10
Message-Id: 20200328071342.8409-1-mgorny@gentoo.org
1 Use UTS namespace to override hostname when network-sandbox is enabled.
2 Set it to 'localhost' as that has a better chance of being present
3 in /etc/hosts. This fixes tests in some packages that try to connect
4 to localhost via hostname obtained using gethostname(), e.g. docker-py,
5 and suffer resolution problems due to the system hostname not being
6 defined in /etc/hosts.
7 ---
8 lib/portage/process.py | 11 ++++++++++-
9 1 file changed, 10 insertions(+), 1 deletion(-)
10
11 diff --git a/lib/portage/process.py b/lib/portage/process.py
12 index c1fc4bcf6..c48503208 100644
13 --- a/lib/portage/process.py
14 +++ b/lib/portage/process.py
15 @@ -348,12 +348,14 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False,
16 if unshare_net or unshare_ipc or unshare_mount or unshare_pid:
17 # from /usr/include/bits/sched.h
18 CLONE_NEWNS = 0x00020000
19 + CLONE_NEWUTS = 0x04000000
20 CLONE_NEWIPC = 0x08000000
21 CLONE_NEWPID = 0x20000000
22 CLONE_NEWNET = 0x40000000
23
24 if unshare_net:
25 - unshare_flags |= CLONE_NEWNET
26 + # UTS namespace to override hostname
27 + unshare_flags |= CLONE_NEWNET | CLONE_NEWUTS
28 if unshare_ipc:
29 unshare_flags |= CLONE_NEWIPC
30 if unshare_mount:
31 @@ -704,6 +706,13 @@ def _exec(binary, mycommand, opt_name, fd_pipes,
32 noiselevel=-1)
33 os._exit(1)
34 if unshare_net:
35 + # use 'localhost' to avoid hostname resolution problems
36 + try:
37 + socket.sethostname('localhost')
38 + except Exception as e:
39 + writemsg("Unable to set hostname: %s (for FEATURES=\"network-sandbox\")\n" % (
40 + e),
41 + noiselevel=-1)
42 _configure_loopback_interface()
43 except AttributeError:
44 # unshare() not supported by libc
45 --
46 2.26.0

Replies