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 uid_t uid = getuid ();
26 puts ("need root privileges");
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);
38 puts ("out of memory");
42 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
43 if (mkdtemp (dirbuf) == NULL)
45 puts ("cannot create temporary directory");
49 add_temp_file (dirbuf);
51 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
54 puts ("cannot open directory");
63 /* fdopendir takes over the descriptor, make a copy. */
64 int dupfd = dup (dir_fd);
70 if (lseek (dupfd, 0, SEEK_SET) != 0)
72 puts ("1st lseek failed");
76 /* The directory should be empty safe the . and .. files. */
77 DIR *dir = fdopendir (dupfd);
80 puts ("fdopendir failed");
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);
92 /* Try to create a file. */
93 int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
98 puts ("*at functions not supported");
102 puts ("file creation failed");
105 write (fd, "hello", 5);
106 puts ("file created");
109 if (fstat64 (fd, &st1) != 0)
111 puts ("fstat64 failed");
117 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
119 puts ("fchownat failed");
124 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
126 puts ("fstatat64 failed");
130 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
132 puts ("owner change failed");
136 if (unlinkat (dir_fd, "some-file", 0) != 0)
138 puts ("unlinkat failed");