diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2015-01-15 20:41:00 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2015-01-15 20:41:00 +0000 |
commit | f16d12bcafef8f447ffe0de1bb738d29e2cb7213 (patch) | |
tree | 7d98e62c62e47fab5917091362861aa79f82841a /regress | |
parent | c3a3437b73d6393de1137324fa786087f8e115de (diff) |
Test shmat() with a non-NULL address, both failure and success
Diffstat (limited to 'regress')
-rw-r--r-- | regress/sys/kern/sysvshm/shmtest.c | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/regress/sys/kern/sysvshm/shmtest.c b/regress/sys/kern/sysvshm/shmtest.c index 230fc87c866..39d163360a2 100644 --- a/regress/sys/kern/sysvshm/shmtest.c +++ b/regress/sys/kern/sysvshm/shmtest.c @@ -36,6 +36,7 @@ #include <sys/param.h> #include <sys/ipc.h> +#include <sys/mman.h> #include <sys/shm.h> #include <sys/wait.h> @@ -288,6 +289,7 @@ receiver() { int shmid; void *shm_buf; + void *block; if ((shmid = shmget(shmkey, pgsize, 0)) == -1) err(1, "receiver: shmget"); @@ -296,9 +298,38 @@ receiver() err(1, "receiver: shmat"); if (verbose) - printf("%s\n", (const char *)shm_buf); + printf("%.*s\n", (int)pgsize, (const char *)shm_buf); if (strcmp((const char *)shm_buf, m_str) != 0) - err(1, "receiver: data isn't correct"); + errx(1, "receiver: data isn't correct"); + + /* mmap() a page to get a distinct, freeable address */ + block = mmap(NULL, pgsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, + -1, 0); + if (block == MAP_FAILED) + err(1, "receiver: mmap"); + + /* detach, then try to attach, conflicting with the mmap() */ + if (shmdt(shm_buf) == -1) + err(1, "receiver: shmdt"); + if ((shm_buf = shmat(shmid, block, 0)) != (void *) -1) + errx(1, "receiver: shmat(conflict) succeeded!"); + if (errno != ENOMEM) + err(1, "receiver: shmat(conflict) wrong error"); + + /* free up that address and try again */ + if (munmap(block, pgsize) == -1) + err(1, "receiver: munmap"); + if ((shm_buf = shmat(shmid, block, 0)) == (void *) -1) + err(1, "receiver: shmat(fixed)"); + + if (shm_buf != block) + errx(1, "receiver: shmat not at expected address: %p != %p", + shm_buf, block); + + if (verbose) + printf("%.*s\n", (int)pgsize, (const char *)shm_buf); + if (strcmp((const char *)shm_buf, m_str) != 0) + errx(1, "receiver: data isn't correct second time"); exit(0); } |