robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 1 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 2 | * Copyright (C) Bull S.A. 2005. $ |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /************************************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : mlockall03 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 21 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 23 | * TEST TITLE : Test for checking basic error conditions for |
| 24 | * mlockall(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 26 | * TEST CASE TOTAL : 3 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * |
| 28 | * AUTHOR : Jacky Malcles |
| 29 | * |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 30 | * SIGNALS |
| 31 | * Uses SIGUSR1 to pause before test if option set. |
| 32 | * (See the parse_opts(3) man page). |
| 33 | * |
| 34 | * DESCRIPTION |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 35 | *$ |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 36 | * Verify that mlockall(2) returns -1 and sets errno to |
| 37 | * |
| 38 | * 1) ENOMEM - If the caller had a non-zero RLIMIT_MEMLOCK |
| 39 | * and tried to lock more memory than the limit permitted. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 40 | * 2) EPERM - If the caller was not privileged |
| 41 | * and its RLIMIT_MEMLOCK soft resource limit was 0. |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 42 | * 3) EINVAL - Unknown flags were specified. |
| 43 | * |
| 44 | * Setup: |
| 45 | * Setup signal handling. |
| 46 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 47 | * |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 48 | * Test: |
| 49 | * Loop if the proper options are given. |
| 50 | * Do necessary setup for each test. |
| 51 | * Execute system call |
| 52 | * Check return code, if system call failed and errno == expected errno |
| 53 | * Issue sys call passed with expected return value and errno. |
| 54 | * Otherwise, |
| 55 | * Issue sys call failed to produce expected error. |
| 56 | * Do cleanup for each test. |
| 57 | * |
| 58 | * Cleanup: |
| 59 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 60 | * |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 61 | * USAGE: <for command-line> |
| 62 | * mlockall03 [-c n] [-e] [-i n] [-I x] [-p x] [-t] |
| 63 | * where, |
| 64 | * -c n : Run n copies concurrently |
| 65 | * -e : Turn on errno logging. |
| 66 | * -h : Show this help screen |
| 67 | * -i n : Execute test n times. |
| 68 | * -I x : Execute test for x seconds. |
| 69 | * -p : Pause for SIGUSR1 before starting |
| 70 | * -P x : Pause for x seconds between iterations. |
| 71 | * -t : Turn on syscall timing. |
| 72 | * |
| 73 | * RESTRICTIONS |
| 74 | * Test must run as root. |
| 75 | *****************************************************************************/ |
| 76 | #include <errno.h> |
| 77 | #include <unistd.h> |
| 78 | #include <pwd.h> |
| 79 | #include <ctype.h> |
| 80 | #include <sys/mman.h> |
| 81 | #include "test.h" |
| 82 | #include "usctest.h" |
| 83 | #include <sys/resource.h> |
| 84 | #include <sys/utsname.h> |
| 85 | |
| 86 | void setup(); |
| 87 | int setup_test(int); |
| 88 | int compare(char s1[], char s2[]); |
| 89 | void cleanup_test(int); |
| 90 | void cleanup(); |
| 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | char *TCID = "mlockall03"; /* Test program identifier. */ |
| 93 | int TST_TOTAL = 3; /* Total number of test cases. */ |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 94 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 95 | #if !defined(UCLINUX) |
| 96 | |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 97 | char *ref_release = "2.6.8\0"; |
| 98 | int exp_enos[] = { ENOMEM, EPERM, EINVAL, 0 }; |
| 99 | |
| 100 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | int flag; /* flag value */ |
| 102 | int error; /* error description */ |
| 103 | char *edesc; /* Expected error no */ |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 104 | } TC[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | { |
| 106 | MCL_CURRENT, ENOMEM, |
| 107 | "tried to lock more memory than the limit permitted"}, { |
| 108 | MCL_CURRENT, EPERM, "Not a superuser and RLIMIT_MEMLOCK was 0"}, { |
| 109 | ~(MCL_CURRENT | MCL_FUTURE), EINVAL, "Unknown flag"} |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | int main(int ac, char **av) |
| 113 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 114 | int lc, i; |
| 115 | char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | struct utsname *buf; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 117 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 118 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 119 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 120 | tst_exit(); |
| 121 | } |
| 122 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | /* allocate some space for buf */ |
| 124 | if ((buf = (struct utsname *)malloc((size_t) |
| 125 | sizeof(struct utsname))) == NULL) { |
| 126 | tst_resm(TFAIL, "malloc failed for buf"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 127 | tst_exit(); |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 128 | } |
| 129 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | if (uname(buf) < 0) { |
| 131 | tst_resm(TFAIL, "uname failed getting release number"); |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 132 | } |
| 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | if ((compare(ref_release, buf->release)) <= 0) { |
| 135 | tst_resm(TCONF, |
| 136 | "In Linux 2.6.8 and earlier this test will not run."); |
| 137 | tst_exit(); |
| 138 | } |
| 139 | |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 140 | setup(); |
| 141 | |
| 142 | /* check looping state */ |
| 143 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 144 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 145 | tst_count = 0; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 146 | |
| 147 | for (i = 0; i < TST_TOTAL; i++) { |
| 148 | |
| 149 | if (setup_test(i)) { |
| 150 | tst_resm(TFAIL, "mlockall() Failed while setup " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | "for checking error %s", TC[i].edesc); |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 152 | continue; |
| 153 | } |
| 154 | |
| 155 | TEST(mlockall(TC[i].flag)); |
| 156 | |
| 157 | /* check return code */ |
| 158 | if (TEST_RETURN == -1) { |
| 159 | TEST_ERROR_LOG(TEST_ERRNO); |
| 160 | if (TEST_ERRNO != TC[i].error) |
| 161 | tst_brkm(TFAIL, cleanup, |
| 162 | "mlockall() Failed with wrong " |
| 163 | "errno, expected errno=%s, " |
| 164 | "got errno=%d : %s", |
| 165 | TC[i].edesc, TEST_ERRNO, |
| 166 | strerror(TEST_ERRNO)); |
| 167 | else |
| 168 | tst_resm(TPASS, |
| 169 | "expected failure - errno " |
| 170 | "= %d : %s", |
| 171 | TEST_ERRNO, |
| 172 | strerror(TEST_ERRNO)); |
| 173 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | tst_brkm(TFAIL, cleanup, |
| 175 | "mlockall() Failed, expected " |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 176 | "return value=-1, got %ld", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | TEST_RETURN); |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 178 | } |
| 179 | cleanup_test(i); |
| 180 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 181 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 182 | |
| 183 | /* cleanup and exit */ |
| 184 | cleanup(); |
| 185 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 186 | tst_exit(); |
| 187 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * setup() - performs all ONE TIME setup for this test. |
| 191 | */ |
| 192 | void setup() |
| 193 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 194 | |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 195 | tst_sig(FORK, DEF_HANDLER, cleanup); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 196 | |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 197 | /* set the expected errnos... */ |
| 198 | TEST_EXP_ENOS(exp_enos); |
| 199 | |
| 200 | TEST_PAUSE; |
| 201 | |
| 202 | return; |
| 203 | } |
| 204 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 205 | int compare(char s1[], char s2[]) |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 206 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | int i = 0; |
| 208 | while (s1[i] == s2[i] && s1[i]) |
| 209 | i++; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 210 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 211 | if (i < 4) |
| 212 | return s2[i] - s1[i]; |
| 213 | if ((i == 4) && (isalnum(s2[i + 1]))) { |
| 214 | return 1; |
| 215 | } else { |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 216 | /* it is not an alphanumeric character */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | return s2[i] - s1[i]; |
| 218 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | int setup_test(int i) |
| 223 | { |
| 224 | struct rlimit rl; |
| 225 | char nobody_uid[] = "nobody"; |
| 226 | struct passwd *ltpuser; |
| 227 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 228 | switch (i) { |
| 229 | case 0: |
| 230 | ltpuser = getpwnam(nobody_uid); |
| 231 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 232 | tst_brkm(TBROK, cleanup, "seteuid() failed to " |
| 233 | "change euid to %d errno = %d : %s", |
| 234 | ltpuser->pw_uid, TEST_ERRNO, |
| 235 | strerror(TEST_ERRNO)); |
| 236 | return 1; |
| 237 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 238 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | rl.rlim_max = 10; |
| 240 | rl.rlim_cur = 7; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 241 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 242 | if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) { |
| 243 | tst_resm(TWARN, "setrlimit failed to set the " |
| 244 | "resource for RLIMIT_MEMLOCK to check " |
| 245 | "for mlockall() error %s\n", TC[i].edesc); |
| 246 | return 1; |
| 247 | } |
| 248 | return 0; |
| 249 | case 1: |
| 250 | rl.rlim_max = 0; |
| 251 | rl.rlim_cur = 0; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 252 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 253 | if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) { |
| 254 | tst_resm(TWARN, "setrlimit failed to set the " |
| 255 | "resource for RLIMIT_MEMLOCK to check " |
| 256 | "for mlockall() error %s\n", TC[i].edesc); |
| 257 | return 1; |
| 258 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 259 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 260 | ltpuser = getpwnam(nobody_uid); |
| 261 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 262 | tst_brkm(TBROK, cleanup, "seteuid() failed to " |
| 263 | "change euid to %d errno = %d : %s", |
| 264 | ltpuser->pw_uid, TEST_ERRNO, |
| 265 | strerror(TEST_ERRNO)); |
| 266 | return 1; |
| 267 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 268 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 269 | return 0; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 270 | } |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | void cleanup_test(int i) |
| 275 | { |
| 276 | struct rlimit rl; |
| 277 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 278 | switch (i) { |
| 279 | case 0: |
| 280 | if (seteuid(0) == -1) { |
| 281 | tst_brkm(TBROK, cleanup, "seteuid() failed to " |
| 282 | "change euid to %d errno = %d : %s", |
| 283 | 0, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 284 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 285 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 286 | rl.rlim_max = -1; |
| 287 | rl.rlim_cur = -1; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 288 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 289 | if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) { |
| 290 | tst_brkm(TFAIL, cleanup, |
| 291 | "setrlimit failed to reset the " |
| 292 | "resource for RLIMIT_MEMLOCK while " |
| 293 | "checking for mlockall() error %s\n", |
| 294 | TC[i].edesc); |
| 295 | } |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 296 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 297 | return; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 298 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 299 | case 1: |
| 300 | if (seteuid(0) == -1) { |
| 301 | tst_brkm(TBROK, cleanup, "seteuid() failed to " |
| 302 | "change euid to %d errno = %d : %s", |
| 303 | 0, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 304 | } |
| 305 | return; |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 306 | |
| 307 | } |
| 308 | } |
| 309 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 310 | #else |
| 311 | |
| 312 | int main() |
| 313 | { |
vapier | 81a6307 | 2006-02-27 04:29:21 +0000 | [diff] [blame] | 314 | tst_resm(TINFO, "test is not available on uClinux"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 315 | tst_exit(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 316 | } |
| 317 | |
robbiew | 960b6af | 2005-08-02 17:28:50 +0000 | [diff] [blame] | 318 | #endif /* if !defined(UCLINUX) */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 319 | |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 320 | /* |
| 321 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 322 | * completion or premature exit. |
| 323 | */ |
| 324 | void cleanup() |
| 325 | { |
| 326 | TEST_CLEANUP; |
| 327 | |
robbiew | 9a15e2c | 2005-05-26 19:15:25 +0000 | [diff] [blame] | 328 | return; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 329 | } |