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
prepare(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 (dirbuflen);
30   if (dirbuf == NULL)
31     {
32       puts ("out of memory");
33       exit (1);
34     }
35 
36   snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
37   if (mkdtemp (dirbuf) == NULL)
38     {
39       puts ("cannot create temporary directory");
40       exit (1);
41     }
42 
43   add_temp_file (dirbuf);
44 
45   dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
46   if (dir_fd == -1)
47     {
48       puts ("cannot open directory");
49       exit (1);
50     }
51 }
52 
53 
54 static int
do_test(void)55 do_test (void)
56 {
57   /* fdopendir takes over the descriptor, make a copy.  */
58   int dupfd = dup (dir_fd);
59   if (dupfd == -1)
60     {
61       puts ("dup failed");
62       return 1;
63     }
64   if (lseek (dupfd, 0, SEEK_SET) != 0)
65     {
66       puts ("1st lseek failed");
67       return 1;
68     }
69 
70   /* The directory should be empty save the . and .. files.  */
71   DIR *dir = fdopendir (dupfd);
72   if (dir == NULL)
73     {
74       puts ("fdopendir failed");
75       return 1;
76     }
77   struct dirent64 *d;
78   while ((d = readdir64 (dir)) != NULL)
79     if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
80       {
81 	printf ("temp directory contains file \"%s\"\n", d->d_name);
82 	return 1;
83       }
84   closedir (dir);
85 
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)
89     {
90       if (errno == ENOSYS)
91 	{
92 	  puts ("*at functions not supported");
93 	  return 0;
94 	}
95 
96       puts ("file creation failed");
97       return 1;
98     }
99   write (fd, "hello", 5);
100   puts ("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, "should-not-work", F_OK, AT_EACCESS) != -1)
105     {
106       puts ("faccessat using descriptor for normal file worked");
107       return 1;
108     }
109   if (errno != ENOTDIR)
110     {
111       puts ("\
112 error for faccessat using descriptor for normal file not ENOTDIR ");
113       return 1;
114     }
115 
116   close (fd);
117 
118   int result = 0;
119 
120   if (faccessat (dir_fd, "some-file", F_OK, AT_EACCESS))
121     {
122       printf ("faccessat F_OK: %m\n");
123       result = 1;
124     }
125   if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS))
126     {
127       printf ("faccessat W_OK: %m\n");
128       result = 1;
129     }
130 
131   errno = 0;
132   if (faccessat (dir_fd, "some-file", X_OK, AT_EACCESS) == 0
133       || errno != EACCES)
134     {
135       printf ("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 ("fchownat failed: %m\n");
142       return 1;
143     }
144 
145   if (faccessat (dir_fd, "some-file", R_OK, AT_EACCESS))
146     {
147       printf ("faccessat R_OK: %m\n");
148       result = 1;
149     }
150 
151   errno = 0;
152   if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS) == 0
153       ? (geteuid () != 0) : (errno != EACCES))
154     {
155       printf ("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 (dir_fd);
161   if (dir_fd2 == -1)
162     {
163       puts ("dup failed");
164       return 1;
165     }
166   close (dir_fd2);
167 
168   /* With the file descriptor closed the next call must fail.  */
169   if (faccessat (dir_fd2, "some-file", F_OK, AT_EACCESS) != -1)
170     {
171       puts ("faccessat using closed descriptor succeeded");
172       return 1;
173     }
174   if (errno != EBADF)
175     {
176       puts ("faccessat using closed descriptor did not set EBADF");
177       return 1;
178     }
179 
180   /* Same with a non-existing file.  */
181   if (faccessat (dir_fd2, "non-existing-file", F_OK, AT_EACCESS) != -1)
182     {
183       puts ("2nd faccessat using closed descriptor succeeded");
184       return 1;
185     }
186   if (errno != EBADF)
187     {
188       puts ("2nd faccessat using closed descriptor did not set EBADF");
189       return 1;
190     }
191 
192   if (unlinkat (dir_fd, "some-file", 0) != 0)
193     {
194       puts ("unlinkat failed");
195       result = 1;
196     }
197 
198   close (dir_fd);
199 
200   fd = faccessat (-1, "some-file", F_OK, AT_EACCESS);
201   if (fd != -1)
202     {
203       puts ("faccessat using -1 descriptor succeeded");
204       return 1;
205     }
206   if (errno != EBADF)
207     {
208       puts ("faccessat using -1 descriptor did not set EBADF");
209       return 1;
210     }
211 
212   return result;
213 }
214