plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * mlock01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Test to see that mlock works |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 26 | *$ |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 27 | * ALGORITHM |
| 28 | * test 1: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * Call mlock with various valid addresses and lengths. No |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 30 | * error should be returned |
| 31 | * |
| 32 | * USAGE: <for command-line> |
| 33 | * -c n Run n copies concurrently |
| 34 | * -e Turn on errno logging |
| 35 | * -f Turn off functional testing |
| 36 | * -h Show this help screen |
| 37 | * -i n Execute test n times |
| 38 | * -I x Execute test for x seconds |
| 39 | * -p Pause for SIGUSR1 before starting |
| 40 | * -P x Pause for x seconds between iterations |
| 41 | * -t Turn on syscall timing |
| 42 | * |
| 43 | * HISTORY |
| 44 | * 06/2002 Written by Paul Larson |
| 45 | * |
| 46 | * RESTRICTIONS |
| 47 | * None |
| 48 | */ |
| 49 | #include <errno.h> |
| 50 | #include <unistd.h> |
| 51 | #include <sys/mman.h> |
| 52 | #include "test.h" |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 53 | |
| 54 | void setup(); |
| 55 | void setup1(int); |
| 56 | void cleanup(); |
| 57 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 58 | char *TCID = "mlock01"; |
| 59 | int TST_TOTAL = 4; |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 60 | |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 61 | void *addr1; |
| 62 | |
| 63 | struct test_case_t { |
| 64 | void **addr; |
| 65 | int len; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | void (*setupfunc) (); |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 67 | } TC[] = { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 68 | /* mlock should return ENOMEM when some or all of the address |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 69 | * range pointed to by addr and len are not valid mapped pages |
| 70 | * in the address space of the process |
| 71 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 72 | { |
| 73 | &addr1, 1, setup1}, { |
| 74 | &addr1, 1024, setup1}, { |
| 75 | &addr1, 1024 * 1024, setup1}, { |
| 76 | &addr1, 1024 * 1024 * 10, setup1} |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 79 | #if !defined(UCLINUX) |
| 80 | |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 81 | int main(int ac, char **av) |
| 82 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 83 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 84 | const char *msg; |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 85 | |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 86 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 87 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 88 | |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 89 | setup(); |
| 90 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 91 | /* |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 92 | * FIXME (garrcoop): this should really test out whether or not the |
| 93 | * process's mappable address space is indeed accessible by the |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 94 | * current user, instead of needing to be run by root all the time. |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 95 | */ |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 96 | tst_require_root(NULL); |
| 97 | |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 99 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 100 | tst_count = 0; |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 101 | |
| 102 | for (i = 0; i < TST_TOTAL; i++) { |
| 103 | |
| 104 | if (TC[i].setupfunc != NULL) |
| 105 | TC[i].setupfunc(TC[i].len); |
| 106 | |
| 107 | TEST(mlock(*(TC[i].addr), TC[i].len)); |
| 108 | |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 109 | /* I'm confused -- given the description above this |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 110 | * should fail as designed, but this application |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 111 | * */ |
| 112 | if (TEST_RETURN == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | tst_resm(TFAIL | TTERRNO, "mlock failed"); |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 114 | else |
| 115 | tst_resm(TPASS, "mlock passed"); |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 116 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 117 | } |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 118 | |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 119 | cleanup(); |
| 120 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 121 | tst_exit(); |
| 122 | } |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 123 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 124 | #else |
| 125 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 126 | int main(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 127 | { |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 128 | tst_brkm(TCONF, NULL, "test is not available on uClinux"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | #endif /* if !defined(UCLINUX) */ |
| 132 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 133 | void setup(void) |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 134 | { |
| 135 | TEST_PAUSE; |
| 136 | } |
| 137 | |
| 138 | void setup1(int len) |
| 139 | { |
Garrett Cooper | 2f41299 | 2010-12-19 06:01:41 -0800 | [diff] [blame] | 140 | addr1 = malloc(len); |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 141 | if (addr1 == NULL) |
| 142 | tst_brkm(TFAIL, cleanup, "malloc failed"); |
| 143 | } |
| 144 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 145 | void cleanup(void) |
plars | 6c15356 | 2002-06-12 21:24:47 +0000 | [diff] [blame] | 146 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 147 | } |