xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/shmt/shmt06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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  *	shmt06
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  * CALLS
28*49cdfc7eSAndroid Build Coastguard Worker  *	shmctl(2) shmget(2) shmat(2)
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
31*49cdfc7eSAndroid Build Coastguard Worker  * Parent process forks a child. Child pauses until parent has created
32*49cdfc7eSAndroid Build Coastguard Worker  * a shared memory segment, attached to it and written to it too. At that
33*49cdfc7eSAndroid Build Coastguard Worker  * time child gets the shared memory segment id, attaches to it at two
34*49cdfc7eSAndroid Build Coastguard Worker  * different addresses than the parents and verifies that their contents
35*49cdfc7eSAndroid Build Coastguard Worker  * are the same as the contents of the parent attached segment.
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  */
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ipc.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/shm.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/utsname.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker #define		SIZE		16*1024
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
53*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "shmt06";		/* Test program identifier.    */
56*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 2;		/* Total number of test cases. */
57*49cdfc7eSAndroid Build Coastguard Worker /**************/
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker key_t key;
60*49cdfc7eSAndroid Build Coastguard Worker sigset_t set;
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker int child();
63*49cdfc7eSAndroid Build Coastguard Worker static int rm_shm(int);
64*49cdfc7eSAndroid Build Coastguard Worker 
main(void)65*49cdfc7eSAndroid Build Coastguard Worker int main(void)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker 	char *cp = NULL;
68*49cdfc7eSAndroid Build Coastguard Worker 	int pid, pid1, shmid;
69*49cdfc7eSAndroid Build Coastguard Worker 	int status;
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	key = (key_t) getpid();
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&set);
74*49cdfc7eSAndroid Build Coastguard Worker 	sigaddset(&set, SIGUSR1);
75*49cdfc7eSAndroid Build Coastguard Worker 	sigprocmask(SIG_BLOCK, &set, NULL);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	pid = fork();
78*49cdfc7eSAndroid Build Coastguard Worker 	switch (pid) {
79*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, NULL, "fork failed");
81*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
82*49cdfc7eSAndroid Build Coastguard Worker 		child();
83*49cdfc7eSAndroid Build Coastguard Worker 	}
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker /*------------------------------------------------------*/
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
88*49cdfc7eSAndroid Build Coastguard Worker 		perror("shmget");
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "Error: shmget: shmid = %d, errno = %d",
90*49cdfc7eSAndroid Build Coastguard Worker 			 shmid, errno);
91*49cdfc7eSAndroid Build Coastguard Worker 		/*
92*49cdfc7eSAndroid Build Coastguard Worker 		 * kill the child if parent failed to do the attach
93*49cdfc7eSAndroid Build Coastguard Worker 		 */
94*49cdfc7eSAndroid Build Coastguard Worker 		(void)kill(pid, SIGINT);
95*49cdfc7eSAndroid Build Coastguard Worker 	} else {
96*49cdfc7eSAndroid Build Coastguard Worker 		cp = shmat(shmid, NULL, 0);
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 		if (cp == (char *)-1) {
99*49cdfc7eSAndroid Build Coastguard Worker 			perror("shmat");
100*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
101*49cdfc7eSAndroid Build Coastguard Worker 				 "Error: shmat: shmid = %d, errno = %d",
102*49cdfc7eSAndroid Build Coastguard Worker 				 shmid, errno);
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 			/* kill the child if parent failed to do the attch */
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 			kill(pid, SIGINT);
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 			/* remove shared memory segment */
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 			rm_shm(shmid);
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 			tst_exit();
113*49cdfc7eSAndroid Build Coastguard Worker 		}
114*49cdfc7eSAndroid Build Coastguard Worker 		*cp = 'A';
115*49cdfc7eSAndroid Build Coastguard Worker 		*(cp + 1) = 'B';
116*49cdfc7eSAndroid Build Coastguard Worker 		*(cp + 2) = 'C';
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 		kill(pid, SIGUSR1);
119*49cdfc7eSAndroid Build Coastguard Worker 		while ((pid1 = wait(&status)) < 0 && (errno == EINTR)) ;
120*49cdfc7eSAndroid Build Coastguard Worker 		if (pid1 != pid) {
121*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Waited on the wrong child");
122*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
123*49cdfc7eSAndroid Build Coastguard Worker 				 "Error: wait_status = %d, pid1= %d", status,
124*49cdfc7eSAndroid Build Coastguard Worker 				 pid1);
125*49cdfc7eSAndroid Build Coastguard Worker 		}
126*49cdfc7eSAndroid Build Coastguard Worker 	}
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "shmget,shmat");
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker /*---------------------------------------------------------------*/
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	if (shmdt(cp) < 0) {
133*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "shmdt");
134*49cdfc7eSAndroid Build Coastguard Worker 	}
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "shmdt");
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker /*-------------------------------------------------------------*/
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	rm_shm(shmid);
141*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
142*49cdfc7eSAndroid Build Coastguard Worker }
143*49cdfc7eSAndroid Build Coastguard Worker 
child(void)144*49cdfc7eSAndroid Build Coastguard Worker int child(void)
145*49cdfc7eSAndroid Build Coastguard Worker {
146*49cdfc7eSAndroid Build Coastguard Worker 	int shmid, chld_pid;
147*49cdfc7eSAndroid Build Coastguard Worker 	char *cp;
148*49cdfc7eSAndroid Build Coastguard Worker 	int sig;
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	sigwait(&set, &sig);
151*49cdfc7eSAndroid Build Coastguard Worker 	chld_pid = getpid();
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker 	if ((shmid = shmget(key, SIZE, 0)) < 0) {
154*49cdfc7eSAndroid Build Coastguard Worker 		perror("shmget:child process");
155*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL,
156*49cdfc7eSAndroid Build Coastguard Worker 			 "Error: shmget: errno=%d, shmid=%d, child_pid=%d",
157*49cdfc7eSAndroid Build Coastguard Worker 			 errno, shmid, chld_pid);
158*49cdfc7eSAndroid Build Coastguard Worker 	} else {
159*49cdfc7eSAndroid Build Coastguard Worker 		cp = shmat(shmid, NULL, 0);
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker 		if (cp == (char *)-1) {
162*49cdfc7eSAndroid Build Coastguard Worker 			perror("shmat:child process");
163*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
164*49cdfc7eSAndroid Build Coastguard Worker 				 "Error: shmat: errno=%d, shmid=%d, child_pid=%d",
165*49cdfc7eSAndroid Build Coastguard Worker 				 errno, shmid, chld_pid);
166*49cdfc7eSAndroid Build Coastguard Worker 		} else {
167*49cdfc7eSAndroid Build Coastguard Worker 			if (*cp != 'A') {
168*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not A");
169*49cdfc7eSAndroid Build Coastguard Worker 			}
170*49cdfc7eSAndroid Build Coastguard Worker 			if (*(cp + 1) != 'B') {
171*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not B");
172*49cdfc7eSAndroid Build Coastguard Worker 			}
173*49cdfc7eSAndroid Build Coastguard Worker 			if (*(cp + 2) != 'C') {
174*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not C");
175*49cdfc7eSAndroid Build Coastguard Worker 			}
176*49cdfc7eSAndroid Build Coastguard Worker 			if (*(cp + 8192) != 0) {
177*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not 0");
178*49cdfc7eSAndroid Build Coastguard Worker 			}
179*49cdfc7eSAndroid Build Coastguard Worker 		}
180*49cdfc7eSAndroid Build Coastguard Worker 
181*49cdfc7eSAndroid Build Coastguard Worker 		/*
182*49cdfc7eSAndroid Build Coastguard Worker 		 * Attach the segment to a different addresse
183*49cdfc7eSAndroid Build Coastguard Worker 		 * and verify it's contents again.
184*49cdfc7eSAndroid Build Coastguard Worker 		 */
185*49cdfc7eSAndroid Build Coastguard Worker 		cp = shmat(shmid, NULL, 0);
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker 		if (cp == (char *)-1) {
188*49cdfc7eSAndroid Build Coastguard Worker 			perror("shmat:child process");
189*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
190*49cdfc7eSAndroid Build Coastguard Worker 				 "Error: shmat: errno=%d, shmid=%d, child_pid=%d",
191*49cdfc7eSAndroid Build Coastguard Worker 				 errno, shmid, chld_pid);
192*49cdfc7eSAndroid Build Coastguard Worker 		} else {
193*49cdfc7eSAndroid Build Coastguard Worker 			if (*cp != 'A') {
194*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not A");
195*49cdfc7eSAndroid Build Coastguard Worker 			}
196*49cdfc7eSAndroid Build Coastguard Worker 			if (*(cp + 1) != 'B') {
197*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not B");
198*49cdfc7eSAndroid Build Coastguard Worker 			}
199*49cdfc7eSAndroid Build Coastguard Worker 			if (*(cp + 2) != 'C') {
200*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not C");
201*49cdfc7eSAndroid Build Coastguard Worker 			}
202*49cdfc7eSAndroid Build Coastguard Worker 			if (*(cp + 8192) != 0) {
203*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "child: not 0");
204*49cdfc7eSAndroid Build Coastguard Worker 			}
205*49cdfc7eSAndroid Build Coastguard Worker 		}
206*49cdfc7eSAndroid Build Coastguard Worker 	}
207*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
208*49cdfc7eSAndroid Build Coastguard Worker }
209*49cdfc7eSAndroid Build Coastguard Worker 
rm_shm(int shmid)210*49cdfc7eSAndroid Build Coastguard Worker static int rm_shm(int shmid)
211*49cdfc7eSAndroid Build Coastguard Worker {
212*49cdfc7eSAndroid Build Coastguard Worker 	if (shmctl(shmid, IPC_RMID, NULL) == -1) {
213*49cdfc7eSAndroid Build Coastguard Worker 		perror("shmctl");
214*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL,
215*49cdfc7eSAndroid Build Coastguard Worker 			 NULL,
216*49cdfc7eSAndroid Build Coastguard Worker 			 "shmctl Failed to remove: shmid = %d, errno = %d",
217*49cdfc7eSAndroid Build Coastguard Worker 			 shmid, errno);
218*49cdfc7eSAndroid Build Coastguard Worker 	}
219*49cdfc7eSAndroid Build Coastguard Worker 	return (0);
220*49cdfc7eSAndroid Build Coastguard Worker }
221