return EBADF for non-existing file descriptors.
* sysdeps/unix/sysv/linux/openat.c (__atfct_seterrno): Likewise.
* io/tst-openat.c (do_test): Add some more tests.
* io/tst-faccessat.c (do_test): Likewise.
* io/tst-renameat.c (do_test): Likewise.
* io/tst-fstatat.c (do_test): Likewise.
* io/tst-fchmodat.c (do_test): Likewise.
* io/tst-fchownat.c (do_test): Likewise.
+2006-01-21 Ulrich Drepper <drepper@redhat.com>
+
+ * sysdeps/unix/sysv/linux/renameat.c (__atfct_seterrno_2): Correcty
+ return EBADF for non-existing file descriptors.
+ * sysdeps/unix/sysv/linux/openat.c (__atfct_seterrno): Likewise.
+ * io/tst-openat.c (do_test): Add some more tests.
+ * io/tst-faccessat.c (do_test): Likewise.
+ * io/tst-renameat.c (do_test): Likewise.
+ * io/tst-fstatat.c (do_test): Likewise.
+ * io/tst-fchmodat.c (do_test): Likewise.
+ * io/tst-fchownat.c (do_test): Likewise.
+
2006-01-21 Roland McGrath <roland@redhat.com>
* sysdeps/unix/sysv/linux/faccessat.c: Move RESULT defn inside block.
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;
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");
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;
}
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)
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;
}
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)
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;
}
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");
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;
}
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");
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;
}
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)
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;
file descriptor number. */
*(char *) strchr (buf + sizeof "/proc/self/fd", '/') = '\0';
- int e = __xstat64 (_STAT_VER, buf, &st);
+ int e = __lxstat64 (_STAT_VER, buf, &st);
if ((e == -1 && errno == ENOENT)
||(e == 0 && !S_ISLNK (st.st_mode)))
errval = EBADF;
__atfct_seterrno_2 (int errval, int fd1, const char *buf1, int fd2,
const char *buf2)
{
- if (errval == ENOTDIR && (buf1 != NULL || buf2 != NULL))
+ if (buf1 != NULL || buf2 != NULL)
{
- /* This can mean either the file descriptor is invalid or
- /proc is not mounted. */
struct stat64 st;
- if (buf1 != NULL)
+ if (errval == ENOTDIR)
{
- if (__fxstat64 (_STAT_VER, fd1, &st) != 0)
- /* errno is already set correctly. */
- return;
-
- /* If /proc is not mounted there is nothing we can do. */
- if (S_ISDIR (st.st_mode)
- && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
- || !S_ISDIR (st.st_mode)))
+ /* This can mean either the file descriptor is invalid or
+ /proc is not mounted. */
+ if (buf1 != NULL)
{
- errval = ENOSYS;
- goto out;
+ if (__fxstat64 (_STAT_VER, fd1, &st) != 0)
+ /* errno is already set correctly. */
+ return;
+
+ /* If /proc is not mounted there is nothing we can do. */
+ if (S_ISDIR (st.st_mode)
+ && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
+ || !S_ISDIR (st.st_mode)))
+ {
+ errval = ENOSYS;
+ goto out;
+ }
}
- }
- if (buf2 != NULL)
+ if (buf2 != NULL)
+ {
+ if (__fxstat64 (_STAT_VER, fd2, &st) != 0)
+ /* errno is already set correctly. */
+ return;
+
+ /* If /proc is not mounted there is nothing we can do. */
+ if (S_ISDIR (st.st_mode)
+ && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
+ || !S_ISDIR (st.st_mode)))
+ errval = ENOSYS;
+ }
+ }
+ else if (errval == ENOENT)
{
- if (__fxstat64 (_STAT_VER, fd2, &st) != 0)
- /* errno is already set correctly. */
- return;
-
- /* If /proc is not mounted there is nothing we can do. */
- if (S_ISDIR (st.st_mode)
- && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0
- || !S_ISDIR (st.st_mode)))
- errval = ENOSYS;
+ /* This could mean the file descriptor is not valid. We
+ reuse BUF for the stat call. Find the slash after the
+ file descriptor number. */
+ if (buf1 != NULL)
+ {
+ *(char *) strchr (buf1 + sizeof "/proc/self/fd", '/') = '\0';
+
+ int e = __lxstat64 (_STAT_VER, buf1, &st);
+ if ((e == -1 && errno == ENOENT)
+ ||(e == 0 && !S_ISLNK (st.st_mode)))
+ {
+ errval = EBADF;
+ goto out;
+ }
+ }
+
+ if (buf2 != NULL)
+ {
+ *(char *) strchr (buf2 + sizeof "/proc/self/fd", '/') = '\0';
+
+ int e = __lxstat64 (_STAT_VER, buf2, &st);
+ if ((e == -1 && errno == ENOENT)
+ ||(e == 0 && !S_ISLNK (st.st_mode)))
+ errval = EBADF;
+ }
}
}