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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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
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"
57
58 char *TCID = "shmt10"; /* Test program identifier. */
59 int TST_TOTAL = 2; /* Total number of test cases. */
60 /**************/
61
62 int shmid;
63 key_t key;
64
65 static int child(int);
66 static int rm_shm(int);
67 static void fini(int);
68
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 char *c1 = NULL;
72 int pid, st;
73 register int i;
74 int iter = 500;
75 int c;
76 extern char *optarg;
77
78 key = (key_t) getpid();
79 signal(SIGTERM, fini);
80
81 /*--------------------------------------------------------*/
82
83 while ((c = getopt(argc, argv, "i:")) != EOF) {
84 switch (c) {
85 case 'i':
86 iter = atoi(optarg);
87 break;
88 default:
89 tst_brkm(TCONF, NULL, "usage: %s [-i <# iterations>]",
90 argv[0]);
91 }
92 }
93
94 /*------------------------------------------------------------------------*/
95
96 if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
97 tst_resm(TFAIL, "shmget");
98 tst_brkm(TFAIL, NULL, "Error: shmid = %d\n", shmid);
99 }
100
101 pid = fork();
102 switch (pid) {
103 case -1:
104 tst_brkm(TBROK, NULL, "fork failed");
105 case 0:
106 child(iter);
107 tst_exit();
108 }
109
110 for (i = 0; i < iter; i++) {
111 if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) {
112 tst_resm(TFAIL,
113 "Error shmat: iter %d, shmid = %d\n", i,
114 shmid);
115 break;
116 }
117 if (shmdt(c1) < 0) {
118 tst_resm(TFAIL, "Error: shmdt: iter %d ", i);
119 break;
120 }
121 }
122 while (wait(&st) < 0 && errno == EINTR) ;
123 tst_resm(TPASS, "shmat,shmdt");
124 /*------------------------------------------------------------------------*/
125
126 rm_shm(shmid);
127 tst_exit();
128 }
129
rm_shm(int shmid)130 static int rm_shm(int shmid)
131 {
132 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
133 perror("shmctl");
134 tst_brkm(TFAIL,
135 NULL,
136 "shmctl Failed to remove: shmid = %d, errno = %d\n",
137 shmid, errno);
138 }
139 return (0);
140 }
141
child(int iter)142 static int child(int iter)
143 {
144 register int i;
145 char *c1;
146
147 for (i = 0; i < iter; i++) {
148 if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) {
149 tst_brkm(TFAIL,
150 NULL,
151 "Error:child proc: shmat: iter %d, shmid = %d\n",
152 i, shmid);
153 }
154 if (shmdt(c1) < 0) {
155 tst_brkm(TFAIL,
156 NULL, "Error: child proc: shmdt: iter %d ",
157 i);
158 }
159 }
160 return (0);
161 }
162
fini(int sig)163 static void fini(int sig)
164 {
165 rm_shm(shmid);
166 }
167