3 * Copyright (c) International Business Machines Corp., 2002
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* 03/21/2003 enable ia64 Jacky.Malcles */
21 /* 12/20/2002 Port to LTP robbiew@us.ibm.com */
22 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
29 * shmctl(2) shmget(2) shmat(2)
32 * Parent process forks a child. Child pauses until parent has created
33 * a shared memory segment, attached to it and written to it too. At that
34 * time child gets the shared memory segment id, attaches to it and
35 * verifies that its contents are the same as the contents of the
36 * parent attached segment.
42 #include <sys/types.h>
46 #include <sys/utsname.h>
57 char *TCID
="shmt04"; /* Test program identifier. */
58 int TST_TOTAL
=2; /* Total number of test cases. */
59 extern int Tst_count
; /* Test Case counter for tst_* routines */
66 #define ADDR1 (void *)0x40000000
67 #define ADDR (void *)0x80000
80 key
= (key_t
) getpid() ;
82 signal(SIGUSR1
, SIG_DFL
);
84 sigemptyset(&mysigset
);
85 sigaddset(&mysigset
,SIGUSR1
);
86 sigprocmask(SIG_BLOCK
,&mysigset
,NULL
);
91 tst_resm(TBROK
,"fork failed");
97 /*----------------------------------------------------------*/
100 if ((shmid
= shmget(key
, SIZE
, IPC_CREAT
|0666)) < 0 ) {
102 tst_resm(TFAIL
,"Error: shmget: shmid = %d, errno = %d\n",
105 * kill the child if parent failed to do the attach
107 (void)kill(pid
, SIGINT
);
111 cp
= (char *) shmat(shmid
, ADDR1
, 0);
112 #elif defined(__ARM_ARCH_4T__) || defined(__minix)
113 cp
= (char *) shmat(shmid
, NULL
, 0);
115 cp
= (char *) shmat(shmid
, ADDR
, 0);
118 if (cp
== (char *)-1) {
121 "Error: shmat: shmid = %d, errno = %d\n",
124 /* kill the child if parent failed to do the attch */
128 /* remove shared memory segment */
139 while ( (pid1
= wait(&status
)) < 0 &&
142 tst_resm(TFAIL
,"Waited on the wrong child") ;
144 "Error: wait_status = %d, pid1= %d\n", status
, pid1
) ;
148 tst_resm(TPASS
,"shmget,shmat");
150 /*----------------------------------------------------------*/
153 if (shmdt(cp
) < 0 ) {
154 tst_resm(TFAIL
,"shmdt");
157 tst_resm(TPASS
,"shmdt");
159 /*----------------------------------------------------------*/
164 /*----------------------------------------------------------*/
174 sigemptyset(&mysigset
);
175 sigsuspend(&mysigset
);
176 chld_pid
= getpid() ;
177 /*--------------------------------------------------------*/
180 if ((shmid
= shmget(key
, SIZE
, 0)) < 0) {
181 perror("shmget:child process");
183 "Error: shmget: errno=%d, shmid=%d, child_pid=%d\n",
184 errno
, shmid
, chld_pid
);
189 cp
= (char *) shmat(shmid
, ADDR1
, 0);
190 #elif defined(__ARM_ARCH_4T__) || defined(__minix)
191 cp
= (char *) shmat(shmid
, NULL
, 0);
193 cp
= (char *) shmat(shmid
, ADDR
, 0);
195 if (cp
== (char *)-1) {
196 perror("shmat:child process");
198 "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n",
199 errno
, shmid
, chld_pid
);
202 tst_resm(TFAIL
,"child: not A\n");
204 if (*(cp
+1) != 'B') {
205 tst_resm(TFAIL
,"child: not B\n");
207 if (*(cp
+2) != 'C') {
208 tst_resm(TFAIL
,"child: not C\n");
210 if (*(cp
+8192) != 0) {
211 tst_resm(TFAIL
,"child: not 0\n");
223 if (shmctl(shmid
, IPC_RMID
, NULL
) == -1) {
226 "shmctl Failed to remove: shmid = %d, errno = %d\n",