Skip to content

Commit 2e5864c

Browse files
kolyshkinlifubang
andcommitted
libct: refactor setnsProcess.start
Factor startWithCgroupFD out of start to reduce the start complexity. This also implements a more future-proof way of calling p.comm.closeChild. Co-authored-by: lifubang <lifubang@acmcoder.com> Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent b061f0b commit 2e5864c

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

libcontainer/process_linux.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -352,32 +352,38 @@ func (p *setnsProcess) prepareCgroupFD() (*os.File, error) {
352352
return fd, nil
353353
}
354354

355-
func (p *setnsProcess) start() (retErr error) {
356-
defer p.comm.closeParent()
355+
// startWithCgroupFD starts a process via clone3 with CLONE_INTO_CGROUP,
356+
// with a fallback if it fails (e.g. not available).
357+
func (p *setnsProcess) startWithCgroupFD() error {
358+
// Close the child side of the pipes.
359+
defer p.comm.closeChild()
357360

358361
fd, err := p.prepareCgroupFD()
359362
if err != nil {
360-
p.comm.closeChild()
361363
return err
362364
}
363-
364-
// Get the "before" value of oom kill count.
365-
oom, _ := p.manager.OOMKillCount()
366-
367-
err = p.startWithCPUAffinity()
368365
if fd != nil {
369-
fd.Close()
366+
defer fd.Close()
370367
}
368+
369+
err = p.startWithCPUAffinity()
371370
if err != nil && p.cmd.SysProcAttr.UseCgroupFD {
372371
logrus.Debugf("exec with CLONE_INTO_CGROUP failed: %v; retrying without", err)
373372
// SysProcAttr.CgroupFD is never used when UseCgroupFD is unset.
374373
p.cmd.SysProcAttr.UseCgroupFD = false
375374
err = p.startWithCPUAffinity()
376375
}
377376

378-
// Close the child-side of the pipes (controlled by child).
379-
p.comm.closeChild()
380-
if err != nil {
377+
return err
378+
}
379+
380+
func (p *setnsProcess) start() (retErr error) {
381+
defer p.comm.closeParent()
382+
383+
// Get the "before" value of oom kill count.
384+
oom, _ := p.manager.OOMKillCount()
385+
386+
if err := p.startWithCgroupFD(); err != nil {
381387
return fmt.Errorf("error starting setns process: %w", err)
382388
}
383389

0 commit comments

Comments
 (0)