1 | /* Test for faccessat function. */ |
2 | |
3 | #include <dirent.h> |
4 | #include <fcntl.h> |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | #include <unistd.h> |
9 | #include <sys/stat.h> |
10 | |
11 | |
12 | static void prepare (void); |
13 | #define PREPARE(argc, argv) prepare () |
14 | |
15 | static int do_test (void); |
16 | #define TEST_FUNCTION do_test () |
17 | |
18 | #include "../test-skeleton.c" |
19 | |
20 | static int dir_fd; |
21 | |
22 | static void |
23 | prepare (void) |
24 | { |
25 | size_t test_dir_len = strlen (test_dir); |
26 | static const char dir_name[] = "/tst-faccessat.XXXXXX" ; |
27 | |
28 | size_t dirbuflen = test_dir_len + sizeof (dir_name); |
29 | char *dirbuf = malloc (size: dirbuflen); |
30 | if (dirbuf == NULL) |
31 | { |
32 | puts (s: "out of memory" ); |
33 | exit (1); |
34 | } |
35 | |
36 | snprintf (s: dirbuf, maxlen: dirbuflen, format: "%s%s" , test_dir, dir_name); |
37 | if (mkdtemp (template: dirbuf) == NULL) |
38 | { |
39 | puts (s: "cannot create temporary directory" ); |
40 | exit (1); |
41 | } |
42 | |
43 | add_temp_file (name: dirbuf); |
44 | |
45 | dir_fd = open (file: dirbuf, O_RDONLY | O_DIRECTORY); |
46 | if (dir_fd == -1) |
47 | { |
48 | puts (s: "cannot open directory" ); |
49 | exit (1); |
50 | } |
51 | } |
52 | |
53 | |
54 | static int |
55 | do_test (void) |
56 | { |
57 | /* fdopendir takes over the descriptor, make a copy. */ |
58 | int dupfd = dup (fd: dir_fd); |
59 | if (dupfd == -1) |
60 | { |
61 | puts (s: "dup failed" ); |
62 | return 1; |
63 | } |
64 | if (lseek (fd: dupfd, offset: 0, SEEK_SET) != 0) |
65 | { |
66 | puts (s: "1st lseek failed" ); |
67 | return 1; |
68 | } |
69 | |
70 | /* The directory should be empty save the . and .. files. */ |
71 | DIR *dir = fdopendir (fd: dupfd); |
72 | if (dir == NULL) |
73 | { |
74 | puts (s: "fdopendir failed" ); |
75 | return 1; |
76 | } |
77 | struct dirent64 *d; |
78 | while ((d = readdir64 (dirp: dir)) != NULL) |
79 | if (strcmp (d->d_name, "." ) != 0 && strcmp (d->d_name, ".." ) != 0) |
80 | { |
81 | printf (format: "temp directory contains file \"%s\"\n" , d->d_name); |
82 | return 1; |
83 | } |
84 | closedir (dirp: dir); |
85 | |
86 | /* Try to create a file. */ |
87 | int fd = openat (fd: dir_fd, file: "some-file" , O_CREAT|O_RDWR|O_EXCL, 0666); |
88 | if (fd == -1) |
89 | { |
90 | if (errno == ENOSYS) |
91 | { |
92 | puts (s: "*at functions not supported" ); |
93 | return 0; |
94 | } |
95 | |
96 | puts (s: "file creation failed" ); |
97 | return 1; |
98 | } |
99 | write (fd, "hello" , 5); |
100 | puts (s: "file created" ); |
101 | |
102 | /* Before closing the file, try using this file descriptor to open |
103 | another file. This must fail. */ |
104 | if (faccessat (fd: fd, file: "should-not-work" , F_OK, AT_EACCESS) != -1) |
105 | { |
106 | puts (s: "faccessat using descriptor for normal file worked" ); |
107 | return 1; |
108 | } |
109 | if (errno != ENOTDIR) |
110 | { |
111 | puts (s: "\ |
112 | error for faccessat using descriptor for normal file not ENOTDIR " ); |
113 | return 1; |
114 | } |
115 | |
116 | close (fd: fd); |
117 | |
118 | int result = 0; |
119 | |
120 | if (faccessat (fd: dir_fd, file: "some-file" , F_OK, AT_EACCESS)) |
121 | { |
122 | printf (format: "faccessat F_OK: %m\n" ); |
123 | result = 1; |
124 | } |
125 | if (faccessat (fd: dir_fd, file: "some-file" , W_OK, AT_EACCESS)) |
126 | { |
127 | printf (format: "faccessat W_OK: %m\n" ); |
128 | result = 1; |
129 | } |
130 | |
131 | errno = 0; |
132 | if (faccessat (fd: dir_fd, file: "some-file" , X_OK, AT_EACCESS) == 0 |
133 | || errno != EACCES) |
134 | { |
135 | printf (format: "faccessat X_OK on nonexecutable: %m\n" ); |
136 | result = 1; |
137 | } |
138 | |
139 | if (fchmodat (dir_fd, "some-file" , 0400, 0) != 0) |
140 | { |
141 | printf (format: "fchownat failed: %m\n" ); |
142 | return 1; |
143 | } |
144 | |
145 | if (faccessat (fd: dir_fd, file: "some-file" , R_OK, AT_EACCESS)) |
146 | { |
147 | printf (format: "faccessat R_OK: %m\n" ); |
148 | result = 1; |
149 | } |
150 | |
151 | errno = 0; |
152 | if (faccessat (fd: dir_fd, file: "some-file" , W_OK, AT_EACCESS) == 0 |
153 | ? (geteuid () != 0) : (errno != EACCES)) |
154 | { |
155 | printf (format: "faccessat W_OK on unwritable file: %m\n" ); |
156 | result = 1; |
157 | } |
158 | |
159 | /* Create a file descriptor which is closed again right away. */ |
160 | int dir_fd2 = dup (fd: dir_fd); |
161 | if (dir_fd2 == -1) |
162 | { |
163 | puts (s: "dup failed" ); |
164 | return 1; |
165 | } |
166 | close (fd: dir_fd2); |
167 | |
168 | /* With the file descriptor closed the next call must fail. */ |
169 | if (faccessat (fd: dir_fd2, file: "some-file" , F_OK, AT_EACCESS) != -1) |
170 | { |
171 | puts (s: "faccessat using closed descriptor succeeded" ); |
172 | return 1; |
173 | } |
174 | if (errno != EBADF) |
175 | { |
176 | puts (s: "faccessat using closed descriptor did not set EBADF" ); |
177 | return 1; |
178 | } |
179 | |
180 | /* Same with a non-existing file. */ |
181 | if (faccessat (fd: dir_fd2, file: "non-existing-file" , F_OK, AT_EACCESS) != -1) |
182 | { |
183 | puts (s: "2nd faccessat using closed descriptor succeeded" ); |
184 | return 1; |
185 | } |
186 | if (errno != EBADF) |
187 | { |
188 | puts (s: "2nd faccessat using closed descriptor did not set EBADF" ); |
189 | return 1; |
190 | } |
191 | |
192 | if (unlinkat (fd: dir_fd, name: "some-file" , flag: 0) != 0) |
193 | { |
194 | puts (s: "unlinkat failed" ); |
195 | result = 1; |
196 | } |
197 | |
198 | close (fd: dir_fd); |
199 | |
200 | fd = faccessat (fd: -1, file: "some-file" , F_OK, AT_EACCESS); |
201 | if (fd != -1) |
202 | { |
203 | puts (s: "faccessat using -1 descriptor succeeded" ); |
204 | return 1; |
205 | } |
206 | if (errno != EBADF) |
207 | { |
208 | puts (s: "faccessat using -1 descriptor did not set EBADF" ); |
209 | return 1; |
210 | } |
211 | |
212 | return result; |
213 | } |
214 | |