1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /* 12/20/2002 Port to LTP [email protected] */
21*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001 Port to Linux [email protected] */
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker /*
24*49cdfc7eSAndroid Build Coastguard Worker * NAME
25*49cdfc7eSAndroid Build Coastguard Worker * shmt07
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * CALLS
28*49cdfc7eSAndroid Build Coastguard Worker * shmctl(2) shmget(2) shmat(2)
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * ALGORITHM
31*49cdfc7eSAndroid Build Coastguard Worker * Create and attach a shared memory segment, write to it
32*49cdfc7eSAndroid Build Coastguard Worker * and then fork a child. The child verifies that the shared memory segment
33*49cdfc7eSAndroid Build Coastguard Worker * that it inherited from the parent contains the same data that was originally
34*49cdfc7eSAndroid Build Coastguard Worker * written to it by the parent.
35*49cdfc7eSAndroid Build Coastguard Worker *
36*49cdfc7eSAndroid Build Coastguard Worker */
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ipc.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/shm.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/utsname.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #define SIZE 16*1024
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
51*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "shmt07"; /* Test program identifier. */
54*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 2; /* Total number of test cases. */
55*49cdfc7eSAndroid Build Coastguard Worker /**************/
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker int child();
58*49cdfc7eSAndroid Build Coastguard Worker static int rm_shm(int);
59*49cdfc7eSAndroid Build Coastguard Worker
main(void)60*49cdfc7eSAndroid Build Coastguard Worker int main(void)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker char *cp = NULL;
63*49cdfc7eSAndroid Build Coastguard Worker int shmid, pid, status;
64*49cdfc7eSAndroid Build Coastguard Worker key_t key;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker key = (key_t) getpid();
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker /*---------------------------------------------------------*/
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
73*49cdfc7eSAndroid Build Coastguard Worker perror("shmget");
74*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, NULL,
75*49cdfc7eSAndroid Build Coastguard Worker "Error: shmget: shmid = %d, errno = %d",
76*49cdfc7eSAndroid Build Coastguard Worker shmid, errno);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker cp = shmat(shmid, NULL, 0);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker if (cp == (char *)-1) {
81*49cdfc7eSAndroid Build Coastguard Worker perror("shmat");
82*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
83*49cdfc7eSAndroid Build Coastguard Worker "Error: shmat: shmid = %d, errno = %d",
84*49cdfc7eSAndroid Build Coastguard Worker shmid, errno);
85*49cdfc7eSAndroid Build Coastguard Worker rm_shm(shmid);
86*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker *cp = '1';
90*49cdfc7eSAndroid Build Coastguard Worker *(cp + 1) = '2';
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "shmget,shmat");
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker /*-------------------------------------------------------*/
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker pid = fork();
97*49cdfc7eSAndroid Build Coastguard Worker switch (pid) {
98*49cdfc7eSAndroid Build Coastguard Worker case -1:
99*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, NULL, "fork failed");
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker case 0:
102*49cdfc7eSAndroid Build Coastguard Worker if (*cp != '1') {
103*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Error: not 1");
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker if (*(cp + 1) != '2') {
106*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Error: not 2");
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker /* parent */
112*49cdfc7eSAndroid Build Coastguard Worker while (wait(&status) < 0 && errno == EINTR) ;
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "cp & cp+1 correct");
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker /*-----------------------------------------------------------*/
117*49cdfc7eSAndroid Build Coastguard Worker rm_shm(shmid);
118*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker
rm_shm(int shmid)121*49cdfc7eSAndroid Build Coastguard Worker static int rm_shm(int shmid)
122*49cdfc7eSAndroid Build Coastguard Worker {
123*49cdfc7eSAndroid Build Coastguard Worker if (shmctl(shmid, IPC_RMID, NULL) == -1) {
124*49cdfc7eSAndroid Build Coastguard Worker perror("shmctl");
125*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL,
126*49cdfc7eSAndroid Build Coastguard Worker NULL,
127*49cdfc7eSAndroid Build Coastguard Worker "shmctl Failed to remove: shmid = %d, errno = %d",
128*49cdfc7eSAndroid Build Coastguard Worker shmid, errno);
129*49cdfc7eSAndroid Build Coastguard Worker }
130*49cdfc7eSAndroid Build Coastguard Worker return (0);
131*49cdfc7eSAndroid Build Coastguard Worker }
132