feature_tests.h: repurpose __UNLEASHED_VISIBLE
[unleashed.git] / usr / src / test / libc-tests / tests / random / inz_inval.c
blob1032814b25080f2e5ce7ecf8d84c9b1ef56159a8
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.
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.
13 * Copyright (c) 2015, Joyent, Inc.
17 * Verify that using MC_INHERIT_ZERO doesn't work on mappings that aren't
18 * anonymous private mappings.
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>
32 int
33 main(void)
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;
42 buf = mmap(NULL, mapsz, PROT_READ | PROT_WRITE,
43 MAP_PRIVATE | MAP_ANON, -1, 0);
44 assert(buf != MAP_FAILED);
46 /* Bad arguments to memcntl */
47 ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, bad, 0, 0);
48 assert(ret == -1);
49 assert(errno == EINVAL);
51 ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, PROT_READ, 0);
52 assert(ret == -1);
53 assert(errno == EINVAL);
55 ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, bad, PROT_READ | PRIVATE, 0);
56 assert(ret == -1);
57 assert(errno == EINVAL);
59 ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 1);
60 assert(ret == -1);
61 assert(errno == EINVAL);
63 ret = munmap(buf, mapsz);
64 assert(ret == 0);
66 /* Mapping non-existant region */
67 ret = memcntl(buf, mapsz, MC_INHERIT_ZERO, 0, 0, 0);
68 assert(ret == -1);
69 assert(errno == ENOMEM);
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);
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);
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);
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);
107 ret = close(fd);
108 assert(ret == 0);
109 (void) unlink(tmpfile);
110 free(tmpfile);
112 return (0);