From 708d0027db547a0128e8a5ee3dbde61011bfde2b Mon Sep 17 00:00:00 2001 From: lfbzhm Date: Thu, 6 Jun 2024 15:22:55 +0000 Subject: [PATCH] refactor init and setns process Introduce a common parent struct `containerProcess`, let both `initProcess` and `setnsProcess` are inherited from it. Signed-off-by: lfbzhm --- libcontainer/container_linux.go | 31 ++++--- libcontainer/process_linux.go | 159 +++++++++++--------------------- 2 files changed, 74 insertions(+), 116 deletions(-) diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index e3e2cf2bdb8..2284342bdb4 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -651,14 +651,16 @@ func (c *Container) newInitProcess(p *Process, cmd *exec.Cmd, comm *processComm) } init := &initProcess{ - cmd: cmd, - comm: comm, - manager: c.cgroupManager, + containerProcess: containerProcess{ + cmd: cmd, + comm: comm, + manager: c.cgroupManager, + config: c.newInitConfig(p), + process: p, + bootstrapData: data, + container: c, + }, intelRdtManager: c.intelRdtManager, - config: c.newInitConfig(p), - container: c, - process: p, - bootstrapData: data, } c.initProcess = init return init, nil @@ -677,15 +679,18 @@ func (c *Container) newSetnsProcess(p *Process, cmd *exec.Cmd, comm *processComm return nil, err } proc := &setnsProcess{ - cmd: cmd, + containerProcess: containerProcess{ + cmd: cmd, + comm: comm, + manager: c.cgroupManager, + config: c.newInitConfig(p), + process: p, + bootstrapData: data, + container: c, + }, cgroupPaths: state.CgroupPaths, rootlessCgroups: c.config.RootlessCgroups, intelRdtPath: state.IntelRdtPath, - comm: comm, - manager: c.cgroupManager, - config: c.newInitConfig(p), - process: p, - bootstrapData: data, initProcessPid: state.InitProcessPid, } if len(p.SubCgroupPaths) > 0 { diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index 9a2473f716e..cb63af2d191 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -95,26 +95,27 @@ func (c *processComm) closeParent() { // c.logPipeParent is kept alive for ForwardLogs } -type setnsProcess struct { - cmd *exec.Cmd - comm *processComm - cgroupPaths map[string]string - rootlessCgroups bool - manager cgroups.Manager - intelRdtPath string - config *initConfig - fds []string - process *Process - bootstrapData io.Reader - initProcessPid int +type containerProcess struct { + cmd *exec.Cmd + comm *processComm + config *initConfig + manager cgroups.Manager + fds []string + process *Process + bootstrapData io.Reader + container *Container +} + +func (p *containerProcess) pid() int { + return p.cmd.Process.Pid } -func (p *setnsProcess) startTime() (uint64, error) { +func (p *containerProcess) startTime() (uint64, error) { stat, err := system.Stat(p.pid()) return stat.StartTime, err } -func (p *setnsProcess) signal(sig os.Signal) error { +func (p *containerProcess) signal(sig os.Signal) error { s, ok := sig.(unix.Signal) if !ok { return errors.New("os: unsupported signal type") @@ -122,6 +123,46 @@ func (p *setnsProcess) signal(sig os.Signal) error { return unix.Kill(p.pid(), s) } +func (p *containerProcess) externalDescriptors() []string { + return p.fds +} + +func (p *containerProcess) setExternalDescriptors(newFds []string) { + p.fds = newFds +} + +func (p *containerProcess) forwardChildLogs() chan error { + return logs.ForwardLogs(p.comm.logPipeParent) +} + +// terminate sends a SIGKILL to the forked process for the setns routine then waits to +// avoid the process becoming a zombie. +func (p *containerProcess) terminate() error { + if p.cmd.Process == nil { + return nil + } + err := p.cmd.Process.Kill() + if _, werr := p.wait(); err == nil { + err = werr + } + return err +} + +func (p *containerProcess) wait() (*os.ProcessState, error) { //nolint:unparam + err := p.cmd.Wait() + + // Return actual ProcessState even on Wait error + return p.cmd.ProcessState, err +} + +type setnsProcess struct { + containerProcess + cgroupPaths map[string]string + rootlessCgroups bool + intelRdtPath string + initProcessPid int +} + func (p *setnsProcess) start() (retErr error) { defer p.comm.closeParent() @@ -327,60 +368,9 @@ func (p *setnsProcess) execSetns() error { return nil } -// terminate sends a SIGKILL to the forked process for the setns routine then waits to -// avoid the process becoming a zombie. -func (p *setnsProcess) terminate() error { - if p.cmd.Process == nil { - return nil - } - err := p.cmd.Process.Kill() - if _, werr := p.wait(); err == nil { - err = werr - } - return err -} - -func (p *setnsProcess) wait() (*os.ProcessState, error) { - err := p.cmd.Wait() - - // Return actual ProcessState even on Wait error - return p.cmd.ProcessState, err -} - -func (p *setnsProcess) pid() int { - return p.cmd.Process.Pid -} - -func (p *setnsProcess) externalDescriptors() []string { - return p.fds -} - -func (p *setnsProcess) setExternalDescriptors(newFds []string) { - p.fds = newFds -} - -func (p *setnsProcess) forwardChildLogs() chan error { - return logs.ForwardLogs(p.comm.logPipeParent) -} - type initProcess struct { - cmd *exec.Cmd - comm *processComm - config *initConfig - manager cgroups.Manager + containerProcess intelRdtManager *intelrdt.Manager - container *Container - fds []string - process *Process - bootstrapData io.Reader -} - -func (p *initProcess) pid() int { - return p.cmd.Process.Pid -} - -func (p *initProcess) externalDescriptors() []string { - return p.fds } // getChildPid receives the final child's pid over the provided pipe. @@ -797,27 +787,6 @@ func (p *initProcess) start() (retErr error) { return nil } -func (p *initProcess) wait() (*os.ProcessState, error) { - err := p.cmd.Wait() - return p.cmd.ProcessState, err -} - -func (p *initProcess) terminate() error { - if p.cmd.Process == nil { - return nil - } - err := p.cmd.Process.Kill() - if _, werr := p.wait(); err == nil { - err = werr - } - return err -} - -func (p *initProcess) startTime() (uint64, error) { - stat, err := system.Stat(p.pid()) - return stat.StartTime, err -} - func (p *initProcess) updateSpecState() error { s, err := p.container.currentOCIState() if err != nil { @@ -845,22 +814,6 @@ func (p *initProcess) createNetworkInterfaces() error { return nil } -func (p *initProcess) signal(sig os.Signal) error { - s, ok := sig.(unix.Signal) - if !ok { - return errors.New("os: unsupported signal type") - } - return unix.Kill(p.pid(), s) -} - -func (p *initProcess) setExternalDescriptors(newFds []string) { - p.fds = newFds -} - -func (p *initProcess) forwardChildLogs() chan error { - return logs.ForwardLogs(p.comm.logPipeParent) -} - func pidGetFd(pid, srcFd int) (*os.File, error) { pidFd, err := unix.PidfdOpen(pid, 0) if err != nil {