Gentoo Archives: gentoo-portage-dev

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

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies