procps/testsuite/pgrep.test/pgrep.exp
Chris Down 866abacf88 pgrep: Support matching on the presence of a userspace signal handler
In production we've had several incidents over the years where a process
has a signal handler registered for SIGHUP or one of the SIGUSR signals
which can be used to signal a request to reload configs, rotate log
files, and the like. While this may seem harmless enough, what we've
seen happen repeatedly is something like the following:

1. A process is using SIGHUP/SIGUSR[12] to request some
   application-handled state change -- reloading configs, rotating a log
   file, etc;
2. This kind of request is deprecated and removed, so the signal handler
   is removed. However, a site where the signal might be sent from is
   missed (often logrotate or a service manager);
3. Because the default disposition of these signals is terminal, sooner
   or later these applications are going to be sent SIGHUP or similar
   and end up unexpectedly killed.

I know for a fact that we're not the only organisation experiencing
this: in general, signal use is pretty tricky to reason about and safely
remove because of the fairly aggressive SIG_DFL behaviour for some
common signals, especially for SIGHUP which has a particularly ambiguous
meaning. Especially in a large, highly interconnected codebase,
reasoning about signal interactions between system configuration and
applications can be highly complex, and it's inevitable that on occasion
a callsite will be missed.

In some cases the right call to avoid this will be to migrate services
towards other forms of IPC for this purpose, but inevitably there will
be some services which must continue using signals, so we need a safe
way to support them.

This patch adds support for the -H/--require-handler flag, which matches
on processes with a userspace handler present for the signal being sent.

With this flag we can enforce that all SIGHUP reload cases and SIGUSR
equivalents use --require-handler. This effectively mitigates the case
we've seen time and time again where SIGHUP is used to rotate log files
or reload configs, but the sending site is mistakenly left present after
the removal of signal handler, resulting in unintended termination of
the process.

Signed-off-by: Chris Down <chris@chrisdown.name>
2023-01-15 04:05:40 +00:00

128 lines
3.8 KiB
Plaintext

#
# Dejagnu tests for pgrep - part of procps
#
set mypid [pid]
set not_ppid [ expr { $mypid + 1 } ]
set pgrep "${topdir}src/pgrep"
set uid [ exec id -u ]
set not_uid [ expr { $uid + 1 } ]
set gid [ exec id -g ]
set not_gid [ expr { $gid + 1 } ]
set ps "${topdir}src/ps/pscommand"
set tty [ get_tty ]
set test "pgprep with no arguments"
spawn $pgrep
expect_pass "$test" "^\(lt-\)\?pgrep: no matching criteria specified\\s*"
make_testproc
set testproc_len [ string length $testproc_comm ]
set testproc_trim [ string range $testproc_comm 0 [ expr { $testproc_len - 2 } ] ]
set testproc1_sid [ string trim [ exec $ps --no-headers -o sid $testproc1_pid ] ]
set not_testproc1_sid [ expr { $testproc1_sid + 1 } ]
set test "pgrep find both test pids"
spawn $pgrep $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
# In Debian only
set test "pgrep counts 2 test pids"
spawn $pgrep -c $testproc_comm
expect_pass "$test" "^2\\s*"
set test "pgrep with : delimiter"
spawn $pgrep -d : $testproc_comm
expect_pass "$test" "^${testproc1_pid}:${testproc2_pid}\\s*$"
set test "pgrep match against full process name"
spawn $pgrep -f "$testproc_path\\s+$testproc_arg_str"
expect_pass "$test" "^$testproc1_pid\\s*$"
set test "pgrep with matching gid"
spawn $pgrep -G $gid $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
set test "pgrep with not matching gid"
spawn $pgrep -G $not_gid $testproc_comm
expect_blank $test
set test "pgrep with process name"
spawn $pgrep -l $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc_comm\\s+$testproc2_pid\\s+$testproc_comm\\s*$"
set test "pgrep with full command line"
spawn $pgrep -af "$testproc_path$"
expect_pass "$test" "^$testproc2_pid\\s+$testproc_path\\s*$"
set test "pgrep find newest test pid"
spawn $pgrep -n $testproc_comm
expect_pass "$test" "^$testproc2_pid\\s*$"
set test "pgrep find oldest test pid"
spawn $pgrep -o $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s*$"
set test "pgrep matches with parent pid"
spawn $pgrep -P $mypid $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
set test "pgrep doesn't match with bogus parent pid"
spawn $pgrep -P $not_ppid $testproc_comm
expect_blank "$test"
set test "pgrep matches with its own sid"
spawn $pgrep -s $testproc1_sid $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
set test "pgrep doesn't match with bogus sid"
spawn $pgrep -s $not_testproc1_sid $testproc_comm
expect_blank "$test"
set test "pgrep matches on tty"
if { $tty == "" } {
untested "$test"
} else {
spawn $pgrep -t $tty $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
}
set test "pgrep doesn't match with bogus tty"
spawn $pgrep -t glass $testproc_comm
expect_blank "$test"
set test "pgrep with matching euid"
spawn $pgrep -u $uid $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
set test "pgrep with not matching euid"
spawn $pgrep -u $not_uid $testproc_comm
expect_blank $test
set test "pgrep with matching uid"
spawn $pgrep -U $uid $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
set test "pgrep with not matching uid"
spawn $pgrep -U $not_uid $testproc_comm
expect_blank $test
set test "pgrep matches on substring"
spawn $pgrep $testproc_trim
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
set test "pgrep matches full string with exact"
spawn $pgrep -x $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
set test "pgrep does not match substring with exact"
spawn $pgrep -x $testproc_trim
expect_blank $test
set test "pgrep with long non-matching pattern gives warning"
spawn $pgrep gnome-session-bi
expect_pass "$test" "pattern that searches for process name longer than 15 characters will result in zero matches"
# Cleanup
kill_testproc