-
Notifications
You must be signed in to change notification settings - Fork 124
child: refactor command execution to use goroutines with Pdeathsig #514
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
Merged
AkihiroSuda
merged 2 commits into
rootless-containers:master
from
fahedouch:fix-Pdeathsig-behavior
Jun 12, 2025
+177
−13
Merged
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,125 @@ | ||
| #!/bin/bash | ||
| # Test script to verify Pdeathsig behavior using rootlesskit itself | ||
| # This script: | ||
| # 1. Uses rootlesskit to spawn a long-running process | ||
| # 2. Kills the rootlesskit parent process | ||
| # 3. Verifies that the child process is killed as expected | ||
| # 4. Tests both with --reaper true and --reaper false | ||
|
|
||
| source $(realpath $(dirname $0))/common.inc.sh | ||
|
|
||
| INFO "Starting Pdeathsig test using rootlesskit..." | ||
|
|
||
| # Function to run the test with a specific reaper setting | ||
| run_test() { | ||
| local reaper_setting=$1 | ||
| INFO "Testing with --reaper $reaper_setting" | ||
|
|
||
| # Create a temporary directory for test artifacts | ||
| TEMP_DIR=$(mktemp -d) | ||
| INFO "Created temporary directory: $TEMP_DIR" | ||
|
|
||
| # Create a marker file that will be touched by the child process if it's still alive | ||
| MARKER_FILE="$TEMP_DIR/child_still_alive" | ||
|
|
||
| # Create a script that will be executed by rootlesskit | ||
| CHILD_SCRIPT="$TEMP_DIR/child_script.sh" | ||
| cat > "$CHILD_SCRIPT" << 'EOF' | ||
| #!/bin/bash | ||
| echo "Child process started with PID: $$" | ||
| echo "Parent PID: $PPID" | ||
|
|
||
| # Register a trap to handle signals | ||
| trap 'echo "Child received signal, exiting"; exit 1' TERM INT | ||
|
|
||
| # Run for 30 seconds, checking if parent is still alive | ||
| for i in {1..30}; do | ||
| echo "Child still running (iteration $i)..." | ||
|
|
||
| # Check if parent has changed (died) | ||
| CURRENT_PPID=$(ps -o ppid= -p $$) | ||
| if [ "$CURRENT_PPID" != "$PPID" ]; then | ||
| echo "Parent changed from $PPID to $CURRENT_PPID" | ||
| if [ "$CURRENT_PPID" = "1" ]; then | ||
| echo "Parent is now init (PID 1), parent has died" | ||
| echo "Child should be killed by Pdeathsig, but if you see this message, it wasn't" | ||
| touch MARKER_FILE_PLACEHOLDER | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| sleep 1 | ||
| done | ||
|
|
||
| # If we reach here, the child wasn't killed | ||
| echo "Child completed normally (this shouldn't happen if Pdeathsig is working)" | ||
| touch MARKER_FILE_PLACEHOLDER | ||
| EOF | ||
|
|
||
| # Replace the placeholder with the actual marker file path | ||
| sed -i "s|MARKER_FILE_PLACEHOLDER|$MARKER_FILE|g" "$CHILD_SCRIPT" | ||
| chmod +x "$CHILD_SCRIPT" | ||
|
|
||
| # Start rootlesskit with the child script | ||
| INFO "Starting rootlesskit with --reaper $reaper_setting..." | ||
| if [ "$reaper_setting" = "true" ]; then | ||
| $ROOTLESSKIT --reaper $reaper_setting --pidns "$CHILD_SCRIPT" & | ||
| else | ||
| $ROOTLESSKIT --reaper $reaper_setting "$CHILD_SCRIPT" & | ||
| fi | ||
| ROOTLESSKIT_PID=$! | ||
| INFO "Rootlesskit started with PID: $ROOTLESSKIT_PID" | ||
|
|
||
| # Wait a moment for the child to start | ||
| sleep 2 | ||
|
|
||
| # Find the child process | ||
| ROOTLESSKIT_CHILD_PID=$(pgrep -P $ROOTLESSKIT_PID) | ||
| if [ -z "$ROOTLESSKIT_CHILD_PID" ]; then | ||
| ERROR "Failed to find rootlesskit child process" | ||
| return 1 | ||
| fi | ||
| INFO "Found rootlesskit child process with PID: $ROOTLESSKIT_CHILD_PID" | ||
|
|
||
| # Kill the rootlesskit process | ||
| INFO "Killing rootlesskit process (PID: $ROOTLESSKIT_PID)..." | ||
| kill -9 $ROOTLESSKIT_PID | ||
|
|
||
| # Wait a moment for the rootlesskit child to be killed | ||
| sleep 2 | ||
|
|
||
| # Check if the rootlesskit child process is still running | ||
| if ps -p $ROOTLESSKIT_CHILD_PID > /dev/null; then | ||
| ERROR "FAIL: Rootlesskit Child process (PID: $ROOTLESSKIT_CHILD_PID) is still running after rootlesskit parent was killed" | ||
| kill -9 $ROOTLESSKIT_CHILD_PID # Clean up | ||
| return 1 | ||
| else | ||
| INFO "PASS: Rootlesskit Child process (PID: $ROOTLESSKIT_CHILD_PID) was killed as expected" | ||
| fi | ||
|
|
||
| # Check if the marker file exists | ||
| if [ -f "$MARKER_FILE" ]; then | ||
| ERROR "FAIL: Marker file exists, which means the child process wasn't killed by Pdeathsig" | ||
| return 1 | ||
| else | ||
| INFO "PASS: Marker file doesn't exist, which means the child process was killed by Pdeathsig" | ||
| fi | ||
|
|
||
| INFO "Test with --reaper $reaper_setting completed successfully!" | ||
| rm -rf "$TEMP_DIR" | ||
| return 0 | ||
| } | ||
|
|
||
| # Run tests with both reaper settings | ||
| if ! run_test "true"; then | ||
| ERROR "Test with --reaper true failed" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if ! run_test "false"; then | ||
| ERROR "Test with --reaper false failed" | ||
| exit 1 | ||
| fi | ||
|
|
||
| INFO "All tests completed successfully!" | ||
| exit 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
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.