• 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  *		 shmt05
26  *
27  * CALLS
28  *		 shmctl(2) shmget(2) shmat(2)
29  *
30  * ALGORITHM
31  * Create two shared memory segments and attach them to the same process
32  * at two different addresses. The addresses DO BUMP into each other.
33  * The second attach should Fail.
34  *
35  */
36 
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/ipc.h>
40 #include <sys/shm.h>
41 #include <sys/utsname.h>
42 #include <errno.h>
43 #include <time.h>
44 
45 /** LTP Port **/
46 #include "test.h"
47 
48 char *TCID = "shmt05";		/* Test program identifier.    */
49 int TST_TOTAL = 2;		/* Total number of test cases. */
50 /**************/
51 
52 key_t key[2];
53 
54 #define		 SIZE		 (2*SHMLBA)
55 
56 static int rm_shm(int);
57 
main(void)58 int main(void)
59 {
60 	int shmid, shmid1;
61 	char *cp, *cp1;
62 
63 	srand48((getpid() << 16) + (unsigned)time(NULL));
64 
65 	key[0] = (key_t) lrand48();
66 	key[1] = (key_t) lrand48();
67 
68 	cp = NULL;
69 	cp1 = NULL;
70 
71 /*--------------------------------------------------------*/
72 
73 	if ((shmid = shmget(key[0], SIZE, IPC_CREAT | 0666)) < 0) {
74 		perror("shmget");
75 		tst_resm(TFAIL,
76 			 "Error: shmget: shmid = %d, errno = %d\n",
77 			 shmid, errno);
78 	} else {
79 		cp = shmat(shmid, NULL, 0);
80 
81 		if (cp == (char *)-1) {
82 			tst_resm(TFAIL, "shmat");
83 			rm_shm(shmid);
84 		}
85 	}
86 
87 	tst_resm(TPASS, "shmget & shmat");
88 
89 /*--------------------------------------------------------*/
90 
91 	if ((shmid1 = shmget(key[1], SIZE, IPC_CREAT | 0666)) < 0) {
92 		perror("shmget2");
93 		tst_resm(TFAIL,
94 			 "Error: shmget: shmid1 = %d, errno = %d\n",
95 			 shmid1, errno);
96 	} else {
97 		cp1 = shmat(shmid1, cp + (SIZE / 2), 0);
98 		if (cp1 != (char *)-1) {
99 			perror("shmat");
100 			tst_resm(TFAIL,
101 				 "Error: shmat: shmid1 = %d, addr= %p, errno = %d\n",
102 				 shmid1, cp1, errno);
103 		} else {
104 			tst_resm(TPASS, "2nd shmget & shmat");
105 		}
106 	}
107 
108 /*------------------------------------------------------*/
109 
110 	rm_shm(shmid);
111 	rm_shm(shmid1);
112 
113 	tst_exit();
114 }
115 
rm_shm(int shmid)116 static int rm_shm(int shmid)
117 {
118 	if (shmctl(shmid, IPC_RMID, NULL) == -1) {
119 		perror("shmctl");
120 		tst_brkm(TFAIL,
121 			 NULL,
122 			 "shmctl Failed to remove: shmid = %d, errno = %d\n",
123 			 shmid, errno);
124 	}
125 	return (0);
126 }
127