Skip to content

Commit 93091e6

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 8afeb58 commit 93091e6

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

libcontainer/process_linux.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,16 @@ func (p *initProcess) start() (retErr error) {
615615
if err := p.createNetworkInterfaces(); err != nil {
616616
return fmt.Errorf("error creating network interfaces: %w", err)
617617
}
618-
if err := p.updateSpecState(); err != nil {
619-
return fmt.Errorf("error updating spec state: %w", err)
618+
619+
// initConfig.SpecState is only needed to run hooks that are executed
620+
// inside a container, i.e. CreateContainer and StartContainer.
621+
if p.config.Config.HasHook(configs.CreateContainer, configs.StartContainer) {
622+
p.config.SpecState, err = p.container.currentOCIState()
623+
if err != nil {
624+
return fmt.Errorf("error getting current state: %w", err)
625+
}
620626
}
627+
621628
if err := utils.WriteJSON(p.comm.initSockParent, p.config); err != nil {
622629
return fmt.Errorf("error sending config to init process: %w", err)
623630
}
@@ -779,16 +786,6 @@ func (p *initProcess) start() (retErr error) {
779786
return nil
780787
}
781788

782-
func (p *initProcess) updateSpecState() error {
783-
s, err := p.container.currentOCIState()
784-
if err != nil {
785-
return err
786-
}
787-
788-
p.config.SpecState = s
789-
return nil
790-
}
791-
792789
func (p *initProcess) createNetworkInterfaces() error {
793790
for _, config := range p.config.Config.Networks {
794791
strategy, err := getStrategy(config.Type)

libcontainer/rootfs_linux.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,12 @@ 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 s := iConfig.SpecState; s != nil {
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
203+
}
203204
}
204205

205206
if config.NoPivotRoot {

libcontainer/standard_init_linux.go

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

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

277278
// Close all file descriptors we are not passing to the container. This is

0 commit comments

Comments
 (0)