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