Skip to content

Commit 76cabbc

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 76cabbc

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
@@ -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: 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
@@ -268,11 +268,12 @@ 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 s := l.config.SpecState; s != nil {
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
276+
}
276277
}
277278

278279
if l.dmzExe != nil {

0 commit comments

Comments
 (0)