Skip to content

Commit 5a98014

Browse files
committed
libct: don't pass SpecState to init unless needed
SpecState field of initConfig is only needed to run hooks that are executed inside a container -- namely CreateContainer and StartContainer. If these hooks are not configured, there is no need to fill, marshal and unmarshal SpecState. While at it, inline updateSpecState as it is trivial and only has one user. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent e58e2ea commit 5a98014

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

libcontainer/process_linux.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,16 @@ func (p *initProcess) start() (retErr error) {
644644
if err := p.createNetworkInterfaces(); err != nil {
645645
return fmt.Errorf("error creating network interfaces: %w", err)
646646
}
647-
if err := p.updateSpecState(); err != nil {
648-
return fmt.Errorf("error updating spec state: %w", err)
647+
648+
// initConfig.SpecState is only needed to run hooks that are executed
649+
// inside a container, i.e. CreateContainer and StartContainer.
650+
if p.config.Config.HasHook(configs.CreateContainer, configs.StartContainer) {
651+
p.config.SpecState, err = p.container.currentOCIState()
652+
if err != nil {
653+
return fmt.Errorf("error getting current state: %w", err)
654+
}
649655
}
656+
650657
if err := utils.WriteJSON(p.comm.initSockParent, p.config); err != nil {
651658
return fmt.Errorf("error sending config to init process: %w", err)
652659
}
@@ -829,16 +836,6 @@ func (p *initProcess) startTime() (uint64, error) {
829836
return stat.StartTime, err
830837
}
831838

832-
func (p *initProcess) updateSpecState() error {
833-
s, err := p.container.currentOCIState()
834-
if err != nil {
835-
return err
836-
}
837-
838-
p.config.SpecState = s
839-
return nil
840-
}
841-
842839
func (p *initProcess) createNetworkInterfaces() error {
843840
for _, config := range p.config.Config.Networks {
844841
strategy, err := getStrategy(config.Type)

libcontainer/rootfs_linux.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,13 @@ func prepareRootfs(pipe *syncSocket, iConfig *initConfig) (err error) {
195195
return &os.PathError{Op: "chdir", Path: config.Rootfs, Err: err}
196196
}
197197

198-
s := iConfig.SpecState
199-
s.Pid = unix.Getpid()
200-
s.Status = specs.StateCreating
201-
if err := iConfig.Config.Hooks.Run(configs.CreateContainer, s); err != nil {
202-
return err
198+
if iConfig.Config.HasHook(configs.CreateContainer) {
199+
s := iConfig.SpecState
200+
s.Pid = unix.Getpid()
201+
s.Status = specs.StateCreating
202+
if err := iConfig.Config.Hooks.Run(configs.CreateContainer, s); err != nil {
203+
return err
204+
}
203205
}
204206

205207
if config.NoPivotRoot {

libcontainer/standard_init_linux.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,13 @@ func (l *linuxStandardInit) Init() error {
268268
// https://github.com/torvalds/linux/blob/v4.9/fs/exec.c#L1290-L1318
269269
_ = l.fifoFile.Close()
270270

271-
s := l.config.SpecState
272-
s.Pid = unix.Getpid()
273-
s.Status = specs.StateCreated
274-
if err := l.config.Config.Hooks.Run(configs.StartContainer, s); err != nil {
275-
return err
271+
if l.config.Config.HasHook(configs.StartContainer) {
272+
s := l.config.SpecState
273+
s.Pid = unix.Getpid()
274+
s.Status = specs.StateCreated
275+
if err := l.config.Config.Hooks.Run(configs.StartContainer, s); err != nil {
276+
return err
277+
}
276278
}
277279

278280
if l.dmzExe != nil {

0 commit comments

Comments
 (0)