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