• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Reads /proc/self/psinfo such that it can be tested whether Valgrind
3  * intercepts the system calls that access this pseudo-file.
4  */
5 
6 #include <fcntl.h>
7 #include <limits.h>
8 #include <procfs.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 
test_psinfo(int op,const char * label,const char * path)12 static void test_psinfo(int op, const char *label,
13                         const char *path)
14 {
15    int fd;
16    if (op == 0) {
17       printf("open for %s:\n", label);
18 
19       fd = open(path, O_RDONLY);
20       if (fd < 0) {
21          perror("open");
22          return;
23       }
24    } else {
25       printf("openat for %s:\n", label);
26 
27       fd = openat(AT_FDCWD, path, O_RDONLY);
28       if (fd < 0) {
29          perror("openat");
30          return;
31       }
32    }
33 
34    psinfo_t psinfo;
35    ssize_t bytes = read(fd, &psinfo, sizeof(psinfo));
36    if (bytes != sizeof(psinfo)) {
37       perror("read");
38       return;
39    }
40 
41    printf("fname: %s\n", psinfo.pr_fname);
42    printf("psargs: %s\n", psinfo.pr_psargs);
43 
44    printf("argc: %d\n", psinfo.pr_argc);
45    unsigned int i;
46    char **argv = (char **) psinfo.pr_argv;
47    for (i = 0; i < psinfo.pr_argc; i++) {
48       printf("argv[%u]: %s\n", i, argv[i]);
49    }
50 
51    close(fd);
52 }
53 
main(int argc,const char * argv[])54 int main(int argc, const char *argv[])
55 {
56    char path[PATH_MAX];
57    snprintf(path, sizeof(path), "/proc/%ld/psinfo", (long int) getpid());
58 
59    test_psinfo(0, "/proc/self/psinfo", "/proc/self/psinfo");
60    printf("\n");
61    test_psinfo(0, "/proc/<pid>/psinfo", path);
62    printf("\n");
63 
64    test_psinfo(1, "/proc/self/psinfo", "/proc/self/psinfo");
65    printf("\n");
66    test_psinfo(1, "/proc/<pid>/psinfo", path);
67 
68    return 0;
69 }
70