Skip to content

Commit ece2f89

Browse files
committed
Add tests and documentation for Pdeathsig behavior in rootlesskit
Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
1 parent bc7e657 commit ece2f89

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

hack/integration-pdeathsig.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
# Test script to verify Pdeathsig behavior using rootlesskit itself
3+
# This script:
4+
# 1. Uses rootlesskit to spawn a long-running process
5+
# 2. Kills the rootlesskit parent process
6+
# 3. Verifies that the child process is killed as expected
7+
8+
source $(realpath $(dirname $0))/common.inc.sh
9+
10+
INFO "Starting Pdeathsig test using rootlesskit..."
11+
12+
# Create a temporary directory for test artifacts
13+
TEMP_DIR=$(mktemp -d)
14+
INFO "Created temporary directory: $TEMP_DIR"
15+
16+
# Create a marker file that will be touched by the child process if it's still alive
17+
MARKER_FILE="$TEMP_DIR/child_still_alive"
18+
19+
# Create a script that will be executed by rootlesskit
20+
CHILD_SCRIPT="$TEMP_DIR/child_script.sh"
21+
cat > "$CHILD_SCRIPT" << 'EOF'
22+
#!/bin/bash
23+
echo "Child process started with PID: $$"
24+
echo "Parent PID: $PPID"
25+
26+
# Register a trap to handle signals
27+
trap 'echo "Child received signal, exiting"; exit 1' TERM INT
28+
29+
# Run for 30 seconds, checking if parent is still alive
30+
for i in {1..30}; do
31+
echo "Child still running (iteration $i)..."
32+
33+
# Check if parent has changed (died)
34+
CURRENT_PPID=$(ps -o ppid= -p $$)
35+
if [ "$CURRENT_PPID" != "$PPID" ]; then
36+
echo "Parent changed from $PPID to $CURRENT_PPID"
37+
if [ "$CURRENT_PPID" = "1" ]; then
38+
echo "Parent is now init (PID 1), parent has died"
39+
echo "Child should be killed by Pdeathsig, but if you see this message, it wasn't"
40+
touch MARKER_FILE_PLACEHOLDER
41+
exit 1
42+
fi
43+
fi
44+
45+
sleep 1
46+
done
47+
48+
# If we reach here, the child wasn't killed
49+
echo "Child completed normally (this shouldn't happen if Pdeathsig is working)"
50+
touch MARKER_FILE_PLACEHOLDER
51+
EOF
52+
53+
# Replace the placeholder with the actual marker file path
54+
sed -i "s|MARKER_FILE_PLACEHOLDER|$MARKER_FILE|g" "$CHILD_SCRIPT"
55+
chmod +x "$CHILD_SCRIPT"
56+
57+
# Start rootlesskit with the child script
58+
INFO "Starting rootlesskit..."
59+
$ROOTLESSKIT "$CHILD_SCRIPT" &
60+
ROOTLESSKIT_PID=$!
61+
INFO "Rootlesskit started with PID: $ROOTLESSKIT_PID"
62+
63+
# Wait a moment for the child to start
64+
sleep 2
65+
66+
# Find the child process
67+
CHILD_PID=$(pgrep -P $ROOTLESSKIT_PID)
68+
if [ -z "$CHILD_PID" ]; then
69+
ERROR "Failed to find child process"
70+
exit 1
71+
fi
72+
INFO "Found child process with PID: $CHILD_PID"
73+
74+
# Kill the rootlesskit process
75+
INFO "Killing rootlesskit process (PID: $ROOTLESSKIT_PID)..."
76+
kill -9 $ROOTLESSKIT_PID
77+
78+
# Wait a moment for the child to be killed
79+
sleep 2
80+
81+
# Check if the child process is still running
82+
if ps -p $CHILD_PID > /dev/null; then
83+
ERROR "FAIL: Child process (PID: $CHILD_PID) is still running after parent was killed"
84+
kill -9 $CHILD_PID # Clean up
85+
exit 1
86+
else
87+
INFO "PASS: Child process (PID: $CHILD_PID) was killed as expected"
88+
fi
89+
90+
# Check if the marker file exists
91+
if [ -f "$MARKER_FILE" ]; then
92+
ERROR "FAIL: Marker file exists, which means the child process wasn't killed by Pdeathsig"
93+
exit 1
94+
else
95+
INFO "PASS: Marker file doesn't exist, which means the child process was killed by Pdeathsig"
96+
fi
97+
98+
INFO "Test completed successfully!"
99+
rm -rf "$TEMP_DIR"
100+
exit 0

0 commit comments

Comments
 (0)