Add thunderx2t99 and thunderx2t99p1 CPU names to tunables list
[glibc.git] / io / tst-faccessat.c
blob7bdeed008c1d85f985b777c9eea24836be61cbb4
1 /* Test for faccessat function. */
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>
12 static void prepare (void);
13 #define PREPARE(argc, argv) prepare ()
15 static int do_test (void);
16 #define TEST_FUNCTION do_test ()
18 #include "../test-skeleton.c"
20 static int dir_fd;
22 static void
23 prepare (void)
25 size_t test_dir_len = strlen (test_dir);
26 static const char dir_name[] = "/tst-faccessat.XXXXXX";
28 size_t dirbuflen = test_dir_len + sizeof (dir_name);
29 char *dirbuf = malloc (dirbuflen);
30 if (dirbuf == NULL)
32 puts ("out of memory");
33 exit (1);
36 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
37 if (mkdtemp (dirbuf) == NULL)
39 puts ("cannot create temporary directory");
40 exit (1);
43 add_temp_file (dirbuf);
45 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
46 if (dir_fd == -1)
48 puts ("cannot open directory");
49 exit (1);
54 static int
55 do_test (void)
57 /* fdopendir takes over the descriptor, make a copy. */
58 int dupfd = dup (dir_fd);
59 if (dupfd == -1)
61 puts ("dup failed");
62 return 1;
64 if (lseek (dupfd, 0, SEEK_SET) != 0)
66 puts ("1st lseek failed");
67 return 1;
70 /* The directory should be empty save the . and .. files. */
71 DIR *dir = fdopendir (dupfd);
72 if (dir == NULL)
74 puts ("fdopendir failed");
75 return 1;
77 struct dirent64 *d;
78 while ((d = readdir64 (dir)) != NULL)
79 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
81 printf ("temp directory contains file \"%s\"\n", d->d_name);
82 return 1;
84 closedir (dir);
86 /* Try to create a file. */
87 int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
88 if (fd == -1)
90 if (errno == ENOSYS)
92 puts ("*at functions not supported");
93 return 0;
96 puts ("file creation failed");
97 return 1;
99 write (fd, "hello", 5);
100 puts ("file created");
102 /* Before closing the file, try using this file descriptor to open
103 another file. This must fail. */
104 if (faccessat (fd, "should-not-work", F_OK, AT_EACCESS) != -1)
106 puts ("faccessat using descriptor for normal file worked");
107 return 1;
109 if (errno != ENOTDIR)
111 puts ("\
112 error for faccessat using descriptor for normal file not ENOTDIR ");
113 return 1;
116 close (fd);
118 int result = 0;
120 if (faccessat (dir_fd, "some-file", F_OK, AT_EACCESS))
122 printf ("faccessat F_OK: %m\n");
123 result = 1;
125 if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS))
127 printf ("faccessat W_OK: %m\n");
128 result = 1;
131 errno = 0;
132 if (faccessat (dir_fd, "some-file", X_OK, AT_EACCESS) == 0
133 || errno != EACCES)
135 printf ("faccessat X_OK on nonexecutable: %m\n");
136 result = 1;
139 if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
141 printf ("fchownat failed: %m\n");
142 return 1;
145 if (faccessat (dir_fd, "some-file", R_OK, AT_EACCESS))
147 printf ("faccessat R_OK: %m\n");
148 result = 1;
151 errno = 0;
152 if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS) == 0
153 ? (geteuid () != 0) : (errno != EACCES))
155 printf ("faccessat W_OK on unwritable file: %m\n");
156 result = 1;
159 /* Create a file descriptor which is closed again right away. */
160 int dir_fd2 = dup (dir_fd);
161 if (dir_fd2 == -1)
163 puts ("dup failed");
164 return 1;
166 close (dir_fd2);
168 /* With the file descriptor closed the next call must fail. */
169 if (faccessat (dir_fd2, "some-file", F_OK, AT_EACCESS) != -1)
171 puts ("faccessat using closed descriptor succeeded");
172 return 1;
174 if (errno != EBADF)
176 puts ("faccessat using closed descriptor did not set EBADF");
177 return 1;
180 /* Same with a non-existing file. */
181 if (faccessat (dir_fd2, "non-existing-file", F_OK, AT_EACCESS) != -1)
183 puts ("2nd faccessat using closed descriptor succeeded");
184 return 1;
186 if (errno != EBADF)
188 puts ("2nd faccessat using closed descriptor did not set EBADF");
189 return 1;
192 if (unlinkat (dir_fd, "some-file", 0) != 0)
194 puts ("unlinkat failed");
195 result = 1;
198 close (dir_fd);
200 fd = faccessat (-1, "some-file", F_OK, AT_EACCESS);
201 if (fd != -1)
203 puts ("faccessat using -1 descriptor succeeded");
204 return 1;
206 if (errno != EBADF)
208 puts ("faccessat using -1 descriptor did not set EBADF");
209 return 1;
212 return result;