Skip to content

Commit e58e2ea

Browse files
committed
libct: add/use configs.HasHook
This allows to omit a call to c.currentOCIState (which can be somewhat costly when there are many annotations) when the hooks of a given kind won't be run. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 9112335 commit e58e2ea

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
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
@@ -353,7 +353,7 @@ func (c *Container) start(process *Process) (retErr error) {
353353

354354
if process.Init {
355355
c.fifo.Close()
356-
if c.config.Hooks != nil {
356+
if c.config.HasHook(configs.Poststart) {
357357
s, err := c.currentOCIState()
358358
if err != nil {
359359
return err

libcontainer/criu_linux.go

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

libcontainer/process_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ func (p *initProcess) start() (retErr error) {
769769
return fmt.Errorf("error setting Intel RDT config for procHooks process: %w", err)
770770
}
771771
}
772-
if len(p.config.Config.Hooks) != 0 {
772+
if p.config.Config.HasHook(configs.Prestart, configs.CreateRuntime) {
773773
s, err := p.container.currentOCIState()
774774
if err != nil {
775775
return err

0 commit comments

Comments
 (0)