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