]> git.ipfire.org Git - thirdparty/glibc.git/blob - io/tst-fchownat.c
* io/Makefile (routines): Add fstatat, fstatat64, fxstatat, fxstatat64,
[thirdparty/glibc.git] / io / tst-fchownat.c
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
8
9 static void prepare (void);
10 #define PREPARE(argc, argv) prepare ()
11
12 static int do_test (void);
13 #define TEST_FUNCTION do_test ()
14
15 #include "../test-skeleton.c"
16
17 static int dir_fd;
18
19 static void
20 prepare (void)
21 {
22 #if _POSIX_CHOWN_RESTRICTED > 0
23 uid_t uid = getuid ();
24 if (uid != 0)
25 {
26 puts ("need root privileges");
27 exit (0);
28 }
29 #endif
30
31 size_t test_dir_len = strlen (test_dir);
32 static const char dir_name[] = "/tst-fchownat.XXXXXX";
33
34 size_t dirbuflen = test_dir_len + sizeof (dir_name);
35 char *dirbuf = malloc (dirbuflen);
36 if (dirbuf == NULL)
37 {
38 puts ("out of memory");
39 exit (1);
40 }
41
42 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
43 if (mkdtemp (dirbuf) == NULL)
44 {
45 puts ("cannot create temporary directory");
46 exit (1);
47 }
48
49 add_temp_file (dirbuf);
50
51 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
52 if (dir_fd == -1)
53 {
54 puts ("cannot open directory");
55 exit (1);
56 }
57 }
58
59
60 static int
61 do_test (void)
62 {
63 /* fdopendir takes over the descriptor, make a copy. */
64 int dupfd = dup (dir_fd);
65 if (dupfd == -1)
66 {
67 puts ("dup failed");
68 return 1;
69 }
70 if (lseek (dupfd, 0, SEEK_SET) != 0)
71 {
72 puts ("1st lseek failed");
73 return 1;
74 }
75
76 /* The directory should be empty safe the . and .. files. */
77 DIR *dir = fdopendir (dupfd);
78 if (dir == NULL)
79 {
80 puts ("fdopendir failed");
81 return 1;
82 }
83 struct dirent64 *d;
84 while ((d = readdir64 (dir)) != NULL)
85 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
86 {
87 printf ("temp directory contains file \"%s\"\n", d->d_name);
88 return 1;
89 }
90 closedir (dir);
91
92 /* Try to create a file. */
93 int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
94 if (fd == -1)
95 {
96 if (errno == ENOSYS)
97 {
98 puts ("*at functions not supported");
99 return 0;
100 }
101
102 puts ("file creation failed");
103 return 1;
104 }
105 write (fd, "hello", 5);
106 puts ("file created");
107
108 struct stat64 st1;
109 if (fstat64 (fd, &st1) != 0)
110 {
111 puts ("fstat64 failed");
112 return 1;
113 }
114
115 close (fd);
116
117 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
118 {
119 puts ("fchownat failed");
120 return 1;
121 }
122
123 struct stat64 st2;
124 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
125 {
126 puts ("fstatat64 failed");
127 return 1;
128 }
129
130 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
131 {
132 puts ("owner change failed");
133 return 1;
134 }
135
136 if (unlinkat (dir_fd, "some-file", 0) != 0)
137 {
138 puts ("unlinkat failed");
139 return 1;
140 }
141
142 close (dir_fd);
143
144 return 0;
145 }