summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog10
-rw-r--r--io/Makefile7
-rw-r--r--io/Versions1
-rw-r--r--io/fchmodat.c50
-rw-r--r--io/sys/stat.h10
-rw-r--r--io/tst-fchmodat.c146
-rw-r--r--sysdeps/unix/sysv/linux/fchmodat.c87
7 files changed, 307 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 3bf66a25a8..d982f36962 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2006-01-05  Roland McGrath  <roland@redhat.com>
+
+	* io/sys/stat.h [__USE_GNU]: Declare fchmodat.
+	* io/fchmodat.c: New file.
+	* io/Makefile (routines): Add fchmodat.
+	* io/Versions (libc: GLIBC_2.4): Likewise.
+	* sysdeps/unix/sysv/linux/fchmodat.c: New file.
+	* io/tst-fchmodat.c: New file.
+	* io/Makefile (tests): Add it.
+
 2006-01-03  Steven Munroe  <sjmunroe@us.ibm.com>
 
 	* sysdeps/powerpc/powerpc32/sysdep.h (ENTRY, EALIGN): Add cfi_startproc
diff --git a/io/Makefile b/io/Makefile
index 4731e24d24..b263a48705 100644
--- a/io/Makefile
+++ b/io/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1992-2002, 2003, 2005 Free Software Foundation, Inc.
+# Copyright (C) 1992-2002,2003,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
@@ -35,7 +35,8 @@ routines :=								\
 	fxstatat fxstatat64						\
 	statfs fstatfs statfs64 fstatfs64				\
 	statvfs fstatvfs statvfs64 fstatvfs64				\
-	umask chmod fchmod lchmod mkdir mkdirat				\
+	umask chmod fchmod lchmod fchmodat \
+	mkdir mkdirat							\
 	open open64 openat openat64 close				\
 	read write lseek lseek64 access euidaccess			\
 	fcntl flock lockf lockf64					\
@@ -63,7 +64,7 @@ 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-renameat tst-fchownat tst-fchmodat
 
 distribute	:= ftwtest-sh
 
diff --git a/io/Versions b/io/Versions
index 2f0f94f3bb..8cb1abdb42 100644
--- a/io/Versions
+++ b/io/Versions
@@ -98,6 +98,7 @@ libc {
     nftw; nftw64;
   }
   GLIBC_2.4 {
+    fchmodat;
     fchownat;
     __fxstatat; __fxstatat64;
     linkat;
diff --git a/io/fchmodat.c b/io/fchmodat.c
new file mode 100644
index 0000000000..6aecf2ac23
--- /dev/null
+++ b/io/fchmodat.c
@@ -0,0 +1,50 @@
+/* Change the protections of file relative to open directory.  Stub version.
+   Copyright (C) 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
+   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 <fcntl.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+int
+fchmodat (fd, file, mode, flag)
+     int fd;
+     const char *file;
+     mode_t mode;
+     int flag;
+{
+  if (file == NULL || (flag & ~AT_SYMLINK_NOFOLLOW) != 0)
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+
+  if (fd < 0 && fd != AT_FDCWD)
+    {
+      __set_errno (EBADF);
+      return -1;
+    }
+
+  __set_errno (ENOSYS);
+  return -1;
+}
+stub_warning (fchownat)
+
+#include <stub-tag.h>
diff --git a/io/sys/stat.h b/io/sys/stat.h
index 6ce3a1be66..93cd7d0610 100644
--- a/io/sys/stat.h
+++ b/io/sys/stat.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991,1992,1995-2004,2005 Free Software Foundation, Inc.
+/* Copyright (C) 1991,1992,1995-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
@@ -293,6 +293,14 @@ extern int lchmod (__const char *__file, __mode_t __mode)
 extern int fchmod (int __fd, __mode_t __mode) __THROW;
 #endif
 
+#ifdef __USE_GNU
+/* Set file access permissions of FILE relative to
+   the directory FD is open on.  */
+extern int fchmodat (int __fd, __const char *__file, __mode_t mode, int __flag)
+     __THROW __nonnull ((2)) __wur;
+#endif /* Use GNU.  */
+
+
 
 /* Set the file creation mask of the current process to MASK,
    and return the old creation mask.  */
diff --git a/io/tst-fchmodat.c b/io/tst-fchmodat.c
new file mode 100644
index 0000000000..0288d6b5d9
--- /dev/null
+++ b/io/tst-fchmodat.c
@@ -0,0 +1,146 @@
+/* Test for fchmodat function.  */
+
+#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-fchmodat.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 save 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);
+
+  umask (022);
+
+  /* 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 ((st1.st_mode & 0777) != 0644)
+    {
+      printf ("openat created mode %04o, not 0644\n", (st1.st_mode & 0777));
+      return 1;
+    }
+
+  if (fchmodat (dir_fd, "some-file", 0400, 0) != 0)
+    {
+      puts ("fchownat failed");
+      return 1;
+    }
+
+  struct stat64 st2;
+  if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0)
+    {
+      puts ("fstatat64 failed");
+      return 1;
+    }
+
+  if ((st2.st_mode & 0777) != 0400)
+    {
+      puts ("mode change failed");
+      return 1;
+    }
+
+  if (unlinkat (dir_fd, "some-file", 0) != 0)
+    {
+      puts ("unlinkat failed");
+      return 1;
+    }
+
+  close (dir_fd);
+
+  return 0;
+}
diff --git a/sysdeps/unix/sysv/linux/fchmodat.c b/sysdeps/unix/sysv/linux/fchmodat.c
new file mode 100644
index 0000000000..de35e4376f
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/fchmodat.c
@@ -0,0 +1,87 @@
+/* Change the protections of file relative to open directory.  Linux version.
+   Copyright (C) 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
+   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 <fcntl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <alloca.h>
+#include <sysdep.h>
+
+int
+fchmodat (fd, file, mode, flag)
+     int fd;
+     const char *file;
+     mode_t mode;
+     int flag;
+{
+  if (flag & ~AT_SYMLINK_NOFOLLOW)
+    {
+      __set_errno (EINVAL);
+      return -1;
+    }
+#ifndef __NR_lchmod		/* Linux so far has no lchmod syscall.  */
+  if (flag & AT_SYMLINK_NOFOLLOW)
+    {
+      __set_errno (ENOTSUP);
+      return -1;
+    }
+#endif
+
+  char *buf = NULL;
+
+  if (fd != AT_FDCWD && file[0] != '/')
+    {
+      size_t filelen = strlen (file);
+      static const char procfd[] = "/proc/self/fd/%d/%s";
+      /* Buffer for the path name we are going to use.  It consists of
+	 - the string /proc/self/fd/
+	 - the file descriptor number
+	 - the file name provided.
+	 The final NUL is included in the sizeof.   A bit of overhead
+	 due to the format elements compensates for possible negative
+	 numbers.  */
+      size_t buflen = sizeof (procfd) + sizeof (int) * 3 + filelen;
+      buf = alloca (buflen);
+
+      __snprintf (buf, buflen, procfd, fd, file);
+      file = buf;
+    }
+
+  int result;
+  INTERNAL_SYSCALL_DECL (err);
+
+#ifdef __NR_lchmod
+  if (flag & AT_SYMLINK_NOFOLLOW)
+    result = INTERNAL_SYSCALL (lchmod, err, 2, file, mode);
+  else
+#endif
+    result = INTERNAL_SYSCALL (chmod, err, 2, file, mode);
+
+  if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (result, err), 0))
+    {
+      __atfct_seterrno (INTERNAL_SYSCALL_ERRNO (result, err), fd, buf);
+      result = -1;
+    }
+
+  return result;
+}