Thread (28 messages) 28 messages, 5 authors, 2019-11-13

Re: [PATCH v2 2/2] pidfd: add tests for NSpid info in fdinfo

flat view

From: Jann Horn <jannh@google.com>
Date: 2019-10-11 15:09:58
Also in: linux-kselftest, lkml

On Wed, Oct 9, 2019 at 6:10 PM Christian Kellner [off-list ref] wrote:
Add tests that check that if pid namespaces are configured the fdinfo
file of a pidfd contains an NSpid: entry containing the process id
in the current and additionally all nested namespaces.
[...]
quoted hunk ↗ jump to hunk
+static int compare_fdinfo_nspid(int pidfd, char *expect, size_t len)
+{
+       char path[512];
+       FILE *f;
+       size_t n = 0;
+       ssize_t k;
+       char *line = NULL;
+       int r = -1;
+
+       snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd);
(Maybe at some point the selftests code should add some more concise
alternative to snprintf() calls on separate lines. A macro or
something like that so that you can write stuff like `f =
fopen(tprintf("/proc/self/fdinfo/%d", pidfd), "re")`.)
quoted hunk ↗ jump to hunk
+       f = fopen(path, "re");
+       if (!f)
+               return -1;
+
+       while ((k = getline(&line, &n, f)) != -1) {
+               if (strncmp(line, "NSpid:", 6))
+                       continue;
+
+               line[k - 1] = '\0';
+               ksft_print_msg("Child: fdinfo NSpid line: '%s'.\n", line);
+               r = strncmp(line + 6, expect, len);
Wouldn't it be better to get rid of the nullbyte assignment and change
the strncmp() into a strcmp() here...

[...]
quoted hunk ↗ jump to hunk
+       /* The child will have pid 1 in the new pid namespace,
+        * so the line must be 'NSPid:\t<pid>\t1'
+        */
+       n = snprintf(expect, sizeof(expect), "\t%d\t%d", pid, 1);
... and add a "\n" to the format string? It's shorter and doesn't
silently ignore it if the line doesn't end at that point.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help