summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Cox <me@jasoncarloscox.com>2023-01-25 20:34:21 +1100
committerCraig Small <csmall@dropbear.xyz>2023-01-25 20:34:21 +1100
commit848be2b1e23d4db4420dcfc15d3166073e813f4e (patch)
tree7372d710c50ea1f434d5f08bb2bf5f7602cb9c39
parent356730edbb96c73c6145103ab86445f96602ba75 (diff)
downloadprocps-ng-848be2b1e23d4db4420dcfc15d3166073e813f4e.tar.gz
pgrep: make --terminal respect other criteria
In some cases the --terminal option to pgrep will cause all processes matching the terminal to be output, even if other criteria would exclude them. Specifically, I noticed that it overrides the --runstates option. Signed-off-by: Craig Small <csmall@dropbear.xyz>
-rw-r--r--NEWS1
-rw-r--r--src/pgrep.c4
-rw-r--r--testsuite/pgrep.test/pgrep.exp14
3 files changed, 12 insertions, 7 deletions
diff --git a/NEWS b/NEWS
index 40a39e9..a0cb848 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ procps-ng-NEXT
---------------
* docs: Don't install English manpages twice
* pgrep: Add -H match on userspace signal handler merge #165
+ * pgrep: make --terminal respect other criteria
* ps: c flag shows command name again Debian #1026326
* ps.1: Match drs description from top.1 merge #156
* skill: Match on -p again Debian #1025915
diff --git a/src/pgrep.c b/src/pgrep.c
index 7d0cc93..6679a8c 100644
--- a/src/pgrep.c
+++ b/src/pgrep.c
@@ -706,8 +706,8 @@ static struct el * select_procs (int *num)
match = 0;
else if (opt_older && (int)PIDS_GETFLT(ELAPSED) < opt_older)
match = 0;
- else if (opt_term)
- match = match_strlist(PIDS_GETSTR(TTYNAME), opt_term);
+ else if (opt_term && ! match_strlist(PIDS_GETSTR(TTYNAME), opt_term))
+ match = 0;
else if (opt_runstates && ! strchr(opt_runstates, PIDS_GETSCH(STA)))
match = 0;
else if (opt_cgroup && ! match_cgroup_list (PIDS_GETSTV(CGROUP), opt_cgroup))
diff --git a/testsuite/pgrep.test/pgrep.exp b/testsuite/pgrep.test/pgrep.exp
index 1110e70..0c4836f 100644
--- a/testsuite/pgrep.test/pgrep.exp
+++ b/testsuite/pgrep.test/pgrep.exp
@@ -5,9 +5,9 @@ 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 not_uid [ expr { $uid + 1 } ]
set gid [ exec id -g ]
-set not_gid [ expr { $gid + 1 } ]
+set not_gid [ expr { $gid + 1 } ]
set ps "${topdir}src/ps/pscommand"
set tty [ get_tty ]
@@ -79,12 +79,16 @@ 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"
+set test1 "pgrep matches on tty"
+set test2 "pgrep doesn't match on tty and wrong runstate"
if { $tty == "" } {
- untested "$test"
+ untested "$test1"
+ untested "$test2"
} else {
spawn $pgrep -t $tty $testproc_comm
- expect_pass "$test" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
+ expect_pass "$test1" "^$testproc1_pid\\s+$testproc2_pid\\s*$"
+ spawn $pgrep -t $tty -r D $testproc_comm
+ expect_blank "$test2"
}
set test "pgrep doesn't match with bogus tty"