Skip to content

Commit f70dc84

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 28b65d3 commit f70dc84

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
@@ -609,14 +609,16 @@ func (c *Container) newInitProcess(p *Process, cmd *exec.Cmd, comm *processComm)
609609
}
610610

611611
init := &initProcess{
612-
cmd: cmd,
613-
comm: comm,
614-
manager: c.cgroupManager,
612+
containerProcess: containerProcess{
613+
cmd: cmd,
614+
comm: comm,
615+
manager: c.cgroupManager,
616+
config: c.newInitConfig(p),
617+
process: p,
618+
bootstrapData: data,
619+
container: c,
620+
},
615621
intelRdtManager: c.intelRdtManager,
616-
config: c.newInitConfig(p),
617-
container: c,
618-
process: p,
619-
bootstrapData: data,
620622
}
621623
c.initProcess = init
622624
return init, nil
@@ -632,15 +634,18 @@ func (c *Container) newSetnsProcess(p *Process, cmd *exec.Cmd, comm *processComm
632634
return nil, err
633635
}
634636
proc := &setnsProcess{
635-
cmd: cmd,
637+
containerProcess: containerProcess{
638+
cmd: cmd,
639+
comm: comm,
640+
manager: c.cgroupManager,
641+
config: c.newInitConfig(p),
642+
process: p,
643+
bootstrapData: data,
644+
container: c,
645+
},
636646
cgroupPaths: state.CgroupPaths,
637647
rootlessCgroups: c.config.RootlessCgroups,
638648
intelRdtPath: state.IntelRdtPath,
639-
comm: comm,
640-
manager: c.cgroupManager,
641-
config: c.newInitConfig(p),
642-
process: p,
643-
bootstrapData: data,
644649
initProcessPid: state.InitProcessPid,
645650
}
646651
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

@@ -318,60 +359,9 @@ func (p *setnsProcess) execSetns() error {
318359
return nil
319360
}
320361

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

377367
// getChildPid receives the final child's pid over the provided pipe.
@@ -789,27 +779,6 @@ func (p *initProcess) start() (retErr error) {
789779
return nil
790780
}
791781

792-
func (p *initProcess) wait() (*os.ProcessState, error) {
793-
err := p.cmd.Wait()
794-
return p.cmd.ProcessState, err
795-
}
796-
797-
func (p *initProcess) terminate() error {
798-
if p.cmd.Process == nil {
799-
return nil
800-
}
801-
err := p.cmd.Process.Kill()
802-
if _, werr := p.wait(); err == nil {
803-
err = werr
804-
}
805-
return err
806-
}
807-
808-
func (p *initProcess) startTime() (uint64, error) {
809-
stat, err := system.Stat(p.pid())
810-
return stat.StartTime, err
811-
}
812-
813782
func (p *initProcess) updateSpecState() error {
814783
s, err := p.container.currentOCIState()
815784
if err != nil {
@@ -837,22 +806,6 @@ func (p *initProcess) createNetworkInterfaces() error {
837806
return nil
838807
}
839808

840-
func (p *initProcess) signal(sig os.Signal) error {
841-
s, ok := sig.(unix.Signal)
842-
if !ok {
843-
return errors.New("os: unsupported signal type")
844-
}
845-
return unix.Kill(p.pid(), s)
846-
}
847-
848-
func (p *initProcess) setExternalDescriptors(newFds []string) {
849-
p.fds = newFds
850-
}
851-
852-
func (p *initProcess) forwardChildLogs() chan error {
853-
return logs.ForwardLogs(p.comm.logPipeParent)
854-
}
855-
856809
func pidGetFd(pid, srcFd int) (*os.File, error) {
857810
pidFd, err := unix.PidfdOpen(pid, 0)
858811
if err != nil {

0 commit comments

Comments
 (0)