From 4973cbe599b0ddc97306eb8bb23bd2104c89a565 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Thu, 10 Nov 2005 00:12:21 +0000 Subject: * io/fcntl.h: Declare openat and openat64. * io/Makefile (routines): Add openat and openat64. (tests): Add tst-openat. * io/Versions: Add openat and openat64 for GLIBC_2.4. * sysdeps/generic/openat.c: New file. * sysdeps/generic/openat64.c: New file. * sysdeps/unix/sysv/linux/openat.c: New file. * sysdeps/unix/sysv/linux/openat64.c: New file. * io/tst-openat.c: New file. * sysdeps/unix/sysv/linux/device-nrs.h: Add info for /dev/full. --- io/Makefile | 8 +-- io/Versions | 3 ++ io/fcntl.h | 29 +++++++++- io/tst-openat.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 io/tst-openat.c (limited to 'io') diff --git a/io/Makefile b/io/Makefile index 906d1e3301..b1ca46814b 100644 --- a/io/Makefile +++ b/io/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1992-2002, 2003 Free Software Foundation, Inc. +# Copyright (C) 1992-2002, 2003, 2005 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 @@ -34,7 +34,8 @@ routines := \ statfs fstatfs statfs64 fstatfs64 \ statvfs fstatvfs statvfs64 fstatvfs64 \ umask chmod fchmod lchmod mkdir \ - open open64 close read write lseek lseek64 access euidaccess \ + open open64 openat openat64 close \ + read write lseek lseek64 access euidaccess \ fcntl flock lockf lockf64 \ dup dup2 pipe \ creat creat64 \ @@ -57,7 +58,8 @@ static-only-routines = stat fstat lstat mknod stat64 fstat64 lstat64 others := pwd 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-fcntl bug-ftw1 bug-ftw2 bug-ftw3 bug-ftw4 tst-statvfs \ + tst-openat distribute := ftwtest-sh diff --git a/io/Versions b/io/Versions index 5b311ee03b..f5cd8d5148 100644 --- a/io/Versions +++ b/io/Versions @@ -97,4 +97,7 @@ libc { # n* nftw; nftw64; } + GLIBC_2.4 { + openat; openat64; + } } diff --git a/io/fcntl.h b/io/fcntl.h index ef9d5f9b81..044a988872 100644 --- a/io/fcntl.h +++ b/io/fcntl.h @@ -1,4 +1,5 @@ -/* Copyright (C) 1991,1992,1994-2001,2003,2004 Free Software Foundation, Inc. +/* Copyright (C) 1991,1992,1994-2001,2003,2004,2005 + 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 @@ -82,6 +83,32 @@ extern int __REDIRECT (open, (__const char *__file, int __oflag, ...), open64) extern int open64 (__const char *__file, int __oflag, ...) __nonnull ((1)); #endif +#ifdef __USE_GNU +/* Similar to OPEN but a relative path name is interpreted relative to + the directory for which FD is a descriptor. + + NOTE: some other OPENAT implementation support additional functionality + through this interface, especially using the O_XATTR flag. This is not + yet supported here. + + This function is a cancellation point and therefore not marked with + __THROW. */ +# ifndef __USE_FILE_OFFSET64 +extern int openat (int __fd, __const char *__file, int __oflag, ...) + __nonnull ((2)); +# else +# ifdef __REDIRECT +extern int __REDIRECT (openat, (int __fd, __const char *__file, int __oflag, + ...), openat64) __nonnull ((2)); +# else +# define openat openat64 +# endif +# endif + +extern int openat64 (int __fd, __const char *__file, int __oflag, ...) + __nonnull ((2)); +#endif + /* Create and open FILE, with mode MODE. This takes an `int' MODE argument because that is what `mode_t' will be widened to. diff --git a/io/tst-openat.c b/io/tst-openat.c new file mode 100644 index 0000000000..c20c95a10d --- /dev/null +++ b/io/tst-openat.c @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include +#include + + +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-openat.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) + { + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + close (fd); + puts ("file created"); + + /* fdopendir takes over the descriptor, make a copy. */ + dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("2nd lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + bool seen_file = false; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + if (strcmp (d->d_name, "some-file") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + + seen_file = true; + } + closedir (dir); + + if (!seen_file) + { + puts ("file not created in correct directory"); + return 1; + } + + int cwdfd = open (".", O_RDONLY | O_DIRECTORY); + if (cwdfd == -1) + { + puts ("cannot get descriptor for cwd"); + return 1; + } + + if (fchdir (dir_fd) != 0) + { + puts ("1st fchdir failed"); + return 1; + } + + if (unlink ("some-file") != 0) + { + puts ("unlink failed"); + return 1; + } + + if (fchdir (cwdfd) != 0) + { + puts ("2nd fchdir failed"); + return 1; + } + + close (dir_fd); + close (cwdfd); + + return 0; +} -- cgit 1.4.1