tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

pgrep -x broken



This commit broke pgrep -x as it compares against the full path in argv[0]:
http://cvsweb.netbsd.org/bsdweb.cgi/src/usr.bin/pkill/pkill.c.diff?r1=1.27&r2=1.28&only_with_tag=MAIN&f=h

# ps ax | grep inetd
 1432 ?        Is     0:00.00 /usr/sbin/inetd -l
20479 pts/0    O+     0:00.00 grep inetd
# pgrep -x inetd
#

The reasoning was the p_comm may be truncated which may confuse the end-user as known running processes will not be matched (however, the 'fix' above means they'll never be matched which is considerably more confusion). The FreeBSD man page notes this trunctation:

"However, presently FreeBSD will only keep track of the first 19 characters of the command name for each process. Attempts to match any characters after the first 19 of a command name will quietly fail."

Options are:
1) Revert above and always use p_comm (and note in man page)
2) Use p_comm only if -x flag given:
--- pkill.c     18 Mar 2017 05:13:22 -0000      1.29.8.2
+++ pkill.c     19 Jan 2018 15:12:50 -0000
@@ -296,7 +296,7 @@
                                            pargv[0]);
                                        pargv++;
                                }
-                       } else if (pargv[0] != NULL)
+                       } else if (pargv[0] != NULL && !fullmatch)
                                strlcpy(buf, pargv[0], sizeof(buf));
                        else
                                strlcpy(buf, kp->p_comm, sizeof(buf));
3) Chop off at last /:
-                       } else if (pargv[0] != NULL)
-                               strlcpy(buf, pargv[0], sizeof(buf));
-                       else
+                       } else if (pargv[0] != NULL) {
+                               q = strrchr(pargv[0], (int)'/');
+                               if (q == NULL || !fullmatch)
+                                       q = pargv[0];
+                               else
+                                       q++;
+                               strlcpy(buf, q, sizeof(buf));
+                       } else
                                strlcpy(buf, kp->p_comm, sizeof(buf));

I favour 1), any dissent before I commit?

--
Stephen


Home | Main Index | Thread Index | Old Index