File: proc_aout.c

package info (click to toggle)
valgrind 1%3A3.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 158,568 kB
  • sloc: ansic: 746,130; exp: 26,134; xml: 22,708; asm: 13,570; cpp: 7,691; makefile: 6,177; perl: 5,965; sh: 5,665; javascript: 929
file content (71 lines) | stat: -rw-r--r-- 1,600 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Test whether /proc/{self,$PID}/path/a.out is correctly simulated. */

#include <limits.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <sys/fcntl.h>

static void test_readlink(const char *cwd, const char *label,
      const char *path)
{
   char buf[PATH_MAX];
   int n;

   if ((n = readlink(path, buf, sizeof(buf) - 1)) >= 0) {
      const char *p;
      size_t len = strlen(cwd);

      buf[n] = '\0';

      p = buf;
      if (!strncmp(buf, cwd, len))
         p += len;
      printf("Result of readlink(\"%s\"): %s\n", label, p);
   }
   else
      perror("readlink");
}

static void test_readlinkat(const char *cwd, const char *label,
      const char *path)
{
   char buf[PATH_MAX];
   int n;

   if ((n = readlinkat(AT_FDCWD, path, buf, sizeof(buf) - 1)) >= 0) {
      const char *p;
      size_t len = strlen(cwd);

      buf[n] = '\0';

      p = buf;
      if (!strncmp(buf, cwd, len))
         p += len;
      printf("Result of readlinkat(\"%s\"): %s\n", label, p);
   }
   else
      perror("readlinkat");
}

int main(void)
{
   char cwd[PATH_MAX];
   char path[PATH_MAX];

   cwd[0] = '\0';
   if (!getcwd(cwd, sizeof(cwd) - 1)) /* '-1' to make room for '/' */
      perror("getcwd");
   strcat(cwd, "/");

   snprintf(path, sizeof(path), "/proc/%ld/path/a.out", (long)getpid());

   test_readlink(cwd, "/proc/self/path/a.out", "/proc/self/path/a.out");
   test_readlink(cwd, "/proc/<pid>/path/a.out", path);

   test_readlinkat(cwd, "/proc/self/path/a.out", "/proc/self/path/a.out");
   test_readlinkat(cwd, "/proc/<pid>/path/a.out", path);

   return 0;
}