1 /* Test for faccessat function. */
12 static void prepare (void);
13 #define PREPARE(argc, argv) prepare ()
15 static int do_test (void);
16 #define TEST_FUNCTION do_test ()
18 #include "../test-skeleton.c"
25 size_t test_dir_len
= strlen (test_dir
);
26 static const char dir_name
[] = "/tst-faccessat.XXXXXX";
28 size_t dirbuflen
= test_dir_len
+ sizeof (dir_name
);
29 char *dirbuf
= malloc (dirbuflen
);
32 puts ("out of memory");
36 snprintf (dirbuf
, dirbuflen
, "%s%s", test_dir
, dir_name
);
37 if (mkdtemp (dirbuf
) == NULL
)
39 puts ("cannot create temporary directory");
43 add_temp_file (dirbuf
);
45 dir_fd
= open (dirbuf
, O_RDONLY
| O_DIRECTORY
);
48 puts ("cannot open directory");
57 /* fdopendir takes over the descriptor, make a copy. */
58 int dupfd
= dup (dir_fd
);
64 if (lseek (dupfd
, 0, SEEK_SET
) != 0)
66 puts ("1st lseek failed");
70 /* The directory should be empty save the . and .. files. */
71 DIR *dir
= fdopendir (dupfd
);
74 puts ("fdopendir failed");
78 while ((d
= readdir64 (dir
)) != NULL
)
79 if (strcmp (d
->d_name
, ".") != 0 && strcmp (d
->d_name
, "..") != 0)
81 printf ("temp directory contains file \"%s\"\n", d
->d_name
);
86 /* Try to create a file. */
87 int fd
= openat (dir_fd
, "some-file", O_CREAT
|O_RDWR
|O_EXCL
, 0666);
92 puts ("*at functions not supported");
96 puts ("file creation failed");
99 write (fd
, "hello", 5);
100 puts ("file created");
102 /* Before closing the file, try using this file descriptor to open
103 another file. This must fail. */
104 if (faccessat (fd
, "should-not-work", F_OK
, AT_EACCESS
) != -1)
106 puts ("faccessat using descriptor for normal file worked");
109 if (errno
!= ENOTDIR
)
112 error for faccessat using descriptor for normal file not ENOTDIR ");
120 if (faccessat (dir_fd
, "some-file", F_OK
, AT_EACCESS
))
122 printf ("faccessat F_OK: %m\n");
125 if (faccessat (dir_fd
, "some-file", W_OK
, AT_EACCESS
))
127 printf ("faccessat W_OK: %m\n");
132 if (faccessat (dir_fd
, "some-file", X_OK
, AT_EACCESS
) == 0
135 printf ("faccessat X_OK on nonexecutable: %m\n");
139 if (fchmodat (dir_fd
, "some-file", 0400, 0) != 0)
141 printf ("fchownat failed: %m\n");
145 if (faccessat (dir_fd
, "some-file", R_OK
, AT_EACCESS
))
147 printf ("faccessat R_OK: %m\n");
152 if (faccessat (dir_fd
, "some-file", W_OK
, AT_EACCESS
) == 0
153 ? (geteuid () != 0) : (errno
!= EACCES
))
155 printf ("faccessat W_OK on unwritable file: %m\n");
159 /* Create a file descriptor which is closed again right away. */
160 int dir_fd2
= dup (dir_fd
);
168 /* With the file descriptor closed the next call must fail. */
169 if (faccessat (dir_fd2
, "some-file", F_OK
, AT_EACCESS
) != -1)
171 puts ("faccessat using closed descriptor succeeded");
176 puts ("faccessat using closed descriptor did not set EBADF");
180 /* Same with a non-existing file. */
181 if (faccessat (dir_fd2
, "non-existing-file", F_OK
, AT_EACCESS
) != -1)
183 puts ("2nd faccessat using closed descriptor succeeded");
188 puts ("2nd faccessat using closed descriptor did not set EBADF");
192 if (unlinkat (dir_fd
, "some-file", 0) != 0)
194 puts ("unlinkat failed");
200 fd
= faccessat (-1, "some-file", F_OK
, AT_EACCESS
);
203 puts ("faccessat using -1 descriptor succeeded");
208 puts ("faccessat using -1 descriptor did not set EBADF");