1*98a610fcSart /*
2*98a610fcSart * Copyright (c) 2011 Artur Grabowski <art@openbsd.org>
3*98a610fcSart *
4*98a610fcSart * Permission to use, copy, modify, and distribute this software for any
5*98a610fcSart * purpose with or without fee is hereby granted, provided that the above
6*98a610fcSart * copyright notice and this permission notice appear in all copies.
7*98a610fcSart *
8*98a610fcSart * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*98a610fcSart * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*98a610fcSart * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*98a610fcSart * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*98a610fcSart * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*98a610fcSart * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*98a610fcSart * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*98a610fcSart */
16*98a610fcSart
17*98a610fcSart
18*98a610fcSart #include <sys/types.h>
19*98a610fcSart #include <sys/mman.h>
20*98a610fcSart #include <err.h>
21*98a610fcSart #include <stdlib.h>
22*98a610fcSart #include <stdio.h>
23*98a610fcSart #include <string.h>
24*98a610fcSart #include <unistd.h>
25*98a610fcSart #include <errno.h>
26*98a610fcSart
27*98a610fcSart /*
28*98a610fcSart * Test a corner case writing to a file from an mmap region from that file
29*98a610fcSart * should fail.
30*98a610fcSart */
31*98a610fcSart int
main(int argc,char ** argv)32*98a610fcSart main(int argc, char **argv)
33*98a610fcSart {
34*98a610fcSart char name[20] = "/tmp/fluff.XXXXXX";
35*98a610fcSart char *buf;
36*98a610fcSart size_t ps;
37*98a610fcSart int fd;
38*98a610fcSart
39*98a610fcSart ps = getpagesize();
40*98a610fcSart
41*98a610fcSart if ((fd = mkstemp(name)) == -1)
42*98a610fcSart err(1, "mkstemp");
43*98a610fcSart
44*98a610fcSart if (unlink(name) == -1)
45*98a610fcSart err(1, "unlink");
46*98a610fcSart
47*98a610fcSart buf = mmap(NULL, ps, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0);
48*98a610fcSart if (buf == MAP_FAILED)
49*98a610fcSart err(1, "mmap");
50*98a610fcSart
51*98a610fcSart if (pwrite(fd, buf, ps, 0) == ps)
52*98a610fcSart errx(1, "write to self succeeded");
53*98a610fcSart
54*98a610fcSart if (errno != EFAULT)
55*98a610fcSart err(1, "unexpected errno");
56*98a610fcSart
57*98a610fcSart return (0);
58*98a610fcSart }
59