This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "fcntl--.h"
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28
29
30
31
32 #define BACKUP_STDERR_FILENO 10
33 #define ASSERT_STREAM myerr
34 #include "macros.h"
35
36 static FILE *myerr;
37
38 #define witness "test-openat-safer.txt"
39
40 int
41 main (void)
42 {
43 int i;
44 int j;
45 int dfd;
46 int fd;
47 char buf[2];
48
49
50 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
51 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
52 return 2;
53
54
55 dfd = openat (AT_FDCWD, ".", O_RDONLY);
56 ASSERT (STDERR_FILENO < dfd);
57
58
59 remove (witness);
60 fd = openat (dfd, witness, O_WRONLY | O_CREAT | O_EXCL, 0600);
61 ASSERT (STDERR_FILENO < fd);
62 ASSERT (write (fd, "hi", 2) == 2);
63 ASSERT (close (fd) == 0);
64
65
66
67 for (i = -1; i <= STDERR_FILENO; i++)
68 {
69 ASSERT (fchdir (dfd) == 0);
70 if (0 <= i)
71 ASSERT (close (i) == 0);
72
73
74 for (j = 0; j <= 1; j++)
75 {
76 if (j)
77 ASSERT (chdir ("..") == 0);
78
79
80 errno = 0;
81 ASSERT (openat (AT_FDCWD, "", O_RDONLY) == -1);
82 ASSERT (errno == ENOENT);
83 errno = 0;
84 ASSERT (openat (dfd, "", O_RDONLY) == -1);
85 ASSERT (errno == ENOENT);
86 errno = 0;
87 ASSERT (openat (-1, ".", O_RDONLY) == -1);
88 ASSERT (errno == EBADF);
89
90
91 errno = 0;
92 ASSERT (openat (dfd, "nonexist.ent/", O_CREAT | O_RDONLY,
93 S_IRUSR | S_IWUSR) == -1);
94 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
95 || errno == EINVAL);
96 errno = 0;
97 ASSERT (openat (dfd, witness "/", O_RDONLY) == -1);
98 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
99 #if defined __linux__ || defined __ANDROID__
100
101 fd = openat (-1, "/dev/null", O_WRONLY);
102 ASSERT (STDERR_FILENO < fd);
103 #endif
104
105 errno = 0;
106 fd = open ("/dev/null", O_RDONLY);
107 ASSERT (STDERR_FILENO < fd);
108 ASSERT (openat (fd, ".", O_RDONLY) == -1);
109 ASSERT (errno == EBADF || errno == ENOTDIR);
110 ASSERT (close (fd) == 0);
111
112
113 fd = openat (dfd, witness, O_RDONLY | O_NOFOLLOW);
114 ASSERT (STDERR_FILENO < fd);
115 ASSERT (read (fd, buf, 2) == 2);
116 ASSERT (buf[0] == 'h' && buf[1] == 'i');
117 ASSERT (close (fd) == 0);
118 }
119 }
120 ASSERT (fchdir (dfd) == 0);
121 ASSERT (unlink (witness) == 0);
122 ASSERT (close (dfd) == 0);
123
124 return 0;
125 }