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