(root)/
glibc-2.38/
resource/
tst-getrlimit.c
       1  /* Copyright (C) 2005-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <stdbool.h>
      20  #include <stdio.h>
      21  #include <sys/resource.h>
      22  
      23  
      24  static struct
      25  {
      26    const char *name;
      27    int resource;
      28    bool required;
      29  } tests[] =
      30    {
      31      /* The following 7 limits are part of POSIX and must exist.  */
      32      { "RLIMIT_CORE", RLIMIT_CORE, true },
      33      { "RLIMIT_CPU", RLIMIT_CPU, true },
      34      { "RLIMIT_DATA", RLIMIT_DATA, true },
      35      { "RLIMIT_FSIZE", RLIMIT_FSIZE, true },
      36      { "RLIMIT_NOFILE", RLIMIT_NOFILE, true },
      37      { "RLIMIT_STACK", RLIMIT_STACK, true },
      38      { "RLIMIT_AS", RLIMIT_AS, true },
      39      /* The following are traditional Unix limits which are also
      40         expected (by us).  */
      41      { "RLIMIT_RSS", RLIMIT_RSS, true },
      42      { "RLIMIT_NPROC", RLIMIT_NPROC, true },
      43      /* The following are extensions.  */
      44  #ifdef RLIMIT_MEMLOCK
      45      { "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK, false },
      46  #endif
      47  #ifdef RLIMIT_LOCKS
      48      { "RLIMIT_LOCKS", RLIMIT_LOCKS, false },
      49  #endif
      50  #ifdef RLIMIT_SIGPENDING
      51      { "RLIMIT_SIGPENDING", RLIMIT_SIGPENDING, false },
      52  #endif
      53  #ifdef RLIMIT_MSGQUEUE
      54      { "RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE, false },
      55  #endif
      56  #ifdef RLIMIT_NICE
      57      { "RLIMIT_NICE", RLIMIT_NICE, false },
      58  #endif
      59  #ifdef RLIMIT_RTPRIO
      60      { "RLIMIT_RTPRIO", RLIMIT_RTPRIO, false },
      61  #endif
      62    };
      63  #define ntests (sizeof (tests) / sizeof (tests[0]))
      64  
      65  
      66  static int
      67  do_test (void)
      68  {
      69    int status = 0;
      70  
      71    for (int i = 0; i < ntests; ++i)
      72      {
      73        bool this_ok = true;
      74  
      75        struct rlimit r;
      76        int res = getrlimit (tests[i].resource, &r);
      77        if (res == -1)
      78  	{
      79  	  if (errno == EINVAL)
      80  	    {
      81  	      if (tests[i].required)
      82  		{
      83  		  printf ("limit %s expectedly not available for getrlimit\n",
      84  			  tests[i].name);
      85  		  status = 1;
      86  		  this_ok = false;
      87  		}
      88  	    }
      89  	  else
      90  	    {
      91  	      printf ("getrlimit for %s returned unexpected error: %m\n",
      92  		      tests[i].name);
      93  	      status = 1;
      94  	      this_ok = false;
      95  	    }
      96  	}
      97  
      98        struct rlimit64 r64;
      99        res = getrlimit64 (tests[i].resource, &r64);
     100        if (res == -1)
     101  	{
     102  	  if (errno == EINVAL)
     103  	    {
     104  	      if (tests[i].required)
     105  		{
     106  		  printf ("limit %s expectedly not available for getrlimit64"
     107  			  "\n", tests[i].name);
     108  		  status = 1;
     109  		  this_ok = false;
     110  		}
     111  	    }
     112  	  else
     113  	    {
     114  	      printf ("getrlimit64 for %s returned unexpected error: %m\n",
     115  		      tests[i].name);
     116  	      status = 1;
     117  	      this_ok = false;
     118  	    }
     119  	}
     120  
     121        if (this_ok)
     122  	printf ("limit %s OK\n", tests[i].name);
     123      }
     124  
     125    return status;
     126  }
     127  
     128  #define TEST_FUNCTION do_test ()
     129  #include "../test-skeleton.c"