2 * Read /proc/self/cmdline and /proc/self/exe such that it can be tested
3 * whether Valgrind intercepts the system calls that access these pseudo-files
4 * properly on Linux and whether Valgrind does not modify the behavior of
5 * accessing these files on other operating systems.
14 #include <sys/types.h>
18 #include "../../config.h"
20 static void test_cmdline(const char* const cwd
, const char* const label
,
21 const char* const path
)
26 fprintf(stderr
, "%s:\n", label
);
30 while ((n
= read(fd
, &ch
, 1)) > 0)
33 fprintf(stderr
, "\\\\");
35 fprintf(stderr
, "\\0");
36 else if (isprint((unsigned)ch
))
37 fprintf(stderr
, "%c", ch
);
39 fprintf(stderr
, "\\0%o", ch
);
41 fprintf(stderr
, "\n");
48 static void test_readlink(const char* const cwd
, const char* const label
,
49 const char* const path
)
55 if ((n
= readlink(path
, buf
, sizeof(buf
) - 1)) >= 0)
59 if (strncmp(buf
, cwd
, strlen(cwd
)) == 0)
61 fprintf(stderr
, "Result of readlink(\"%s\"): %s\n", label
, p
);
67 static void test_readlinkat(const char* const cwd
, const char* const label
,
68 const char* const path
)
75 if ((n
= readlinkat(AT_FDCWD
, path
, buf
, sizeof(buf
) - 1)) >= 0)
79 if (strncmp(buf
, cwd
, strlen(cwd
)) == 0)
81 fprintf(stderr
, "Result of readlinkat(\"%s\"): %s\n", label
, p
);
91 int main(int argc
, char** argv
)
97 if (! getcwd(cwd
, sizeof(cwd
)))
101 snprintf(path
, sizeof(path
), "/proc/%ld/cmdline", (long) getpid());
103 test_cmdline(cwd
, "/proc/self/cmdline", "/proc/self/cmdline");
104 test_cmdline(cwd
, "/proc/<pid>/cmdline", path
);
106 snprintf(path
, sizeof(path
), "/proc/%ld/exe", (long) getpid());
108 test_readlink(cwd
, "/proc/self/exe", "/proc/self/exe");
109 test_readlink(cwd
, "/proc/<pid>/exe", path
);
111 test_readlinkat(cwd
, "/proc/self/exe", "/proc/self/exe");
112 test_readlinkat(cwd
, "/proc/<pid>/exe", path
);