-
Notifications
You must be signed in to change notification settings - Fork 748
pidns: support single-level nested PID namespace dump/restore #2968
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
Open
nidhishgajjar
wants to merge
1
commit into
checkpoint-restore:criu-dev
Choose a base branch
from
nidhishgajjar:pidns-nested-support
base: criu-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−10
Open
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
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,161 @@ | ||
| #include <sys/types.h> | ||
| #include <sys/wait.h> | ||
| #include <sys/mount.h> | ||
| #include <sched.h> | ||
| #include <unistd.h> | ||
| #include <errno.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <signal.h> | ||
| #include <fcntl.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include "zdtmtst.h" | ||
|
|
||
| const char *test_doc = "Check dump/restore of a process tree with a child PID namespace"; | ||
| const char *test_author = "Nidhish Gajjar <hacker@scigic.com>"; | ||
|
|
||
| int main(int argc, char **argv) | ||
| { | ||
| int pipe_ready[2], pipe_go[2], pipe_result[2]; | ||
| int status; | ||
| pid_t child, ret; | ||
| char buf; | ||
|
|
||
| test_init(argc, argv); | ||
|
|
||
| if (pipe(pipe_ready) || pipe(pipe_go) || pipe(pipe_result)) { | ||
| pr_perror("pipe"); | ||
| return 1; | ||
| } | ||
|
|
||
| /* | ||
| * Create a new PID namespace. The next fork()'d child | ||
| * will be PID 1 inside this new namespace. | ||
| */ | ||
| if (unshare(CLONE_NEWPID)) { | ||
| pr_perror("unshare(CLONE_NEWPID)"); | ||
| return 1; | ||
| } | ||
|
|
||
| child = fork(); | ||
| if (child < 0) { | ||
| pr_perror("fork child"); | ||
| return 1; | ||
| } | ||
|
|
||
| if (child == 0) { | ||
| /* | ||
| * Child: PID 1 inside the new PID namespace. | ||
| */ | ||
| pid_t my_pid; | ||
| char res = '0'; | ||
|
|
||
| close(pipe_ready[0]); | ||
| close(pipe_go[1]); | ||
| close(pipe_result[0]); | ||
|
|
||
| /* | ||
| * Create a new session inside the child PID namespace. | ||
| * Without this, getsid() returns 0 because the inherited | ||
| * session leader lives in the parent namespace and is not | ||
| * visible here. | ||
| */ | ||
| if (setsid() < 0) { | ||
| pr_perror("setsid"); | ||
| _exit(1); | ||
| } | ||
|
|
||
| my_pid = getpid(); | ||
| if (my_pid != 1) { | ||
| fprintf(stderr, "Child expected PID 1 before C/R, got %d\n", my_pid); | ||
| _exit(1); | ||
| } | ||
|
|
||
| /* Signal parent we're ready */ | ||
| write(pipe_ready[1], "R", 1); | ||
| close(pipe_ready[1]); | ||
|
|
||
| /* | ||
| * Wait for parent to tell us to check PID. | ||
| * Dump/restore happens while we're blocked here. | ||
| */ | ||
| if (read(pipe_go[0], &buf, 1) != 1) { | ||
| _exit(1); | ||
| } | ||
| close(pipe_go[0]); | ||
|
|
||
| /* After restore: verify PID is still 1 inside our namespace */ | ||
| my_pid = getpid(); | ||
| if (my_pid != 1) { | ||
| fprintf(stderr, "Child expected PID 1 after C/R, got %d\n", my_pid); | ||
| res = '1'; | ||
| } | ||
|
|
||
| write(pipe_result[1], &res, 1); | ||
| close(pipe_result[1]); | ||
| _exit(0); | ||
| } | ||
|
|
||
| /* Parent: in the original PID namespace */ | ||
| close(pipe_ready[1]); | ||
| close(pipe_go[0]); | ||
| close(pipe_result[1]); | ||
|
|
||
| /* Wait for child to be ready */ | ||
| if (read(pipe_ready[0], &buf, 1) != 1 || buf != 'R') { | ||
| pr_perror("child not ready"); | ||
| kill(child, SIGKILL); | ||
| return 1; | ||
| } | ||
| close(pipe_ready[0]); | ||
|
|
||
| test_msg("Child host PID: %d (namespace PID should be 1)\n", child); | ||
|
|
||
| /* Checkpoint happens here */ | ||
| test_daemon(); | ||
| test_waitsig(); | ||
|
|
||
| /* | ||
| * After restore: tell child to verify its PID and report. | ||
| * Use pipe instead of kill() since host PID may change. | ||
| */ | ||
| write(pipe_go[1], "G", 1); | ||
| close(pipe_go[1]); | ||
|
|
||
| /* Read result from child */ | ||
| if (read(pipe_result[0], &buf, 1) != 1) { | ||
| fail("Failed to read result from child"); | ||
| return 1; | ||
| } | ||
| close(pipe_result[0]); | ||
|
|
||
| ret = waitpid(child, &status, 0); | ||
| if (ret < 0 && errno == ECHILD) { | ||
| /* | ||
| * After restore, the host PID may have changed. | ||
| * Wait for any child instead. | ||
| */ | ||
| ret = waitpid(-1, &status, 0); | ||
| } | ||
|
|
||
| if (ret < 0) { | ||
| fail("waitpid: %m"); | ||
| return 1; | ||
| } | ||
|
|
||
| if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { | ||
| fail("Child exit status: exited=%d code=%d signaled=%d sig=%d", | ||
| WIFEXITED(status), WEXITSTATUS(status), | ||
| WIFSIGNALED(status), WTERMSIG(status)); | ||
| return 1; | ||
| } | ||
|
|
||
| if (buf != '0') { | ||
| fail("Child PID was not preserved across dump/restore"); | ||
| return 1; | ||
| } | ||
|
|
||
| pass(); | ||
| return 0; | ||
| } | ||
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 @@ | ||
| {'flavor': 'h', 'flags': 'suid'} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is why the feature shouldn't be labeled as 'support for single-level nested PID namespace dump/restore.' It feels more like a workaround for a specific use case. At a minimum, you should introduce an option to enable this feature; it should not be active by default. We probably need to do something to be sure that we use/mount proper /proc mounts...