basic/process-util: allow quoting of commandlines
Since the new functionality is controlled by an option, this causes no change in output yet, except tests. The login in the old branch of !(flags & PROCESS_CMDLINE_QUOTE) is essentially unmodified. But there is an important difference in behaviour: instead of unconditionally reading the whole virtual file, we now read only 'max_columns' bytes. This makes out code to write process lists quite a bit more efficient when there are processes with long command lines.
This commit is contained in:
@@ -248,9 +248,15 @@ static void test_get_process_cmdline_harder(void) {
|
||||
assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) == -ENOENT);
|
||||
|
||||
assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
|
||||
log_info("'%s'", line);
|
||||
assert_se(streq(line, "[testa]"));
|
||||
line = mfree(line);
|
||||
|
||||
assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_QUOTE, &line) >= 0);
|
||||
log_info("'%s'", line);
|
||||
assert_se(streq(line, "\"[testa]\"")); /* quoting is enabled here */
|
||||
line = mfree(line);
|
||||
|
||||
assert_se(get_process_cmdline(0, 0, PROCESS_CMDLINE_COMM_FALLBACK, &line) >= 0);
|
||||
log_info("'%s'", line);
|
||||
assert_se(streq(line, ""));
|
||||
@@ -288,6 +294,8 @@ static void test_get_process_cmdline_harder(void) {
|
||||
assert_se(streq(line, "[testa]"));
|
||||
line = mfree(line);
|
||||
|
||||
/* Test with multiple arguments that don't require quoting */
|
||||
|
||||
assert_se(write(fd, "foo\0bar", 8) == 8);
|
||||
|
||||
assert_se(get_process_cmdline(0, SIZE_MAX, 0, &line) >= 0);
|
||||
@@ -390,6 +398,32 @@ static void test_get_process_cmdline_harder(void) {
|
||||
assert_se(streq(line, "[aaaa bbbb …"));
|
||||
line = mfree(line);
|
||||
|
||||
/* Test with multiple arguments that do require quoting */
|
||||
|
||||
#define CMDLINE1 "foo\0'bar'\0\"bar$\"\0x y z\0!``\0"
|
||||
#define EXPECT1 "foo \"'bar'\" \"\\\"bar\\$\\\"\" \"x y z\" \"!\\`\\`\" \"\""
|
||||
assert_se(lseek(fd, SEEK_SET, 0) == 0);
|
||||
assert_se(write(fd, CMDLINE1, sizeof CMDLINE1) == sizeof CMDLINE1);
|
||||
assert_se(ftruncate(fd, sizeof CMDLINE1) == 0);
|
||||
|
||||
assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
|
||||
log_info("got: ==%s==", line);
|
||||
log_info("exp: ==%s==", EXPECT1);
|
||||
assert_se(streq(line, EXPECT1));
|
||||
line = mfree(line);
|
||||
|
||||
#define CMDLINE2 "foo\0\1\2\3\0\0"
|
||||
#define EXPECT2 "foo \"\\001\\002\\003\" \"\" \"\""
|
||||
assert_se(lseek(fd, SEEK_SET, 0) == 0);
|
||||
assert_se(write(fd, CMDLINE2, sizeof CMDLINE2) == sizeof CMDLINE2);
|
||||
assert_se(ftruncate(fd, sizeof CMDLINE2) == 0);
|
||||
|
||||
assert_se(get_process_cmdline(0, SIZE_MAX, PROCESS_CMDLINE_QUOTE, &line) >= 0);
|
||||
log_info("got: ==%s==", line);
|
||||
log_info("exp: ==%s==", EXPECT2);
|
||||
assert_se(streq(line, EXPECT2));
|
||||
line = mfree(line);
|
||||
|
||||
safe_close(fd);
|
||||
_exit(EXIT_SUCCESS);
|
||||
}
|
||||
@@ -403,6 +437,8 @@ static void test_rename_process_now(const char *p, int ret) {
|
||||
(ret == 0 && r >= 0) ||
|
||||
(ret > 0 && r > 0));
|
||||
|
||||
log_info_errno(r, "rename_process(%s): %m", p);
|
||||
|
||||
if (r < 0)
|
||||
return;
|
||||
|
||||
@@ -425,9 +461,12 @@ static void test_rename_process_now(const char *p, int ret) {
|
||||
if (r == 0 && detect_container() > 0)
|
||||
log_info("cmdline = <%s> (not verified, Running in unprivileged container?)", cmdline);
|
||||
else {
|
||||
log_info("cmdline = <%s>", cmdline);
|
||||
assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
|
||||
assert_se(startswith(p, cmdline));
|
||||
log_info("cmdline = <%s> (expected <%.*s>)", cmdline, (int) strlen("test-process-util"), p);
|
||||
|
||||
bool skip = cmdline[0] == '"'; /* A shortcut to check if the string is quoted */
|
||||
|
||||
assert_se(strneq(cmdline + skip, p, strlen("test-process-util")));
|
||||
assert_se(startswith(cmdline + skip, p));
|
||||
}
|
||||
} else
|
||||
log_info("cmdline = <%s> (not verified)", cmdline);
|
||||
|
||||
Reference in New Issue
Block a user