1 /* Test that /proc/{self,$PID}/auxv can be opened and read simultaneously
2    using two different file descriptors. */
3 
4 #include <assert.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/auxv.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 
14 /* Reads one auxv_t entry from the input file. */
read_entry(int fi,auxv_t * out)15 int read_entry(int fi, auxv_t *out)
16 {
17    size_t toread = sizeof(*out);
18    char *ptr = (char*)out;
19 
20    while (toread) {
21       ssize_t r;
22 
23       r = read(fi, ptr, toread);
24       if (r == 0) {
25          fprintf(stderr, "unexpected EOF\n");
26          return 1;
27       }
28       if (r == -1) {
29          perror("read");
30          return 1;
31       }
32 
33       ptr += r;
34       toread -= r;
35 
36       assert(toread >= 0);
37    }
38 
39    return 0;
40 }
41 
main(void)42 int main(void)
43 {
44    auxv_t vector[2][4];
45    int fi[2] = {-1, -1};
46    size_t i;
47    int res = 1;
48 
49    /* Open the first input file. */
50    if ((fi[0] = open("/proc/self/auxv", O_RDONLY)) == -1) {
51       perror("open");
52       goto out;
53    }
54 
55    /* Read the first two entries from the first file. */
56    for (i = 0; i < 2; i++)
57       if (read_entry(fi[0], &vector[0][i]))
58          goto out;
59 
60    /* Open the second input file. */
61    if ((fi[1] = open("/proc/self/auxv", O_RDONLY)) == -1) {
62       perror("open");
63       goto out;
64    }
65 
66    /* Read the first two entries from the first file. */
67    for (i = 2; i < 4; i++)
68       if (read_entry(fi[0], &vector[0][i]))
69          goto out;
70 
71    /* Read the first four entries from the second file. */
72    for (i = 0; i < 4; i++)
73       if (read_entry(fi[1], &vector[1][i]))
74          goto out;
75 
76    /* Compare read vectors. */
77    if (memcmp(vector[0], vector[1], 4 * sizeof(vector[0][0]))) {
78       fprintf(stderr, "vectors differ\n");
79       goto out;
80    }
81 
82    res = 0;
83 
84 out:
85    for (i = 0; i < 2; i++)
86       if (fi[i] >= 0)
87          close(fi[i]);
88 
89    return res;
90 }
91 
92