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"
22 #if _POSIX_CHOWN_RESTRICTED == 0
23 if (pathconf (test_dir, _PC_CHOWN_RESTRICTED) != 0)
26 uid_t uid = getuid ();
29 puts ("need root privileges");
34 size_t test_dir_len = strlen (test_dir);
35 static const char dir_name[] = "/tst-fchownat.XXXXXX";
37 size_t dirbuflen = test_dir_len + sizeof (dir_name);
38 char *dirbuf = malloc (dirbuflen);
41 puts ("out of memory");
45 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
46 if (mkdtemp (dirbuf) == NULL)
48 puts ("cannot create temporary directory");
52 add_temp_file (dirbuf);
54 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
57 puts ("cannot open directory");
66 /* fdopendir takes over the descriptor, make a copy. */
67 int dupfd = dup (dir_fd);
73 if (lseek (dupfd, 0, SEEK_SET) != 0)
75 puts ("1st lseek failed");
79 /* The directory should be empty safe the . and .. files. */
80 DIR *dir = fdopendir (dupfd);
83 puts ("fdopendir failed");
87 while ((d = readdir64 (dir)) != NULL)
88 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
90 printf ("temp directory contains file \"%s\"\n", d->d_name);
95 /* Try to create a file. */
96 int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
101 puts ("*at functions not supported");
105 puts ("file creation failed");
108 write (fd, "hello", 5);
109 puts ("file created");
112 if (fstat64 (fd, &st1) != 0)
114 puts ("fstat64 failed");
118 /* Before closing the file, try using this file descriptor to open
119 another file. This must fail. */
120 if (fchownat (fd, "some-file", 1, 1, 0) != -1)
122 puts ("fchownat using descriptor for normal file worked");
125 if (errno != ENOTDIR)
128 error for fchownat using descriptor for normal file not ENOTDIR ");
134 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
136 puts ("fchownat failed");
141 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
143 puts ("fstatat64 failed");
147 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
149 puts ("owner change failed");
153 if (unlinkat (dir_fd, "some-file", 0) != 0)
155 puts ("unlinkat failed");
159 /* Create a file descriptor which is closed again right away. */
160 int dir_fd2 = dup (dir_fd);
168 if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
170 puts ("fchownat using closed descriptor worked");
175 puts ("error for fchownat using closed descriptor not EBADF ");
181 if (fchownat (-1, "some-file", 1, 1, 0) != -1)
183 puts ("fchownat using invalid descriptor worked");
188 puts ("error for fchownat using invalid descriptor not EBADF ");