Process `match "regex"` now requires an extra space at the end

Issue #926 resolved
Ciprian Dorin Craciun created an issue

I have the following process in my Monit configuration:

check process "system-ssh"
    match "^/usr/sbin/sshd -D$"

In version 5.25.1 it correctly identifies the process.

In version 5.27 it fails to match it and requires an extra space between -D and $, namely:

check process "system-ssh"
    match "^/usr/sbin/sshd -D $"

(I've classified this issue as "major" because it causes a regression of previously correct behavior.)


My assumption is that when reading from /proc/$PID/cmdline the last \0 used to be stripped, and now it was converted to a space:

>>> hexdump -C /proc/1183/cmdline
00000000  2f 75 73 72 2f 73 62 69  6e 2f 73 73 68 64 00 2d  |/usr/sbin/sshd.-|
00000010  44 00                                             |D.|
00000012

I've applied the following patch that solves the issue (at least on Linux):

--- ./src/process/sysdep_LINUX.c.orig   2020-06-18 16:07:56.000000000 +0300
+++ ./src/process/sysdep_LINUX.c    2020-08-07 18:43:03.099566896 +0300
@@ -336,10 +336,12 @@
                                         StringBuffer_append(proc->name, " ");
                                 else
                                         StringBuffer_append(proc->name, "%c", buf[i]);
                         }
                 }
+                if (StringBuffer_length(proc->name) > 0)
+                        StringBuffer_delete(proc->name, StringBuffer_length(proc->name) - 1);
                 fclose(f);
                 // Fallback to procfs stat process name if cmdline was empty (even kernel-space processes have information here)
                 if (! StringBuffer_length(proc->name)) {
                         char buffer[8192];
                         char *tmp = NULL;

Comments (1)

  1. Log in to comment