aboutsummaryrefslogtreecommitdiff
path: root/none/tests/solaris/proc_psinfo.c
blob: da4109f66be14a40bcd83a7e0694538d048c2f4f (plain)
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
/*
 * Reads /proc/self/psinfo such that it can be tested whether Valgrind
 * intercepts the system calls that access this pseudo-file.
 */

#include <fcntl.h>
#include <limits.h>
#include <procfs.h>
#include <stdio.h>
#include <unistd.h>

static void test_psinfo(int op, const char *label,
                        const char *path)
{
   int fd;
   if (op == 0) {
      printf("open for %s:\n", label);

      fd = open(path, O_RDONLY); 
      if (fd < 0) {
         perror("open");
         return;
      }
   } else {
      printf("openat for %s:\n", label);

      fd = openat(AT_FDCWD, path, O_RDONLY);
      if (fd < 0) {
         perror("openat");
         return;
      }
   }

   psinfo_t psinfo;
   ssize_t bytes = read(fd, &psinfo, sizeof(psinfo));
   if (bytes != sizeof(psinfo)) {
      perror("read");
      return;
   }

   printf("fname: %s\n", psinfo.pr_fname);
   printf("psargs: %s\n", psinfo.pr_psargs);

   printf("argc: %d\n", psinfo.pr_argc);
   unsigned int i;
   char **argv = (char **) psinfo.pr_argv;
   for (i = 0; i < psinfo.pr_argc; i++) {
      printf("argv[%u]: %s\n", i, argv[i]);
   }

   close(fd);
}

int main(int argc, const char *argv[])
{
   char path[PATH_MAX];
   snprintf(path, sizeof(path), "/proc/%ld/psinfo", (long int) getpid());

   test_psinfo(0, "/proc/self/psinfo", "/proc/self/psinfo");
   printf("\n");
   test_psinfo(0, "/proc/<pid>/psinfo", path);
   printf("\n");

   test_psinfo(1, "/proc/self/psinfo", "/proc/self/psinfo");
   printf("\n");
   test_psinfo(1, "/proc/<pid>/psinfo", path);

   return 0;
}