DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program 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 |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Test Description: |
| 21 | * Verify MAP_LOCKED works fine. |
| 22 | * "Lock the pages of the mapped region into memory in the manner of mlock(2)." |
| 23 | * |
| 24 | * Expected Result: |
| 25 | * mmap() should succeed returning the address of the mapped region, |
| 26 | * and this region should be locked into memory. |
| 27 | */ |
| 28 | #include <stdio.h> |
| 29 | #include <sys/mman.h> |
| 30 | |
| 31 | #include "test.h" |
DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 32 | |
| 33 | #define TEMPFILE "mmapfile" |
| 34 | #define MMAPSIZE (1UL<<20) |
| 35 | #define LINELEN 256 |
| 36 | |
| 37 | char *TCID = "mmap14"; |
| 38 | int TST_TOTAL = 1; |
| 39 | |
| 40 | static char *addr; |
| 41 | |
| 42 | static void getvmlck(unsigned int *lock_sz); |
| 43 | static void setup(void); |
| 44 | static void cleanup(void); |
| 45 | |
| 46 | int main(int argc, char *argv[]) |
| 47 | { |
| 48 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 49 | const char *msg; |
DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 50 | unsigned int sz_before; |
| 51 | unsigned int sz_after; |
| 52 | unsigned int sz_ch; |
| 53 | |
| 54 | msg = parse_opts(argc, argv, NULL, NULL); |
| 55 | if (msg != NULL) |
| 56 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 57 | |
| 58 | setup(); |
| 59 | |
| 60 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 61 | |
| 62 | tst_count = 0; |
| 63 | |
| 64 | getvmlck(&sz_before); |
| 65 | |
| 66 | addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE, |
| 67 | MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS, |
| 68 | -1, 0); |
| 69 | |
| 70 | if (addr == MAP_FAILED) { |
| 71 | tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE); |
| 72 | continue; |
| 73 | } |
| 74 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 75 | getvmlck(&sz_after); |
DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 76 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 77 | sz_ch = sz_after - sz_before; |
| 78 | if (sz_ch == MMAPSIZE / 1024) { |
| 79 | tst_resm(TPASS, "Functionality of mmap() " |
| 80 | "successful"); |
DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 81 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 82 | tst_resm(TFAIL, "Expected %luK locked, " |
| 83 | "get %uK locked", |
| 84 | MMAPSIZE / 1024, sz_ch); |
DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (munmap(addr, MMAPSIZE) != 0) |
| 88 | tst_brkm(TFAIL | TERRNO, NULL, "munmap failed"); |
| 89 | } |
| 90 | |
| 91 | cleanup(); |
| 92 | tst_exit(); |
DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void getvmlck(unsigned int *lock_sz) |
| 96 | { |
| 97 | int ret; |
| 98 | char line[LINELEN]; |
| 99 | FILE *fstatus = NULL; |
| 100 | |
| 101 | fstatus = fopen("/proc/self/status", "r"); |
| 102 | if (fstatus == NULL) |
| 103 | tst_brkm(TFAIL | TERRNO, NULL, "Open dev status failed"); |
| 104 | |
| 105 | while (fgets(line, LINELEN, fstatus) != NULL) |
| 106 | if (strstr(line, "VmLck") != NULL) |
| 107 | break; |
| 108 | |
| 109 | ret = sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz); |
| 110 | if (ret != 1) |
| 111 | tst_brkm(TFAIL | TERRNO, NULL, "Get lock size failed"); |
| 112 | |
| 113 | fclose(fstatus); |
| 114 | } |
| 115 | |
| 116 | static void setup(void) |
| 117 | { |
| 118 | tst_require_root(NULL); |
| 119 | |
| 120 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 121 | |
| 122 | TEST_PAUSE; |
| 123 | } |
| 124 | |
| 125 | static void cleanup(void) |
| 126 | { |
DAN LI | 8f9db55 | 2013-07-02 09:56:17 +0800 | [diff] [blame] | 127 | } |