Skip to content

Commit 1314b45

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 e3c1336 commit 1314b45

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
@@ -625,9 +625,16 @@ func (p *initProcess) start() (retErr error) {
625625
if err := p.createNetworkInterfaces(); err != nil {
626626
return fmt.Errorf("error creating network interfaces: %w", err)
627627
}
628-
if err := p.updateSpecState(); err != nil {
629-
return fmt.Errorf("error updating spec state: %w", err)
628+
629+
// initConfig.SpecState is only needed to run hooks that are executed
630+
// inside a container, i.e. CreateContainer and StartContainer.
631+
if p.config.Config.HasHook(configs.CreateContainer, configs.StartContainer) {
632+
p.config.SpecState, err = p.container.currentOCIState()
633+
if err != nil {
634+
return fmt.Errorf("error getting current state: %w", err)
635+
}
630636
}
637+
631638
if err := utils.WriteJSON(p.comm.initSockParent, p.config); err != nil {
632639
return fmt.Errorf("error sending config to init process: %w", err)
633640
}
@@ -810,16 +817,6 @@ func (p *initProcess) startTime() (uint64, error) {
810817
return stat.StartTime, err
811818
}
812819

813-
func (p *initProcess) updateSpecState() error {
814-
s, err := p.container.currentOCIState()
815-
if err != nil {
816-
return err
817-
}
818-
819-
p.config.SpecState = s
820-
return nil
821-
}
822-
823820
func (p *initProcess) createNetworkInterfaces() error {
824821
for _, config := range p.config.Config.Networks {
825822
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)