blob: 41fafa662954553b5e6c8c650add35c7a402f04f [file] [log] [blame]
DAN LI8f9db552013-07-02 09:56:17 +08001/*
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 LI8f9db552013-07-02 09:56:17 +080032
33#define TEMPFILE "mmapfile"
34#define MMAPSIZE (1UL<<20)
35#define LINELEN 256
36
37char *TCID = "mmap14";
38int TST_TOTAL = 1;
39
40static char *addr;
41
42static void getvmlck(unsigned int *lock_sz);
43static void setup(void);
44static void cleanup(void);
45
46int main(int argc, char *argv[])
47{
48 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020049 const char *msg;
DAN LI8f9db552013-07-02 09:56:17 +080050 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 Hrubise38b9612014-06-02 17:20:57 +020075 getvmlck(&sz_after);
DAN LI8f9db552013-07-02 09:56:17 +080076
Cyril Hrubise38b9612014-06-02 17:20:57 +020077 sz_ch = sz_after - sz_before;
78 if (sz_ch == MMAPSIZE / 1024) {
79 tst_resm(TPASS, "Functionality of mmap() "
80 "successful");
DAN LI8f9db552013-07-02 09:56:17 +080081 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020082 tst_resm(TFAIL, "Expected %luK locked, "
83 "get %uK locked",
84 MMAPSIZE / 1024, sz_ch);
DAN LI8f9db552013-07-02 09:56:17 +080085 }
86
87 if (munmap(addr, MMAPSIZE) != 0)
88 tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
89 }
90
91 cleanup();
92 tst_exit();
DAN LI8f9db552013-07-02 09:56:17 +080093}
94
95void 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
116static void setup(void)
117{
118 tst_require_root(NULL);
119
120 tst_sig(FORK, DEF_HANDLER, cleanup);
121
122 TEST_PAUSE;
123}
124
125static void cleanup(void)
126{
DAN LI8f9db552013-07-02 09:56:17 +0800127}