Home | History | Annotate | Download | only in mlock
      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  */
     36 #include <sys/mman.h>
     37 #include <stdio.h>
     38 #include <string.h>
     39 #include "test.h"
     40 
     41 #define KB 1024
     42 
     43 char *TCID = "mlock03";
     44 int TST_TOTAL = 1;
     45 
     46 static void setup(void);
     47 static void cleanup(void);
     48 
     49 int main(int argc, char *argv[])
     50 {
     51 	int lc;
     52 	long from, to;
     53 	long first = -1, last = -1;
     54 	char b[KB];
     55 	FILE *fp;
     56 
     57 	tst_parse_opts(argc, argv, NULL, NULL);
     58 
     59 	setup();
     60 
     61 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     62 		fp = fopen("/proc/self/maps", "r");
     63 		if (fp == NULL)
     64 			tst_brkm(TBROK | TERRNO, cleanup, "fopen");
     65 		while (!feof(fp)) {
     66 			if (!fgets(b, KB - 1, fp))
     67 				break;
     68 			b[strlen(b) - 1] = '\0';
     69 			sscanf(b, "%lx-%lx", &from, &to);
     70 
     71 			/* Record the initial stack size. */
     72 			if (lc == 0 && strstr(b, "[stack]") != NULL)
     73 				first = (to - from) / KB;
     74 
     75 			switch (lc & 1) {
     76 			case 0:
     77 				if (mlock((const void *)from, to - from) == -1)
     78 					tst_resm(TINFO | TERRNO,
     79 						 "mlock failed");
     80 				break;
     81 			case 1:
     82 				if (munlock((void *)from, to - from) == -1)
     83 					tst_resm(TINFO | TERRNO,
     84 						 "munlock failed");
     85 				break;
     86 			default:
     87 				break;
     88 			}
     89 			tst_resm(TINFO, "%s from %lx to %0lx",
     90 				 (lc & 1) ? "munlock" : "mlock", from, to);
     91 
     92 			/* Record the final stack size. */
     93 			if (strstr(b, "[stack]") != NULL)
     94 				last = (to - from) / KB;
     95 		}
     96 		fclose(fp);
     97 	}
     98 	tst_resm(TINFO, "starting stack size is %ld", first);
     99 	tst_resm(TINFO, "final stack size is %ld", last);
    100 	if (last < first)
    101 		tst_resm(TFAIL, "stack size is decreased.");
    102 	else
    103 		tst_resm(TPASS, "stack size is not decreased.");
    104 
    105 	cleanup();
    106 	tst_exit();
    107 }
    108 
    109 void setup(void)
    110 {
    111 	tst_require_root();
    112 
    113 	tst_sig(FORK, DEF_HANDLER, cleanup);
    114 	TEST_PAUSE;
    115 }
    116 
    117 void cleanup(void)
    118 {
    119 }
    120