Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Stack size mapping is decreased through mlock/munlock call. |
| 3 | * |
| 4 | * This is to test kernel if it has a problem with shortening [stack] |
| 5 | * mapping through several loops of mlock/munlock of /proc/self/maps. |
| 6 | * |
| 7 | * From: |
| 8 | * munlock 76KiB bfef2000-bff05000 rw-p 00000000 00:00 0 [stack] |
| 9 | * |
| 10 | * To: |
| 11 | * munlock 44KiB bfefa000-bff05000 rw-p 00000000 00:00 0 [stack] |
| 12 | * |
| 13 | * with more iterations - could drop to 0KiB. |
| 14 | * |
| 15 | * Copyright (C) 2010 Red Hat, Inc. |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of version 2 of the GNU General Public |
| 18 | * License as published by the Free Software Foundation. |
| 19 | * |
| 20 | * This program is distributed in the hope that it would be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 23 | * |
| 24 | * Further, this software is distributed without any warranty that it |
| 25 | * is free of the rightful claim of any third person regarding |
| 26 | * infringement or the like. Any license provided herein, whether |
| 27 | * implied or otherwise, applies only to this software file. Patent |
| 28 | * licenses, if any, provided herein do not apply to combinations of |
| 29 | * this program with other software, or any other product whatsoever. |
| 30 | * |
| 31 | * You should have received a copy of the GNU General Public License |
| 32 | * along with this program; if not, write the Free Software |
| 33 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 34 | * 02110-1301, USA. |
| 35 | */ |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 36 | #include <sys/mman.h> |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #include <string.h> |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 39 | #include "test.h" |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 40 | |
| 41 | #define KB 1024 |
| 42 | |
| 43 | char *TCID = "mlock03"; |
| 44 | int TST_TOTAL = 1; |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 45 | |
| 46 | static void setup(void); |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 47 | static void cleanup(void); |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 48 | |
| 49 | int main(int argc, char *argv[]) |
| 50 | { |
| 51 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 52 | const char *msg; |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 53 | long from, to; |
| 54 | long first = -1, last = -1; |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 55 | char b[KB]; |
| 56 | FILE *fp; |
| 57 | |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 58 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 59 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 60 | |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 61 | setup(); |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 62 | |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 63 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 64 | fp = fopen("/proc/self/maps", "r"); |
| 65 | if (fp == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | tst_brkm(TBROK | TERRNO, cleanup, "fopen"); |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 67 | while (!feof(fp)) { |
| 68 | if (!fgets(b, KB - 1, fp)) |
| 69 | break; |
| 70 | b[strlen(b) - 1] = '\0'; |
| 71 | sscanf(b, "%lx-%lx", &from, &to); |
| 72 | |
| 73 | /* Record the initial stack size. */ |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 74 | if (lc == 0 && strstr(b, "[stack]") != NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | first = (to - from) / KB; |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 76 | |
| 77 | switch (lc & 1) { |
| 78 | case 0: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | if (mlock((const void *)from, to - from) == -1) |
| 80 | tst_resm(TINFO | TERRNO, |
| 81 | "mlock failed"); |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 82 | break; |
| 83 | case 1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | if (munlock((void *)from, to - from) == -1) |
| 85 | tst_resm(TINFO | TERRNO, |
| 86 | "munlock failed"); |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 87 | break; |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | tst_resm(TINFO, "%s from %lx to %0lx", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | (lc & 1) ? "munlock" : "mlock", from, to); |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 93 | |
| 94 | /* Record the final stack size. */ |
| 95 | if (strstr(b, "[stack]") != NULL) |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 96 | last = (to - from) / KB; |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 97 | } |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 98 | fclose(fp); |
| 99 | } |
| 100 | tst_resm(TINFO, "starting stack size is %ld", first); |
| 101 | tst_resm(TINFO, "final stack size is %ld", last); |
| 102 | if (last < first) |
| 103 | tst_resm(TFAIL, "stack size is decreased."); |
| 104 | else |
| 105 | tst_resm(TPASS, "stack size is not decreased."); |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 106 | |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 107 | cleanup(); |
Garrett Cooper | 6cb2688 | 2010-12-19 05:51:25 -0800 | [diff] [blame] | 108 | tst_exit(); |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void setup(void) |
| 112 | { |
Garrett Cooper | 40495b8 | 2011-01-19 00:48:39 -0800 | [diff] [blame] | 113 | tst_require_root(NULL); |
| 114 | |
Garrett Cooper | a041336 | 2010-12-08 00:52:25 -0800 | [diff] [blame] | 115 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 116 | TEST_PAUSE; |
| 117 | } |
| 118 | |
| 119 | void cleanup(void) |
| 120 | { |
Garrett Cooper | 40495b8 | 2011-01-19 00:48:39 -0800 | [diff] [blame] | 121 | } |