diff options
author | Jakub Jelinek <jakub@redhat.com> | 2006-01-30 09:30:09 +0000 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2006-01-30 09:30:09 +0000 |
commit | 3e543bc56346540cbf73fd48d0172fc6a588efd5 (patch) | |
tree | b2c3502b6596d238ac861cc82cafdc6f8b84e143 /io | |
parent | 06f313e361a523605ba6d4c9cdc67a7353cd367c (diff) |
Updated to fedora-glibc-20060130T0922
Diffstat (limited to 'io')
-rw-r--r-- | io/Makefile | 7 | ||||
-rw-r--r-- | io/Versions | 2 | ||||
-rw-r--r-- | io/ppoll.c | 76 | ||||
-rw-r--r-- | io/sys/poll.h | 21 | ||||
-rw-r--r-- | io/tst-faccessat.c | 61 | ||||
-rw-r--r-- | io/tst-fchmodat.c | 45 | ||||
-rw-r--r-- | io/tst-fchownat.c | 45 | ||||
-rw-r--r-- | io/tst-fstatat.c | 45 | ||||
-rw-r--r-- | io/tst-linkat.c | 171 | ||||
-rw-r--r-- | io/tst-mkdirat.c | 162 | ||||
-rw-r--r-- | io/tst-mkfifoat.c | 162 | ||||
-rw-r--r-- | io/tst-mknodat.c | 162 | ||||
-rw-r--r-- | io/tst-openat.c | 40 | ||||
-rw-r--r-- | io/tst-readlinkat.c | 136 | ||||
-rw-r--r-- | io/tst-renameat.c | 76 | ||||
-rw-r--r-- | io/tst-symlinkat.c | 164 |
16 files changed, 1371 insertions, 4 deletions
diff --git a/io/Makefile b/io/Makefile index 5e6d72eace..895b3e3a73 100644 --- a/io/Makefile +++ b/io/Makefile @@ -48,7 +48,7 @@ routines := \ ttyname ttyname_r isatty \ link linkat symlink symlinkat readlink readlinkat \ unlink unlinkat rmdir \ - ftw ftw64 fts poll \ + ftw ftw64 fts poll ppoll \ posix_fadvise posix_fadvise64 \ posix_fallocate posix_fallocate64 \ sendfile sendfile64 @@ -64,7 +64,9 @@ test-srcs := ftwtest tests := test-utime test-stat test-stat2 test-lfs tst-getcwd \ tst-fcntl bug-ftw1 bug-ftw2 bug-ftw3 bug-ftw4 tst-statvfs \ tst-openat tst-unlinkat tst-fstatat tst-futimesat \ - tst-renameat tst-fchownat tst-fchmodat tst-faccessat + tst-renameat tst-fchownat tst-fchmodat tst-faccessat \ + tst-symlinkat tst-linkat tst-readlinkat tst-mkdirat \ + tst-mknodat tst-mkfifoat distribute := ftwtest-sh @@ -72,6 +74,7 @@ include ../Rules CFLAGS-fcntl.c = -fexceptions -fasynchronous-unwind-tables CFLAGS-poll.c = -fexceptions -fasynchronous-unwind-tables +CFLAGS-ppoll.c = -fexceptions -fasynchronous-unwind-tables CFLAGS-lockf.c = -fexceptions CFLAGS-statfs.c = -fexceptions CFLAGS-fstatfs.c = -fexceptions diff --git a/io/Versions b/io/Versions index 16006a8e08..bc9c9d2685 100644 --- a/io/Versions +++ b/io/Versions @@ -110,5 +110,7 @@ libc { readlinkat; symlinkat; unlinkat; + + ppoll; } } diff --git a/io/ppoll.c b/io/ppoll.c new file mode 100644 index 0000000000..a035cfeb1f --- /dev/null +++ b/io/ppoll.c @@ -0,0 +1,76 @@ +/* Copyright (C) 2006 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper <drepper@redhat.com>, 2006. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include <errno.h> +#include <limits.h> +#include <signal.h> +#include <stddef.h> /* For NULL. */ +#include <sys/poll.h> +#include <sysdep-cancel.h> + + +int +ppoll (struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, + const sigset_t *sigmask) +{ + int tval = -1; + + /* poll uses a simple millisecond value. Convert it. */ + if (timeout != NULL) + { + if (timeout->tv_sec < 0 + || timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000) + { + __set_errno (EINVAL); + return -1; + } + + if (timeout->tv_sec > INT_MAX / 1000 + || (timeout->tv_sec == INT_MAX / 1000 + && ((timeout->tv_nsec + 999999) / 1000000 > INT_MAX % 1000))) + /* We cannot represent the timeout in an int value. Wait + forever. */ + tval = -1; + else + tval = (timeout->tv_sec * 1000 + + (timeout->tv_nsec + 999999) / 1000000); + } + + /* The setting and restoring of the signal mask and the select call + should be an atomic operation. This can't be done without kernel + help. */ + sigset_t savemask; + if (sigmask != NULL) + __sigprocmask (SIG_SETMASK, sigmask, &savemask); + + /* Note the ppoll() is a cancellation point. But since we call + poll() which itself is a cancellation point we do not have + to do anything here. */ + int retval = __poll (fds, nfds, tval); + + if (sigmask != NULL) + __sigprocmask (SIG_SETMASK, &savemask, NULL); + + return retval; +} + +#ifndef ppoll +/* __poll handles cancellation. */ +LIBC_CANCEL_HANDLED (); +#endif diff --git a/io/sys/poll.h b/io/sys/poll.h index 9cf1b9f1c0..4085b785ee 100644 --- a/io/sys/poll.h +++ b/io/sys/poll.h @@ -1,5 +1,5 @@ /* Compatibility definitions for System V `poll' interface. - Copyright (C) 1994,1996-2001,2004,2005 Free Software Foundation, Inc. + Copyright (C) 1994,1996-2001,2004,2005,2006 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -24,6 +24,13 @@ /* Get the platform dependent bits of `poll'. */ #include <bits/poll.h> +#ifdef __USE_GNU +/* Get the __sigset_t definition. */ +# include <bits/sigset.h> +/* Get the timespec definition. */ +# define __need_timespec +# include <time.h> +#endif /* Type used for the number of file descriptors. */ @@ -50,6 +57,18 @@ __BEGIN_DECLS __THROW. */ extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout); +#ifdef __USE_GNU +/* Like poll, but before waiting the threads signal mask is replaced + with that specified in the fourth parameter. For better usability, + the timeout value is specified using a TIMESPEC object. + + This function is a cancellation point and therefore not marked with + __THROW. */ +extern int ppoll (struct pollfd *__fds, nfds_t __nfds, + __const struct timespec *__timeout, + __const __sigset_t *__ss); +#endif + __END_DECLS #endif /* sys/poll.h */ diff --git a/io/tst-faccessat.c b/io/tst-faccessat.c index 3bf7aed2e5..48532070a7 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; @@ -135,12 +149,45 @@ do_test (void) errno = 0; if (faccessat (dir_fd, "some-file", W_OK, AT_EACCESS) == 0 - || errno != EACCES) + ? (geteuid () != 0) : (errno != EACCES)) { printf ("faccessat W_OK on unwritable file: %m\n"); 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 0288d6b5d9..bfb75d62e5 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 0cbf78b2b0..fd32ac9e6b 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 6ddf30d87b..5493102167 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-linkat.c b/io/tst-linkat.c new file mode 100644 index 0000000000..a77ceb1db4 --- /dev/null +++ b/io/tst-linkat.c @@ -0,0 +1,171 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-linkat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Try to create a file. */ + int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); + if (fd == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + puts ("file created"); + + struct stat64 st1; + if (fstat64 (fd, &st1) != 0) + { + puts ("fstat64 failed"); + return 1; + } + + close (fd); + + if (linkat (dir_fd, "some-file", dir_fd, "another-file") != 0) + { + puts ("symlinkat failed"); + return 1; + } + + struct stat64 st2; + if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0) + { + puts ("fstatat64 failed"); + return 1; + } + if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) + { + puts ("file changed after symlinkat"); + return 1; + } + + if (fstatat64 (dir_fd, "another-file", &st2, AT_SYMLINK_NOFOLLOW) != 0) + { + puts ("2nd fstatat64 failed"); + return 1; + } + if (S_ISLNK (st2.st_mode)) + { + puts ("2nd fstatat64 shows file is a symlink"); + return 1; + } + if (st1.st_dev != st2.st_dev + || st1.st_ino != st2.st_ino + || st1.st_size != st2.st_size) + { + puts ("stat results for linked file do not match"); + return 1; + } + + if (fstatat64 (dir_fd, "another-file", &st2, 0) != 0) + { + puts ("3rd fstatat64 failed"); + return 1; + } + if (st1.st_dev != st2.st_dev + || st1.st_ino != st2.st_ino + || st1.st_size != st2.st_size) + { + puts ("stat results do not match"); + return 1; + } + + if (unlinkat (dir_fd, "another-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + if (unlinkat (dir_fd, "some-file", 0) != 0) + { + puts ("2nd unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-mkdirat.c b/io/tst-mkdirat.c new file mode 100644 index 0000000000..3efa4622ef --- /dev/null +++ b/io/tst-mkdirat.c @@ -0,0 +1,162 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-mkdirat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Create a new directory. */ + int e = mkdirat (dir_fd, "some-dir", 0777); + if (e == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("directory creation failed"); + return 1; + } + + struct stat64 st1; + if (fstatat64 (dir_fd, "some-dir", &st1, 0) != 0) + { + puts ("fstat64 failed"); + return 1; + } + if (!S_ISDIR (st1.st_mode)) + { + puts ("mkdirat did not create a directory"); + return 1; + } + + dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("2nd fdopendir failed"); + return 1; + } + bool has_some_dir = false; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, "some-dir") == 0) + { + has_some_dir = true; +#ifdef _DIRENT_HAVE_D_TYPE + if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR) + { + puts ("d_type for some-dir wrong"); + return 1; + } +#endif + } + else if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + if (!has_some_dir) + { + puts ("some-dir not in directory list"); + return 1; + } + + if (unlinkat (dir_fd, "some-dir", AT_REMOVEDIR) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-mkfifoat.c b/io/tst-mkfifoat.c new file mode 100644 index 0000000000..2bf29f63af --- /dev/null +++ b/io/tst-mkfifoat.c @@ -0,0 +1,162 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-mkfifoat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Create a new directory. */ + int e = mkfifoat (dir_fd, "some-fifo", 0777); + if (e == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("fifo creation failed"); + return 1; + } + + struct stat64 st1; + if (fstatat64 (dir_fd, "some-fifo", &st1, 0) != 0) + { + puts ("fstat64 failed"); + return 1; + } + if (!S_ISFIFO (st1.st_mode)) + { + puts ("mkfifoat did not create FIFO"); + return 1; + } + + dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("2nd fdopendir failed"); + return 1; + } + bool has_some_fifo = false; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, "some-fifo") == 0) + { + has_some_fifo = true; +#ifdef _DIRENT_HAVE_D_TYPE + if (d->d_type != DT_UNKNOWN && d->d_type != DT_FIFO) + { + puts ("d_type for some-fifo wrong"); + return 1; + } +#endif + } + else if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + if (!has_some_fifo) + { + puts ("some-fifo not in directory list"); + return 1; + } + + if (unlinkat (dir_fd, "some-fifo", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-mknodat.c b/io/tst-mknodat.c new file mode 100644 index 0000000000..9158c0dfd4 --- /dev/null +++ b/io/tst-mknodat.c @@ -0,0 +1,162 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-mknodat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Create a new directory. */ + int e = mknodat (dir_fd, "some-sock", 0777 | S_IFSOCK, 0); + if (e == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("socket creation failed"); + return 1; + } + + struct stat64 st1; + if (fstatat64 (dir_fd, "some-sock", &st1, 0) != 0) + { + puts ("fstat64 failed"); + return 1; + } + if (!S_ISSOCK (st1.st_mode)) + { + puts ("mknodat did not create a Unix domain socket"); + return 1; + } + + dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("2nd fdopendir failed"); + return 1; + } + bool has_some_sock = false; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, "some-sock") == 0) + { + has_some_sock = true; +#ifdef _DIRENT_HAVE_D_TYPE + if (d->d_type != DT_UNKNOWN && d->d_type != DT_SOCK) + { + puts ("d_type for some-sock wrong"); + return 1; + } +#endif + } + else if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + if (!has_some_sock) + { + puts ("some-sock not in directory list"); + return 1; + } + + if (unlinkat (dir_fd, "some-sock", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-openat.c b/io/tst-openat.c index d10b654fa9..0ceb745880 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-readlinkat.c b/io/tst-readlinkat.c new file mode 100644 index 0000000000..891b6e6817 --- /dev/null +++ b/io/tst-readlinkat.c @@ -0,0 +1,136 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-symlinkat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + static const char symlinkcontent[] = "some-file"; + if (symlinkat (symlinkcontent, dir_fd, "another-file") != 0) + { + puts ("symlinkat failed"); + return 1; + } + + struct stat64 st2; + if (fstatat64 (dir_fd, "another-file", &st2, AT_SYMLINK_NOFOLLOW) != 0) + { + puts ("fstatat64 failed"); + return 1; + } + if (!S_ISLNK (st2.st_mode)) + { + puts ("2nd fstatat64 does not show file is a symlink"); + return 1; + } + + if (fstatat64 (dir_fd, symlinkcontent, &st2, AT_SYMLINK_NOFOLLOW) == 0) + { + puts ("2nd fstatat64 succeeded"); + return 1; + } + + char buf[100]; + int n = readlinkat (dir_fd, "another-file", buf, sizeof (buf)); + if (n == -1) + { + puts ("readlinkat failed"); + return 1; + } + if (n != sizeof (symlinkcontent) - 1) + { + printf ("readlinkat returned %d, expected %zu\n", + n, sizeof (symlinkcontent) - 1); + return 1; + } + if (strncmp (buf, symlinkcontent, n) != 0) + { + puts ("readlinkat retrieved wrong link content"); + return 1; + } + + if (unlinkat (dir_fd, "another-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-renameat.c b/io/tst-renameat.c index fb494594f5..a7c0ec16c4 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/io/tst-symlinkat.c b/io/tst-symlinkat.c new file mode 100644 index 0000000000..1e98588fc8 --- /dev/null +++ b/io/tst-symlinkat.c @@ -0,0 +1,164 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-symlinkat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Try to create a file. */ + int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); + if (fd == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + puts ("file created"); + + struct stat64 st1; + if (fstat64 (fd, &st1) != 0) + { + puts ("fstat64 failed"); + return 1; + } + + close (fd); + + if (symlinkat ("some-file", dir_fd, "another-file") != 0) + { + puts ("symlinkat failed"); + return 1; + } + + struct stat64 st2; + if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0) + { + puts ("fstatat64 failed"); + return 1; + } + if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) + { + puts ("file changed after symlinkat"); + return 1; + } + + if (fstatat64 (dir_fd, "another-file", &st2, AT_SYMLINK_NOFOLLOW) != 0) + { + puts ("2nd fstatat64 failed"); + return 1; + } + if (!S_ISLNK (st2.st_mode)) + { + puts ("2nd fstatat64 does not show file is a symlink"); + return 1; + } + + if (fstatat64 (dir_fd, "another-file", &st2, 0) != 0) + { + puts ("3rd fstatat64 failed"); + return 1; + } + if (st1.st_dev != st2.st_dev + || st1.st_ino != st2.st_ino + || st1.st_size != st2.st_size) + { + puts ("stat results do not match"); + return 1; + } + + if (unlinkat (dir_fd, "another-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + if (unlinkat (dir_fd, "some-file", 0) != 0) + { + puts ("2nd unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} |