Blame resource/tst-getrlimit.c

Packit 6c4009
/* Copyright (C) 2005-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 <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <sys/resource.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static struct
Packit 6c4009
{
Packit 6c4009
  const char *name;
Packit 6c4009
  int resource;
Packit 6c4009
  bool required;
Packit 6c4009
} tests[] =
Packit 6c4009
  {
Packit 6c4009
    /* The following 7 limits are part of POSIX and must exist.  */
Packit 6c4009
    { "RLIMIT_CORE", RLIMIT_CORE, true },
Packit 6c4009
    { "RLIMIT_CPU", RLIMIT_CPU, true },
Packit 6c4009
    { "RLIMIT_DATA", RLIMIT_DATA, true },
Packit 6c4009
    { "RLIMIT_FSIZE", RLIMIT_FSIZE, true },
Packit 6c4009
    { "RLIMIT_NOFILE", RLIMIT_NOFILE, true },
Packit 6c4009
    { "RLIMIT_STACK", RLIMIT_STACK, true },
Packit 6c4009
    { "RLIMIT_AS", RLIMIT_AS, true },
Packit 6c4009
    /* The following are traditional Unix limits which are also
Packit 6c4009
       expected (by us).  */
Packit 6c4009
    { "RLIMIT_RSS", RLIMIT_RSS, true },
Packit 6c4009
    { "RLIMIT_NPROC", RLIMIT_NPROC, true },
Packit 6c4009
    /* The following are extensions.  */
Packit 6c4009
#ifdef RLIMIT_MEMLOCK
Packit 6c4009
    { "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK, false },
Packit 6c4009
#endif
Packit 6c4009
#ifdef RLIMIT_LOCKS
Packit 6c4009
    { "RLIMIT_LOCKS", RLIMIT_LOCKS, false },
Packit 6c4009
#endif
Packit 6c4009
#ifdef RLIMIT_SIGPENDING
Packit 6c4009
    { "RLIMIT_SIGPENDING", RLIMIT_SIGPENDING, false },
Packit 6c4009
#endif
Packit 6c4009
#ifdef RLIMIT_MSGQUEUE
Packit 6c4009
    { "RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE, false },
Packit 6c4009
#endif
Packit 6c4009
#ifdef RLIMIT_NICE
Packit 6c4009
    { "RLIMIT_NICE", RLIMIT_NICE, false },
Packit 6c4009
#endif
Packit 6c4009
#ifdef RLIMIT_RTPRIO
Packit 6c4009
    { "RLIMIT_RTPRIO", RLIMIT_RTPRIO, false },
Packit 6c4009
#endif
Packit 6c4009
  };
Packit 6c4009
#define ntests (sizeof (tests) / sizeof (tests[0]))
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int status = 0;
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < ntests; ++i)
Packit 6c4009
    {
Packit 6c4009
      bool this_ok = true;
Packit 6c4009
Packit 6c4009
      struct rlimit r;
Packit 6c4009
      int res = getrlimit (tests[i].resource, &r);
Packit 6c4009
      if (res == -1)
Packit 6c4009
	{
Packit 6c4009
	  if (errno == EINVAL)
Packit 6c4009
	    {
Packit 6c4009
	      if (tests[i].required)
Packit 6c4009
		{
Packit 6c4009
		  printf ("limit %s expectedly not available for getrlimit\n",
Packit 6c4009
			  tests[i].name);
Packit 6c4009
		  status = 1;
Packit 6c4009
		  this_ok = false;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      printf ("getrlimit for %s returned unexpected error: %m\n",
Packit 6c4009
		      tests[i].name);
Packit 6c4009
	      status = 1;
Packit 6c4009
	      this_ok = false;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      struct rlimit64 r64;
Packit 6c4009
      res = getrlimit64 (tests[i].resource, &r64);
Packit 6c4009
      if (res == -1)
Packit 6c4009
	{
Packit 6c4009
	  if (errno == EINVAL)
Packit 6c4009
	    {
Packit 6c4009
	      if (tests[i].required)
Packit 6c4009
		{
Packit 6c4009
		  printf ("limit %s expectedly not available for getrlimit64"
Packit 6c4009
			  "\n", tests[i].name);
Packit 6c4009
		  status = 1;
Packit 6c4009
		  this_ok = false;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      printf ("getrlimit64 for %s returned unexpected error: %m\n",
Packit 6c4009
		      tests[i].name);
Packit 6c4009
	      status = 1;
Packit 6c4009
	      this_ok = false;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (this_ok)
Packit 6c4009
	printf ("limit %s OK\n", tests[i].name);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"