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

Packit 6c4009
/* Test the mlock2 function.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
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 <stdio.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
Packit 6c4009
/* Allocate a page using mmap.  */
Packit 6c4009
static void *
Packit 6c4009
get_page (void)
Packit 6c4009
{
Packit 6c4009
  return xmmap (NULL, 1, PROT_READ | PROT_WRITE,
Packit 6c4009
                MAP_ANONYMOUS | MAP_PRIVATE, -1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Current kernels have a small reserve of locked memory, so this
Packit 6c4009
     test does not need any privileges to run.  */
Packit 6c4009
Packit 6c4009
  void *page = get_page ();
Packit 6c4009
  if (mlock (page, 1) != 0)
Packit 6c4009
    FAIL_EXIT1 ("mlock: %m\n");
Packit 6c4009
  xmunmap (page, 1);
Packit 6c4009
Packit 6c4009
  page = get_page ();
Packit 6c4009
  if (mlock2 (page, 1, 0) != 0)
Packit 6c4009
    /* Should be implemented using mlock if necessary.  */
Packit 6c4009
    FAIL_EXIT1 ("mlock2 (0): %m\n");
Packit 6c4009
  xmunmap (page, 1);
Packit 6c4009
Packit 6c4009
  page = get_page ();
Packit 6c4009
  int ret = mlock2 (page, 1, MLOCK_ONFAULT);
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    {
Packit 6c4009
      TEST_VERIFY (ret == -1);
Packit 6c4009
      if (errno != EINVAL)
Packit 6c4009
        /* EINVAL means the system does not support the mlock2 system
Packit 6c4009
           call.  */
Packit 6c4009
        FAIL_EXIT1 ("mlock2 (0): %m\n");
Packit 6c4009
      else
Packit 6c4009
        puts ("warning: mlock2 system call not supported");
Packit 6c4009
    }
Packit 6c4009
  xmunmap (page, 1);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>