* nis/nss_nisplus/nisplus-service.c: Fix locking to use
[glibc.git] / io / tst-fchownat.c
blob0cbf78b2b0dccc921afc0327234150aefdbfe189
1 #include <dirent.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
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"
17 static int dir_fd;
19 static void
20 prepare (void)
22 #if _POSIX_CHOWN_RESTRICTED > 0
23 uid_t uid = getuid ();
24 if (uid != 0)
26 puts ("need root privileges");
27 exit (0);
29 #endif
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);
36 if (dirbuf == NULL)
38 puts ("out of memory");
39 exit (1);
42 snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name);
43 if (mkdtemp (dirbuf) == NULL)
45 puts ("cannot create temporary directory");
46 exit (1);
49 add_temp_file (dirbuf);
51 dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY);
52 if (dir_fd == -1)
54 puts ("cannot open directory");
55 exit (1);
60 static int
61 do_test (void)
63 /* fdopendir takes over the descriptor, make a copy. */
64 int dupfd = dup (dir_fd);
65 if (dupfd == -1)
67 puts ("dup failed");
68 return 1;
70 if (lseek (dupfd, 0, SEEK_SET) != 0)
72 puts ("1st lseek failed");
73 return 1;
76 /* The directory should be empty safe the . and .. files. */
77 DIR *dir = fdopendir (dupfd);
78 if (dir == NULL)
80 puts ("fdopendir failed");
81 return 1;
83 struct dirent64 *d;
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);
88 return 1;
90 closedir (dir);
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)
96 if (errno == ENOSYS)
98 puts ("*at functions not supported");
99 return 0;
102 puts ("file creation failed");
103 return 1;
105 write (fd, "hello", 5);
106 puts ("file created");
108 struct stat64 st1;
109 if (fstat64 (fd, &st1) != 0)
111 puts ("fstat64 failed");
112 return 1;
115 close (fd);
117 if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
119 puts ("fchownat failed");
120 return 1;
123 struct stat64 st2;
124 if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
126 puts ("fstatat64 failed");
127 return 1;
130 if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid)
132 puts ("owner change failed");
133 return 1;
136 if (unlinkat (dir_fd, "some-file", 0) != 0)
138 puts ("unlinkat failed");
139 return 1;
142 close (dir_fd);
144 return 0;