localedata: miq_NI: convert to UTF-8
[glibc.git] / io / tst-fchownat.c
blobc0b87cda8f42baf0a1574638a9f9c687fb3be8a5
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>
9 #include <support/xunistd.h>
11 static void prepare (void);
12 #define PREPARE(argc, argv) prepare ()
14 static int do_test (void);
15 #define TEST_FUNCTION do_test ()
17 #include "../test-skeleton.c"
19 static int dir_fd;
21 static void
22 prepare (void)
24 #if _POSIX_CHOWN_RESTRICTED == 0
25 if (pathconf (test_dir, _PC_CHOWN_RESTRICTED) != 0)
26 #endif
28 uid_t uid = getuid ();
29 if (uid != 0)
31 puts ("need root privileges");
32 exit (0);
36 size_t test_dir_len = strlen (test_dir);
37 static const char dir_name[] = "/tst-fchownat.XXXXXX";
39 size_t dirbuflen = test_dir_len + sizeof (dir_name);
40 char *dirbuf = malloc (dirbuflen);
41 if (dirbuf == NULL)
43 puts ("out of memory");
44 exit (1);
47 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
48 if (mkdtemp (dirbuf) == NULL)
50 puts ("cannot create temporary directory");
51 exit (1);
54 add_temp_file (dirbuf);
56 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
57 if (dir_fd == -1)
59 puts ("cannot open directory");
60 exit (1);
65 static int
66 do_test (void)
68 /* fdopendir takes over the descriptor, make a copy. */
69 int dupfd = dup (dir_fd);
70 if (dupfd == -1)
72 puts ("dup failed");
73 return 1;
75 if (lseek (dupfd, 0, SEEK_SET) != 0)
77 puts ("1st lseek failed");
78 return 1;
81 /* The directory should be empty safe the . and .. files. */
82 DIR *dir = fdopendir (dupfd);
83 if (dir == NULL)
85 puts ("fdopendir failed");
86 return 1;
88 struct dirent64 *d;
89 while ((d = readdir64 (dir)) != NULL)
90 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
92 printf ("temp directory contains file \"%s\"\n", d->d_name);
93 return 1;
95 closedir (dir);
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)
101 if (errno == ENOSYS)
103 puts ("*at functions not supported");
104 return 0;
107 puts ("file creation failed");
108 return 1;
110 xwrite (fd, "hello", 5);
111 puts ("file created");
113 struct stat64 st1;
114 if (fstat64 (fd, &st1) != 0)
116 puts ("fstat64 failed");
117 return 1;
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)
124 puts ("fchownat using descriptor for normal file worked");
125 return 1;
127 if (errno != ENOTDIR)
129 puts ("\
130 error for fchownat using descriptor for normal file not ENOTDIR ");
131 return 1;
134 close (fd);
136 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
138 puts ("fchownat failed");
139 return 1;
142 struct stat64 st2;
143 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
145 puts ("fstatat64 failed");
146 return 1;
149 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
151 puts ("owner change failed");
152 return 1;
155 if (unlinkat (dir_fd, "some-file", 0) != 0)
157 puts ("unlinkat failed");
158 return 1;
161 /* Create a file descriptor which is closed again right away. */
162 int dir_fd2 = dup (dir_fd);
163 if (dir_fd2 == -1)
165 puts ("dup failed");
166 return 1;
168 close (dir_fd2);
170 if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
172 puts ("fchownat using closed descriptor worked");
173 return 1;
175 if (errno != EBADF)
177 puts ("error for fchownat using closed descriptor not EBADF ");
178 return 1;
181 close (dir_fd);
183 if (fchownat (-1, "some-file", 1, 1, 0) != -1)
185 puts ("fchownat using invalid descriptor worked");
186 return 1;
188 if (errno != EBADF)
190 puts ("error for fchownat using invalid descriptor not EBADF ");
191 return 1;
194 return 0;