remove extra mkfs.1
[minix.git] / test / ipc / shmt / shmt06.c
blob116acade0770ca9094e0a6583cd451e0b570b9c7
1 /*
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 /* 12/20/2002 Port to LTP robbiew@us.ibm.com */
21 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
24 * NAME
25 * shmt06
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2)
30 * ALGORITHM
31 * Parent process forks a child. Child pauses until parent has created
32 * a shared memory segment, attached to it and written to it too. At that
33 * time child gets the shared memory segment id, attaches to it at a
34 * different address than the parents and verifies that its contents are
35 * the same as the contents of the parent attached segment.
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
42 #include <sys/ipc.h>
43 #include <sys/shm.h>
44 #include <sys/utsname.h>
45 #include <signal.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <unistd.h>
50 #define ADDR (void *)0x80000
51 #define ADDR1 (void *)0xA0000
52 #define ADDR_MIPS (void *)0x80000
53 #define ADDR1_MIPS (void *)0xC0000
54 #define ADDR_IA (void *)0x40000000
55 #define ADDR1_IA (void *)0x50000000
56 #define SIZE 16*1024
58 /** LTP Port **/
59 #include "test.h"
60 #include "usctest.h"
63 char *TCID="shmt06"; /* Test program identifier. */
64 int TST_TOTAL=2; /* Total number of test cases. */
65 extern int Tst_count; /* Test Case counter for tst_* routines */
66 /**************/
68 key_t key;
69 sigset_t mysigset;
71 int child(void);
72 int rm_shm(int);
74 int main(void)
76 char *cp=NULL;
77 int pid, pid1, shmid;
78 int status;
80 key = (key_t)getpid() ;
82 signal(SIGUSR1, SIG_DFL);
84 sigemptyset(&mysigset);
85 sigaddset(&mysigset,SIGUSR1);
86 sigprocmask(SIG_BLOCK,&mysigset,NULL);
88 pid = fork();
89 switch (pid) {
90 case -1:
91 tst_resm(TBROK,"fork failed");
92 tst_exit() ;
93 case 0:
94 child();
97 /*------------------------------------------------------*/
100 if ((shmid = shmget(key, SIZE, IPC_CREAT|0666)) < 0) {
101 perror("shmget");
102 tst_resm(TFAIL,"Error: shmget: shmid = %d, errno = %d\n",
103 shmid, errno) ;
105 * kill the child if parent failed to do the attach
107 (void)kill(pid, SIGINT);
109 else {
110 #ifdef __ia64__
111 cp = (char *) shmat(shmid, ADDR_IA, 0);
112 #elif defined(__ARM_ARCH_4T__)
113 cp = (char *) shmat(shmid, (void*) NULL, 0);
114 #elif defined(__mips__)
115 cp = (char *) shmat(shmid, ADDR_MIPS, 0);
116 #else
117 cp = (char *) shmat(shmid, ADDR, 0);
118 #endif
119 if (cp == (char *)-1) {
120 perror("shmat") ;
121 tst_resm(TFAIL,
122 "Error: shmat: shmid = %d, errno = %d\n",
123 shmid, errno) ;
125 /* kill the child if parent failed to do the attch */
127 kill(pid, SIGINT) ;
129 /* remove shared memory segment */
131 rm_shm(shmid) ;
133 tst_exit() ;
135 *cp = 'A';
136 *(cp+1) = 'B';
137 *(cp+2) = 'C';
139 kill(pid, SIGUSR1);
140 while ( (pid1 = wait(&status)) < 0 &&
141 (errno == EINTR) ) ;
142 if (pid1 != pid) {
143 tst_resm(TFAIL,"Waited on the wrong child") ;
144 tst_resm(TFAIL,
145 "Error: wait_status = %d, pid1= %d\n", status, pid1) ;
149 tst_resm(TPASS,"shmget,shmat");
151 /*---------------------------------------------------------------*/
154 if (shmdt(cp) < 0) {
155 tst_resm(TFAIL,"shmdt");
158 tst_resm(TPASS,"shmdt");
160 /*-------------------------------------------------------------*/
162 rm_shm(shmid) ;
163 tst_exit() ;
165 /*-----------------------------------------------------------*/
166 return(0);
169 int child(void)
171 int shmid ,
172 chld_pid ;
173 char *cp;
175 sigemptyset(&mysigset);
176 sigsuspend(&mysigset);
177 chld_pid = getpid() ;
179 if ((shmid = shmget(key, SIZE, 0)) < 0) {
180 perror("shmget:child process");
181 tst_resm(TFAIL,
182 "Error: shmget: errno=%d, shmid=%d, child_pid=%d\n",
183 errno, shmid, chld_pid);
185 else {
186 #ifdef __ia64__
187 cp = (char *) shmat(shmid, ADDR1_IA, 0);
188 #elif defined(__ARM_ARCH_4T__)
189 cp = (char *) shmat(shmid, (void *) NULL, 0);
190 #elif defined(__mips__)
191 cp = (char *) shmat(shmid, ADDR1_MIPS, 0);
192 #else
193 cp = (char *) shmat(shmid, ADDR1, 0);
194 #endif
195 if (cp == (char *)-1) {
196 perror("shmat:child process");
197 tst_resm(TFAIL,
198 "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n",
199 errno, shmid, chld_pid);
200 } else {
201 if (*cp != 'A') {
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");
216 tst_exit() ;
217 return(0);
220 int rm_shm(shmid)
221 int shmid ;
223 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
224 perror("shmctl");
225 tst_resm(TFAIL,
226 "shmctl Failed to remove: shmid = %d, errno = %d\n",
227 shmid, errno) ;
228 tst_exit();
230 return(0);