aboutsummaryrefslogtreecommitdiffhomepage
path: root/container
diff options
context:
space:
mode:
Diffstat (limited to 'container')
-rw-r--r--container/container.go38
-rw-r--r--container/container_test.go2
-rw-r--r--container/init_test.go2
3 files changed, 14 insertions, 28 deletions
diff --git a/container/container.go b/container/container.go
index ea715d96..54cee25f 100644
--- a/container/container.go
+++ b/container/container.go
@@ -27,8 +27,6 @@ type (
// Container represents a container environment being prepared or run.
// None of [Container] methods are safe for concurrent use.
Container struct {
- // Name of initial process in the container.
- name string
// Cgroup fd, nil to disable.
Cgroup *int
// ExtraFiles passed through to initial process in the container,
@@ -190,31 +188,12 @@ func (p *Container) Serve() error {
setup := p.setup
p.setup = nil
- if p.Path != "" && !path.IsAbs(p.Path) {
+ if !path.IsAbs(p.Path) {
p.cancel()
return msg.WrapErr(EINVAL,
fmt.Sprintf("invalid executable path %q", p.Path))
}
- if p.Path == "" {
- if p.name == "" {
- p.Path = os.Getenv("SHELL")
- if !path.IsAbs(p.Path) {
- p.cancel()
- return msg.WrapErr(EBADE,
- "no command specified and $SHELL is invalid")
- }
- p.name = path.Base(p.Path)
- } else if path.IsAbs(p.name) {
- p.Path = p.name
- } else if v, err := exec.LookPath(p.name); err != nil {
- p.cancel()
- return msg.WrapErr(err, err.Error())
- } else {
- p.Path = v
- }
- }
-
if p.SeccompRules == nil {
// do not transmit nil
p.SeccompRules = make([]seccomp.NativeRule, 0)
@@ -251,8 +230,15 @@ func (p *Container) ProcessState() *os.ProcessState {
return p.cmd.ProcessState
}
-func New(ctx context.Context, name string, args ...string) *Container {
- return &Container{name: name, ctx: ctx,
- Params: Params{Args: append([]string{name}, args...), Dir: FHSRoot, Ops: new(Ops)},
- }
+// New returns the address to a new instance of [Container] that requires further initialisation before use.
+func New(ctx context.Context) *Container {
+ return &Container{ctx: ctx, Params: Params{Dir: FHSRoot, Ops: new(Ops)}}
+}
+
+// NewCommand calls [New] and initialises the [Params.Path] and [Params.Args] fields.
+func NewCommand(ctx context.Context, pathname, name string, args ...string) *Container {
+ z := New(ctx)
+ z.Path = pathname
+ z.Args = append([]string{name}, args...)
+ return z
}
diff --git a/container/container_test.go b/container/container_test.go
index 62134f70..10c20659 100644
--- a/container/container_test.go
+++ b/container/container_test.go
@@ -392,7 +392,7 @@ func testContainerCancel(
}
func TestContainerString(t *testing.T) {
- c := container.New(t.Context(), "ldd", "/usr/bin/env")
+ c := container.NewCommand(t.Context(), "/run/current-system/sw/bin/ldd", "ldd", "/usr/bin/env")
c.SeccompFlags |= seccomp.AllowMultiarch
c.SeccompRules = seccomp.Preset(
seccomp.PresetExt|seccomp.PresetDenyNS|seccomp.PresetDenyTTY,
diff --git a/container/init_test.go b/container/init_test.go
index 6e99cc09..7716458c 100644
--- a/container/init_test.go
+++ b/container/init_test.go
@@ -47,7 +47,7 @@ func TestMain(m *testing.M) {
}
func helperNewContainerLibPaths(ctx context.Context, libPaths *[]string, args ...string) (c *container.Container) {
- c = container.New(ctx, helperInnerPath, args...)
+ c = container.NewCommand(ctx, helperInnerPath, "helper", args...)
c.Env = append(c.Env, envDoCheck+"=1")
c.Bind(os.Args[0], helperInnerPath, 0)