• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2002
4  *
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.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /* 12/20/2002   Port to LTP     robbiew@us.ibm.com */
21 /* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
22 
23 /*
24  * NAME
25  *	shmt3
26  *
27  * CALLS
28  *	shmctl(2) shmget(2) shmat(2)
29  *
30  * ALGORITHM
31  * Create one shared memory segment and attach it twice to the same process,
32  * at an address that is chosen by the system. After the first attach has
33  * completed, write to it and then do the second attach.
34  * Verify that the doubly attached segment contains the same data.
35  *
36  */
37 
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/ipc.h>
41 #include <sys/shm.h>
42 #include <errno.h>
43 
44 /** LTP Port **/
45 #include "test.h"
46 
47 char *TCID = "shmt03";		/* Test program identifier.    */
48 int TST_TOTAL = 4;		/* Total number of test cases. */
49 /**************/
50 
51 #define		K_1 		1024
52 #define 	SUCCESSFUL	 1
53 
54 int first_attach, second_attach;
55 static int rm_shm(int);
56 
main(void)57 int main(void)
58 {
59 	char *cp1, *cp2;
60 	int shmid;
61 	key_t key;
62 
63 	key = (key_t) getpid();
64 	errno = 0;
65 
66 /*------------------------------------------------------------*/
67 
68 	if ((shmid = shmget(key, 16 * K_1, IPC_CREAT | 0666)) < 0) {
69 		perror("shmget");
70 		tst_brkm(TFAIL, NULL,
71 			 "shmget Failed: shmid = %d, errno = %d\n",
72 			 shmid, errno);
73 	}
74 
75 	tst_resm(TPASS, "shmget");
76 
77 /*------------------------------------------------------------*/
78 
79 	if ((cp1 = shmat(shmid, NULL, 0)) == (char *)-1) {
80 		perror("shmat");
81 		tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d\n",
82 			 shmid, errno);
83 	} else {
84 		*cp1 = '1';
85 		*(cp1 + 5 * K_1) = '2';
86 		first_attach = SUCCESSFUL;
87 	}
88 
89 	tst_resm(TPASS, "1st shmat");
90 
91 /*------------------------------------------------------------*/
92 
93 	if ((cp2 = shmat(shmid, NULL, 0)) == (char *)-1) {
94 		perror("shmat");
95 		tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d\n",
96 			 shmid, errno);
97 	} else {
98 		second_attach = SUCCESSFUL;
99 		if ((*cp2 != '1' || *(cp2 + 5 * K_1) != '2') &&
100 		    first_attach == SUCCESSFUL) {
101 			tst_resm(TFAIL, "Error: Shared memory contents\n");
102 		}
103 	}
104 
105 	tst_resm(TPASS, "2nd shmat");
106 
107 /*---------------------------------------------------------------*/
108 
109 	rm_shm(shmid);
110 
111 	if (first_attach && second_attach) {
112 		if (*cp2 != '1' || *(cp2 + 5 * K_1) != '2' ||
113 		    *cp1 != '1' || *(cp1 + 5 * K_1) != '2') {
114 			tst_resm(TFAIL, "Error: Shared memory contents\n");
115 		}
116 	}
117 
118 	tst_resm(TPASS, "Correct shared memory contents");
119 /*-----------------------------------------------------------------*/
120 	tst_exit();
121 }
122 
rm_shm(int shmid)123 static int rm_shm(int shmid)
124 {
125 	if (shmctl(shmid, IPC_RMID, NULL) == -1) {
126 		perror("shmctl");
127 		tst_brkm(TFAIL,
128 			 NULL,
129 			 "shmctl Failed to remove: shmid = %d, errno = %d\n",
130 			 shmid, errno);
131 	}
132 	return (0);
133 }
134