robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* 12/20/2002 Port to LTP robbiew@us.ibm.com */ |
| 21 | /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ |
| 22 | |
| 23 | /* |
| 24 | * NAME |
| 25 | * shmt10.c - test simultaneous shmat/shmdt |
| 26 | * |
| 27 | * CALLS |
| 28 | * shmget, shmat, shmdt, shmctl |
| 29 | * |
| 30 | * ALGORITHM |
| 31 | * Create a shared memory segment and fork a child. Both |
| 32 | * parent and child spin in a loop attaching and detaching |
| 33 | * the segment. After completing the specified number of |
| 34 | * iterations, the child exits and the parent deletes the |
| 35 | * segment. |
| 36 | * |
| 37 | * USAGE |
| 38 | * shmt10 [-i 500] |
| 39 | * -i # of iterations, default 500 |
| 40 | * |
| 41 | */ |
| 42 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/wait.h> |
| 46 | #include <sys/ipc.h> |
| 47 | #include <sys/shm.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <unistd.h> |
| 50 | #include <signal.h> |
| 51 | #include <errno.h> |
| 52 | |
| 53 | #define SIZE 0x32768 |
| 54 | |
| 55 | /** LTP Port **/ |
| 56 | #include "test.h" |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 57 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 58 | char *TCID = "shmt10"; /* Test program identifier. */ |
| 59 | int TST_TOTAL = 2; /* Total number of test cases. */ |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 60 | /**************/ |
| 61 | |
| 62 | int shmid; |
| 63 | key_t key; |
| 64 | |
| 65 | int child(int); |
| 66 | int rm_shm(int); |
| 67 | void fini(); |
| 68 | |
| 69 | int main(argc, argv) |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 70 | int argc; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 71 | char *argv[]; |
| 72 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 73 | char *c1 = NULL; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 74 | int pid, st; |
| 75 | register int i; |
| 76 | int iter = 500; |
| 77 | int c; |
| 78 | extern char *optarg; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 79 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 80 | key = (key_t) getpid(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 81 | signal(SIGTERM, fini); |
| 82 | |
| 83 | /*--------------------------------------------------------*/ |
| 84 | |
| 85 | while ((c = getopt(argc, argv, "i:")) != EOF) { |
| 86 | switch (c) { |
| 87 | case 'i': |
| 88 | iter = atoi(optarg); |
| 89 | break; |
| 90 | default: |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 91 | tst_brkm(TCONF, NULL, "usage: %s [-i <# iterations>]", |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 92 | argv[0]); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 96 | /*------------------------------------------------------------------------*/ |
| 97 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 98 | if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) { |
| 99 | tst_resm(TFAIL, "shmget"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 100 | tst_brkm(TFAIL, NULL, "Error: shmid = %d\n", shmid); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | pid = fork(); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 104 | switch (pid) { |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 105 | case -1: |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 106 | tst_brkm(TBROK, NULL, "fork failed"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 107 | case 0: |
| 108 | child(iter); |
| 109 | tst_exit(); |
| 110 | } |
| 111 | |
| 112 | for (i = 0; i < iter; i++) { |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 113 | if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 114 | tst_resm(TFAIL, |
| 115 | "Error shmat: iter %d, shmid = %d\n", i, |
| 116 | shmid); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | if (shmdt(c1) < 0) { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 120 | tst_resm(TFAIL, "Error: shmdt: iter %d ", i); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | } |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 124 | while (wait(&st) < 0 && errno == EINTR) ; |
| 125 | tst_resm(TPASS, "shmat,shmdt"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 126 | /*------------------------------------------------------------------------*/ |
| 127 | |
| 128 | rm_shm(shmid); |
| 129 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int rm_shm(shmid) |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 133 | int shmid; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 134 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 135 | if (shmctl(shmid, IPC_RMID, NULL) == -1) { |
| 136 | perror("shmctl"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 137 | tst_brkm(TFAIL, |
| 138 | NULL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 139 | "shmctl Failed to remove: shmid = %d, errno = %d\n", |
| 140 | shmid, errno); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 141 | } |
| 142 | return (0); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int child(iter) |
| 146 | int iter; |
| 147 | { |
| 148 | register int i; |
| 149 | char *c1; |
| 150 | |
| 151 | for (i = 0; i < iter; i++) { |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 152 | if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 153 | tst_brkm(TFAIL, |
| 154 | NULL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 155 | "Error:child proc: shmat: iter %d, shmid = %d\n", |
| 156 | i, shmid); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 157 | } |
| 158 | if (shmdt(c1) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 159 | tst_brkm(TFAIL, |
| 160 | NULL, "Error: child proc: shmdt: iter %d ", |
| 161 | i); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 164 | return (0); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 165 | } |
| 166 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 167 | void fini() |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 168 | { |
| 169 | rm_shm(shmid); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 170 | } |