12 /* - Checks if mapping of already closed file survives fork()
13 - Checks if mapping the same region of the same file twice
17 sigset_t unblock_sigsegv;
21 char const line[] = "y1 y1 y1 y1 y1 y1 y1 y1 y1 y1 y1 y1 y1 y1 y1 y1 y1";
24 perror_exit (char *str)
26 printf ("%s: %s\n", str, strerror (errno));
33 sigprocmask (SIG_UNBLOCK, &unblock_sigsegv, 0);
38 main(int argc, char **argv)
45 /* Create data file */
46 if ((fd = open("y.txt", O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1)
47 perror_exit ("Can't create data file");
48 write (fd, line, sizeof(line) - 1);
52 if ((fd = open("y.txt", O_RDONLY)) == -1)
53 perror_exit ("Can't open data file");
55 if (fstat(fd, &statbuf) < 0)
56 perror_exit ("fstat failed");
59 perror_exit ("filesize is 0");
61 if ((buf1 = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))
63 perror_exit ("mmap 1 failed");
67 /* Open data file a second time */
68 if ((fd = open("y.txt", O_RDONLY)) == -1)
69 perror_exit ("Can't open data file in second run");
71 if ((buf2 = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))
73 perror_exit ("mmap 2 failed");
77 sigemptyset (&unblock_sigsegv);
78 sigaddset (&unblock_sigsegv, SIGSEGV);
79 signal (SIGSEGV, sigsegv);
82 perror_exit ("SEGV in fork");
87 perror_exit ("fork failed");
90 perror_exit (pid ? "SEGV in parent" : "SEGV in child");
96 perror_exit (pid ? "SEGV in parent's munmap" : "SEGV in child's munmap");
98 if (munmap(buf1, statbuf.st_size))
99 perror_exit (pid ? "munmap failed in parent" : "munmap failed in child");
104 perror_exit (pid ? "no SEGV in parent after munmap" : "no SEGV in child after munmap");
108 perror_exit (pid ? "SEGV in parent after munmap" : "SEGV in child after munmap");
113 perror_exit (pid ? "SEGV in parent's munmap" : "SEGV in child's munmap");
115 if (munmap(buf2, statbuf.st_size))
116 perror_exit (pid ? "munmap failed in parent" : "munmap failed in child");
120 waitpid (pid, &status, 0);
122 if (!WIFEXITED (status) || WEXITSTATUS (status))