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 * shmt3
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 one shared memory segment and attach it twice to the same process,
32*49cdfc7eSAndroid Build Coastguard Worker * at an address that is chosen by the system. After the first attach has
33*49cdfc7eSAndroid Build Coastguard Worker * completed, write to it and then do the second attach.
34*49cdfc7eSAndroid Build Coastguard Worker * Verify that the doubly attached segment contains the same data.
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/ipc.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/shm.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
45*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "shmt03"; /* Test program identifier. */
48*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 4; /* Total number of test cases. */
49*49cdfc7eSAndroid Build Coastguard Worker /**************/
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker #define K_1 1024
52*49cdfc7eSAndroid Build Coastguard Worker #define SUCCESSFUL 1
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker int first_attach, second_attach;
55*49cdfc7eSAndroid Build Coastguard Worker static int rm_shm(int);
56*49cdfc7eSAndroid Build Coastguard Worker
main(void)57*49cdfc7eSAndroid Build Coastguard Worker int main(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker char *cp1, *cp2;
60*49cdfc7eSAndroid Build Coastguard Worker int shmid;
61*49cdfc7eSAndroid Build Coastguard Worker key_t key;
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker key = (key_t) getpid();
64*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker /*------------------------------------------------------------*/
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker if ((shmid = shmget(key, 16 * K_1, IPC_CREAT | 0666)) < 0) {
69*49cdfc7eSAndroid Build Coastguard Worker perror("shmget");
70*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, NULL,
71*49cdfc7eSAndroid Build Coastguard Worker "shmget Failed: shmid = %d, errno = %d",
72*49cdfc7eSAndroid Build Coastguard Worker shmid, errno);
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "shmget");
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker /*------------------------------------------------------------*/
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker if ((cp1 = shmat(shmid, NULL, 0)) == (char *)-1) {
80*49cdfc7eSAndroid Build Coastguard Worker perror("shmat");
81*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d",
82*49cdfc7eSAndroid Build Coastguard Worker shmid, errno);
83*49cdfc7eSAndroid Build Coastguard Worker } else {
84*49cdfc7eSAndroid Build Coastguard Worker *cp1 = '1';
85*49cdfc7eSAndroid Build Coastguard Worker *(cp1 + 5 * K_1) = '2';
86*49cdfc7eSAndroid Build Coastguard Worker first_attach = SUCCESSFUL;
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "1st shmat");
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker /*------------------------------------------------------------*/
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker if ((cp2 = shmat(shmid, NULL, 0)) == (char *)-1) {
94*49cdfc7eSAndroid Build Coastguard Worker perror("shmat");
95*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d",
96*49cdfc7eSAndroid Build Coastguard Worker shmid, errno);
97*49cdfc7eSAndroid Build Coastguard Worker } else {
98*49cdfc7eSAndroid Build Coastguard Worker second_attach = SUCCESSFUL;
99*49cdfc7eSAndroid Build Coastguard Worker if ((*cp2 != '1' || *(cp2 + 5 * K_1) != '2') &&
100*49cdfc7eSAndroid Build Coastguard Worker first_attach == SUCCESSFUL) {
101*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Error: Shared memory contents");
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker }
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "2nd shmat");
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker /*---------------------------------------------------------------*/
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker rm_shm(shmid);
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker if (first_attach && second_attach) {
112*49cdfc7eSAndroid Build Coastguard Worker if (*cp2 != '1' || *(cp2 + 5 * K_1) != '2' ||
113*49cdfc7eSAndroid Build Coastguard Worker *cp1 != '1' || *(cp1 + 5 * K_1) != '2') {
114*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Error: Shared memory contents");
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "Correct shared memory contents");
119*49cdfc7eSAndroid Build Coastguard Worker /*-----------------------------------------------------------------*/
120*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker
rm_shm(int shmid)123*49cdfc7eSAndroid Build Coastguard Worker static int rm_shm(int shmid)
124*49cdfc7eSAndroid Build Coastguard Worker {
125*49cdfc7eSAndroid Build Coastguard Worker if (shmctl(shmid, IPC_RMID, NULL) == -1) {
126*49cdfc7eSAndroid Build Coastguard Worker perror("shmctl");
127*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL,
128*49cdfc7eSAndroid Build Coastguard Worker NULL,
129*49cdfc7eSAndroid Build Coastguard Worker "shmctl Failed to remove: shmid = %d, errno = %d",
130*49cdfc7eSAndroid Build Coastguard Worker shmid, errno);
131*49cdfc7eSAndroid Build Coastguard Worker }
132*49cdfc7eSAndroid Build Coastguard Worker return (0);
133*49cdfc7eSAndroid Build Coastguard Worker }
134