aboutsummaryrefslogtreecommitdiff
path: root/io
diff options
context:
space:
mode:
Diffstat (limited to 'io')
-rw-r--r--io/tst-faccessat.c59
-rw-r--r--io/tst-fchmodat.c45
-rw-r--r--io/tst-fchownat.c45
-rw-r--r--io/tst-fstatat.c45
-rw-r--r--io/tst-openat.c40
-rw-r--r--io/tst-renameat.c76
6 files changed, 310 insertions, 0 deletions
diff --git a/io/tst-faccessat.c b/io/tst-faccessat.c
index 52b2e7b..4853207 100644
--- a/io/tst-faccessat.c
+++ b/io/tst-faccessat.c
@@ -98,6 +98,20 @@ do_test (void)
write (fd, "hello", 5);
puts ("file created");
+ /* Before closing the file, try using this file descriptor to open
+ another file. This must fail. */
+ if (faccessat (fd, "should-not-work", F_OK, AT_EACCESS) != -1)
+ {
+ puts ("faccessat using descriptor for normal file worked");
+ return 1;
+ }
+ if (errno != ENOTDIR)
+ {
+ puts ("\
+error for faccessat using descriptor for normal file not ENOTDIR ");
+ return 1;
+ }
+
close (fd);
int result = 0;
@@ -141,6 +155,39 @@ do_test (void)
result = 1;
}
+ /* Create a file descriptor which is closed again right away. */
+ int dir_fd2 = dup (dir_fd);
+ if (dir_fd2 == -1)
+ {
+ puts ("dup failed");
+ return 1;
+ }
+ close (dir_fd2);
+
+ /* With the file descriptor closed the next call must fail. */
+ if (faccessat (dir_fd2, "some-file", F_OK, AT_EACCESS) != -1)
+ {
+ puts ("faccessat using closed descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("faccessat using closed descriptor did not set EBADF");
+ return 1;
+ }
+
+ /* Same with a non-existing file. */
+ if (faccessat (dir_fd2, "non-existing-file", F_OK, AT_EACCESS) != -1)
+ {
+ puts ("2nd faccessat using closed descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("2nd faccessat using closed descriptor did not set EBADF");
+ return 1;
+ }
+
if (unlinkat (dir_fd, "some-file", 0) != 0)
{
puts ("unlinkat failed");
@@ -149,5 +196,17 @@ do_test (void)
close (dir_fd);
+ fd = faccessat (-1, "some-file", F_OK, AT_EACCESS);
+ if (fd != -1)
+ {
+ puts ("faccessat using -1 descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("faccessat using -1 descriptor did not set EBADF");
+ return 1;
+ }
+
return result;
}
diff --git a/io/tst-fchmodat.c b/io/tst-fchmodat.c
index 0288d6b..bfb75d6 100644
--- a/io/tst-fchmodat.c
+++ b/io/tst-fchmodat.c
@@ -107,6 +107,20 @@ do_test (void)
return 1;
}
+ /* Before closing the file, try using this file descriptor to open
+ another file. This must fail. */
+ if (fchmodat (fd, "some-file", 0400, 0) != -1)
+ {
+ puts ("fchmodat using descriptor for normal file worked");
+ return 1;
+ }
+ if (errno != ENOTDIR)
+ {
+ puts ("\
+error for fchmodat using descriptor for normal file not ENOTDIR ");
+ return 1;
+ }
+
close (fd);
if ((st1.st_mode & 0777) != 0644)
@@ -140,7 +154,38 @@ do_test (void)
return 1;
}
+ /* Create a file descriptor which is closed again right away. */
+ int dir_fd2 = dup (dir_fd);
+ if (dir_fd2 == -1)
+ {
+ puts ("dup failed");
+ return 1;
+ }
+ close (dir_fd2);
+
+ if (fchmodat (dir_fd2, "some-file", 0400, 0) != -1)
+ {
+ puts ("fchmodat using closed descriptor worked");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for fchmodat using closed descriptor not EBADF ");
+ return 1;
+ }
+
close (dir_fd);
+ if (fchmodat (-1, "some-file", 0400, 0) != -1)
+ {
+ puts ("fchmodat using invalid descriptor worked");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for fchmodat using invalid descriptor not EBADF ");
+ return 1;
+ }
+
return 0;
}
diff --git a/io/tst-fchownat.c b/io/tst-fchownat.c
index 0cbf78b..fd32ac9 100644
--- a/io/tst-fchownat.c
+++ b/io/tst-fchownat.c
@@ -112,6 +112,20 @@ do_test (void)
return 1;
}
+ /* Before closing the file, try using this file descriptor to open
+ another file. This must fail. */
+ if (fchownat (fd, "some-file", 1, 1, 0) != -1)
+ {
+ puts ("fchownat using descriptor for normal file worked");
+ return 1;
+ }
+ if (errno != ENOTDIR)
+ {
+ puts ("\
+error for fchownat using descriptor for normal file not ENOTDIR ");
+ return 1;
+ }
+
close (fd);
if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0)
@@ -139,7 +153,38 @@ do_test (void)
return 1;
}
+ /* Create a file descriptor which is closed again right away. */
+ int dir_fd2 = dup (dir_fd);
+ if (dir_fd2 == -1)
+ {
+ puts ("dup failed");
+ return 1;
+ }
+ close (dir_fd2);
+
+ if (fchownat (dir_fd2, "some-file", 1, 1, 0) != -1)
+ {
+ puts ("fchownat using closed descriptor worked");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for fchownat using closed descriptor not EBADF ");
+ return 1;
+ }
+
close (dir_fd);
+ if (fchownat (-1, "some-file", 1, 1, 0) != -1)
+ {
+ puts ("fchownat using invalid descriptor worked");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for fchownat using invalid descriptor not EBADF ");
+ return 1;
+ }
+
return 0;
}
diff --git a/io/tst-fstatat.c b/io/tst-fstatat.c
index 6ddf30d..5493102 100644
--- a/io/tst-fstatat.c
+++ b/io/tst-fstatat.c
@@ -97,6 +97,20 @@ do_test (void)
puts ("file created");
struct stat64 st1;
+
+ /* Before closing the file, try using this file descriptor to open
+ another file. This must fail. */
+ if (fstatat64 (fd, "some-file", &st1, 0) != -1)
+ {
+ puts ("fstatatat using descriptor for normal file worked");
+ return 1;
+ }
+ if (errno != ENOTDIR)
+ {
+ puts ("error for fstatat using descriptor for normal file not ENOTDIR ");
+ return 1;
+ }
+
if (fstat64 (fd, &st1) != 0)
{
puts ("fstat64 failed");
@@ -137,7 +151,38 @@ do_test (void)
return 1;
}
+ /* Create a file descriptor which is closed again right away. */
+ int dir_fd2 = dup (dir_fd);
+ if (dir_fd2 == -1)
+ {
+ puts ("dup failed");
+ return 1;
+ }
+ close (dir_fd2);
+
+ if (fstatat64 (dir_fd2, "some-file", &st1, 0) != -1)
+ {
+ puts ("fstatat64 using closed descriptor worked");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for fstatat using closed descriptor not EBADF ");
+ return 1;
+ }
+
close (dir_fd);
+ if (fstatat64 (-1, "some-file", &st1, 0) != -1)
+ {
+ puts ("fstatat64 using invalid descriptor worked");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for fstatat using invalid descriptor not EBADF ");
+ return 1;
+ }
+
return 0;
}
diff --git a/io/tst-openat.c b/io/tst-openat.c
index d10b654..0ceb745 100644
--- a/io/tst-openat.c
+++ b/io/tst-openat.c
@@ -94,6 +94,21 @@ do_test (void)
return 1;
}
write (fd, "hello", 5);
+
+ /* Before closing the file, try using this file descriptor to open
+ another file. This must fail. */
+ int fd2 = openat (fd, "should-not-work", O_CREAT|O_RDWR, 0666);
+ if (fd2 != -1)
+ {
+ puts ("openat using descriptor for normal file worked");
+ return 1;
+ }
+ if (errno != ENOTDIR)
+ {
+ puts ("error for openat using descriptor for normal file not ENOTDIR ");
+ return 1;
+ }
+
close (fd);
puts ("file created");
@@ -165,5 +180,30 @@ do_test (void)
close (dir_fd);
close (cwdfd);
+ /* With the file descriptor closed the next call must fail. */
+ fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
+ if (fd != -1)
+ {
+ puts ("openat using closed descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("openat using closed descriptor did not set EBADF");
+ return 1;
+ }
+
+ fd = openat (-1, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666);
+ if (fd != -1)
+ {
+ puts ("openat using -1 descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("openat using -1 descriptor did not set EBADF");
+ return 1;
+ }
+
return 0;
}
diff --git a/io/tst-renameat.c b/io/tst-renameat.c
index fb49459..a7c0ec1 100644
--- a/io/tst-renameat.c
+++ b/io/tst-renameat.c
@@ -103,6 +103,29 @@ do_test (void)
return 1;
}
+ /* Using a descriptor for a normal file must fail. */
+ if (renameat (fd, "some-file", dir_fd, "another-file") == 0)
+ {
+ puts ("renameat with normal file descriptor succeeded");
+ return 1;
+ }
+ if (errno != ENOTDIR)
+ {
+ puts ("error for renameat with normal file descriptor not ENOTDIR");
+ return 1;
+ }
+
+ if (renameat (dir_fd, "some-file", fd, "another-file") == 0)
+ {
+ puts ("2nd renameat with normal file descriptor succeeded");
+ return 1;
+ }
+ if (errno != ENOTDIR)
+ {
+ puts ("error for 2nd renameat with normal file descriptor not ENOTDIR");
+ return 1;
+ }
+
close (fd);
if (renameat (dir_fd, "some-file", dir_fd, "another-file") != 0)
@@ -137,12 +160,65 @@ do_test (void)
return 1;
}
+ /* Create a file descriptor which is closed again right away. */
+ int dir_fd2 = dup (dir_fd);
+ if (dir_fd2 == -1)
+ {
+ puts ("dup failed");
+ return 1;
+ }
+ close (dir_fd2);
+
+ if (renameat (dir_fd2, "another-file", dir_fd, "some-file") == 0)
+ {
+ puts ("renameat with closed file descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for renameat with closed file descriptor not EBADF");
+ return 1;
+ }
+
+ if (renameat (dir_fd, "another-file", dir_fd2, "some-file") == 0)
+ {
+ puts ("2nd renameat with closed file descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for 2nd renameat with closed file descriptor not EBADF");
+ return 1;
+ }
+
if (unlinkat (dir_fd, "another-file", 0) != 0)
{
puts ("unlinkat failed");
return 1;
}
+ if (renameat (-1, "another-file", dir_fd, "some-file") == 0)
+ {
+ puts ("renameat with invalid file descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for renameat with invalid file descriptor not EBADF");
+ return 1;
+ }
+
+ if (renameat (dir_fd, "another-file", -1, "some-file") == 0)
+ {
+ puts ("2nd renameat with invalid file descriptor succeeded");
+ return 1;
+ }
+ if (errno != EBADF)
+ {
+ puts ("error for 2nd renameat with invalid file descriptor not EBADF");
+ return 1;
+ }
+
close (dir_fd);
return 0;