blob: 363c0c2b142379fc4e5349aa7a7604e4deb8da49 [file] [log] [blame]
Garrett Coopera0413362010-12-08 00:52:25 -08001/*
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 Cooper6cb26882010-12-19 05:51:25 -080036#include <sys/mman.h>
Garrett Coopera0413362010-12-08 00:52:25 -080037#include <stdio.h>
38#include <string.h>
Garrett Coopera0413362010-12-08 00:52:25 -080039#include "test.h"
Garrett Coopera0413362010-12-08 00:52:25 -080040
41#define KB 1024
42
43char *TCID = "mlock03";
44int TST_TOTAL = 1;
Garrett Coopera0413362010-12-08 00:52:25 -080045
46static void setup(void);
Garrett Cooper6cb26882010-12-19 05:51:25 -080047static void cleanup(void);
Garrett Coopera0413362010-12-08 00:52:25 -080048
49int main(int argc, char *argv[])
50{
51 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020052 const char *msg;
Garrett Cooper6cb26882010-12-19 05:51:25 -080053 long from, to;
54 long first = -1, last = -1;
Garrett Coopera0413362010-12-08 00:52:25 -080055 char b[KB];
56 FILE *fp;
57
Garrett Cooper6cb26882010-12-19 05:51:25 -080058 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
Garrett Cooper53740502010-12-16 00:04:01 -080059 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper6cb26882010-12-19 05:51:25 -080060
Garrett Coopera0413362010-12-08 00:52:25 -080061 setup();
Garrett Cooper6cb26882010-12-19 05:51:25 -080062
Garrett Coopera0413362010-12-08 00:52:25 -080063 for (lc = 0; TEST_LOOPING(lc); lc++) {
64 fp = fopen("/proc/self/maps", "r");
65 if (fp == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 tst_brkm(TBROK | TERRNO, cleanup, "fopen");
Garrett Coopera0413362010-12-08 00:52:25 -080067 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 Cooper6cb26882010-12-19 05:51:25 -080074 if (lc == 0 && strstr(b, "[stack]") != NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 first = (to - from) / KB;
Garrett Coopera0413362010-12-08 00:52:25 -080076
77 switch (lc & 1) {
78 case 0:
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 if (mlock((const void *)from, to - from) == -1)
80 tst_resm(TINFO | TERRNO,
81 "mlock failed");
Garrett Coopera0413362010-12-08 00:52:25 -080082 break;
83 case 1:
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 if (munlock((void *)from, to - from) == -1)
85 tst_resm(TINFO | TERRNO,
86 "munlock failed");
Garrett Coopera0413362010-12-08 00:52:25 -080087 break;
88 default:
89 break;
90 }
91 tst_resm(TINFO, "%s from %lx to %0lx",
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 (lc & 1) ? "munlock" : "mlock", from, to);
Garrett Coopera0413362010-12-08 00:52:25 -080093
94 /* Record the final stack size. */
95 if (strstr(b, "[stack]") != NULL)
Garrett Cooper6cb26882010-12-19 05:51:25 -080096 last = (to - from) / KB;
Garrett Coopera0413362010-12-08 00:52:25 -080097 }
Garrett Coopera0413362010-12-08 00:52:25 -080098 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 Cooper6cb26882010-12-19 05:51:25 -0800106
Garrett Coopera0413362010-12-08 00:52:25 -0800107 cleanup();
Garrett Cooper6cb26882010-12-19 05:51:25 -0800108 tst_exit();
Garrett Coopera0413362010-12-08 00:52:25 -0800109}
110
111void setup(void)
112{
Garrett Cooper40495b82011-01-19 00:48:39 -0800113 tst_require_root(NULL);
114
Garrett Coopera0413362010-12-08 00:52:25 -0800115 tst_sig(FORK, DEF_HANDLER, cleanup);
116 TEST_PAUSE;
117}
118
119void cleanup(void)
120{
Garrett Cooper40495b82011-01-19 00:48:39 -0800121}