Skip to content

Commit

Permalink
Merge pull request #4433 from kolyshkin/hasHook
Browse files Browse the repository at this point in the history
libct: add/use configs.HasHook
  • Loading branch information
lifubang authored Dec 25, 2024
2 parents 2e906e2 + 93091e6 commit fd932ec
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
13 changes: 13 additions & 0 deletions libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ const (
Poststop HookName = "poststop"
)

// HasHook checks if config has any hooks with any given names configured.
func (c *Config) HasHook(names ...HookName) bool {
if c.Hooks == nil {
return false
}
for _, h := range names {
if len(c.Hooks[h]) > 0 {
return true
}
}
return false
}

// KnownHookNames returns the known hook names.
// Used by `runc features`.
func KnownHookNames() []string {
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (c *Container) start(process *Process) (retErr error) {

if process.Init {
c.fifo.Close()
if c.config.Hooks != nil {
if c.config.HasHook(configs.Poststart) {
s, err := c.currentOCIState()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/criu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ func (c *Container) criuNotifications(resp *criurpc.CriuResp, process *Process,
return err
}
case "setup-namespaces":
if c.config.Hooks != nil {
if c.config.HasHook(configs.Prestart, configs.CreateRuntime) {
s, err := c.currentOCIState()
if err != nil {
return nil
Expand Down
23 changes: 10 additions & 13 deletions libcontainer/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,16 @@ func (p *initProcess) start() (retErr error) {
if err := p.createNetworkInterfaces(); err != nil {
return fmt.Errorf("error creating network interfaces: %w", err)
}
if err := p.updateSpecState(); err != nil {
return fmt.Errorf("error updating spec state: %w", err)

// initConfig.SpecState is only needed to run hooks that are executed
// inside a container, i.e. CreateContainer and StartContainer.
if p.config.Config.HasHook(configs.CreateContainer, configs.StartContainer) {
p.config.SpecState, err = p.container.currentOCIState()
if err != nil {
return fmt.Errorf("error getting current state: %w", err)
}
}

if err := utils.WriteJSON(p.comm.initSockParent, p.config); err != nil {
return fmt.Errorf("error sending config to init process: %w", err)
}
Expand Down Expand Up @@ -740,7 +747,7 @@ func (p *initProcess) start() (retErr error) {
return fmt.Errorf("error setting Intel RDT config for procHooks process: %w", err)
}
}
if len(p.config.Config.Hooks) != 0 {
if p.config.Config.HasHook(configs.Prestart, configs.CreateRuntime) {
s, err := p.container.currentOCIState()
if err != nil {
return err
Expand Down Expand Up @@ -779,16 +786,6 @@ func (p *initProcess) start() (retErr error) {
return nil
}

func (p *initProcess) updateSpecState() error {
s, err := p.container.currentOCIState()
if err != nil {
return err
}

p.config.SpecState = s
return nil
}

func (p *initProcess) createNetworkInterfaces() error {
for _, config := range p.config.Config.Networks {
strategy, err := getStrategy(config.Type)
Expand Down
11 changes: 6 additions & 5 deletions libcontainer/rootfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ func prepareRootfs(pipe *syncSocket, iConfig *initConfig) (err error) {
return &os.PathError{Op: "chdir", Path: config.Rootfs, Err: err}
}

s := iConfig.SpecState
s.Pid = unix.Getpid()
s.Status = specs.StateCreating
if err := iConfig.Config.Hooks.Run(configs.CreateContainer, s); err != nil {
return err
if s := iConfig.SpecState; s != nil {
s.Pid = unix.Getpid()
s.Status = specs.StateCreating
if err := iConfig.Config.Hooks.Run(configs.CreateContainer, s); err != nil {
return err
}
}

if config.NoPivotRoot {
Expand Down
11 changes: 6 additions & 5 deletions libcontainer/standard_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,12 @@ func (l *linuxStandardInit) Init() error {
// https://github.com/torvalds/linux/blob/v4.9/fs/exec.c#L1290-L1318
_ = l.fifoFile.Close()

s := l.config.SpecState
s.Pid = unix.Getpid()
s.Status = specs.StateCreated
if err := l.config.Config.Hooks.Run(configs.StartContainer, s); err != nil {
return err
if s := l.config.SpecState; s != nil {
s.Pid = unix.Getpid()
s.Status = specs.StateCreated
if err := l.config.Config.Hooks.Run(configs.StartContainer, s); err != nil {
return err
}
}

// Close all file descriptors we are not passing to the container. This is
Expand Down

0 comments on commit fd932ec

Please sign in to comment.