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 3/3] Introduce a tiny init replacement for inside pid namespace
Date: Sun, 18 Nov 2018 08:54:03
Message-Id: 20181118085341.3835-3-mgorny@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH v2 1/3] Add FEATURES=mount-sandbox to take advantage of mount ns by "Michał Górny"
1 Signed-off-by: Michał Górny <mgorny@g.o>
2 ---
3 bin/pid-ns-init | 25 +++++++++++++++++++++++++
4 lib/portage/process.py | 11 ++++++-----
5 2 files changed, 31 insertions(+), 5 deletions(-)
6 create mode 100644 bin/pid-ns-init
7
8 diff --git a/bin/pid-ns-init b/bin/pid-ns-init
9 new file mode 100644
10 index 000000000..90660571a
11 --- /dev/null
12 +++ b/bin/pid-ns-init
13 @@ -0,0 +1,25 @@
14 +#!/usr/bin/env python
15 +# Copyright 2018 Gentoo Authors
16 +# Distributed under the terms of the GNU General Public License v2
17 +
18 +import os
19 +import sys
20 +
21 +
22 +def main(argv):
23 + if len(argv) < 2:
24 + return 'Usage: {} <main-child-pid>'.format(argv[0])
25 + main_child_pid = int(argv[1])
26 +
27 + # wait for child processes
28 + while True:
29 + pid, status = os.wait()
30 + if pid == main_child_pid:
31 + return os.WEXITSTATUS(status)
32 +
33 + # this should never be reached
34 + return 127
35 +
36 +
37 +if __name__ == '__main__':
38 + sys.exit(main(sys.argv))
39 diff --git a/lib/portage/process.py b/lib/portage/process.py
40 index dee126c3c..75ec299f0 100644
41 --- a/lib/portage/process.py
42 +++ b/lib/portage/process.py
43 @@ -544,13 +544,14 @@ def _exec(binary, mycommand, opt_name, fd_pipes, env, gid, groups, uid, umask,
44 else:
45 if unshare_pid:
46 # pid namespace requires us to become init
47 - # TODO: do init-ty stuff
48 - # therefore, fork() ASAP
49 fork_ret = os.fork()
50 if fork_ret != 0:
51 - pid, status = os.waitpid(fork_ret, 0)
52 - assert pid == fork_ret
53 - os._exit(status)
54 + os.execv(portage._python_interpreter, [
55 + portage._python_interpreter,
56 + os.path.join(portage._bin_path,
57 + 'pid-ns-init'),
58 + '%s' % fork_ret,
59 + ])
60 if unshare_mount:
61 # mark the whole filesystem as slave to avoid
62 # mounts escaping the namespace
63 --
64 2.19.1

Replies