1 /* Test for fchmodat 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-fchmodat.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   umask (022);
87 
88   /* Try to create a file.  */
89   int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
90   if (fd == -1)
91     {
92       if (errno == ENOSYS)
93 	{
94 	  puts ("*at functions not supported");
95 	  return 0;
96 	}
97 
98       puts ("file creation failed");
99       return 1;
100     }
101   write (fd, "hello", 5);
102   puts ("file created");
103 
104   struct stat64 st1;
105   if (fstat64 (fd, &st1) != 0)
106     {
107       puts ("fstat64 failed");
108       return 1;
109     }
110 
111   /* Before closing the file, try using this file descriptor to open
112      another file.  This must fail.  */
113   if (fchmodat (fd, "some-file", 0400, 0) != -1)
114     {
115       puts ("fchmodat using descriptor for normal file worked");
116       return 1;
117     }
118   if (errno != ENOTDIR)
119     {
120       puts ("\
121 error for fchmodat using descriptor for normal file not ENOTDIR ");
122       return 1;
123     }
124 
125   close (fd);
126 
127   if ((st1.st_mode & 0777) != 0644)
128     {
129       printf ("openat created mode %04o, not 0644\n", (st1.st_mode & 0777));
130       return 1;
131     }
132 
133   if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
134     {
135       puts ("fchownat failed");
136       return 1;
137     }
138 
139   struct stat64 st2;
140   if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
141     {
142       puts ("fstatat64 failed");
143       return 1;
144     }
145 
146   if ((st2.st_mode & 0777) != 0400)
147     {
148       puts ("mode change failed");
149       return 1;
150     }
151 
152   if (unlinkat (dir_fd, "some-file", 0) != 0)
153     {
154       puts ("unlinkat failed");
155       return 1;
156     }
157 
158   /* Create a file descriptor which is closed again right away.  */
159   int dir_fd2 = dup (dir_fd);
160   if (dir_fd2 == -1)
161     {
162       puts ("dup failed");
163       return 1;
164     }
165   close (dir_fd2);
166 
167   if (fchmodat (dir_fd2, "some-file", 0400, 0) != -1)
168     {
169       puts ("fchmodat using closed descriptor worked");
170       return 1;
171     }
172   if (errno != EBADF)
173     {
174       puts ("error for fchmodat using closed descriptor not EBADF ");
175       return 1;
176     }
177 
178   close (dir_fd);
179 
180   if (fchmodat (-1, "some-file", 0400, 0) != -1)
181     {
182       puts ("fchmodat using invalid descriptor worked");
183       return 1;
184     }
185   if (errno != EBADF)
186     {
187       puts ("error for fchmodat using invalid descriptor not EBADF ");
188       return 1;
189     }
190 
191   return 0;
192 }
193