OSDN Git Service

Basic success/failure return from pgrep/pkill.
authorElliott Hughes <enh@google.com>
Sat, 4 Jun 2016 16:26:14 +0000 (09:26 -0700)
committerRob Landley <rob@landley.net>
Sat, 4 Jun 2016 21:57:18 +0000 (16:57 -0500)
The man page says they also return 2 for syntax errors and 3 for "fatal
error: out of memory etc", but I don't know how to implement that and
don't need it (or have any reason to believe anyone needs it).

Bug: 29092208

tests/pgrep.test
tests/pkill.test
toys/posix/ps.c

index 753a55a..3319c79 100755 (executable)
@@ -29,5 +29,9 @@ testing "-o pattern" "pgrep -o yes" "$proc\n" "" ""
 testing "-s" "pgrep -s $session_id yes" "$proc\n" "" ""
 testing "-P" "pgrep -P $proc_parent yes" "$proc\n" "" ""
 
+testing "return success" "pgrep yes && echo success" "$proc\nsuccess\n" "" ""
+testing "return failure" "pgrep almost-certainly-not || echo failure" \
+    "failure\n" "" ""
+
 #Clean-up
 killall yes >/dev/null 2>&1
index 0bea32a..3f20d26 100755 (executable)
@@ -92,10 +92,16 @@ testing "-P parent_prodId pattern" "pkill -P $proc_p yes && sleep 1 &&
 killall yes >/dev/null 2>&1
 
 yes >/dev/null &
-proc=$!
-proc_parent=`cat /proc/${proc}/stat | awk '{ print $4 }'`
 sleep 1
 testing "-9 pattern" "pkill -9 yes && sleep 1 &&
    (pgrep yes || echo 'yes')" "yes\n" "" ""
 killall yes >/dev/null 2>&1
 
+yes >/dev/null &
+sleep 1
+testing "return success" "pkill yes && echo success" "success\n" "" ""
+killall yes >/dev/null 2>&1
+
+testing "return failure" "pkill almost-certainly-not || echo failure" \
+    "failure\n" "" ""
+
index 82a8443..def81c5 100644 (file)
@@ -1685,7 +1685,10 @@ static void match_pgrep(struct carveup *tb)
     if ((toys.optflags&FLAG_v) ? !!reg : !reg) return;
   }
 
-  // Repurpose a field for -c count
+  // pgrep should return success if there's a match.
+  toys.exitval = 0;
+
+  // Repurpose a field for -c count.
   TT.sortpos++;
   if (toys.optflags&(FLAG_n|FLAG_o)) {
     long long ll = tb->slot[SLOT_starttime];
@@ -1738,6 +1741,9 @@ void pgrep_main(void)
   TT.match_process = pgrep_match_process;
   TT.show_process = (void *)match_pgrep;
 
+  // pgrep should return failure if there are no matches.
+  toys.exitval = 1;
+
   dirtree_read("/proc", get_ps);
   if (toys.optflags&FLAG_c) printf("%d\n", TT.sortpos);
   if (TT.pgrep.snapshot) {