Skip to content

Commit fd2e81a

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 57798c6 commit fd2e81a

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

668668
init := &initProcess{
669-
cmd: cmd,
670-
comm: comm,
671-
manager: c.cgroupManager,
669+
containerProcess: containerProcess{
670+
cmd: cmd,
671+
comm: comm,
672+
manager: c.cgroupManager,
673+
config: c.newInitConfig(p),
674+
process: p,
675+
bootstrapData: data,
676+
container: c,
677+
},
672678
intelRdtManager: c.intelRdtManager,
673-
config: c.newInitConfig(p),
674-
container: c,
675-
process: p,
676-
bootstrapData: data,
677679
}
678680
c.initProcess = init
679681
return init, nil
@@ -689,15 +691,18 @@ func (c *Container) newSetnsProcess(p *Process, cmd *exec.Cmd, comm *processComm
689691
return nil, err
690692
}
691693
proc := &setnsProcess{
692-
cmd: cmd,
694+
containerProcess: containerProcess{
695+
cmd: cmd,
696+
comm: comm,
697+
manager: c.cgroupManager,
698+
config: c.newInitConfig(p),
699+
process: p,
700+
bootstrapData: data,
701+
container: c,
702+
},
693703
cgroupPaths: state.CgroupPaths,
694704
rootlessCgroups: c.config.RootlessCgroups,
695705
intelRdtPath: state.IntelRdtPath,
696-
comm: comm,
697-
manager: c.cgroupManager,
698-
config: c.newInitConfig(p),
699-
process: p,
700-
bootstrapData: data,
701706
initProcessPid: state.InitProcessPid,
702707
}
703708
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.
@@ -808,27 +798,6 @@ func (p *initProcess) start() (retErr error) {
808798
return nil
809799
}
810800

811-
func (p *initProcess) wait() (*os.ProcessState, error) {
812-
err := p.cmd.Wait()
813-
return p.cmd.ProcessState, err
814-
}
815-
816-
func (p *initProcess) terminate() error {
817-
if p.cmd.Process == nil {
818-
return nil
819-
}
820-
err := p.cmd.Process.Kill()
821-
if _, werr := p.wait(); err == nil {
822-
err = werr
823-
}
824-
return err
825-
}
826-
827-
func (p *initProcess) startTime() (uint64, error) {
828-
stat, err := system.Stat(p.pid())
829-
return stat.StartTime, err
830-
}
831-
832801
func (p *initProcess) updateSpecState() error {
833802
s, err := p.container.currentOCIState()
834803
if err != nil {
@@ -856,22 +825,6 @@ func (p *initProcess) createNetworkInterfaces() error {
856825
return nil
857826
}
858827

859-
func (p *initProcess) signal(sig os.Signal) error {
860-
s, ok := sig.(unix.Signal)
861-
if !ok {
862-
return errors.New("os: unsupported signal type")
863-
}
864-
return unix.Kill(p.pid(), s)
865-
}
866-
867-
func (p *initProcess) setExternalDescriptors(newFds []string) {
868-
p.fds = newFds
869-
}
870-
871-
func (p *initProcess) forwardChildLogs() chan error {
872-
return logs.ForwardLogs(p.comm.logPipeParent)
873-
}
874-
875828
func pidGetFd(pid, srcFd int) (*os.File, error) {
876829
pidFd, err := unix.PidfdOpen(pid, 0)
877830
if err != nil {

0 commit comments

Comments
 (0)