VM: simplify slab allocator
[minix.git] / test / ipc / shmt / shmt07.c
blob1342607da233151fed34f9888dfba98c07a2116f
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 * shmt07
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2)
30 * ALGORITHM
31 * Create and attach a shared memory segment, write to it
32 * and then fork a child. The child Verifies that the shared memory segment
33 * that it inherited from the parent conatins the same data that was originally
34 * written to it by the parent.
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 <errno.h>
46 #include <stdlib.h>
47 #include <unistd.h>
49 #define ADDR (void *)0x80000
50 #define ADDR_IA (void *)0x40000000
51 #define SIZE 16*1024
53 /** LTP Port **/
54 #include "test.h"
55 #include "usctest.h"
58 char *TCID="shmt07"; /* Test program identifier. */
59 int TST_TOTAL=2; /* Total number of test cases. */
60 extern int Tst_count; /* Test Case counter for tst_* routines */
61 /**************/
63 int child(void);
64 int rm_shm(int);
66 int main(void)
68 char *cp=NULL;
69 int shmid, pid, status;
70 key_t key;
72 key = (key_t) getpid() ;
74 /*---------------------------------------------------------*/
76 errno = 0;
78 if ((shmid = shmget(key, SIZE, IPC_CREAT|0666)) < 0) {
79 perror("shmget");
80 tst_resm(TFAIL,"Error: shmget: shmid = %d, errno = %d\n",
81 shmid, errno) ;
82 tst_exit() ;
85 #ifdef __ia64__
86 cp = (char *) shmat(shmid, ADDR_IA, 0);
87 #elif defined(__ARM_ARCH_4T__) || defined(__minix)
88 cp = (char *) shmat(shmid, (void *) NULL, 0);
89 #else
90 cp = (char *) shmat(shmid, ADDR, 0);
91 #endif
92 if (cp == (char *)-1) {
93 perror("shmat");
94 tst_resm(TFAIL,
95 "Error: shmat: shmid = %d, errno = %d\n",
96 shmid, errno) ;
97 rm_shm(shmid) ;
98 tst_exit() ;
101 *cp = '1';
102 *(cp+1) = '2';
104 tst_resm(TPASS,"shmget,shmat");
106 /*-------------------------------------------------------*/
109 pid = fork() ;
110 switch (pid) {
111 case -1 :
112 tst_resm(TBROK,"fork failed");
113 tst_exit() ;
115 case 0 :
116 if (*cp != '1') {
117 tst_resm(TFAIL, "Error: not 1\n");
119 if (*(cp+1) != '2') {
120 tst_resm(TFAIL, "Error: not 2\n");
122 tst_exit() ;
125 /* parent */
126 while( wait(&status) < 0 && errno == EINTR ) ;
128 tst_resm(TPASS,"cp & cp+1 correct") ;
130 /*-----------------------------------------------------------*/
131 rm_shm(shmid) ;
132 tst_exit() ;
133 /*-----------------------------------------------------------*/
134 return(0);
137 int rm_shm(shmid)
138 int shmid ;
140 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
141 perror("shmctl");
142 tst_resm(TFAIL,
143 "shmctl Failed to remove: shmid = %d, errno = %d\n",
144 shmid, errno) ;
145 tst_exit();
147 return(0);