mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Author: Rajeev Tiwari: rajeevti@in.ibm.com |
| 4 | * Copyright (c) 2004 Gernot Payer <gpayer@suse.de> |
| 5 | * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz> |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU General Public License for more details. |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 23 | * This test case provides a functional validation for mincore system call. |
| 24 | * We mmap a file of known size (multiple of page size) and lock it in |
| 25 | * memory. Then we obtain page location information via mincore and compare |
| 26 | * the result with the expected value. |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 29 | #include <sys/mman.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 32 | #include <sys/wait.h> |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
| 35 | #include <signal.h> |
| 36 | #include <errno.h> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 37 | |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 38 | #include "test.h" |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 39 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 40 | char *TCID = "mincore02"; |
| 41 | int TST_TOTAL = 1; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 43 | static int fd = 0; |
| 44 | static void *addr = NULL; |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 45 | static int page_size; |
| 46 | static int num_pages = 4; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | static unsigned char *vec = NULL; |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 48 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 49 | static void cleanup(void) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 50 | { |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 51 | free(vec); |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 52 | munlock(addr, page_size * num_pages); |
| 53 | munmap(addr, page_size * num_pages); |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 54 | close(fd); |
| 55 | tst_rmdir(); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 58 | static void setup(void) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 59 | { |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 60 | char *buf; |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 61 | size_t size; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 62 | |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 63 | tst_tmpdir(); |
| 64 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 65 | page_size = getpagesize(); |
| 66 | if (page_size == -1) |
| 67 | tst_brkm(TBROK | TERRNO, cleanup, "Unable to get page size"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 68 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 69 | size = page_size * num_pages; |
| 70 | buf = malloc(size); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | |
| 72 | memset(buf, 42, size); |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 73 | vec = malloc((size + page_size - 1) / page_size); |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 74 | |
| 75 | fd = open("mincore02", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
| 76 | if (fd == -1) { |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 77 | tst_brkm(TBROK | TERRNO, cleanup, |
| 78 | "Unable to create temporary file"); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 79 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 80 | |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 81 | /* fill the temporary file with two pages of data */ |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 82 | if (write(fd, buf, size) < 0) { |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 83 | tst_brkm(TBROK | TERRNO, cleanup, |
| 84 | "Error in writing to the file"); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 85 | } |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 86 | free(buf); |
| 87 | |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 88 | addr = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 89 | MAP_SHARED, fd, 0); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 90 | |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 91 | if (addr == MAP_FAILED) { |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 92 | tst_brkm(TBROK | TERRNO, cleanup, |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 93 | "Unable to map file for read/write"); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 96 | /* lock mmapped file, so mincore returns "in core" for all pages */ |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 97 | if (mlock(addr, size) == -1) |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 98 | tst_brkm(TBROK | TERRNO, cleanup, "Unable to lock the file"); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 101 | int main(int argc, char **argv) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 102 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | int lock_pages, counter; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 104 | const char *msg; |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 105 | int lc; |
| 106 | |
| 107 | msg = parse_opts(argc, argv, NULL, NULL); |
| 108 | if (msg != NULL) |
| 109 | tst_brkm(TBROK, cleanup, "error parsing options: %s", msg); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 110 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 111 | setup(); |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 112 | |
| 113 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 114 | tst_count = 0; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 115 | |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 116 | if (mincore(addr, num_pages * page_size, vec) == -1) { |
| 117 | tst_brkm(TBROK | TERRNO, cleanup, |
| 118 | "Unable to execute mincore system call"); |
| 119 | } |
| 120 | |
| 121 | /* check status of pages */ |
| 122 | lock_pages = 0; |
| 123 | |
| 124 | for (counter = 0; counter < num_pages; counter++) { |
| 125 | if (vec[counter] & 1) |
| 126 | lock_pages++; |
| 127 | } |
| 128 | |
| 129 | if (lock_pages == num_pages) { |
| 130 | tst_resm(TPASS, "%d pages locked, %d pages in-core", num_pages, |
| 131 | lock_pages); |
| 132 | } else { |
| 133 | tst_resm(TFAIL, |
| 134 | "not all locked pages are in-core: no. locked: %d, no. in-core: %d", |
| 135 | num_pages, lock_pages); |
| 136 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | } |
| 138 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 139 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 140 | tst_exit(); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 141 | } |