VM: simplify slab allocator
[minix.git] / test / ipc / shmt / shmt04.c
blobf2324f1875eae379ebba98e0300b17d7bb9053ff
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 /* 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 */
25 * NAME
26 * shmt04
28 * CALLS
29 * shmctl(2) shmget(2) shmat(2)
31 * ALGORITHM
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.
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <sys/ipc.h>
44 #include <sys/shm.h>
45 #include <sys/wait.h>
46 #include <sys/utsname.h>
47 #include <signal.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <unistd.h>
52 /** LTP Port **/
53 #include "test.h"
54 #include "usctest.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 */
60 /**************/
63 key_t key;
64 sigset_t mysigset;
66 #define ADDR1 (void *)0x40000000
67 #define ADDR (void *)0x80000
68 #define SIZE 16*1024
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, ADDR1, 0);
112 #elif defined(__ARM_ARCH_4T__) || defined(__minix)
113 cp = (char *) shmat(shmid, NULL, 0);
114 #else
115 cp = (char *) shmat(shmid, ADDR, 0);
116 #endif
118 if (cp == (char *)-1) {
119 perror("shmat");
120 tst_resm(TFAIL,
121 "Error: shmat: shmid = %d, errno = %d\n",
122 shmid, errno) ;
124 /* kill the child if parent failed to do the attch */
126 kill(pid, SIGINT) ;
128 /* remove shared memory segment */
130 rm_shm(shmid) ;
132 tst_exit() ;
134 *cp = 'A';
135 *(cp+1) = 'B';
136 *(cp+2) = 'C';
138 kill(pid, SIGUSR1);
139 while ( (pid1 = wait(&status)) < 0 &&
140 (errno == EINTR) ) ;
141 if (pid1 != pid) {
142 tst_resm(TFAIL,"Waited on the wrong child") ;
143 tst_resm(TFAIL,
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 /*----------------------------------------------------------*/
161 rm_shm(shmid) ;
162 tst_exit() ;
164 /*----------------------------------------------------------*/
165 return(0);
168 int child(void)
170 int shmid,
171 chld_pid ;
172 char *cp;
174 sigemptyset(&mysigset);
175 sigsuspend(&mysigset);
176 chld_pid = getpid() ;
177 /*--------------------------------------------------------*/
180 if ((shmid = shmget(key, SIZE, 0)) < 0) {
181 perror("shmget:child process");
182 tst_resm(TFAIL,
183 "Error: shmget: errno=%d, shmid=%d, child_pid=%d\n",
184 errno, shmid, chld_pid);
186 else
188 #ifdef __ia64__
189 cp = (char *) shmat(shmid, ADDR1, 0);
190 #elif defined(__ARM_ARCH_4T__) || defined(__minix)
191 cp = (char *) shmat(shmid, NULL, 0);
192 #else
193 cp = (char *) shmat(shmid, ADDR, 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);