diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f42f11309e..92e1b23cfde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### libcontainer API + * `configs.CommandHook` struct has changed, Command is now a pointer. + Also, `configs.NewCommandHook` now accepts a `*Command`. (#4325) + ## [1.2.0] - 2024-10-22 > できるときにできることをやるんだ。それが今だ。 diff --git a/libcontainer/configs/config.go b/libcontainer/configs/config.go index 22fe0f9b4c1..bf202a15650 100644 --- a/libcontainer/configs/config.go +++ b/libcontainer/configs/config.go @@ -427,6 +427,16 @@ func (hooks Hooks) Run(name HookName, state *specs.State) error { return nil } +// SetDefaultEnv sets the environment for those CommandHook entries +// that do not have one set. +func (hooks HookList) SetDefaultEnv(env []string) { + for _, h := range hooks { + if ch, ok := h.(CommandHook); ok && len(ch.Env) == 0 { + ch.Env = env + } + } +} + type Hook interface { // Run executes the hook with the provided state. Run(*specs.State) error @@ -456,17 +466,17 @@ type Command struct { } // NewCommandHook will execute the provided command when the hook is run. -func NewCommandHook(cmd Command) CommandHook { +func NewCommandHook(cmd *Command) CommandHook { return CommandHook{ Command: cmd, } } type CommandHook struct { - Command + *Command } -func (c Command) Run(s *specs.State) error { +func (c *Command) Run(s *specs.State) error { b, err := json.Marshal(s) if err != nil { return err diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 79a9a790049..6e3e5bde371 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -1256,8 +1256,8 @@ func createHooks(rspec *specs.Spec, config *configs.Config) { } } -func createCommandHook(h specs.Hook) configs.Command { - cmd := configs.Command{ +func createCommandHook(h specs.Hook) *configs.Command { + cmd := &configs.Command{ Path: h.Path, Args: h.Args, Env: h.Env,