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 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <fcntl.h>
23 #include "test.h"
24
25 char *TCID = "ima_mmap";
26 int TST_TOTAL = 1;
27
main(int argc,char * argv[])28 int main(int argc, char *argv[])
29 {
30 int fd;
31 void *file;
32 char *filename;
33
34 if (argc != 2)
35 printf("%s: filename\n", argv[1]);
36 filename = argv[1];
37
38 fd = open(filename, O_CREAT | O_RDWR, S_IRWXU);
39 if (fd < 0) {
40 perror("open");
41 return (-1);
42 }
43
44 file = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
45 if (file == (void *)-1) {
46 perror("mmap");
47 return (-1);
48 }
49 close(fd);
50 sleep(30);
51 if (munmap(file, 1024) < 0) {
52 perror("unmap");
53 return (-1);
54 }
55 tst_exit();
56 }
57