Skip to content

Commit 8afeb58

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 2e906e2 commit 8afeb58

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
@@ -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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ func (p *initProcess) start() (retErr error) {
740740
return fmt.Errorf("error setting Intel RDT config for procHooks process: %w", err)
741741
}
742742
}
743-
if len(p.config.Config.Hooks) != 0 {
743+
if p.config.Config.HasHook(configs.Prestart, configs.CreateRuntime) {
744744
s, err := p.container.currentOCIState()
745745
if err != nil {
746746
return err

0 commit comments

Comments
 (0)