Blame sysdeps/unix/sysv/linux/tst-rlimit-infinity.c

Packit Service 82fcde
/* Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <sys/resource.h>
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
Packit Service 82fcde
static int resources[] = {
Packit Service 82fcde
  /* The following 7 limits are part of POSIX and must exist.  */
Packit Service 82fcde
  RLIMIT_CORE,
Packit Service 82fcde
  RLIMIT_CPU,
Packit Service 82fcde
  RLIMIT_DATA,
Packit Service 82fcde
  RLIMIT_FSIZE,
Packit Service 82fcde
  RLIMIT_NOFILE,
Packit Service 82fcde
  RLIMIT_STACK,
Packit Service 82fcde
  RLIMIT_AS
Packit Service 82fcde
};
Packit Service 82fcde
Packit Service 82fcde
#define nresources (sizeof (resources) / sizeof (resources[0]))
Packit Service 82fcde
Packit Service 82fcde
/* Assume that the prlimit64 function calls the prlimit64 syscall without
Packit Service 82fcde
   mangling the arguments.  */
Packit Service 82fcde
#define PRLIMIT64_INFINITY	((rlim64_t) -1)
Packit Service 82fcde
Packit Service 82fcde
/* As we don't know which limit will be modified, use a sufficiently high
Packit Service 82fcde
   value to not shoot ourself in the foot.  Use a 32-bit value to test
Packit Service 82fcde
   both the 32- and 64-bit versions, and keep the highest bit clear to
Packit Service 82fcde
   avoid sign extension.  */
Packit Service 82fcde
#define PRLIMIT64_TESTVAL	((rlim64_t) 0x42420000)
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_getrlimit (int resource, rlim_t exp_cur, rlim_t exp_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit r;
Packit Service 82fcde
  TEST_VERIFY_EXIT (getrlimit (resource, &r) == 0);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_cur, exp_cur);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_max, exp_max);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_getrlimit64 (int resource, rlim64_t exp_cur, rlim64_t exp_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit64 r;
Packit Service 82fcde
  TEST_VERIFY_EXIT (getrlimit64 (resource, &r) == 0);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_cur, exp_cur);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_max, exp_max);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_prlimit_get (int resource, rlim_t exp_cur, rlim_t exp_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit r;
Packit Service 82fcde
  TEST_VERIFY_EXIT (prlimit (0, resource, NULL, &r) == 0);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_cur, exp_cur);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_max, exp_max);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_prlimit64_get (int resource, rlim64_t exp_cur, rlim64_t exp_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit64 r;
Packit Service 82fcde
  TEST_COMPARE (prlimit64 (0, resource, NULL, &r), 0);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_cur, exp_cur);
Packit Service 82fcde
  TEST_COMPARE (r.rlim_max, exp_max);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_setrlimit (int resource, rlim_t new_cur, rlim_t new_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit r = { new_cur, new_max };
Packit Service 82fcde
  TEST_COMPARE (setrlimit (resource, &r), 0);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_setrlimit64 (int resource, rlim64_t new_cur, rlim64_t new_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit64 r = { new_cur, new_max };
Packit Service 82fcde
  TEST_COMPARE (setrlimit64 (resource, &r), 0);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_prlimit_set (int resource, rlim_t new_cur, rlim_t new_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit r = { new_cur, new_max };
Packit Service 82fcde
  TEST_COMPARE (prlimit (0, resource, &r, NULL), 0);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
test_prlimit64_set (int resource, rlim64_t new_cur, rlim64_t new_max)
Packit Service 82fcde
{
Packit Service 82fcde
  struct rlimit64 r = { new_cur, new_max };
Packit Service 82fcde
  TEST_COMPARE (prlimit64 (0, resource, &r, NULL), 0);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  int resource = -1;
Packit Service 82fcde
Packit Service 82fcde
  /* Find a resource with hard limit set to infinity, so that the soft limit
Packit Service 82fcde
     can be manipulated to any value.  */
Packit Service 82fcde
  for (int i = 0; i < nresources; ++i)
Packit Service 82fcde
    {
Packit Service 82fcde
      struct rlimit64 r64;
Packit Service 82fcde
      int res = prlimit64 (0, resources[i], NULL, &r64);
Packit Service 82fcde
      if ((res == 0) && (r64.rlim_max == PRLIMIT64_INFINITY))
Packit Service 82fcde
	{
Packit Service 82fcde
	  resource = resources[i];
Packit Service 82fcde
	  break;
Packit Service 82fcde
	}
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  if (resource == -1)
Packit Service 82fcde
    FAIL_UNSUPPORTED
Packit Service 82fcde
      ("Could not find and limit with hard limit set to infinity.");
Packit Service 82fcde
Packit Service 82fcde
  /* First check that the get functions work correctly with the test value.  */
Packit Service 82fcde
  test_prlimit64_set (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
Packit Service 82fcde
  test_getrlimit (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
Packit Service 82fcde
  test_getrlimit64 (resource, PRLIMIT64_TESTVAL, RLIM64_INFINITY);
Packit Service 82fcde
  test_prlimit_get (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, PRLIMIT64_TESTVAL, RLIM64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  /* Then check that the get functions work correctly with infinity.  */
Packit Service 82fcde
  test_prlimit64_set (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
Packit Service 82fcde
  test_getrlimit (resource, RLIM_INFINITY, RLIM_INFINITY);
Packit Service 82fcde
  test_getrlimit64 (resource, RLIM64_INFINITY, RLIM64_INFINITY);
Packit Service 82fcde
  test_prlimit_get (resource, RLIM_INFINITY, RLIM_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, RLIM64_INFINITY, RLIM64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  /* Then check that setrlimit works correctly with the test value.  */
Packit Service 82fcde
  test_setrlimit (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  /* Then check that setrlimit works correctly with infinity.  */
Packit Service 82fcde
  test_setrlimit (resource, RLIM_INFINITY, RLIM_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  /* Then check that setrlimit64 works correctly with the test value.  */
Packit Service 82fcde
  test_setrlimit64 (resource, PRLIMIT64_TESTVAL, RLIM64_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  /* Then check that setrlimit64 works correctly with infinity.  */
Packit Service 82fcde
  test_setrlimit64 (resource, RLIM64_INFINITY, RLIM64_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  /* Then check that prlimit works correctly with the test value.  */
Packit Service 82fcde
  test_prlimit_set (resource, RLIM_INFINITY, RLIM_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  /* Finally check that prlimit works correctly with infinity.  */
Packit Service 82fcde
  test_prlimit_set (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
Packit Service 82fcde
  test_prlimit64_get (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
Packit Service 82fcde
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#include <support/test-driver.c>