From: Ulrich Drepper Date: Sun, 22 Jan 2006 00:57:24 +0000 (+0000) Subject: * sysdeps/unix/sysv/linux/renameat.c (__atfct_seterrno_2): Correcty X-Git-Tag: upstream/2.20~8838 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bdc7e223aaced59cd03436e1518845db011fba4b;p=platform%2Fupstream%2Flinaro-glibc.git * 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. --- diff --git a/ChangeLog b/ChangeLog index 606fe35..e5e3657 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2006-01-21 Ulrich Drepper + + * 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 * sysdeps/unix/sysv/linux/faccessat.c: Move RESULT defn inside block. 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; diff --git a/sysdeps/unix/sysv/linux/openat.c b/sysdeps/unix/sysv/linux/openat.c index bcdbada..67e9df2 100644 --- a/sysdeps/unix/sysv/linux/openat.c +++ b/sysdeps/unix/sysv/linux/openat.c @@ -60,7 +60,7 @@ __atfct_seterrno (int errval, int fd, const char *buf) 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; diff --git a/sysdeps/unix/sysv/linux/renameat.c b/sysdeps/unix/sysv/linux/renameat.c index bc8ced4..86bb75a 100644 --- a/sysdeps/unix/sysv/linux/renameat.c +++ b/sysdeps/unix/sysv/linux/renameat.c @@ -30,39 +30,70 @@ attribute_hidden __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; + } } }