forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshmt04.c
More file actions
231 lines (190 loc) · 5.01 KB
/
Copy pathshmt04.c
File metadata and controls
231 lines (190 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
*
* Copyright (c) International Business Machines Corp., 2002
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* 03/21/2003 enable ia64 Jacky.Malcles */
/* 12/20/2002 Port to LTP robbiew@us.ibm.com */
/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
/*
* NAME
* shmt04
*
* CALLS
* shmctl(2) shmget(2) shmat(2)
*
* ALGORITHM
* Parent process forks a child. Child pauses until parent has created
* a shared memory segment, attached to it and written to it too. At that
* time child gets the shared memory segment id, attaches to it and
* verifies that its contents are the same as the contents of the
* parent attached segment.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
/** LTP Port **/
#include "test.h"
#include "usctest.h"
char *TCID="shmt04"; /* Test program identifier. */
int TST_TOTAL=2; /* Total number of test cases. */
extern int Tst_count; /* Test Case counter for tst_* routines */
/**************/
key_t key;
sigset_t mysigset;
#define ADDR1 (void *)0x40000000
#define ADDR (void *)0x80000
#define SIZE 16*1024
int child(void);
int rm_shm(int);
int main(void)
{
char *cp=NULL;
int pid, pid1, shmid;
int status;
key = (key_t) getpid() ;
signal(SIGUSR1, SIG_DFL);
sigemptyset(&mysigset);
sigaddset(&mysigset,SIGUSR1);
sigprocmask(SIG_BLOCK,&mysigset,NULL);
pid = fork();
switch (pid) {
case -1:
tst_resm(TBROK,"fork failed");
tst_exit() ;
case 0:
child();
}
/*----------------------------------------------------------*/
if ((shmid = shmget(key, SIZE, IPC_CREAT|0666)) < 0 ) {
perror("shmget");
tst_resm(TFAIL,"Error: shmget: shmid = %d, errno = %d\n",
shmid, errno) ;
/*
* kill the child if parent failed to do the attach
*/
(void)kill(pid, SIGINT);
}
else {
#ifdef __ia64__
cp = (char *) shmat(shmid, ADDR1, 0);
#elif defined(__ARM_ARCH_4T__)
cp = (char *) shmat(shmid, NULL, 0);
#else
cp = (char *) shmat(shmid, ADDR, 0);
#endif
if (cp == (char *)-1) {
perror("shmat");
tst_resm(TFAIL,
"Error: shmat: shmid = %d, errno = %d\n",
shmid, errno) ;
/* kill the child if parent failed to do the attch */
kill(pid, SIGINT) ;
/* remove shared memory segment */
rm_shm(shmid) ;
tst_exit() ;
}
*cp = 'A';
*(cp+1) = 'B';
*(cp+2) = 'C';
kill(pid, SIGUSR1);
while ( (pid1 = wait(&status)) < 0 &&
(errno == EINTR) ) ;
if (pid1 != pid) {
tst_resm(TFAIL,"Waited on the wrong child") ;
tst_resm(TFAIL,
"Error: wait_status = %d, pid1= %d\n", status, pid1) ;
}
}
tst_resm(TPASS,"shmget,shmat");
/*----------------------------------------------------------*/
if (shmdt(cp) < 0 ) {
tst_resm(TFAIL,"shmdt");
}
tst_resm(TPASS,"shmdt");
/*----------------------------------------------------------*/
rm_shm(shmid) ;
tst_exit() ;
/*----------------------------------------------------------*/
return(0);
}
int child(void)
{
int shmid,
chld_pid ;
char *cp;
sigemptyset(&mysigset);
sigsuspend(&mysigset);
chld_pid = getpid() ;
/*--------------------------------------------------------*/
if ((shmid = shmget(key, SIZE, 0)) < 0) {
perror("shmget:child process");
tst_resm(TFAIL,
"Error: shmget: errno=%d, shmid=%d, child_pid=%d\n",
errno, shmid, chld_pid);
}
else
{
#ifdef __ia64__
cp = (char *) shmat(shmid, ADDR1, 0);
#elif defined(__ARM_ARCH_4T__)
cp = (char *) shmat(shmid, NULL, 0);
#else
cp = (char *) shmat(shmid, ADDR, 0);
#endif
if (cp == (char *)-1) {
perror("shmat:child process");
tst_resm(TFAIL,
"Error: shmat: errno=%d, shmid=%d, child_pid=%d\n",
errno, shmid, chld_pid);
} else {
if (*cp != 'A') {
tst_resm(TFAIL,"child: not A\n");
}
if (*(cp+1) != 'B') {
tst_resm(TFAIL,"child: not B\n");
}
if (*(cp+2) != 'C') {
tst_resm(TFAIL,"child: not C\n");
}
if (*(cp+8192) != 0) {
tst_resm(TFAIL,"child: not 0\n");
}
}
}
tst_exit() ;
return(0);
}
int rm_shm(shmid)
int shmid ;
{
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl");
tst_resm(TFAIL,
"shmctl Failed to remove: shmid = %d, errno = %d\n",
shmid, errno) ;
tst_exit();
}
return(0);
}