• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2009
3  *
4  * Authors:
5  * Mimi Zohar <zohar@us.ibm.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation, version 2 of the
10  * License.
11  *
12  * File: ima_mmap.c
13  *
14  * Open and mmap a file and sleep. Another process will open the
15  * mmapped file in read mode, resulting in a open_writer violation.
16  */
17 
18 #include "tst_test.h"
19 
20 #define SLEEP_AFTER_CLOSE 3
21 #define MMAPSIZE 1024
22 
23 static char *filename;
24 static void *file;
25 static int fd;
26 
27 static struct tst_option options[] = {
28 	{"f:", &filename,
29 	 "-f file  File to mmap"},
30 	{NULL, NULL, NULL}
31 };
32 
cleanup(void)33 static void cleanup(void)
34 {
35 	if (file)
36 		SAFE_MUNMAP(file, MMAPSIZE);
37 
38 	if (fd > 0)
39 		SAFE_CLOSE(fd);
40 }
41 
run(void)42 static void run(void)
43 {
44 	if (!filename)
45 		tst_brk(TBROK, "Usage: %s -f filename", TCID);
46 
47 	fd = SAFE_OPEN(filename, O_CREAT | O_RDWR, S_IRWXU);
48 
49 	file = SAFE_MMAP(NULL, MMAPSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
50 	SAFE_CLOSE(fd);
51 
52 	tst_res(TINFO, "sleep %ds", SLEEP_AFTER_CLOSE);
53 	sleep(SLEEP_AFTER_CLOSE);
54 
55 	tst_res(TPASS, "test completed");
56 }
57 
58 static struct tst_test test = {
59 	.options = options,
60 	.test_all = run,
61 	.cleanup = cleanup,
62 };
63