[BZ #3306]
[glibc.git] / resource / tst-getrlimit.c
blob67480340f0a3f0cda9904972a17dc865328a2102
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <sys/resource.h>
7 static struct
9 const char *name;
10 int resource;
11 bool required;
12 } tests[] =
14 /* The following 7 limits are part of POSIX and must exist. */
15 { "RLIMIT_CORE", RLIMIT_CORE, true },
16 { "RLIMIT_CPU", RLIMIT_CPU, true },
17 { "RLIMIT_DATA", RLIMIT_DATA, true },
18 { "RLIMIT_FSIZE", RLIMIT_FSIZE, true },
19 { "RLIMIT_NOFILE", RLIMIT_NOFILE, true },
20 { "RLIMIT_STACK", RLIMIT_STACK, true },
21 { "RLIMIT_AS", RLIMIT_AS, true },
22 /* The following are traditional Unix limits which are also
23 expected (by us). */
24 { "RLIMIT_RSS", RLIMIT_RSS, true },
25 { "RLIMIT_NPROC", RLIMIT_NPROC, true },
26 /* The following are extensions. */
27 #ifdef RLIMIT_MEMLOCK
28 { "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK, false },
29 #endif
30 #ifdef RLIMIT_LOCKS
31 { "RLIMIT_LOCKS", RLIMIT_LOCKS, false },
32 #endif
33 #ifdef RLIMIT_SIGPENDING
34 { "RLIMIT_SIGPENDING", RLIMIT_SIGPENDING, false },
35 #endif
36 #ifdef RLIMIT_MSGQUEUE
37 { "RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE, false },
38 #endif
39 #ifdef RLIMIT_NICE
40 { "RLIMIT_NICE", RLIMIT_NICE, false },
41 #endif
42 #ifdef RLIMIT_RTPRIO
43 { "RLIMIT_RTPRIO", RLIMIT_RTPRIO, false },
44 #endif
46 #define ntests (sizeof (tests) / sizeof (tests[0]))
49 static int
50 do_test (void)
52 int status = 0;
54 for (int i = 0; i < ntests; ++i)
56 bool this_ok = true;
58 struct rlimit r;
59 int res = getrlimit (tests[i].resource, &r);
60 if (res == -1)
62 if (errno == EINVAL)
64 if (tests[i].required)
66 printf ("limit %s expectedly not available for getrlimit\n",
67 tests[i].name);
68 status = 1;
69 this_ok = false;
72 else
74 printf ("getrlimit for %s returned unexpected error: %m\n",
75 tests[i].name);
76 status = 1;
77 this_ok = false;
81 struct rlimit64 r64;
82 res = getrlimit64 (tests[i].resource, &r64);
83 if (res == -1)
85 if (errno == EINVAL)
87 if (tests[i].required)
89 printf ("limit %s expectedly not available for getrlimit64"
90 "\n", tests[i].name);
91 status = 1;
92 this_ok = false;
95 else
97 printf ("getrlimit64 for %s returned unexpected error: %m\n",
98 tests[i].name);
99 status = 1;
100 this_ok = false;
104 if (this_ok)
105 printf ("limit %s OK\n", tests[i].name);
108 return status;
111 #define TEST_FUNCTION do_test ()
112 #include "../test-skeleton.c"