powerpc: Fix strncat ifunc selection
[glibc.git] / io / tst-fchownat.c
blobe8adf6229fc6033b018c951cd21c3dd59c6faed5
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>
10 static void prepare (void);
11 #define PREPARE(argc, argv) prepare ()
13 static int do_test (void);
14 #define TEST_FUNCTION do_test ()
16 #include "../test-skeleton.c"
18 static int dir_fd;
20 static void
21 prepare (void)
23 #if _POSIX_CHOWN_RESTRICTED == 0
24 if (pathconf (test_dir, _PC_CHOWN_RESTRICTED) != 0)
25 #endif
27 uid_t uid = getuid ();
28 if (uid != 0)
30 puts ("need root privileges");
31 exit (0);
35 size_t test_dir_len = strlen (test_dir);
36 static const char dir_name[] = "/tst-fchownat.XXXXXX";
38 size_t dirbuflen = test_dir_len + sizeof (dir_name);
39 char *dirbuf = malloc (dirbuflen);
40 if (dirbuf == NULL)
42 puts ("out of memory");
43 exit (1);
46 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
47 if (mkdtemp (dirbuf) == NULL)
49 puts ("cannot create temporary directory");
50 exit (1);
53 add_temp_file (dirbuf);
55 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
56 if (dir_fd == -1)
58 puts ("cannot open directory");
59 exit (1);
64 static int
65 do_test (void)
67 /* fdopendir takes over the descriptor, make a copy. */
68 int dupfd = dup (dir_fd);
69 if (dupfd == -1)
71 puts ("dup failed");
72 return 1;
74 if (lseek (dupfd, 0, SEEK_SET) != 0)
76 puts ("1st lseek failed");
77 return 1;
80 /* The directory should be empty safe the . and .. files. */
81 DIR *dir = fdopendir (dupfd);
82 if (dir == NULL)
84 puts ("fdopendir failed");
85 return 1;
87 struct dirent64 *d;
88 while ((d = readdir64 (dir)) != NULL)
89 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0)
91 printf ("temp directory contains file \"%s\"\n", d->d_name);
92 return 1;
94 closedir (dir);
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)
100 if (errno == ENOSYS)
102 puts ("*at functions not supported");
103 return 0;
106 puts ("file creation failed");
107 return 1;
109 write (fd, "hello", 5);
110 puts ("file created");
112 struct stat64 st1;
113 if (fstat64 (fd, &st1) != 0)
115 puts ("fstat64 failed");
116 return 1;
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)
123 puts ("fchownat using descriptor for normal file worked");
124 return 1;
126 if (errno != ENOTDIR)
128 puts ("\
129 error for fchownat using descriptor for normal file not ENOTDIR ");
130 return 1;
133 close (fd);
135 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
137 puts ("fchownat failed");
138 return 1;
141 struct stat64 st2;
142 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
144 puts ("fstatat64 failed");
145 return 1;
148 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
150 puts ("owner change failed");
151 return 1;
154 if (unlinkat (dir_fd, "some-file", 0) != 0)
156 puts ("unlinkat failed");
157 return 1;
160 /* Create a file descriptor which is closed again right away. */
161 int dir_fd2 = dup (dir_fd);
162 if (dir_fd2 == -1)
164 puts ("dup failed");
165 return 1;
167 close (dir_fd2);
169 if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
171 puts ("fchownat using closed descriptor worked");
172 return 1;
174 if (errno != EBADF)
176 puts ("error for fchownat using closed descriptor not EBADF ");
177 return 1;
180 close (dir_fd);
182 if (fchownat (-1, "some-file", 1, 1, 0) != -1)
184 puts ("fchownat using invalid descriptor worked");
185 return 1;
187 if (errno != EBADF)
189 puts ("error for fchownat using invalid descriptor not EBADF ");
190 return 1;
193 return 0;