From ae9676a337e38526b994d189444f3fd02ab800ad Mon Sep 17 00:00:00 2001 From: Jaromir Capik Date: Fri, 24 Jan 2014 18:07:34 +0100 Subject: [PATCH] library: skip replacement of trailing '\0' in read_unvectored() Under some circumstances the ksh shell doesn't fork new processes when executing scripts and the script is interpreted by the parent process. That makes the execution faster, but it means ksh needs to reuse the /proc/PID/cmdline for the new script name and arguments while the file length needs to stay untouched. The fork is skipped only when the new cmdline is shorter than the parent's cmdline and the rest of the file is filled with '\0'. This is perfectly ok until we try to read the cmdline of such process. As the read_unvectored() function replaces all zeros with chosen separator, these trailing zeros are replaced with spaces in case of the ps tool. Consequently it appends multiple spaces at the end of the arguments string even when these zeros do not represent any separators and therefore shouldn't be replaced. With this commit the read_unvectored() function skips the replacement of trailing zeros and separates valid content only. Reference: https://bugzilla.redhat.com/show_bug.cgi?id=1057600 --- proc/readproc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/proc/readproc.c b/proc/readproc.c index 286e1736..46dfa101 100644 --- a/proc/readproc.c +++ b/proc/readproc.c @@ -686,6 +686,7 @@ static int read_unvectored(char *restrict const dst, unsigned sz, const char* wh close(fd); if(n){ int i=n; + while(i && dst[i-1]=='\0') --i; // skip trailing zeroes while(i--) if(dst[i]=='\n' || dst[i]=='\0') dst[i]=sep; if(dst[n-1]==' ') dst[n-1]='\0';