-
Notifications
You must be signed in to change notification settings - Fork 2.3k
libcontainer: map pre-exec termination signals to 128+signal #5189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+192
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package integration | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/opencontainers/runc/libcontainer" | ||
| "github.com/opencontainers/runc/libcontainer/configs" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // Test to simulate problem we saw in https://github.com/kubernetes/kubernetes/issues/135713 | ||
| func TestPreExecSignalMapping(t *testing.T) { | ||
|
dims marked this conversation as resolved.
|
||
| if testing.Short() { | ||
| return | ||
| } | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| sig unix.Signal | ||
| }{ | ||
| {name: "SIGTERM", sig: unix.SIGTERM}, | ||
| {name: "SIGINT", sig: unix.SIGINT}, | ||
| {name: "SIGHUP", sig: unix.SIGHUP}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| container, process, hookRelease := newPreExecSignalProcess(t) | ||
| defer destroyContainer(container) | ||
|
|
||
| ok(t, container.Signal(tc.sig)) | ||
| if got, want := waitForExitCodeWhileHookBlocked(t, process, hookRelease), 128+int(tc.sig); got != want { | ||
| t.Fatalf("expected %s to exit %d, got %d", tc.name, want, got) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func newPreExecSignalProcess(t testing.TB) (*libcontainer.Container, *libcontainer.Process, string) { | ||
| t.Helper() | ||
|
|
||
| config := newTemplateConfig(t, nil) | ||
| hookReady := filepath.Join(config.Rootfs, "startContainer-hook-ready") | ||
| hookRelease := filepath.Join(config.Rootfs, "startContainer-hook-release") | ||
| ok(t, unix.Mkfifo(hookRelease, 0o600)) | ||
| config.Hooks = configs.Hooks{ | ||
| configs.StartContainer: configs.HookList{ | ||
| configs.NewCommandHook(&configs.Command{ | ||
| Path: "/bin/sh", | ||
| Args: []string{"/bin/sh", "-c", "touch /startContainer-hook-ready && cat /startContainer-hook-release >/dev/null"}, | ||
| }), | ||
| }, | ||
| } | ||
|
|
||
| container, err := newContainer(t, config) | ||
| ok(t, err) | ||
|
|
||
| process := &libcontainer.Process{ | ||
| Cwd: "/", | ||
| Args: []string{"false"}, | ||
| Env: standardEnvironment, | ||
| Init: true, | ||
| } | ||
|
|
||
| ok(t, container.Run(process)) | ||
| waitForFile(t, hookReady) | ||
| return container, process, hookRelease | ||
| } | ||
|
|
||
| func waitForFile(t testing.TB, path string) { | ||
| t.Helper() | ||
|
|
||
| deadline := time.Now().Add(5 * time.Second) | ||
| for time.Now().Before(deadline) { | ||
| if _, err := os.Stat(path); err == nil { | ||
| return | ||
| } else if !errors.Is(err, os.ErrNotExist) { | ||
| t.Fatalf("stat %s: %v", path, err) | ||
| } | ||
| time.Sleep(10 * time.Millisecond) | ||
| } | ||
|
|
||
| t.Fatalf("timed out waiting for %s", path) | ||
| } | ||
|
|
||
| func processExitCode(process *libcontainer.Process) (int, error) { | ||
| ps, err := process.Wait() | ||
| if err != nil && ps == nil { | ||
| return 0, err | ||
| } | ||
| if ps == nil { | ||
| return 0, errors.New("wait returned nil process state") | ||
| } | ||
|
|
||
| status, ok := ps.Sys().(syscall.WaitStatus) | ||
| if !ok { | ||
| return 0, errors.New("unexpected wait status type") | ||
| } | ||
| if !status.Exited() { | ||
| return 0, errors.New("process did not exit") | ||
| } | ||
| return status.ExitStatus(), nil | ||
| } | ||
|
|
||
| func waitForExitCodeWhileHookBlocked(t testing.TB, process *libcontainer.Process, hookRelease string) int { | ||
| t.Helper() | ||
|
|
||
| type waitResult struct { | ||
| code int | ||
| err error | ||
| } | ||
|
|
||
| results := make(chan waitResult, 1) | ||
| go func() { | ||
| code, err := processExitCode(process) | ||
| results <- waitResult{code: code, err: err} | ||
| }() | ||
|
|
||
| select { | ||
| case result := <-results: | ||
| releaseHook(t, hookRelease) | ||
| if result.err != nil { | ||
| t.Fatalf("wait: %v", result.err) | ||
| } | ||
| return result.code | ||
| case <-time.After(500 * time.Millisecond): | ||
| releaseHook(t, hookRelease) | ||
| t.Fatal("process did not exit while startContainer hook was still blocking") | ||
| return 0 | ||
| } | ||
| } | ||
|
|
||
| func releaseHook(t testing.TB, path string) { | ||
| t.Helper() | ||
|
|
||
| fd, err := unix.Open(path, unix.O_WRONLY|unix.O_NONBLOCK|unix.O_CLOEXEC, 0) | ||
| if errors.Is(err, unix.ENXIO) || errors.Is(err, os.ErrNotExist) { | ||
| return | ||
| } | ||
| ok(t, err) | ||
| f := os.NewFile(uintptr(fd), path) | ||
| if _, err := f.Write([]byte("ok\n")); err != nil { | ||
| _ = f.Close() | ||
| t.Fatalf("write %s: %v", path, err) | ||
| } | ||
| ok(t, f.Close()) | ||
|
|
||
| deadline := time.Now().Add(500 * time.Millisecond) | ||
| for time.Now().Before(deadline) { | ||
| fd, err := unix.Open(path, unix.O_WRONLY|unix.O_NONBLOCK|unix.O_CLOEXEC, 0) | ||
| if errors.Is(err, unix.ENXIO) || errors.Is(err, os.ErrNotExist) { | ||
| return | ||
| } | ||
| if err == nil { | ||
| _ = unix.Close(fd) | ||
| } | ||
| time.Sleep(10 * time.Millisecond) | ||
| } | ||
|
|
||
| t.Fatalf("timed out waiting for startContainer hook to exit") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.