Back to index

glibc  2.9
tst-getrlimit.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <stdbool.h>
00003 #include <stdio.h>
00004 #include <sys/resource.h>
00005 
00006 
00007 static struct
00008 {
00009   const char *name;
00010   int resource;
00011   bool required;
00012 } tests[] =
00013   {
00014     /* The following 7 limits are part of POSIX and must exist.  */
00015     { "RLIMIT_CORE", RLIMIT_CORE, true },
00016     { "RLIMIT_CPU", RLIMIT_CPU, true },
00017     { "RLIMIT_DATA", RLIMIT_DATA, true },
00018     { "RLIMIT_FSIZE", RLIMIT_FSIZE, true },
00019     { "RLIMIT_NOFILE", RLIMIT_NOFILE, true },
00020     { "RLIMIT_STACK", RLIMIT_STACK, true },
00021     { "RLIMIT_AS", RLIMIT_AS, true },
00022     /* The following are traditional Unix limits which are also
00023        expected (by us).  */
00024     { "RLIMIT_RSS", RLIMIT_RSS, true },
00025     { "RLIMIT_NPROC", RLIMIT_NPROC, true },
00026     /* The following are extensions.  */
00027 #ifdef RLIMIT_MEMLOCK
00028     { "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK, false },
00029 #endif
00030 #ifdef RLIMIT_LOCKS
00031     { "RLIMIT_LOCKS", RLIMIT_LOCKS, false },
00032 #endif
00033 #ifdef RLIMIT_SIGPENDING
00034     { "RLIMIT_SIGPENDING", RLIMIT_SIGPENDING, false },
00035 #endif
00036 #ifdef RLIMIT_MSGQUEUE
00037     { "RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE, false },
00038 #endif
00039 #ifdef RLIMIT_NICE
00040     { "RLIMIT_NICE", RLIMIT_NICE, false },
00041 #endif
00042 #ifdef RLIMIT_RTPRIO
00043     { "RLIMIT_RTPRIO", RLIMIT_RTPRIO, false },
00044 #endif
00045   };
00046 #define ntests (sizeof (tests) / sizeof (tests[0]))
00047 
00048 
00049 static int
00050 do_test (void)
00051 {
00052   int status = 0;
00053 
00054   for (int i = 0; i < ntests; ++i)
00055     {
00056       bool this_ok = true;
00057 
00058       struct rlimit r;
00059       int res = getrlimit (tests[i].resource, &r);
00060       if (res == -1)
00061        {
00062          if (errno == EINVAL)
00063            {
00064              if (tests[i].required)
00065               {
00066                 printf ("limit %s expectedly not available for getrlimit\n",
00067                        tests[i].name);
00068                 status = 1;
00069                 this_ok = false;
00070               }
00071            }
00072          else
00073            {
00074              printf ("getrlimit for %s returned unexpected error: %m\n",
00075                     tests[i].name);
00076              status = 1;
00077              this_ok = false;
00078            }
00079        }
00080 
00081       struct rlimit64 r64;
00082       res = getrlimit64 (tests[i].resource, &r64);
00083       if (res == -1)
00084        {
00085          if (errno == EINVAL)
00086            {
00087              if (tests[i].required)
00088               {
00089                 printf ("limit %s expectedly not available for getrlimit64"
00090                        "\n", tests[i].name);
00091                 status = 1;
00092                 this_ok = false;
00093               }
00094            }
00095          else
00096            {
00097              printf ("getrlimit64 for %s returned unexpected error: %m\n",
00098                     tests[i].name);
00099              status = 1;
00100              this_ok = false;
00101            }
00102        }
00103 
00104       if (this_ok)
00105        printf ("limit %s OK\n", tests[i].name);
00106     }
00107 
00108   return status;
00109 }
00110 
00111 #define TEST_FUNCTION do_test ()
00112 #include "../test-skeleton.c"