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");
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");
122 if (errno
!= ENOTDIR
)
125 error for fchownat using descriptor for normal file not ENOTDIR ");
131 if (fchownat (dir_fd
, "some-file", st1
.st_uid
+ 1, st1
.st_gid
+ 1, 0) != 0)
133 puts ("fchownat failed");
138 if (fstatat64 (dir_fd
, "some-file", &st2
, 0) != 0)
140 puts ("fstatat64 failed");
144 if (st1
.st_uid
+ 1 != st2
.st_uid
|| st1
.st_gid
+ 1 != st2
.st_gid
)
146 puts ("owner change failed");
150 if (unlinkat (dir_fd
, "some-file", 0) != 0)
152 puts ("unlinkat failed");
156 /* Create a file descriptor which is closed again right away. */
157 int dir_fd2
= dup (dir_fd
);
165 if (fchownat (dir_fd2
, "some-file", 1, 1, 0) != -1)
167 puts ("fchownat using closed descriptor worked");
172 puts ("error for fchownat using closed descriptor not EBADF ");
178 if (fchownat (-1, "some-file", 1, 1, 0) != -1)
180 puts ("fchownat using invalid descriptor worked");
185 puts ("error for fchownat using invalid descriptor not EBADF ");