]> git.ipfire.org Git - thirdparty/glibc.git/blob - io/tst-fchownat.c
AArch64: Simplify rounding-multiply pattern in several AdvSIMD routines
[thirdparty/glibc.git] / io / tst-fchownat.c
1 #include <dirent.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
8
9 #include <support/xunistd.h>
10
11 static void prepare (void);
12 #define PREPARE(argc, argv) prepare ()
13
14 static int do_test (void);
15 #define TEST_FUNCTION do_test ()
16
17 #include "../test-skeleton.c"
18
19 static int dir_fd;
20
21 static void
22 prepare (void)
23 {
24 #if _POSIX_CHOWN_RESTRICTED == 0
25 if (pathconf (test_dir, _PC_CHOWN_RESTRICTED) != 0)
26 #endif
27 {
28 uid_t uid = getuid ();
29 if (uid != 0)
30 {
31 puts ("need root privileges");
32 exit (0);
33 }
34 }
35
36 size_t test_dir_len = strlen (test_dir);
37 static const char dir_name[] = "/tst-fchownat.XXXXXX";
38
39 size_t dirbuflen = test_dir_len + sizeof (dir_name);
40 char *dirbuf = malloc (dirbuflen);
41 if (dirbuf == NULL)
42 {
43 puts ("out of memory");
44 exit (1);
45 }
46
47 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
48 if (mkdtemp (dirbuf) == NULL)
49 {
50 puts ("cannot create temporary directory");
51 exit (1);
52 }
53
54 add_temp_file (dirbuf);
55
56 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
57 if (dir_fd == -1)
58 {
59 puts ("cannot open directory");
60 exit (1);
61 }
62 }
63
64
65 static int
66 do_test (void)
67 {
68 /* fdopendir takes over the descriptor, make a copy. */
69 int dupfd = dup (dir_fd);
70 if (dupfd == -1)
71 {
72 puts ("dup failed");
73 return 1;
74 }
75 if (lseek (dupfd, 0, SEEK_SET) != 0)
76 {
77 puts ("1st lseek failed");
78 return 1;
79 }
80
81 /* The directory should be empty safe the . and .. files. */
82 DIR *dir = fdopendir (dupfd);
83 if (dir == NULL)
84 {
85 puts ("fdopendir failed");
86 return 1;
87 }
88 struct dirent64 *d;
89 while ((d = readdir64 (dir)) != NULL)
90 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
91 {
92 printf ("temp directory contains file \"%s\"\n", d->d_name);
93 return 1;
94 }
95 closedir (dir);
96
97 /* Try to create a file. */
98 int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
99 if (fd == -1)
100 {
101 if (errno == ENOSYS)
102 {
103 puts ("*at functions not supported");
104 return 0;
105 }
106
107 puts ("file creation failed");
108 return 1;
109 }
110 xwrite (fd, "hello", 5);
111 puts ("file created");
112
113 struct stat64 st1;
114 if (fstat64 (fd, &st1) != 0)
115 {
116 puts ("fstat64 failed");
117 return 1;
118 }
119
120 /* Before closing the file, try using this file descriptor to open
121 another file. This must fail. */
122 if (fchownat (fd, "some-file", 1, 1, 0) != -1)
123 {
124 puts ("fchownat using descriptor for normal file worked");
125 return 1;
126 }
127 if (errno != ENOTDIR)
128 {
129 puts ("\
130 error for fchownat using descriptor for normal file not ENOTDIR ");
131 return 1;
132 }
133
134 close (fd);
135
136 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
137 {
138 puts ("fchownat failed");
139 return 1;
140 }
141
142 struct stat64 st2;
143 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
144 {
145 puts ("fstatat64 failed");
146 return 1;
147 }
148
149 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
150 {
151 puts ("owner change failed");
152 return 1;
153 }
154
155 if (unlinkat (dir_fd, "some-file", 0) != 0)
156 {
157 puts ("unlinkat failed");
158 return 1;
159 }
160
161 /* Create a file descriptor which is closed again right away. */
162 int dir_fd2 = dup (dir_fd);
163 if (dir_fd2 == -1)
164 {
165 puts ("dup failed");
166 return 1;
167 }
168 close (dir_fd2);
169
170 if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
171 {
172 puts ("fchownat using closed descriptor worked");
173 return 1;
174 }
175 if (errno != EBADF)
176 {
177 puts ("error for fchownat using closed descriptor not EBADF ");
178 return 1;
179 }
180
181 close (dir_fd);
182
183 if (fchownat (-1, "some-file", 1, 1, 0) != -1)
184 {
185 puts ("fchownat using invalid descriptor worked");
186 return 1;
187 }
188 if (errno != EBADF)
189 {
190 puts ("error for fchownat using invalid descriptor not EBADF ");
191 return 1;
192 }
193
194 return 0;
195 }