Skip to content

Commit

Permalink
refactor init and setns process
Browse files Browse the repository at this point in the history
Introduce a common parent struct `containerProcess`,
let both `initProcess` and `setnsProcess` are inherited
from it.

Signed-off-by: lfbzhm <lifubang@acmcoder.com>
  • Loading branch information
lifubang committed Jul 23, 2024
1 parent 3778ae6 commit 708d002
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 116 deletions.
31 changes: 18 additions & 13 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
159 changes: 56 additions & 103 deletions libcontainer/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,33 +95,74 @@ 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")
}
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()

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 708d002

Please sign in to comment.