2 * Test the POSIX shared-memory API.
3 * Dedicated to the public domain by Garrett A. Wollman, 2000.
21 * Signal handler which does nothing.
24 ignoreit(int sig __unused)
30 main(int argc, char **argv)
32 char buf[1024], *cp, c;
40 #ifndef _POSIX_SHARED_MEMORY_OBJECTS
41 printf("_POSIX_SHARED_MEMORY_OBJECTS is undefined\n");
43 printf("_POSIX_SHARED_MEMORY_OBJECTS is defined as %ld\n",
44 (long)_POSIX_SHARED_MEMORY_OBJECTS - 0);
45 if (_POSIX_SHARED_MEMORY_OBJECTS - 0 == -1)
46 printf("***Indicates this feature may be unsupported!\n");
49 scval = sysconf(_SC_SHARED_MEMORY_OBJECTS);
50 if (scval == -1 && errno != 0) {
51 err(1, "sysconf(_SC_SHARED_MEMORY_OBJECTS)");
53 printf("sysconf(_SC_SHARED_MEMORY_OBJECTS) returns %ld\n",
56 printf("***Indicates this feature is unsupported!\n");
60 scval = sysconf(_SC_PAGESIZE);
61 if (scval == -1 && errno != 0) {
62 err(1, "sysconf(_SC_PAGESIZE)");
63 } else if (scval <= 0 || (size_t)psize != psize) {
64 warnx("bogus return from sysconf(_SC_PAGESIZE): %ld",
68 printf("sysconf(_SC_PAGESIZE) returns %ld\n", scval);
75 strncat(buf, *argv, (sizeof buf) - 1);
76 desc = shm_open(buf, O_EXCL | O_CREAT | O_RDWR, 0600);
80 * Can't use mkstemp for obvious reasons...
82 strcpy(buf, "/tmp/shmtest.XXXXXXXXXXXX");
84 desc = shm_open(buf, O_EXCL | O_CREAT | O_RDWR, 0600);
85 } while (desc < 0 && errno == EEXIST);
91 if (shm_unlink(buf) < 0)
94 if (ftruncate(desc, (off_t)psize) < 0)
97 region = mmap((void *)0, psize, PROT_READ | PROT_WRITE, MAP_SHARED,
99 if (region == MAP_FAILED)
101 memset(region, '\377', psize);
104 sa.sa_handler = ignoreit;
105 sigemptyset(&sa.sa_mask);
106 if (sigaction(SIGUSR1, &sa, (struct sigaction *)0) < 0)
110 sigaddset(&ss, SIGUSR1);
111 if (sigprocmask(SIG_BLOCK, &ss, (sigset_t *)0) < 0)
112 err(1, "sigprocmask");
117 } else if (rv == 0) {
121 for (cp = region; cp < (char *)region + psize; cp++) {
125 if (lseek(desc, 0, SEEK_SET) == -1)
127 for (i = 0; i < psize; i++) {
128 error = read(desc, &c, 1);
136 memset(region, '\151', psize - 2);
137 error = pwrite(desc, region, 2, psize - 2);
140 errx(1, "short write %d", error);
142 err(1, "shmfd write");
145 waitpid(rv, &status, 0);
147 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
148 printf("Functionality test successful\n");
150 } else if (WIFEXITED(status)) {
151 printf("Child process exited with status %d\n",
152 WEXITSTATUS(status));
154 printf("Child process terminated with %s\n",
155 strsignal(WTERMSIG(status)));