Skip to content

Commit fd932ec

Browse files
authored
Merge pull request #4433 from kolyshkin/hasHook
libct: add/use configs.HasHook
2 parents 2e906e2 + 93091e6 commit fd932ec

File tree

6 files changed

+37
-25
lines changed

6 files changed

+37
-25
lines changed

libcontainer/configs/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ const (
332332
Poststop HookName = "poststop"
333333
)
334334

335+
// HasHook checks if config has any hooks with any given names configured.
336+
func (c *Config) HasHook(names ...HookName) bool {
337+
if c.Hooks == nil {
338+
return false
339+
}
340+
for _, h := range names {
341+
if len(c.Hooks[h]) > 0 {
342+
return true
343+
}
344+
}
345+
return false
346+
}
347+
335348
// KnownHookNames returns the known hook names.
336349
// Used by `runc features`.
337350
func KnownHookNames() []string {

libcontainer/container_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func (c *Container) start(process *Process) (retErr error) {
350350

351351
if process.Init {
352352
c.fifo.Close()
353-
if c.config.Hooks != nil {
353+
if c.config.HasHook(configs.Poststart) {
354354
s, err := c.currentOCIState()
355355
if err != nil {
356356
return err

libcontainer/criu_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ func (c *Container) criuNotifications(resp *criurpc.CriuResp, process *Process,
11131113
return err
11141114
}
11151115
case "setup-namespaces":
1116-
if c.config.Hooks != nil {
1116+
if c.config.HasHook(configs.Prestart, configs.CreateRuntime) {
11171117
s, err := c.currentOCIState()
11181118
if err != nil {
11191119
return nil

libcontainer/process_linux.go

Lines changed: 10 additions & 13 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
}
@@ -740,7 +747,7 @@ func (p *initProcess) start() (retErr error) {
740747
return fmt.Errorf("error setting Intel RDT config for procHooks process: %w", err)
741748
}
742749
}
743-
if len(p.config.Config.Hooks) != 0 {
750+
if p.config.Config.HasHook(configs.Prestart, configs.CreateRuntime) {
744751
s, err := p.container.currentOCIState()
745752
if err != nil {
746753
return err
@@ -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)