2.5-18.1
[glibc.git] / io / tst-fchownat.c
blobfd32ac9e6bdd31d1ff35eb73afb1a9885149ccb8
1 #include <dirent.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
9 static void prepare (void);
10 #define PREPARE(argc, argv) prepare ()
12 static int do_test (void);
13 #define TEST_FUNCTION do_test ()
15 #include "../test-skeleton.c"
17 static int dir_fd;
19 static void
20 prepare (void)
22 #if _POSIX_CHOWN_RESTRICTED > 0
23 uid_t uid = getuid ();
24 if (uid != 0)
26 puts ("need root privileges");
27 exit (0);
29 #endif
31 size_t test_dir_len = strlen (test_dir);
32 static const char dir_name[] = "/tst-fchownat.XXXXXX";
34 size_t dirbuflen = test_dir_len + sizeof (dir_name);
35 char *dirbuf = malloc (dirbuflen);
36 if (dirbuf == NULL)
38 puts ("out of memory");
39 exit (1);
42 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
43 if (mkdtemp (dirbuf) == NULL)
45 puts ("cannot create temporary directory");
46 exit (1);
49 add_temp_file (dirbuf);
51 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
52 if (dir_fd == -1)
54 puts ("cannot open directory");
55 exit (1);
60 static int
61 do_test (void)
63 /* fdopendir takes over the descriptor, make a copy. */
64 int dupfd = dup (dir_fd);
65 if (dupfd == -1)
67 puts ("dup failed");
68 return 1;
70 if (lseek (dupfd, 0, SEEK_SET) != 0)
72 puts ("1st lseek failed");
73 return 1;
76 /* The directory should be empty safe the . and .. files. */
77 DIR *dir = fdopendir (dupfd);
78 if (dir == NULL)
80 puts ("fdopendir failed");
81 return 1;
83 struct dirent64 *d;
84 while ((d = readdir64 (dir)) != NULL)
85 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
87 printf ("temp directory contains file \"%s\"\n", d->d_name);
88 return 1;
90 closedir (dir);
92 /* Try to create a file. */
93 int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
94 if (fd == -1)
96 if (errno == ENOSYS)
98 puts ("*at functions not supported");
99 return 0;
102 puts ("file creation failed");
103 return 1;
105 write (fd, "hello", 5);
106 puts ("file created");
108 struct stat64 st1;
109 if (fstat64 (fd, &st1) != 0)
111 puts ("fstat64 failed");
112 return 1;
115 /* Before closing the file, try using this file descriptor to open
116 another file. This must fail. */
117 if (fchownat (fd, "some-file", 1, 1, 0) != -1)
119 puts ("fchownat using descriptor for normal file worked");
120 return 1;
122 if (errno != ENOTDIR)
124 puts ("\
125 error for fchownat using descriptor for normal file not ENOTDIR ");
126 return 1;
129 close (fd);
131 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
133 puts ("fchownat failed");
134 return 1;
137 struct stat64 st2;
138 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
140 puts ("fstatat64 failed");
141 return 1;
144 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
146 puts ("owner change failed");
147 return 1;
150 if (unlinkat (dir_fd, "some-file", 0) != 0)
152 puts ("unlinkat failed");
153 return 1;
156 /* Create a file descriptor which is closed again right away. */
157 int dir_fd2 = dup (dir_fd);
158 if (dir_fd2 == -1)
160 puts ("dup failed");
161 return 1;
163 close (dir_fd2);
165 if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
167 puts ("fchownat using closed descriptor worked");
168 return 1;
170 if (errno != EBADF)
172 puts ("error for fchownat using closed descriptor not EBADF ");
173 return 1;
176 close (dir_fd);
178 if (fchownat (-1, "some-file", 1, 1, 0) != -1)
180 puts ("fchownat using invalid descriptor worked");
181 return 1;
183 if (errno != EBADF)
185 puts ("error for fchownat using invalid descriptor not EBADF ");
186 return 1;
189 return 0;