Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor init and setns process #4312

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,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 @@ -689,15 +691,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
kolyshkin marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -808,27 +798,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 @@ -856,22 +825,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
Loading