Closed N-Storm closed 4 months ago
One of the possible fixes example:
--- a/json.c
+++ b/json.c
@@ -1256,8 +1256,8 @@ json_print_PRC(char *hp, struct sstat *ss,
struct tstat *ps, int nact,
struct cgchainer *cs, int ncgroups)
{
- register int i;
- char *cgrpath;
+ register int i, backslashes = 0;
+ char *cgrpath, *cgrout;
printf(", %s: [", hp);
@@ -1268,10 +1268,27 @@ json_print_PRC(char *hp, struct sstat *ss,
printf(", ");
}
- if (supportflags & CGROUPV2 && ps->gen.cgroupix != -1)
+ if (supportflags & CGROUPV2 && ps->gen.cgroupix != -1) {
cgrpath = cggetpath((cs + ps->gen.cgroupix), cs);
+ for (const char* p = cgrpath; *p != '\0'; ++p) {
+ if (*p == '\\')
+ ++backslashes;
+ }
+
+ cgrout = calloc(sizeof(char), strlen(cgrpath) + backslashes + 1);
+ ptrverify(cgrout, "Malloc failed for output cgroup path\n");
+ char *p = cgrout, *p2 = cgrpath;
+ while (*p2) {
+ *p++ = *p2;
+ if (*p2 == '\\')
+ *p++ = '\\'; // Double the backslash
+ p2++;
+ }
+ *p = '\0'; // Null terminate the new string
+ free(cgrpath);
+ }
else
- cgrpath = "-";
+ cgrout = "-";
printf("{\"pid\": %d, "
"\"name\": \"(%.19s)\", "
@@ -1302,10 +1319,10 @@ json_print_PRC(char *hp, struct sstat *ss,
ps->cpu.nvcsw,
ps->cpu.nivcsw,
ps->cpu.sleepavg,
- cgrpath);
+ cgrout);
if (supportflags & CGROUPV2 && ps->gen.cgroupix != -1)
- free(cgrpath);
+ free(cgrout);
}
printf("]");
Testing:
$ ./atop -J PRC 1 1 | sed -E 's/,/,\n/g' | jq | grep 'x2d'
"cgroup": "/system.slice/system-serial\\x2dgetty.slice/serial-getty@ttyS0.service"
"cgroup": "/system.slice/system-serial\\x2dgetty.slice/serial-getty@ttyS1.service"
Coding style & naming probably should be converted to adapt to project style. And escape routine should be done as a function, similar to parsable.c.
In json.c v2.10.0 adds cgroup field to JSON output of PR* labels. Which prints cgrpath variable. On most modern Linux systems with SystemD slices could include backslash escaped codes. For example Debian 12 based system:
These, when printed in JSON output as is produce incorrect JSON:
These should be escaped as '\' to produce valid JSON:
So
char *cgrpath
should double backslashes in json.c before printing.