Blame sysdeps/unix/sysv/linux/tst-memfd_create.c

Packit 6c4009
/* Test for the memfd_create system call.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
Packit 6c4009
/* Return true if the descriptor has the FD_CLOEXEC flag set.  */
Packit 6c4009
static bool
Packit 6c4009
is_cloexec (int fd)
Packit 6c4009
{
Packit 6c4009
  int flags = fcntl (fd, F_GETFD);
Packit 6c4009
  TEST_VERIFY (flags >= 0);
Packit 6c4009
  return flags & FD_CLOEXEC;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return the seals set on FD.  */
Packit 6c4009
static int
Packit 6c4009
get_seals (int fd)
Packit 6c4009
{
Packit 6c4009
  int flags = fcntl (fd, F_GET_SEALS);
Packit 6c4009
  TEST_VERIFY (flags >= 0);
Packit 6c4009
  return flags;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return true if the F_SEAL_SEAL flag is set on the descriptor.  */
Packit 6c4009
static bool
Packit 6c4009
is_sealed (int fd)
Packit 6c4009
{
Packit 6c4009
  return get_seals (fd) & F_SEAL_SEAL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Initialized by the first call to memfd_create to 0 (memfd_create
Packit 6c4009
     unsupported) or 1 (memfd_create is implemented in the kernel).
Packit 6c4009
     Subsequent iterations check that the success/failure state is
Packit 6c4009
     consistent.  */
Packit 6c4009
  int supported = -1;
Packit 6c4009
Packit 6c4009
  for (int do_cloexec = 0; do_cloexec < 2; ++do_cloexec)
Packit 6c4009
    for (int do_sealing = 0; do_sealing < 2; ++do_sealing)
Packit 6c4009
      {
Packit 6c4009
        int flags = 0;
Packit 6c4009
        if (do_cloexec)
Packit 6c4009
          flags |= MFD_CLOEXEC;
Packit 6c4009
        if (do_sealing)
Packit 6c4009
          flags |= MFD_ALLOW_SEALING;
Packit 6c4009
        if  (test_verbose > 0)
Packit 6c4009
          printf ("info: memfd_create with flags=0x%x\n", flags);
Packit 6c4009
        int fd = memfd_create ("tst-memfd_create", flags);
Packit 6c4009
        if (fd < 0)
Packit 6c4009
          {
Packit 6c4009
            if (errno == ENOSYS)
Packit 6c4009
              {
Packit 6c4009
                if (supported < 0)
Packit 6c4009
                  {
Packit 6c4009
                    printf ("warning: memfd_create is unsupported\n");
Packit 6c4009
                    supported = 0;
Packit 6c4009
                    continue;
Packit 6c4009
                  }
Packit 6c4009
                TEST_VERIFY (supported == 0);
Packit 6c4009
                continue;
Packit 6c4009
              }
Packit 6c4009
            else
Packit 6c4009
              FAIL_EXIT1 ("memfd_create: %m");
Packit 6c4009
          }
Packit 6c4009
        if (supported < 0)
Packit 6c4009
          supported = 1;
Packit 6c4009
        TEST_VERIFY (supported > 0);
Packit 6c4009
Packit 6c4009
        char *fd_path = xasprintf ("/proc/self/fd/%d", fd);
Packit 6c4009
        char *link = xreadlink (fd_path);
Packit 6c4009
        if (test_verbose > 0)
Packit 6c4009
          printf ("info: memfd link: %s\n", link);
Packit 6c4009
        TEST_VERIFY (strcmp (link, "memfd:tst-memfd_create (deleted)"));
Packit 6c4009
        TEST_VERIFY (is_cloexec (fd) == do_cloexec);
Packit 6c4009
        TEST_VERIFY (is_sealed (fd) == !do_sealing);
Packit 6c4009
        if (do_sealing)
Packit 6c4009
          {
Packit 6c4009
            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_WRITE) == 0);
Packit 6c4009
            TEST_VERIFY (!is_sealed (fd));
Packit 6c4009
            TEST_VERIFY (get_seals (fd) & F_SEAL_WRITE);
Packit 6c4009
            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_SEAL) == 0);
Packit 6c4009
            TEST_VERIFY (is_sealed (fd));
Packit 6c4009
          }
Packit 6c4009
        xclose (fd);
Packit 6c4009
        free (fd_path);
Packit 6c4009
        free (link);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (supported == 0)
Packit 6c4009
    return EXIT_UNSUPPORTED;
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>