Skip to content

Commit e04ae8c

Browse files
committed
refactor init and setns process
Introduce a common parent struct `containerProcess`, let both `initProcess` and `setnsProcess` are inherited from it. Signed-off-by: lfbzhm <[email protected]>
1 parent ab010ae commit e04ae8c

File tree

2 files changed

+74
-116
lines changed

2 files changed

+74
-116
lines changed

libcontainer/container_linux.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,16 @@ func (c *Container) newInitProcess(p *Process, cmd *exec.Cmd, comm *processComm)
651651
}
652652

653653
init := &initProcess{
654-
cmd: cmd,
655-
comm: comm,
656-
manager: c.cgroupManager,
654+
containerProcess: containerProcess{
655+
cmd: cmd,
656+
comm: comm,
657+
manager: c.cgroupManager,
658+
config: c.newInitConfig(p),
659+
process: p,
660+
bootstrapData: data,
661+
container: c,
662+
},
657663
intelRdtManager: c.intelRdtManager,
658-
config: c.newInitConfig(p),
659-
container: c,
660-
process: p,
661-
bootstrapData: data,
662664
}
663665
c.initProcess = init
664666
return init, nil
@@ -677,15 +679,18 @@ func (c *Container) newSetnsProcess(p *Process, cmd *exec.Cmd, comm *processComm
677679
return nil, err
678680
}
679681
proc := &setnsProcess{
680-
cmd: cmd,
682+
containerProcess: containerProcess{
683+
cmd: cmd,
684+
comm: comm,
685+
manager: c.cgroupManager,
686+
config: c.newInitConfig(p),
687+
process: p,
688+
bootstrapData: data,
689+
container: c,
690+
},
681691
cgroupPaths: state.CgroupPaths,
682692
rootlessCgroups: c.config.RootlessCgroups,
683693
intelRdtPath: state.IntelRdtPath,
684-
comm: comm,
685-
manager: c.cgroupManager,
686-
config: c.newInitConfig(p),
687-
process: p,
688-
bootstrapData: data,
689694
initProcessPid: state.InitProcessPid,
690695
}
691696
if len(p.SubCgroupPaths) > 0 {

libcontainer/process_linux.go

Lines changed: 56 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,74 @@ func (c *processComm) closeParent() {
9595
// c.logPipeParent is kept alive for ForwardLogs
9696
}
9797

98-
type setnsProcess struct {
99-
cmd *exec.Cmd
100-
comm *processComm
101-
cgroupPaths map[string]string
102-
rootlessCgroups bool
103-
manager cgroups.Manager
104-
intelRdtPath string
105-
config *initConfig
106-
fds []string
107-
process *Process
108-
bootstrapData io.Reader
109-
initProcessPid int
98+
type containerProcess struct {
99+
cmd *exec.Cmd
100+
comm *processComm
101+
config *initConfig
102+
manager cgroups.Manager
103+
fds []string
104+
process *Process
105+
bootstrapData io.Reader
106+
container *Container
107+
}
108+
109+
func (p *containerProcess) pid() int {
110+
return p.cmd.Process.Pid
110111
}
111112

112-
func (p *setnsProcess) startTime() (uint64, error) {
113+
func (p *containerProcess) startTime() (uint64, error) {
113114
stat, err := system.Stat(p.pid())
114115
return stat.StartTime, err
115116
}
116117

117-
func (p *setnsProcess) signal(sig os.Signal) error {
118+
func (p *containerProcess) signal(sig os.Signal) error {
118119
s, ok := sig.(unix.Signal)
119120
if !ok {
120121
return errors.New("os: unsupported signal type")
121122
}
122123
return unix.Kill(p.pid(), s)
123124
}
124125

126+
func (p *containerProcess) externalDescriptors() []string {
127+
return p.fds
128+
}
129+
130+
func (p *containerProcess) setExternalDescriptors(newFds []string) {
131+
p.fds = newFds
132+
}
133+
134+
func (p *containerProcess) forwardChildLogs() chan error {
135+
return logs.ForwardLogs(p.comm.logPipeParent)
136+
}
137+
138+
// terminate sends a SIGKILL to the forked process for the setns routine then waits to
139+
// avoid the process becoming a zombie.
140+
func (p *containerProcess) terminate() error {
141+
if p.cmd.Process == nil {
142+
return nil
143+
}
144+
err := p.cmd.Process.Kill()
145+
if _, werr := p.wait(); err == nil {
146+
err = werr
147+
}
148+
return err
149+
}
150+
151+
func (p *containerProcess) wait() (*os.ProcessState, error) { //nolint:unparam
152+
err := p.cmd.Wait()
153+
154+
// Return actual ProcessState even on Wait error
155+
return p.cmd.ProcessState, err
156+
}
157+
158+
type setnsProcess struct {
159+
containerProcess
160+
cgroupPaths map[string]string
161+
rootlessCgroups bool
162+
intelRdtPath string
163+
initProcessPid int
164+
}
165+
125166
func (p *setnsProcess) start() (retErr error) {
126167
defer p.comm.closeParent()
127168

@@ -327,60 +368,9 @@ func (p *setnsProcess) execSetns() error {
327368
return nil
328369
}
329370

330-
// terminate sends a SIGKILL to the forked process for the setns routine then waits to
331-
// avoid the process becoming a zombie.
332-
func (p *setnsProcess) terminate() error {
333-
if p.cmd.Process == nil {
334-
return nil
335-
}
336-
err := p.cmd.Process.Kill()
337-
if _, werr := p.wait(); err == nil {
338-
err = werr
339-
}
340-
return err
341-
}
342-
343-
func (p *setnsProcess) wait() (*os.ProcessState, error) {
344-
err := p.cmd.Wait()
345-
346-
// Return actual ProcessState even on Wait error
347-
return p.cmd.ProcessState, err
348-
}
349-
350-
func (p *setnsProcess) pid() int {
351-
return p.cmd.Process.Pid
352-
}
353-
354-
func (p *setnsProcess) externalDescriptors() []string {
355-
return p.fds
356-
}
357-
358-
func (p *setnsProcess) setExternalDescriptors(newFds []string) {
359-
p.fds = newFds
360-
}
361-
362-
func (p *setnsProcess) forwardChildLogs() chan error {
363-
return logs.ForwardLogs(p.comm.logPipeParent)
364-
}
365-
366371
type initProcess struct {
367-
cmd *exec.Cmd
368-
comm *processComm
369-
config *initConfig
370-
manager cgroups.Manager
372+
containerProcess
371373
intelRdtManager *intelrdt.Manager
372-
container *Container
373-
fds []string
374-
process *Process
375-
bootstrapData io.Reader
376-
}
377-
378-
func (p *initProcess) pid() int {
379-
return p.cmd.Process.Pid
380-
}
381-
382-
func (p *initProcess) externalDescriptors() []string {
383-
return p.fds
384374
}
385375

386376
// getChildPid receives the final child's pid over the provided pipe.
@@ -797,27 +787,6 @@ func (p *initProcess) start() (retErr error) {
797787
return nil
798788
}
799789

800-
func (p *initProcess) wait() (*os.ProcessState, error) {
801-
err := p.cmd.Wait()
802-
return p.cmd.ProcessState, err
803-
}
804-
805-
func (p *initProcess) terminate() error {
806-
if p.cmd.Process == nil {
807-
return nil
808-
}
809-
err := p.cmd.Process.Kill()
810-
if _, werr := p.wait(); err == nil {
811-
err = werr
812-
}
813-
return err
814-
}
815-
816-
func (p *initProcess) startTime() (uint64, error) {
817-
stat, err := system.Stat(p.pid())
818-
return stat.StartTime, err
819-
}
820-
821790
func (p *initProcess) updateSpecState() error {
822791
s, err := p.container.currentOCIState()
823792
if err != nil {
@@ -845,22 +814,6 @@ func (p *initProcess) createNetworkInterfaces() error {
845814
return nil
846815
}
847816

848-
func (p *initProcess) signal(sig os.Signal) error {
849-
s, ok := sig.(unix.Signal)
850-
if !ok {
851-
return errors.New("os: unsupported signal type")
852-
}
853-
return unix.Kill(p.pid(), s)
854-
}
855-
856-
func (p *initProcess) setExternalDescriptors(newFds []string) {
857-
p.fds = newFds
858-
}
859-
860-
func (p *initProcess) forwardChildLogs() chan error {
861-
return logs.ForwardLogs(p.comm.logPipeParent)
862-
}
863-
864817
func pidGetFd(pid, srcFd int) (*os.File, error) {
865818
pidFd, err := unix.PidfdOpen(pid, 0)
866819
if err != nil {

0 commit comments

Comments
 (0)