10 static void prepare (void);
11 #define PREPARE(argc, argv) prepare ()
13 static int do_test (void);
14 #define TEST_FUNCTION do_test ()
16 #include "../test-skeleton.c"
23 #if _POSIX_CHOWN_RESTRICTED == 0
24 if (pathconf (test_dir, _PC_CHOWN_RESTRICTED) != 0)
27 uid_t uid = getuid ();
30 puts ("need root privileges");
35 size_t test_dir_len = strlen (test_dir);
36 static const char dir_name[] = "/tst-fchownat.XXXXXX";
38 size_t dirbuflen = test_dir_len + sizeof (dir_name);
39 char *dirbuf = malloc (dirbuflen);
42 puts ("out of memory");
46 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
47 if (mkdtemp (dirbuf) == NULL)
49 puts ("cannot create temporary directory");
53 add_temp_file (dirbuf);
55 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
58 puts ("cannot open directory");
67 /* fdopendir takes over the descriptor, make a copy. */
68 int dupfd = dup (dir_fd);
74 if (lseek (dupfd, 0, SEEK_SET) != 0)
76 puts ("1st lseek failed");
80 /* The directory should be empty safe the . and .. files. */
81 DIR *dir = fdopendir (dupfd);
84 puts ("fdopendir failed");
88 while ((d = readdir64 (dir)) != NULL)
89 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
91 printf ("temp directory contains file \"%s\"\n", d->d_name);
96 /* Try to create a file. */
97 int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
102 puts ("*at functions not supported");
106 puts ("file creation failed");
109 write (fd, "hello", 5);
110 puts ("file created");
113 if (fstat64 (fd, &st1) != 0)
115 puts ("fstat64 failed");
119 /* Before closing the file, try using this file descriptor to open
120 another file. This must fail. */
121 if (fchownat (fd, "some-file", 1, 1, 0) != -1)
123 puts ("fchownat using descriptor for normal file worked");
126 if (errno != ENOTDIR)
129 error for fchownat using descriptor for normal file not ENOTDIR ");
135 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
137 puts ("fchownat failed");
142 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
144 puts ("fstatat64 failed");
148 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
150 puts ("owner change failed");
154 if (unlinkat (dir_fd, "some-file", 0) != 0)
156 puts ("unlinkat failed");
160 /* Create a file descriptor which is closed again right away. */
161 int dir_fd2 = dup (dir_fd);
169 if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
171 puts ("fchownat using closed descriptor worked");
176 puts ("error for fchownat using closed descriptor not EBADF ");
182 if (fchownat (-1, "some-file", 1, 1, 0) != -1)
184 puts ("fchownat using invalid descriptor worked");
189 puts ("error for fchownat using invalid descriptor not EBADF ");