File: procfs-cmdline-exe.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (115 lines) | stat: -rw-r--r-- 2,697 bytes parent folder | download | duplicates (7)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Read /proc/self/cmdline and /proc/self/exe such that it can be tested
 * whether Valgrind intercepts the system calls that access these pseudo-files
 * properly on Linux and whether Valgrind does not modify the behavior of
 * accessing these files on other operating systems.
 */

#define _ATFILE_SOURCE

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include "../../config.h"

static void test_cmdline(const char* const cwd, const char* const label,
                         const char* const path)
{
  int fd, n;
  char ch;
      
  fprintf(stderr, "%s:\n", label);
  fd = open(path, 0); 
  if (fd >= 0)
  {
    while ((n = read(fd, &ch, 1)) > 0)
    {
      if (ch == '\\')
        fprintf(stderr, "\\\\");
      else if (ch == 0)
        fprintf(stderr, "\\0");
      else if (isprint((unsigned)ch))
        fprintf(stderr, "%c", ch);
      else
        fprintf(stderr, "\\0%o", ch);
    }
    fprintf(stderr, "\n");
    close(fd);
  }
  else
    perror("open()");
}

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

  if ((n = readlink(path, buf, sizeof(buf) - 1)) >= 0)
  {
    buf[n] = 0;
    p = buf;
    if (strncmp(buf, cwd, strlen(cwd)) == 0)
      p += strlen(cwd);
    fprintf(stderr, "Result of readlink(\"%s\"): %s\n", label, p);
  }
  else
    perror("readlink");
}

static void test_readlinkat(const char* const cwd, const char* const label,
                            const char* const path)
{
#if HAVE_READLINKAT
  char buf[512];
  const char* p;
  int n;

  if ((n = readlinkat(AT_FDCWD, path, buf, sizeof(buf) - 1)) >= 0)
  {
    buf[n] = 0;
    p = buf;
    if (strncmp(buf, cwd, strlen(cwd)) == 0)
      p += strlen(cwd);
    fprintf(stderr, "Result of readlinkat(\"%s\"): %s\n", label, p);
  }
  else
    perror("readlinkat");
#else
  errno = ENOSYS;
  perror("readlinkat");
#endif
}

int main(int argc, char** argv)
{
  char cwd[512];
  char path[512];

  cwd[0] = 0;
  if (! getcwd(cwd, sizeof(cwd)))
    perror("getcwd");
  strcat(cwd, "/");

  snprintf(path, sizeof(path), "/proc/%ld/cmdline", (long) getpid());

  test_cmdline(cwd, "/proc/self/cmdline", "/proc/self/cmdline");
  test_cmdline(cwd, "/proc/<pid>/cmdline", path);

  snprintf(path, sizeof(path), "/proc/%ld/exe", (long) getpid());

  test_readlink(cwd, "/proc/self/exe", "/proc/self/exe");
  test_readlink(cwd, "/proc/<pid>/exe", path);

  test_readlinkat(cwd, "/proc/self/exe", "/proc/self/exe");
  test_readlinkat(cwd, "/proc/<pid>/exe", path);

  return 0;
}