malloc/Makefile: Split and sort tests
[glibc.git] / resource / tst-getrlimit.c
blob17bf9134647cba59bbcd199f79e396f943ffc870
1 /* Copyright (C) 2005-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <errno.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <sys/resource.h>
24 static struct
26 const char *name;
27 int resource;
28 bool required;
29 } tests[] =
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
63 #define ntests (sizeof (tests) / sizeof (tests[0]))
66 static int
67 do_test (void)
69 int status = 0;
71 for (int i = 0; i < ntests; ++i)
73 bool this_ok = true;
75 struct rlimit r;
76 int res = getrlimit (tests[i].resource, &r);
77 if (res == -1)
79 if (errno == EINVAL)
81 if (tests[i].required)
83 printf ("limit %s expectedly not available for getrlimit\n",
84 tests[i].name);
85 status = 1;
86 this_ok = false;
89 else
91 printf ("getrlimit for %s returned unexpected error: %m\n",
92 tests[i].name);
93 status = 1;
94 this_ok = false;
98 struct rlimit64 r64;
99 res = getrlimit64 (tests[i].resource, &r64);
100 if (res == -1)
102 if (errno == EINVAL)
104 if (tests[i].required)
106 printf ("limit %s expectedly not available for getrlimit64"
107 "\n", tests[i].name);
108 status = 1;
109 this_ok = false;
112 else
114 printf ("getrlimit64 for %s returned unexpected error: %m\n",
115 tests[i].name);
116 status = 1;
117 this_ok = false;
121 if (this_ok)
122 printf ("limit %s OK\n", tests[i].name);
125 return status;
128 #define TEST_FUNCTION do_test ()
129 #include "../test-skeleton.c"