robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. |
| 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 | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /************************************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : munlock02 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 21 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 23 | * TEST TITLE : Test for checking basic error conditions for |
| 24 | * munlock(2) |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 26 | * TEST CASE TOTAL : 2 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 28 | * AUTHOR : Nirmala Devi Dhanasekar <nirmala.devi@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +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 |
| 35 | * Check for basic errors returned by munlock(2) system call. |
| 36 | * |
| 37 | * Verify that munlock(2) returns -1 and sets errno to |
| 38 | * |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 39 | * 1) ENOMEM - Some of the specified address range does not correspond to |
| 40 | * mapped pages in the address space of the process. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 41 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 42 | * Setup: |
| 43 | * Setup signal handling. |
| 44 | * Pause for SIGUSR1 if option specified. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 45 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 46 | * Test: |
| 47 | * Loop if the proper options are given. |
| 48 | * Do necessary setup for each test. |
| 49 | * Execute system call |
| 50 | * Check return code, if system call failed (return=-1) |
| 51 | * Log the errno and Issue a FAIL message. |
| 52 | * Otherwise, Issue a PASS message. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 53 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 54 | * Cleanup: |
| 55 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 56 | * |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 57 | * USAGE: <for command-line> |
| 58 | * munlock02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] |
| 59 | * where, -c n : Run n copies concurrently |
| 60 | * -e : Turn on errno logging. |
| 61 | * -h : Show this help screen |
| 62 | * -i n : Execute test n times. |
| 63 | * -I x : Execute test for x seconds. |
| 64 | * -p : Pause for SIGUSR1 before starting |
| 65 | * -P x : Pause for x seconds between iterations. |
| 66 | * -t : Turn on syscall timing. |
| 67 | * |
| 68 | * RESTRICTIONS |
| 69 | * Test must run as root. |
| 70 | *****************************************************************************/ |
| 71 | #include <errno.h> |
| 72 | #include <unistd.h> |
| 73 | #include <sys/mman.h> |
| 74 | #include <pwd.h> |
| 75 | #include "test.h" |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 76 | |
| 77 | void setup(); |
| 78 | void cleanup(); |
| 79 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 80 | char *TCID = "munlock02"; |
| 81 | int TST_TOTAL = 1; |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 82 | |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 83 | #define LEN 1024 |
| 84 | |
| 85 | void *addr1; |
| 86 | |
| 87 | struct test_case_t { |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 88 | void *addr; |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 89 | int len; |
| 90 | int error; |
| 91 | char *edesc; |
| 92 | } TC[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | { |
| 94 | NULL, 0, ENOMEM, "address range out of address space"},}; |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 95 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 96 | #if !defined(UCLINUX) |
| 97 | |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 98 | int main(int ac, char **av) |
| 99 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 100 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 101 | const char *msg; |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 103 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 104 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 105 | } |
| 106 | |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 107 | setup(); |
| 108 | |
| 109 | /* check looping state */ |
| 110 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 111 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 112 | tst_count = 0; |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 113 | for (i = 0; i < TST_TOTAL; i++) { |
| 114 | #ifdef __ia64__ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | TC[0].len = 8 * getpagesize(); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 116 | #endif |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 117 | TEST(munlock(TC[i].addr, TC[i].len)); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 118 | |
| 119 | /* check return code */ |
| 120 | if (TEST_RETURN == -1) { |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 121 | if (TEST_ERRNO != TC[i].error) |
| 122 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | "munlock() Failed with wrong " |
| 124 | "errno, expected errno=%s, " |
| 125 | "got errno=%d : %s", |
| 126 | TC[i].edesc, TEST_ERRNO, |
| 127 | strerror(TEST_ERRNO)); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 128 | else |
| 129 | tst_resm(TPASS, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | "expected failure - errno " |
| 131 | "= %d : %s", |
| 132 | TEST_ERRNO, |
| 133 | strerror(TEST_ERRNO)); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 134 | } else { |
| 135 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | "munlock() Failed, expected " |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 137 | "return value=-1, got %ld", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | TEST_RETURN); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 141 | } |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 142 | |
| 143 | /* cleanup and exit */ |
| 144 | cleanup(); |
| 145 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | tst_exit(); |
| 147 | } |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 148 | |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 149 | /* setup() - performs all ONE TIME setup for this test. */ |
| 150 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 151 | void setup(void) |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 152 | { |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 153 | |
| 154 | char *address; |
| 155 | |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 156 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 157 | |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 158 | TC[0].len = 8 * getpagesize(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | address = mmap(0, TC[0].len, PROT_READ | PROT_WRITE, |
| 160 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 161 | if (address == MAP_FAILED) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | tst_brkm(TFAIL, cleanup, "mmap_failed"); |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 163 | memset(address, 0x20, TC[0].len); |
| 164 | TEST(mlock(address, TC[0].len)); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 165 | |
| 166 | /* check return code */ |
| 167 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | tst_brkm(TFAIL | TTERRNO, cleanup, |
| 169 | "mlock(%p, %d) Failed with return=%ld", address, |
| 170 | TC[0].len, TEST_RETURN); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 171 | } |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 172 | TC[0].addr = address; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 173 | /* |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 174 | * unmap part of the area, to create the condition for ENOMEM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | */ |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 176 | address += 2 * getpagesize(); |
| 177 | munmap(address, 4 * getpagesize()); |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 178 | |
| 179 | TEST_PAUSE; |
| 180 | |
| 181 | return; |
| 182 | } |
| 183 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 184 | #else |
| 185 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 186 | int main(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 187 | { |
vapier | 81a6307 | 2006-02-27 04:29:21 +0000 | [diff] [blame] | 188 | tst_resm(TINFO, "test is not available on uClinux"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 189 | tst_exit(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | #endif /* if !defined(UCLINUX) */ |
| 193 | |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 194 | /* |
| 195 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 196 | * completion or premature exit. |
| 197 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 198 | void cleanup(void) |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 199 | { |
robbiew | f033cf9 | 2003-06-23 15:28:06 +0000 | [diff] [blame] | 200 | return; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 201 | } |