diff --git a/linux/proc/init.go b/linux/proc/init.go index 0d88e9bc5..212e14f63 100644 --- a/linux/proc/init.go +++ b/linux/proc/init.go @@ -15,7 +15,6 @@ import ( "time" "github.com/containerd/console" - "github.com/containerd/containerd/identifiers" "github.com/containerd/containerd/linux/runctypes" shimapi "github.com/containerd/containerd/linux/shim/v1" "github.com/containerd/containerd/log" @@ -61,13 +60,26 @@ type Init struct { IoGID int } +// NewRunc returns a new runc instance for a process +func NewRunc(root, path, namespace, runtime, criu string, systemd bool) *runc.Runc { + if root == "" { + root = RuncRoot + } + return &runc.Runc{ + Command: runtime, + Log: filepath.Join(path, "log.json"), + LogFormat: runc.JSON, + PdeathSignal: syscall.SIGKILL, + Root: filepath.Join(root, namespace), + Criu: criu, + SystemdCgroup: systemd, + } +} + // New returns a new init process func New(context context.Context, path, workDir, runtimeRoot, namespace, criu string, systemdCgroup bool, platform Platform, r *shimapi.CreateTaskRequest) (*Init, error) { var success bool - if err := identifiers.Validate(r.ID); err != nil { - return nil, errors.Wrapf(err, "invalid task id") - } var options runctypes.CreateOptions if r.Options != nil { v, err := typeurl.UnmarshalAny(r.Options) @@ -99,19 +111,7 @@ func New(context context.Context, path, workDir, runtimeRoot, namespace, criu st return nil, errors.Wrapf(err, "failed to mount rootfs component %v", m) } } - root := runtimeRoot - if root == "" { - root = RuncRoot - } - runtime := &runc.Runc{ - Command: r.Runtime, - Log: filepath.Join(path, "log.json"), - LogFormat: runc.JSON, - PdeathSignal: syscall.SIGKILL, - Root: filepath.Join(root, namespace), - Criu: criu, - SystemdCgroup: systemdCgroup, - } + runtime := NewRunc(runtimeRoot, path, namespace, r.Runtime, criu, systemdCgroup) p := &Init{ id: r.ID, bundle: r.Bundle, @@ -339,9 +339,6 @@ func (p *Init) Runtime() *runc.Runc { // Exec returns a new exec'd process func (p *Init) Exec(context context.Context, path string, r *shimapi.ExecProcessRequest) (Process, error) { - if err := identifiers.Validate(r.ID); err != nil { - return nil, errors.Wrapf(err, "invalid exec id") - } // process exec request var spec specs.Process if err := json.Unmarshal(r.Spec.Value, &spec); err != nil { diff --git a/linux/shim/service.go b/linux/shim/service.go index 25e9ac93a..0347c0b37 100644 --- a/linux/shim/service.go +++ b/linux/shim/service.go @@ -19,7 +19,6 @@ import ( "github.com/containerd/containerd/linux/runctypes" shimapi "github.com/containerd/containerd/linux/shim/v1" "github.com/containerd/containerd/log" - "github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/reaper" "github.com/containerd/containerd/runtime" runc "github.com/containerd/go-runc" @@ -47,15 +46,15 @@ func NewService(config Config, publisher events.Publisher) (*Service, error) { if config.Namespace == "" { return nil, fmt.Errorf("shim namespace cannot be empty") } - context := namespaces.WithNamespace(context.Background(), config.Namespace) - context = log.WithLogger(context, logrus.WithFields(logrus.Fields{ + ctx := context.Background() + ctx = log.WithLogger(ctx, logrus.WithFields(logrus.Fields{ "namespace": config.Namespace, "path": config.Path, "pid": os.Getpid(), })) s := &Service{ config: config, - context: context, + context: ctx, processes: make(map[string]proc.Process), events: make(chan interface{}, 128), ec: reaper.Default.Subscribe(), diff --git a/linux/task.go b/linux/task.go index 235e67166..11b5032d3 100644 --- a/linux/task.go +++ b/linux/task.go @@ -13,6 +13,7 @@ import ( "github.com/containerd/containerd/api/types/task" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/events/exchange" + "github.com/containerd/containerd/identifiers" "github.com/containerd/containerd/linux/shim/client" shim "github.com/containerd/containerd/linux/shim/v1" "github.com/containerd/containerd/runtime" @@ -167,6 +168,9 @@ func (t *Task) Kill(ctx context.Context, signal uint32, all bool) error { // Exec creates a new process inside the task func (t *Task) Exec(ctx context.Context, id string, opts runtime.ExecOpts) (runtime.Process, error) { + if err := identifiers.Validate(id); err != nil { + return nil, errors.Wrapf(err, "invalid exec id") + } request := &shim.ExecProcessRequest{ ID: id, Stdin: opts.IO.Stdin, diff --git a/reaper/reaper.go b/reaper/reaper.go index d7dfbb2aa..9127fc5a1 100644 --- a/reaper/reaper.go +++ b/reaper/reaper.go @@ -15,7 +15,7 @@ import ( // ErrNoSuchProcess is returned when the process no longer exists var ErrNoSuchProcess = errors.New("no such process") -const bufferSize = 2048 +const bufferSize = 1024 // Reap should be called when the process receives an SIGCHLD. Reap will reap // all exited processes and close their wait channels