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>
8
9#include <support/xunistd.h>
10
11static void prepare (void);
12#define PREPARE(argc, argv) prepare ()
13
14static int do_test (void);
15#define TEST_FUNCTION do_test ()
16
17#include "../test-skeleton.c"
18
19static int dir_fd;
20
21static void
22prepare (void)
23{
24#if _POSIX_CHOWN_RESTRICTED == 0
25 if (pathconf (path: test_dir, _PC_CHOWN_RESTRICTED) != 0)
26#endif
27 {
28 uid_t uid = getuid ();
29 if (uid != 0)
30 {
31 puts (s: "need root privileges");
32 exit (0);
33 }
34 }
35
36 size_t test_dir_len = strlen (test_dir);
37 static const char dir_name[] = "/tst-fchownat.XXXXXX";
38
39 size_t dirbuflen = test_dir_len + sizeof (dir_name);
40 char *dirbuf = malloc (size: dirbuflen);
41 if (dirbuf == NULL)
42 {
43 puts (s: "out of memory");
44 exit (1);
45 }
46
47 snprintf (s: dirbuf, maxlen: dirbuflen, format: "%s%s", test_dir, dir_name);
48 if (mkdtemp (template: dirbuf) == NULL)
49 {
50 puts (s: "cannot create temporary directory");
51 exit (1);
52 }
53
54 add_temp_file (name: dirbuf);
55
56 dir_fd = open (file: dirbuf, O_RDONLY | O_DIRECTORY);
57 if (dir_fd == -1)
58 {
59 puts (s: "cannot open directory");
60 exit (1);
61 }
62}
63
64
65static int
66do_test (void)
67{
68 /* fdopendir takes over the descriptor, make a copy. */
69 int dupfd = dup (fd: dir_fd);
70 if (dupfd == -1)
71 {
72 puts (s: "dup failed");
73 return 1;
74 }
75 if (lseek (fd: dupfd, offset: 0, SEEK_SET) != 0)
76 {
77 puts (s: "1st lseek failed");
78 return 1;
79 }
80
81 /* The directory should be empty safe the . and .. files. */
82 DIR *dir = fdopendir (fd: dupfd);
83 if (dir == NULL)
84 {
85 puts (s: "fdopendir failed");
86 return 1;
87 }
88 struct dirent64 *d;
89 while ((d = readdir64 (dirp: dir)) != NULL)
90 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
91 {
92 printf (format: "temp directory contains file \"%s\"\n", d->d_name);
93 return 1;
94 }
95 closedir (dirp: dir);
96
97 /* Try to create a file. */
98 int fd = openat (fd: dir_fd, file: "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
99 if (fd == -1)
100 {
101 if (errno == ENOSYS)
102 {
103 puts (s: "*at functions not supported");
104 return 0;
105 }
106
107 puts (s: "file creation failed");
108 return 1;
109 }
110 xwrite (fd, "hello", 5);
111 puts (s: "file created");
112
113 struct stat64 st1;
114 if (fstat64 (fd: fd, buf: &st1) != 0)
115 {
116 puts (s: "fstat64 failed");
117 return 1;
118 }
119
120 /* Before closing the file, try using this file descriptor to open
121 another file. This must fail. */
122 if (fchownat (fd: fd, file: "some-file", owner: 1, group: 1, flag: 0) != -1)
123 {
124 puts (s: "fchownat using descriptor for normal file worked");
125 return 1;
126 }
127 if (errno != ENOTDIR)
128 {
129 puts (s: "\
130error for fchownat using descriptor for normal file not ENOTDIR ");
131 return 1;
132 }
133
134 close (fd: fd);
135
136 if (fchownat (fd: dir_fd, file: "some-file", owner: st1.st_uid + 1, group: st1.st_gid + 1, flag: 0) != 0)
137 {
138 puts (s: "fchownat failed");
139 return 1;
140 }
141
142 struct stat64 st2;
143 if (fstatat64 (fd: dir_fd, file: "some-file", buf: &st2, flag: 0) != 0)
144 {
145 puts (s: "fstatat64 failed");
146 return 1;
147 }
148
149 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
150 {
151 puts (s: "owner change failed");
152 return 1;
153 }
154
155 if (unlinkat (fd: dir_fd, name: "some-file", flag: 0) != 0)
156 {
157 puts (s: "unlinkat failed");
158 return 1;
159 }
160
161 /* Create a file descriptor which is closed again right away. */
162 int dir_fd2 = dup (fd: dir_fd);
163 if (dir_fd2 == -1)
164 {
165 puts (s: "dup failed");
166 return 1;
167 }
168 close (fd: dir_fd2);
169
170 if (fchownat (fd: dir_fd2, file: "some-file", owner: 1, group: 1, flag: 0) != -1)
171 {
172 puts (s: "fchownat using closed descriptor worked");
173 return 1;
174 }
175 if (errno != EBADF)
176 {
177 puts (s: "error for fchownat using closed descriptor not EBADF ");
178 return 1;
179 }
180
181 close (fd: dir_fd);
182
183 if (fchownat (fd: -1, file: "some-file", owner: 1, group: 1, flag: 0) != -1)
184 {
185 puts (s: "fchownat using invalid descriptor worked");
186 return 1;
187 }
188 if (errno != EBADF)
189 {
190 puts (s: "error for fchownat using invalid descriptor not EBADF ");
191 return 1;
192 }
193
194 return 0;
195}
196

source code of glibc/io/tst-fchownat.c