From: Cyril H. <su...@li...> - 2012-10-30 11:56:41
|
The branch, master, has been updated via f91355883a951cc6aa6d1f20ea65b0cadc94e508 (commit) from e209c6b0cae08255fa28b2c262ccfa3ecc54d9a8 (commit) - Log ----------------------------------------------------------------- commit f91355883a951cc6aa6d1f20ea65b0cadc94e508 Author: Jan Stancek <jst...@re...> Date: Tue Oct 30 11:52:42 2012 +0100 ipc/shmat: pick suitable base_addr before each testcase This test picked fixed address in setup() which was used throughout the test. There has been reports that it can fail with EINVAL. I could reproduce it by adding single printf in between detach/attach: http://article.gmane.org/gmane.linux.ltp/16480 This patch picks suitable base_addr before each test. So even if glibc would mmap extra page with addr == base_addr picked by setup(), new suitable address will be chosen. Signed-off-by: Jan Stancek <jst...@re...> Signed-off-by: Wanlong Gao <gao...@cn...> ----------------------------------------------------------------------- Summary of changes: testcases/kernel/syscalls/ipc/shmat/shmat01.c | 18 ++--------- testcases/kernel/syscalls/ipc/shmat/shmat02.c | 16 +-------- testcases/kernel/syscalls/ipc/shmat/shmat_common.h | 33 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 testcases/kernel/syscalls/ipc/shmat/shmat_common.h diff --git a/testcases/kernel/syscalls/ipc/shmat/shmat01.c b/testcases/kernel/syscalls/ipc/shmat/shmat01.c index 6e4dee1..b64cf61 100644 --- a/testcases/kernel/syscalls/ipc/shmat/shmat01.c +++ b/testcases/kernel/syscalls/ipc/shmat/shmat01.c @@ -56,6 +56,7 @@ */ #include "ipcshm.h" +#include "shmat_common.h" char *TCID = "shmat01"; @@ -108,6 +109,7 @@ int main(int ac, char **av) /* * Use TEST macro to make the call */ + base_addr = probe_free_addr(); errno = 0; addr = shmat(*(TC[i].shmid), base_addr+TC[i].offset, TC[i].flags); @@ -128,7 +130,7 @@ int main(int ac, char **av) * clean up things in case we are looping - in * this case, detach the shared memory */ - if (shmdt((const void *)addr) == -1) { + if (shmdt(addr) == -1) { tst_brkm(TBROK, cleanup, "Couldn't detach shared memory"); } @@ -261,20 +263,6 @@ void setup(void) tst_brkm(TBROK, cleanup, "Failed to create shared memory " "resource 1 in setup()"); } - - /* Probe an available linear address for attachment */ - if ((base_addr = shmat(shm_id_1, NULL, 0)) == (void *)-1) { - tst_brkm(TBROK, cleanup, "Couldn't attach shared memory"); - } - if (shmdt((const void *)base_addr) == -1) { - tst_brkm(TBROK, cleanup, "Couldn't detach shared memory"); - } - - /* some architectures (e.g. parisc) are strange, so better always align to - * next SHMLBA address. */ - base_addr = - (void *)(((unsigned long)(base_addr) + (SHMLBA - 1)) & - ~(SHMLBA - 1)); } /* diff --git a/testcases/kernel/syscalls/ipc/shmat/shmat02.c b/testcases/kernel/syscalls/ipc/shmat/shmat02.c index d4cca0e..af28d6c 100644 --- a/testcases/kernel/syscalls/ipc/shmat/shmat02.c +++ b/testcases/kernel/syscalls/ipc/shmat/shmat02.c @@ -55,6 +55,7 @@ #include "ipcshm.h" #include <pwd.h> +#include "shmat_common.h" char *TCID = "shmat02"; char nobody_uid[] = "nobody"; @@ -125,6 +126,7 @@ int main(int ac, char **av) setup_tc(i, tc); + base_addr = probe_free_addr(); errno = 0; addr = shmat(*(tc->shmid), base_addr + tc->offset, 0); @@ -180,20 +182,6 @@ void setup(void) if ((shm_id_3 = shmget(shmkey2, INT_SIZE, IPC_CREAT|IPC_EXCL)) == -1) tst_brkm(TBROK|TERRNO, cleanup, "shmget #2 failed"); - - /* Probe an available linear address for attachment */ - if ((base_addr = shmat(shm_id_2, NULL, 0)) == (void *)-1) - tst_brkm(TBROK|TERRNO, cleanup, "shmat #1 failed"); - - if (shmdt((const void *)base_addr) == -1) - tst_brkm(TBROK|TERRNO, cleanup, "shmat #2 failed"); - - /* - * some architectures (e.g. parisc) are strange, so better always align - * to next SHMLBA address - */ - base_addr = - (void *)(((unsigned long)(base_addr) & ~(SHMLBA - 1)) + SHMLBA); } /* diff --git a/testcases/kernel/syscalls/ipc/shmat/shmat_common.h b/testcases/kernel/syscalls/ipc/shmat/shmat_common.h new file mode 100644 index 0000000..06cf5ab --- /dev/null +++ b/testcases/kernel/syscalls/ipc/shmat/shmat_common.h @@ -0,0 +1,33 @@ +static key_t probe_key; + +void *probe_free_addr(void) +{ + void *p; + int ret; + int shm_id = -1; + + if (probe_key == 0) + probe_key = getipckey(); + + /* create a shared memory resource with read and write permissions + * We align this to SHMLBA so we should allocate at least + * SHMLBA*2 in case SHMLBA > page_size. */ + shm_id = shmget(probe_key, SHMLBA*2, SHM_RW | IPC_CREAT | IPC_EXCL); + if (shm_id == -1) + tst_brkm(TBROK, cleanup, "probe: shmget failed"); + + /* Probe an available linear address for attachment */ + p = shmat(shm_id, NULL, 0); + if (p == (void *)-1) + tst_brkm(TBROK, cleanup, "probe: shmat failed"); + ret = shmdt(p); + if (ret == -1) + tst_brkm(TBROK, cleanup, "probe: shmdt failed"); + + rm_shm(shm_id); + + /* some architectures (e.g. parisc) are strange, so better always + * align to next SHMLBA address. */ + p = (void *)(((unsigned long)(p) + (SHMLBA - 1)) & ~(SHMLBA - 1)); + return p; +} hooks/post-receive -- ltp |