summaryrefslogtreecommitdiffhomepage
path: root/pkg/shim
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/shim')
-rw-r--r--pkg/shim/BUILD9
-rw-r--r--pkg/shim/epoll.go1
-rw-r--r--pkg/shim/proc/BUILD2
-rw-r--r--pkg/shim/proc/exec.go90
-rw-r--r--pkg/shim/proc/exec_state.go12
-rw-r--r--pkg/shim/proc/init_state.go3
-rw-r--r--pkg/shim/runtimeoptions/runtimeoptions_cri.go3
-rw-r--r--pkg/shim/service.go30
-rw-r--r--pkg/shim/service_linux.go1
-rw-r--r--pkg/shim/utils/BUILD14
-rw-r--r--pkg/shim/utils/errors.go (renamed from pkg/shim/errors.go)37
-rw-r--r--pkg/shim/utils/errors_test.go (renamed from pkg/shim/errors_test.go)9
12 files changed, 127 insertions, 84 deletions
diff --git a/pkg/shim/BUILD b/pkg/shim/BUILD
index b115556f5..367765209 100644
--- a/pkg/shim/BUILD
+++ b/pkg/shim/BUILD
@@ -8,7 +8,6 @@ go_library(
"api.go",
"debug.go",
"epoll.go",
- "errors.go",
"options.go",
"service.go",
"service_linux.go",
@@ -45,8 +44,6 @@ go_library(
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_opencontainers_runtime_spec//specs-go:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
- "@org_golang_google_grpc//codes:go_default_library",
- "@org_golang_google_grpc//status:go_default_library",
"@org_golang_x_sys//unix:go_default_library",
],
)
@@ -54,14 +51,10 @@ go_library(
go_test(
name = "shim_test",
size = "small",
- srcs = [
- "errors_test.go",
- "service_test.go",
- ],
+ srcs = ["service_test.go"],
library = ":shim",
deps = [
"//pkg/shim/utils",
- "@com_github_containerd_containerd//errdefs:go_default_library",
"@com_github_opencontainers_runtime_spec//specs-go:go_default_library",
],
)
diff --git a/pkg/shim/epoll.go b/pkg/shim/epoll.go
index 737d2b781..463e11a84 100644
--- a/pkg/shim/epoll.go
+++ b/pkg/shim/epoll.go
@@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+//go:build linux
// +build linux
package shim
diff --git a/pkg/shim/proc/BUILD b/pkg/shim/proc/BUILD
index 544bdc170..c8527a6d9 100644
--- a/pkg/shim/proc/BUILD
+++ b/pkg/shim/proc/BUILD
@@ -20,7 +20,9 @@ go_library(
"//shim:__subpackages__",
],
deps = [
+ "//pkg/cleanup",
"//pkg/shim/runsc",
+ "//pkg/shim/utils",
"@com_github_containerd_console//:go_default_library",
"@com_github_containerd_containerd//errdefs:go_default_library",
"@com_github_containerd_containerd//log:go_default_library",
diff --git a/pkg/shim/proc/exec.go b/pkg/shim/proc/exec.go
index 14df3a778..da2e21598 100644
--- a/pkg/shim/proc/exec.go
+++ b/pkg/shim/proc/exec.go
@@ -26,11 +26,13 @@ import (
"github.com/containerd/console"
"github.com/containerd/containerd/errdefs"
+ "github.com/containerd/containerd/log"
"github.com/containerd/containerd/pkg/stdio"
"github.com/containerd/fifo"
runc "github.com/containerd/go-runc"
specs "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
+ "gvisor.dev/gvisor/pkg/cleanup"
"gvisor.dev/gvisor/pkg/shim/runsc"
)
@@ -92,6 +94,12 @@ func (e *execProcess) SetExited(status int) {
}
func (e *execProcess) setExited(status int) {
+ if !e.exited.IsZero() {
+ log.L.Debugf("Exec: status already set to %d, ignoring status: %d", e.status, status)
+ return
+ }
+
+ log.L.Debugf("Exec: setting status: %d", status)
e.status = status
e.exited = time.Now()
e.parent.Platform.ShutdownConsole(context.Background(), e.console)
@@ -105,7 +113,7 @@ func (e *execProcess) Delete(ctx context.Context) error {
return e.execState.Delete(ctx)
}
-func (e *execProcess) delete(ctx context.Context) error {
+func (e *execProcess) delete() {
e.wg.Wait()
if e.io != nil {
for _, c := range e.closers {
@@ -113,13 +121,6 @@ func (e *execProcess) delete(ctx context.Context) error {
}
e.io.Close()
}
- pidfile := filepath.Join(e.path, fmt.Sprintf("%s.pid", e.id))
- // silently ignore error
- os.Remove(pidfile)
- internalPidfile := filepath.Join(e.path, fmt.Sprintf("%s-internal.pid", e.id))
- // silently ignore error
- os.Remove(internalPidfile)
- return nil
}
func (e *execProcess) Resize(ws console.WinSize) error {
@@ -171,42 +172,53 @@ func (e *execProcess) Start(ctx context.Context) error {
return e.execState.Start(ctx)
}
-func (e *execProcess) start(ctx context.Context) (err error) {
- var (
- socket *runc.Socket
- pidfile = filepath.Join(e.path, fmt.Sprintf("%s.pid", e.id))
- internalPidfile = filepath.Join(e.path, fmt.Sprintf("%s-internal.pid", e.id))
- )
- if e.stdio.Terminal {
- if socket, err = runc.NewTempConsoleSocket(); err != nil {
+func (e *execProcess) start(ctx context.Context) error {
+ var socket *runc.Socket
+
+ switch {
+ case e.stdio.Terminal:
+ s, err := runc.NewTempConsoleSocket()
+ if err != nil {
return fmt.Errorf("failed to create runc console socket: %w", err)
}
- defer socket.Close()
- } else if e.stdio.IsNull() {
- if e.io, err = runc.NewNullIO(); err != nil {
+ defer s.Close()
+ socket = s
+
+ case e.stdio.IsNull():
+ io, err := runc.NewNullIO()
+ if err != nil {
return fmt.Errorf("creating new NULL IO: %w", err)
}
- } else {
- if e.io, err = runc.NewPipeIO(e.parent.IoUID, e.parent.IoGID, withConditionalIO(e.stdio)); err != nil {
+ e.io = io
+
+ default:
+ io, err := runc.NewPipeIO(e.parent.IoUID, e.parent.IoGID, withConditionalIO(e.stdio))
+ if err != nil {
return fmt.Errorf("failed to create runc io pipes: %w", err)
}
+ e.io = io
}
+
opts := &runsc.ExecOpts{
- PidFile: pidfile,
- InternalPidFile: internalPidfile,
+ PidFile: filepath.Join(e.path, fmt.Sprintf("%s.pid", e.id)),
+ InternalPidFile: filepath.Join(e.path, fmt.Sprintf("%s-internal.pid", e.id)),
IO: e.io,
Detach: true,
}
+ defer func() {
+ _ = os.Remove(opts.PidFile)
+ _ = os.Remove(opts.InternalPidFile)
+ }()
if socket != nil {
opts.ConsoleSocket = socket
}
+
eventCh := e.parent.Monitor.Subscribe()
- defer func() {
- // Unsubscribe if an error is returned.
- if err != nil {
- e.parent.Monitor.Unsubscribe(eventCh)
- }
- }()
+ cu := cleanup.Make(func() {
+ e.parent.Monitor.Unsubscribe(eventCh)
+ })
+ defer cu.Clean()
+
if err := e.parent.runtime.Exec(ctx, e.parent.id, e.spec, opts); err != nil {
close(e.waitBlock)
return e.parent.runtimeError(err, "OCI runtime exec failed")
@@ -234,6 +246,7 @@ func (e *execProcess) start(ctx context.Context) (err error) {
return fmt.Errorf("failed to start io pipe copy: %w", err)
}
}
+
pid, err := runc.ReadPidFile(opts.PidFile)
if err != nil {
return fmt.Errorf("failed to retrieve OCI runtime exec pid: %w", err)
@@ -244,6 +257,7 @@ func (e *execProcess) start(ctx context.Context) (err error) {
return fmt.Errorf("failed to retrieve OCI runtime exec internal pid: %w", err)
}
e.internalPid = internalPid
+
go func() {
defer e.parent.Monitor.Unsubscribe(eventCh)
for event := range eventCh {
@@ -257,21 +271,25 @@ func (e *execProcess) start(ctx context.Context) (err error) {
}
}
}()
+
+ cu.Release() // cancel cleanup on success.
return nil
}
-func (e *execProcess) Status(ctx context.Context) (string, error) {
+func (e *execProcess) Status(context.Context) (string, error) {
e.mu.Lock()
defer e.mu.Unlock()
// if we don't have a pid then the exec process has just been created
if e.pid == 0 {
return "created", nil
}
- // if we have a pid and it can be signaled, the process is running
- // TODO(random-liu): Use `runsc kill --pid`.
- if err := unix.Kill(e.pid, 0); err == nil {
- return "running", nil
+ // This checks that `runsc exec` process is still running. This process has
+ // the same lifetime as the process executing inside the container. So instead
+ // of calling `runsc kill --pid`, just do a quick check that `runsc exec` is
+ // still running.
+ if err := unix.Kill(e.pid, 0); err != nil {
+ // Can't signal the process, it must have exited.
+ return "stopped", nil
}
- // else if we have a pid but it can nolonger be signaled, it has stopped
- return "stopped", nil
+ return "running", nil
}
diff --git a/pkg/shim/proc/exec_state.go b/pkg/shim/proc/exec_state.go
index 9c6edd3f5..03ecb401a 100644
--- a/pkg/shim/proc/exec_state.go
+++ b/pkg/shim/proc/exec_state.go
@@ -63,10 +63,8 @@ func (s *execCreatedState) Start(ctx context.Context) error {
return nil
}
-func (s *execCreatedState) Delete(ctx context.Context) error {
- if err := s.p.delete(ctx); err != nil {
- return err
- }
+func (s *execCreatedState) Delete(context.Context) error {
+ s.p.delete()
s.transition(deleted)
return nil
}
@@ -143,10 +141,8 @@ func (s *execStoppedState) Start(context.Context) error {
return fmt.Errorf("cannot start a stopped process")
}
-func (s *execStoppedState) Delete(ctx context.Context) error {
- if err := s.p.delete(ctx); err != nil {
- return err
- }
+func (s *execStoppedState) Delete(context.Context) error {
+ s.p.delete()
s.transition(deleted)
return nil
}
diff --git a/pkg/shim/proc/init_state.go b/pkg/shim/proc/init_state.go
index d65020e76..5347ddefe 100644
--- a/pkg/shim/proc/init_state.go
+++ b/pkg/shim/proc/init_state.go
@@ -23,6 +23,7 @@ import (
"github.com/containerd/containerd/pkg/process"
runc "github.com/containerd/go-runc"
"golang.org/x/sys/unix"
+ "gvisor.dev/gvisor/pkg/shim/utils"
)
type stateTransition int
@@ -235,6 +236,6 @@ func handleStoppedKill(signal uint32) error {
// already been killed.
return nil
default:
- return errdefs.ToGRPCf(errdefs.ErrNotFound, "process not found")
+ return utils.ErrToGRPCf(errdefs.ErrNotFound, "process not found")
}
}
diff --git a/pkg/shim/runtimeoptions/runtimeoptions_cri.go b/pkg/shim/runtimeoptions/runtimeoptions_cri.go
index e6102b4cf..23bbd82be 100644
--- a/pkg/shim/runtimeoptions/runtimeoptions_cri.go
+++ b/pkg/shim/runtimeoptions/runtimeoptions_cri.go
@@ -13,6 +13,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+//go:build go1.1
+// +build go1.1
+
package runtimeoptions
import (
diff --git a/pkg/shim/service.go b/pkg/shim/service.go
index 0b41f0e72..24e3b7a82 100644
--- a/pkg/shim/service.go
+++ b/pkg/shim/service.go
@@ -452,10 +452,10 @@ func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (*ta
}
process, err := newInit(r.Bundle, filepath.Join(r.Bundle, "work"), ns, s.platform, config, &s.opts, st.Rootfs)
if err != nil {
- return nil, errToGRPC(err)
+ return nil, utils.ErrToGRPC(err)
}
if err := process.Create(ctx, config); err != nil {
- return nil, errToGRPC(err)
+ return nil, utils.ErrToGRPC(err)
}
// Set up OOM notification on the sandbox's cgroup. This is done on
@@ -530,10 +530,10 @@ func (s *service) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (*typ
p := s.processes[r.ExecID]
s.mu.Unlock()
if p != nil {
- return nil, errdefs.ToGRPCf(errdefs.ErrAlreadyExists, "id %s", r.ExecID)
+ return nil, utils.ErrToGRPCf(errdefs.ErrAlreadyExists, "id %s", r.ExecID)
}
if s.task == nil {
- return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
+ return nil, utils.ErrToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
}
process, err := s.task.Exec(ctx, s.bundle, &proc.ExecConfig{
ID: r.ExecID,
@@ -544,7 +544,7 @@ func (s *service) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (*typ
Spec: r.Spec,
})
if err != nil {
- return nil, errToGRPC(err)
+ return nil, utils.ErrToGRPC(err)
}
s.mu.Lock()
s.processes[r.ExecID] = process
@@ -565,7 +565,7 @@ func (s *service) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (*
Height: uint16(r.Height),
}
if err := p.Resize(ws); err != nil {
- return nil, errToGRPC(err)
+ return nil, utils.ErrToGRPC(err)
}
return empty, nil
}
@@ -615,7 +615,7 @@ func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (*types.Em
log.L.Debugf("Pause, id: %s", r.ID)
if s.task == nil {
log.L.Debugf("Pause error, id: %s: container not created", r.ID)
- return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
+ return nil, utils.ErrToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
}
err := s.task.Runtime().Pause(ctx, r.ID)
if err != nil {
@@ -629,7 +629,7 @@ func (s *service) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (*types.
log.L.Debugf("Resume, id: %s", r.ID)
if s.task == nil {
log.L.Debugf("Resume error, id: %s: container not created", r.ID)
- return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
+ return nil, utils.ErrToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
}
err := s.task.Runtime().Resume(ctx, r.ID)
if err != nil {
@@ -648,7 +648,7 @@ func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (*types.Empt
}
if err := p.Kill(ctx, r.Signal, r.All); err != nil {
log.L.Debugf("Kill failed: %v", err)
- return nil, errToGRPC(err)
+ return nil, utils.ErrToGRPC(err)
}
log.L.Debugf("Kill succeeded")
return empty, nil
@@ -660,7 +660,7 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.Pi
pids, err := s.getContainerPids(ctx, r.ID)
if err != nil {
- return nil, errToGRPC(err)
+ return nil, utils.ErrToGRPC(err)
}
var processes []*task.ProcessInfo
for _, pid := range pids {
@@ -706,7 +706,7 @@ func (s *service) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (*type
// Checkpoint checkpoints the container.
func (s *service) Checkpoint(ctx context.Context, r *taskAPI.CheckpointTaskRequest) (*types.Empty, error) {
log.L.Debugf("Checkpoint, id: %s", r.ID)
- return empty, errdefs.ToGRPC(errdefs.ErrNotImplemented)
+ return empty, utils.ErrToGRPC(errdefs.ErrNotImplemented)
}
// Connect returns shim information such as the shim's pid.
@@ -737,7 +737,7 @@ func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.
log.L.Debugf("Stats, id: %s", r.ID)
if s.task == nil {
log.L.Debugf("Stats error, id: %s: container not created", r.ID)
- return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
+ return nil, utils.ErrToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
}
stats, err := s.task.Stats(ctx, s.id)
if err != nil {
@@ -811,7 +811,7 @@ func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.
// Update updates a running container.
func (s *service) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (*types.Empty, error) {
- return empty, errdefs.ToGRPC(errdefs.ErrNotImplemented)
+ return empty, utils.ErrToGRPC(errdefs.ErrNotImplemented)
}
// Wait waits for a process to exit.
@@ -908,14 +908,14 @@ func (s *service) getProcess(execID string) (process.Process, error) {
if execID == "" {
if s.task == nil {
- return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
+ return nil, utils.ErrToGRPCf(errdefs.ErrFailedPrecondition, "container must be created")
}
return s.task, nil
}
p := s.processes[execID]
if p == nil {
- return nil, errdefs.ToGRPCf(errdefs.ErrNotFound, "process does not exist %s", execID)
+ return nil, utils.ErrToGRPCf(errdefs.ErrNotFound, "process does not exist %s", execID)
}
return p, nil
}
diff --git a/pkg/shim/service_linux.go b/pkg/shim/service_linux.go
index 829f69282..fb2f8b062 100644
--- a/pkg/shim/service_linux.go
+++ b/pkg/shim/service_linux.go
@@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+//go:build linux
// +build linux
package shim
diff --git a/pkg/shim/utils/BUILD b/pkg/shim/utils/BUILD
index 54a0aabb7..2eb82f63c 100644
--- a/pkg/shim/utils/BUILD
+++ b/pkg/shim/utils/BUILD
@@ -6,6 +6,7 @@ go_library(
name = "utils",
srcs = [
"annotations.go",
+ "errors.go",
"utils.go",
"volumes.go",
],
@@ -14,14 +15,23 @@ go_library(
"//shim:__subpackages__",
],
deps = [
+ "@com_github_containerd_containerd//errdefs:go_default_library",
"@com_github_opencontainers_runtime_spec//specs-go:go_default_library",
+ "@org_golang_google_grpc//codes:go_default_library",
+ "@org_golang_google_grpc//status:go_default_library",
],
)
go_test(
name = "utils_test",
size = "small",
- srcs = ["volumes_test.go"],
+ srcs = [
+ "errors_test.go",
+ "volumes_test.go",
+ ],
library = ":utils",
- deps = ["@com_github_opencontainers_runtime_spec//specs-go:go_default_library"],
+ deps = [
+ "@com_github_containerd_containerd//errdefs:go_default_library",
+ "@com_github_opencontainers_runtime_spec//specs-go:go_default_library",
+ ],
)
diff --git a/pkg/shim/errors.go b/pkg/shim/utils/errors.go
index 75d036411..971d68c36 100644
--- a/pkg/shim/errors.go
+++ b/pkg/shim/utils/errors.go
@@ -12,23 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package shim
+package utils
import (
"context"
"errors"
+ "fmt"
"github.com/containerd/containerd/errdefs"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
-// errToGRPC wraps containerd's ToGRPC error mapper which depends on
+// ErrToGRPC wraps containerd's ToGRPC error mapper which depends on
// github.com/pkg/errors to work correctly. Once we upgrade to containerd v1.4,
// this function can go away and we can use errdefs.ToGRPC directly instead.
//
// TODO(gvisor.dev/issue/6232): Remove after upgrading to containerd v1.4
-func errToGRPC(err error) error {
+func ErrToGRPC(err error) error {
+ return errToGRPCMsg(err, err.Error())
+}
+
+// ErrToGRPCf maps the error to grpc error codes, assembling the formatting
+// string and combining it with the target error string.
+//
+// TODO(gvisor.dev/issue/6232): Remove after upgrading to containerd v1.4
+func ErrToGRPCf(err error, format string, args ...interface{}) error {
+ formatted := fmt.Sprintf(format, args...)
+ msg := fmt.Sprintf("%s: %s", formatted, err.Error())
+ return errToGRPCMsg(err, msg)
+}
+
+func errToGRPCMsg(err error, msg string) error {
if err == nil {
return nil
}
@@ -38,21 +53,21 @@ func errToGRPC(err error) error {
switch {
case errors.Is(err, errdefs.ErrInvalidArgument):
- return status.Errorf(codes.InvalidArgument, err.Error())
+ return status.Errorf(codes.InvalidArgument, msg)
case errors.Is(err, errdefs.ErrNotFound):
- return status.Errorf(codes.NotFound, err.Error())
+ return status.Errorf(codes.NotFound, msg)
case errors.Is(err, errdefs.ErrAlreadyExists):
- return status.Errorf(codes.AlreadyExists, err.Error())
+ return status.Errorf(codes.AlreadyExists, msg)
case errors.Is(err, errdefs.ErrFailedPrecondition):
- return status.Errorf(codes.FailedPrecondition, err.Error())
+ return status.Errorf(codes.FailedPrecondition, msg)
case errors.Is(err, errdefs.ErrUnavailable):
- return status.Errorf(codes.Unavailable, err.Error())
+ return status.Errorf(codes.Unavailable, msg)
case errors.Is(err, errdefs.ErrNotImplemented):
- return status.Errorf(codes.Unimplemented, err.Error())
+ return status.Errorf(codes.Unimplemented, msg)
case errors.Is(err, context.Canceled):
- return status.Errorf(codes.Canceled, err.Error())
+ return status.Errorf(codes.Canceled, msg)
case errors.Is(err, context.DeadlineExceeded):
- return status.Errorf(codes.DeadlineExceeded, err.Error())
+ return status.Errorf(codes.DeadlineExceeded, msg)
}
return errdefs.ToGRPC(err)
diff --git a/pkg/shim/errors_test.go b/pkg/shim/utils/errors_test.go
index 3c10866cc..0a8fe34c8 100644
--- a/pkg/shim/errors_test.go
+++ b/pkg/shim/utils/errors_test.go
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package shim
+package utils
import (
"fmt"
@@ -39,8 +39,11 @@ func TestGRPCRoundTripsErrors(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
- if err := errdefs.FromGRPC(errToGRPC(tc.err)); !tc.test(err) {
- t.Errorf("got %+v", err)
+ if err := errdefs.FromGRPC(ErrToGRPC(tc.err)); !tc.test(err) {
+ t.Errorf("errToGRPC got %+v", err)
+ }
+ if err := errdefs.FromGRPC(ErrToGRPCf(tc.err, "testing %s", "123")); !tc.test(err) {
+ t.Errorf("errToGRPCf got %+v", err)
}
})
}