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