ce426f
commit 85f7554cd97e7f03d8dc66278653045ef63a2221
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Wed Sep 21 15:24:01 2016 +0200
ce426f
ce426f
    Add test case for O_TMPFILE handling in open, openat
ce426f
    
ce426f
    Also put xasprintf into test-skeleton.c (written in such a way that
ce426f
    including <stdarg.h> is not needed).
ce426f
ce426f
commit 51364ff23e9760777bfea4eb9ac89c29a794074b
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Fri Sep 23 09:41:35 2016 +0200
ce426f
ce426f
    test-skeleton.c: Remove unintended #include <stdarg.h>.
ce426f
ce426f
Index: b/io/tst-open-tmpfile.c
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ b/io/tst-open-tmpfile.c
ce426f
@@ -0,0 +1,319 @@
ce426f
+/* Test open and openat with O_TMPFILE.
ce426f
+   Copyright (C) 2016 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+/* This test verifies that open and openat work as expected, i.e. they
ce426f
+   create a deleted file with the requested file mode.  */
ce426f
+
ce426f
+#include <errno.h>
ce426f
+#include <fcntl.h>
ce426f
+#include <stdbool.h>
ce426f
+#include <stdio.h>
ce426f
+#include <stdlib.h>
ce426f
+#include <string.h>
ce426f
+#include <sys/stat.h>
ce426f
+#include <unistd.h>
ce426f
+
ce426f
+static int do_test (void);
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#include "../test-skeleton.c"
ce426f
+
ce426f
+#ifdef O_TMPFILE
ce426f
+typedef int (*wrapper_func) (const char *, int, mode_t);
ce426f
+
ce426f
+/* Error-checking wrapper for the open function, compatible with the
ce426f
+   wrapper_func type.  */
ce426f
+static int
ce426f
+wrap_open (const char *path, int flags, mode_t mode)
ce426f
+{
ce426f
+  int ret = open (path, flags, mode);
ce426f
+  if (ret < 0)
ce426f
+    {
ce426f
+      printf ("error: open (\"%s\", 0x%x, 0%03o): %m\n", path, flags, mode);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  return ret;
ce426f
+}
ce426f
+
ce426f
+/* Error-checking wrapper for the openat function, compatible with the
ce426f
+   wrapper_func type.  */
ce426f
+static int
ce426f
+wrap_openat (const char *path, int flags, mode_t mode)
ce426f
+{
ce426f
+  int ret = openat (AT_FDCWD, path, flags, mode);
ce426f
+  if (ret < 0)
ce426f
+    {
ce426f
+      printf ("error: openat (\"%s\", 0x%x, 0%03o): %m\n", path, flags, mode);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  return ret;
ce426f
+}
ce426f
+
ce426f
+/* Error-checking wrapper for the open64 function, compatible with the
ce426f
+   wrapper_func type.  */
ce426f
+static int
ce426f
+wrap_open64 (const char *path, int flags, mode_t mode)
ce426f
+{
ce426f
+  int ret = open64 (path, flags, mode);
ce426f
+  if (ret < 0)
ce426f
+    {
ce426f
+      printf ("error: open64 (\"%s\", 0x%x, 0%03o): %m\n", path, flags, mode);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  return ret;
ce426f
+}
ce426f
+
ce426f
+/* Error-checking wrapper for the openat64 function, compatible with the
ce426f
+   wrapper_func type.  */
ce426f
+static int
ce426f
+wrap_openat64 (const char *path, int flags, mode_t mode)
ce426f
+{
ce426f
+  int ret = openat64 (AT_FDCWD, path, flags, mode);
ce426f
+  if (ret < 0)
ce426f
+    {
ce426f
+      printf ("error: openat64 (\"%s\", 0x%x, 0%03o): %m\n", path, flags, mode);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  return ret;
ce426f
+}
ce426f
+
ce426f
+/* Return true if FD is flagged as deleted in /proc/self/fd, false if
ce426f
+   not.  */
ce426f
+static bool
ce426f
+is_file_deteted (int fd)
ce426f
+{
ce426f
+  char *proc_fd_path = xasprintf ("/proc/self/fd/%d", fd);
ce426f
+  char file_path[4096];
ce426f
+  ssize_t file_path_length
ce426f
+    = readlink (proc_fd_path, file_path, sizeof (file_path));
ce426f
+  if (file_path_length < 0)
ce426f
+    {
ce426f
+      printf ("error: readlink (\"%s\"): %m", proc_fd_path);
ce426f
+      free (proc_fd_path);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  free (proc_fd_path);
ce426f
+  if (file_path_length == sizeof (file_path))
ce426f
+    {
ce426f
+      printf ("error: path in /proc resolves to overlong file name: %.*s\n",
ce426f
+              (int) file_path_length, file_path);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  const char *deleted = " (deleted)";
ce426f
+  if (file_path_length < strlen (deleted))
ce426f
+    {
ce426f
+      printf ("error: path in /proc is too short: %.*s\n",
ce426f
+              (int) file_path_length, file_path);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  return memcmp (file_path + file_path_length - strlen (deleted),
ce426f
+              deleted, strlen (deleted)) == 0;
ce426f
+}
ce426f
+
ce426f
+/* Obtain a file name which is difficult to guess.  */
ce426f
+static char *
ce426f
+get_random_name (void)
ce426f
+{
ce426f
+  unsigned long long bytes[2];
ce426f
+  int random_device = open ("/dev/urandom", O_RDONLY);
ce426f
+  if (random_device < 0)
ce426f
+    {
ce426f
+      printf ("error: open (\"/dev/urandom\"): %m\n");
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  ssize_t ret = read (random_device, bytes, sizeof (bytes));
ce426f
+  if (ret < 0)
ce426f
+    {
ce426f
+      printf ("error: read (\"/dev/urandom\"): %m\n");
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  if (ret != sizeof (bytes))
ce426f
+    {
ce426f
+      printf ("error: short read from /dev/urandom: %zd\n", ret);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  close (random_device);
ce426f
+  return xasprintf ("tst-open-tmpfile-%08llx%08llx.tmp", bytes[0], bytes[1]);
ce426f
+}
ce426f
+
ce426f
+/* Check open/openat (as specified by OP and WRAPPER) with a specific
ce426f
+   PATH/FLAGS/MODE combination.  */
ce426f
+static void
ce426f
+check_wrapper_flags_mode (const char *op, wrapper_func wrapper,
ce426f
+                          const char *path, int flags, mode_t mode)
ce426f
+{
ce426f
+  int fd = wrapper (path, flags | O_TMPFILE, mode);
ce426f
+  struct stat64 st;
ce426f
+  if (fstat64 (fd, &st) != 0)
ce426f
+    {
ce426f
+      printf ("error: fstat64: %m\n");
ce426f
+      exit (1);
ce426f
+    }
ce426f
+
ce426f
+  /* Verify that the mode was correctly processed.  */
ce426f
+  int actual_mode = st.st_mode & 0777;
ce426f
+  if (actual_mode != mode)
ce426f
+    {
ce426f
+      printf ("error: unexpected mode; expected 0%03o, actual 0%03o\n",
ce426f
+              mode, actual_mode);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+
ce426f
+  /* Check that the file is marked as deleted in /proc.  */
ce426f
+  if (!is_file_deteted (fd))
ce426f
+    {
ce426f
+      printf ("error: path in /proc is not marked as deleted\n");
ce426f
+      exit (1);
ce426f
+    }
ce426f
+
ce426f
+  /* Check that the file can be turned into a regular file with
ce426f
+     linkat.  Open a file descriptor for the directory at PATH.  Use
ce426f
+     AT_FDCWD if PATH is ".", to exercise that functionality as
ce426f
+     well.  */
ce426f
+  int path_fd;
ce426f
+  if (strcmp (path, ".") == 0)
ce426f
+    path_fd = AT_FDCWD;
ce426f
+  else
ce426f
+    {
ce426f
+      path_fd = open (path, O_RDONLY | O_DIRECTORY);
ce426f
+      if (path_fd < 0)
ce426f
+        {
ce426f
+          printf ("error: open (\"%s\"): %m\n", path);
ce426f
+          exit (1);
ce426f
+        }
ce426f
+    }
ce426f
+
ce426f
+  /* Use a hard-to-guess name for the new directory entry.  */
ce426f
+  char *new_name = get_random_name ();
ce426f
+
ce426f
+  /* linkat does not require privileges if the path in /proc/self/fd
ce426f
+     is used.  */
ce426f
+  char *proc_fd_path = xasprintf ("/proc/self/fd/%d", fd);
ce426f
+  if (linkat (AT_FDCWD, proc_fd_path, path_fd, new_name,
ce426f
+              AT_SYMLINK_FOLLOW) == 0)
ce426f
+    {
ce426f
+      if (unlinkat (path_fd, new_name, 0) != 0 && errno != ENOENT)
ce426f
+        {
ce426f
+          printf ("error: unlinkat (\"%s/%s\"): %m\n", path, new_name);
ce426f
+          exit (1);
ce426f
+        }
ce426f
+    }
ce426f
+  else
ce426f
+    {
ce426f
+      /* linkat failed.  This is expected if O_EXCL was specified.  */
ce426f
+      if ((flags & O_EXCL) == 0)
ce426f
+        {
ce426f
+          printf ("error: linkat failed after %s (\"%s\", 0x%x, 0%03o): %m\n",
ce426f
+                  op, path, flags, mode);
ce426f
+          exit (1);
ce426f
+        }
ce426f
+    }
ce426f
+
ce426f
+  free (proc_fd_path);
ce426f
+  free (new_name);
ce426f
+  if (path_fd != AT_FDCWD)
ce426f
+    close (path_fd);
ce426f
+  close (fd);
ce426f
+}
ce426f
+
ce426f
+/* Check OP/WRAPPER with various flags at a specific PATH and
ce426f
+   MODE.  */
ce426f
+static void
ce426f
+check_wrapper_mode (const char *op, wrapper_func wrapper,
ce426f
+                    const char *path, mode_t mode)
ce426f
+{
ce426f
+  check_wrapper_flags_mode (op, wrapper, path, O_WRONLY, mode);
ce426f
+  check_wrapper_flags_mode (op, wrapper, path, O_WRONLY | O_EXCL, mode);
ce426f
+  check_wrapper_flags_mode (op, wrapper, path, O_RDWR, mode);
ce426f
+  check_wrapper_flags_mode (op, wrapper, path, O_RDWR | O_EXCL, mode);
ce426f
+}
ce426f
+
ce426f
+/* Check open/openat with varying permissions.  */
ce426f
+static void
ce426f
+check_wrapper (const char *op, wrapper_func wrapper,
ce426f
+                    const char *path)
ce426f
+{
ce426f
+  printf ("info: testing %s at: %s\n", op, path);
ce426f
+  check_wrapper_mode (op, wrapper, path, 0);
ce426f
+  check_wrapper_mode (op, wrapper, path, 0640);
ce426f
+  check_wrapper_mode (op, wrapper, path, 0600);
ce426f
+  check_wrapper_mode (op, wrapper, path, 0755);
ce426f
+  check_wrapper_mode (op, wrapper, path, 0750);
ce426f
+}
ce426f
+
ce426f
+/* Verify that the directory at PATH supports O_TMPFILE.  Exit with
ce426f
+   status 77 (unsupported) if the kernel does not support O_TMPFILE.
ce426f
+   Even with kernel support, not all file systems O_TMPFILE, so return
ce426f
+   true if the directory supports O_TMPFILE, false if not.  */
ce426f
+static bool
ce426f
+probe_path (const char *path)
ce426f
+{
ce426f
+  int fd = openat (AT_FDCWD, path, O_TMPFILE | O_RDWR, 0);
ce426f
+  if (fd < 0)
ce426f
+    {
ce426f
+      if (errno == EISDIR)
ce426f
+        /* The system does not support O_TMPFILE.  */
ce426f
+        {
ce426f
+          printf ("info: kernel does not support O_TMPFILE\n");
ce426f
+          exit (77);
ce426f
+        }
ce426f
+      if (errno == EOPNOTSUPP)
ce426f
+        {
ce426f
+          printf ("info: path does not support O_TMPFILE: %s\n", path);
ce426f
+          return false;
ce426f
+        }
ce426f
+      printf ("error: openat (\"%s\", O_TMPFILE | O_RDWR): %m\n", path);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  close (fd);
ce426f
+  return true;
ce426f
+}
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  umask (0);
ce426f
+  const char *paths[] = { ".", "/dev/shm", "/tmp",
ce426f
+                          getenv ("TEST_TMPFILE_PATH"),
ce426f
+                          NULL };
ce426f
+  bool supported = false;
ce426f
+  for (int i = 0; paths[i] != NULL; ++i)
ce426f
+    if (probe_path (paths[i]))
ce426f
+      {
ce426f
+        supported = true;
ce426f
+        check_wrapper ("open", wrap_open, paths[i]);
ce426f
+        check_wrapper ("openat", wrap_openat, paths[i]);
ce426f
+        check_wrapper ("open64", wrap_open64, paths[i]);
ce426f
+        check_wrapper ("openat64", wrap_openat64, paths[i]);
ce426f
+      }
ce426f
+
ce426f
+  if (!supported)
ce426f
+    return 77;
ce426f
+
ce426f
+  return 0;
ce426f
+}
ce426f
+
ce426f
+#else  /* !O_TMPFILE */
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  return 77;
ce426f
+}
ce426f
+
ce426f
+#endif  /* O_TMPFILE */
ce426f
Index: b/test-skeleton.c
ce426f
===================================================================
ce426f
--- a/test-skeleton.c
ce426f
+++ b/test-skeleton.c
ce426f
@@ -32,6 +32,7 @@
ce426f
 #include <sys/wait.h>
ce426f
 #include <sys/param.h>
ce426f
 #include <time.h>
ce426f
+#include <stdarg.h>
ce426f
 
ce426f
 /* The test function is normally called `do_test' and it is called
ce426f
    with argc and argv as the arguments.  We nevertheless provide the
ce426f
@@ -63,6 +64,20 @@ static pid_t pid;
ce426f
 /* Directory to place temporary files in.  */
ce426f
 static const char *test_dir;
ce426f
 
ce426f
+/* Call asprintf with error checking.  */
ce426f
+__attribute__ ((always_inline, format (printf, 1, 2)))
ce426f
+static __inline__ char *
ce426f
+xasprintf (const char *format, ...)
ce426f
+{
ce426f
+  char *result;
ce426f
+  if (asprintf (&result, format, __builtin_va_arg_pack ()) < 0)
ce426f
+    {
ce426f
+      printf ("error: asprintf: %m\n");
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  return result;
ce426f
+}
ce426f
+
ce426f
 /* List of temporary files.  */
ce426f
 struct temp_name_list
ce426f
 {
ce426f
Index: b/io/Makefile
ce426f
===================================================================
ce426f
--- a/io/Makefile
ce426f
+++ b/io/Makefile
ce426f
@@ -69,7 +69,8 @@ tests		:= test-utime test-stat test-stat
ce426f
 		   tst-renameat tst-fchownat tst-fchmodat tst-faccessat \
ce426f
 		   tst-symlinkat tst-linkat tst-readlinkat tst-mkdirat \
ce426f
 		   tst-mknodat tst-mkfifoat tst-ttyname_r bug-ftw5 \
ce426f
-		   tst-posix_fallocate
ce426f
+		   tst-posix_fallocate \
ce426f
+		   tst-open-tmpfile
ce426f
 
ce426f
 include ../Rules
ce426f