1 /* Test whether /proc/{self,$PID}/path/a.out is correctly simulated. */
2
3 #include <limits.h>
4 #include <stdio.h>
5 #include <strings.h>
6 #include <unistd.h>
7 #include <sys/fcntl.h>
8
test_readlink(const char * cwd,const char * label,const char * path)9 static void test_readlink(const char *cwd, const char *label,
10 const char *path)
11 {
12 char buf[PATH_MAX];
13 int n;
14
15 if ((n = readlink(path, buf, sizeof(buf) - 1)) >= 0) {
16 const char *p;
17 size_t len = strlen(cwd);
18
19 buf[n] = '\0';
20
21 p = buf;
22 if (!strncmp(buf, cwd, len))
23 p += len;
24 printf("Result of readlink(\"%s\"): %s\n", label, p);
25 }
26 else
27 perror("readlink");
28 }
29
test_readlinkat(const char * cwd,const char * label,const char * path)30 static void test_readlinkat(const char *cwd, const char *label,
31 const char *path)
32 {
33 char buf[PATH_MAX];
34 int n;
35
36 if ((n = readlinkat(AT_FDCWD, path, buf, sizeof(buf) - 1)) >= 0) {
37 const char *p;
38 size_t len = strlen(cwd);
39
40 buf[n] = '\0';
41
42 p = buf;
43 if (!strncmp(buf, cwd, len))
44 p += len;
45 printf("Result of readlinkat(\"%s\"): %s\n", label, p);
46 }
47 else
48 perror("readlinkat");
49 }
50
main(void)51 int main(void)
52 {
53 char cwd[PATH_MAX];
54 char path[PATH_MAX];
55
56 cwd[0] = '\0';
57 if (!getcwd(cwd, sizeof(cwd) - 1)) /* '-1' to make room for '/' */
58 perror("getcwd");
59 strcat(cwd, "/");
60
61 snprintf(path, sizeof(path), "/proc/%ld/path/a.out", (long)getpid());
62
63 test_readlink(cwd, "/proc/self/path/a.out", "/proc/self/path/a.out");
64 test_readlink(cwd, "/proc/<pid>/path/a.out", path);
65
66 test_readlinkat(cwd, "/proc/self/path/a.out", "/proc/self/path/a.out");
67 test_readlinkat(cwd, "/proc/<pid>/path/a.out", path);
68
69 return 0;
70 }
71
72