xref: /titanic_50/usr/src/test/libc-tests/tests/random/inz_inval.c (revision 5c5f137104b2d56181283389fa902220f2023809)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2015, Joyent, Inc.
14  */
15 
16 /*
17  * Verify that using MC_INHERIT_ZERO doesn't work on mappings that aren't
18  * anonymous private mappings.
19  */
20 
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <sys/mman.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <wait.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 
32 int
33 main(void)
34 {
35 	void *buf;
36 	int ret, fd;
37 	char *template = "/tmp/inz_inval.XXXXXX";
38 	char *tmpfile;
39 	size_t mapsz = sysconf(_SC_PAGESIZE) * 2;
40 	caddr_t bad = (caddr_t)(uintptr_t)23;
41 
42 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE,
43 	    MAP_PRIVATE | MAP_ANON, -1, 0);
44 	assert(buf != MAP_FAILED);
45 
46 	/* Bad arguments to memcntl */
47 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, bad, 0, 0);
48 	assert(ret == -1);
49 	assert(errno == EINVAL);
50 
51 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, PROT_READ, 0);
52 	assert(ret == -1);
53 	assert(errno == EINVAL);
54 
55 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, bad, PROT_READ | PRIVATE, 0);
56 	assert(ret == -1);
57 	assert(errno == EINVAL);
58 
59 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 1);
60 	assert(ret == -1);
61 	assert(errno == EINVAL);
62 
63 	ret = munmap(buf, mapsz);
64 	assert(ret == 0);
65 
66 	/* Mapping non-existant region */
67 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
68 	assert(ret == -1);
69 	assert(errno == ENOMEM);
70 
71 	/* Map anon MAP_SHARED */
72 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE,
73 	    MAP_SHARED | MAP_ANON, -1, 0);
74 	assert(buf != MAP_FAILED);
75 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
76 	assert(ret == -1);
77 	assert(errno == EINVAL);
78 	ret = munmap(buf, mapsz);
79 	assert(ret == 0);
80 
81 	/* Grab a temp file and get it to be the right size */
82 	tmpfile = strdup(template);
83 	assert(tmpfile != NULL);
84 	fd = mkstemp(tmpfile);
85 	assert(fd >= 0);
86 	ret = ftruncate(fd, mapsz);
87 	assert(ret == 0);
88 
89 	/* MAP_PRIVATE file */
90 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
91 	assert(buf != MAP_FAILED);
92 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
93 	assert(ret == -1);
94 	assert(errno == EINVAL);
95 	ret = munmap(buf, mapsz);
96 	assert(ret == 0);
97 
98 	/* MAP_SHARED file */
99 	buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
100 	assert(buf != MAP_FAILED);
101 	ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
102 	assert(ret == -1);
103 	assert(errno == EINVAL);
104 	ret = munmap(buf, mapsz);
105 	assert(ret == 0);
106 
107 	ret = close(fd);
108 	assert(ret == 0);
109 	(void) unlink(tmpfile);
110 	free(tmpfile);
111 
112 	return (0);
113 }
114