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