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 * shmt08
26 *
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2) shmdt(2)
29 *
30 * ALGORITHM
31 * Create a shared memory segment. Attach it twice at an address
32 * that is provided by the system. Detach the previously attached
33 * segments from the process.
34 *
35 */
36
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/ipc.h>
40 #include <sys/shm.h>
41 #include <errno.h>
42
43 #define K_1 1024
44
45 /** LTP Port **/
46 #include "test.h"
47
48 char *TCID = "shmt08"; /* Test program identifier. */
49 int TST_TOTAL = 2; /* Total number of test cases. */
50 /**************/
51
52 key_t key;
53
54 static int rm_shm(int);
55
main(void)56 int main(void)
57 {
58 char *cp = NULL, *cp1 = NULL;
59 int shmid;
60
61 key = (key_t) getpid();
62 errno = 0;
63 /*-------------------------------------------------------*/
64
65 if ((shmid = shmget(key, 24 * K_1, IPC_CREAT | 0666)) < 0) {
66 perror("shmget");
67 tst_brkm(TFAIL, NULL,
68 "Error: shmget: shmid = %d, errno = %d\n",
69 shmid, errno);
70 }
71
72 cp = shmat(shmid, NULL, 0);
73 if (cp == (char *)-1) {
74 tst_resm(TFAIL, "shmat1 Failed");
75 rm_shm(shmid);
76 tst_exit();
77 }
78
79 cp1 = shmat(shmid, NULL, 0);
80 if (cp1 == (char *)-1) {
81 perror("shmat2");
82 rm_shm(shmid);
83 tst_exit();
84 }
85
86 tst_resm(TPASS, "shmget,shmat");
87
88 /*--------------------------------------------------------*/
89
90 if (shmdt(cp) < 0) {
91 perror("shmdt2");
92 tst_resm(TFAIL, "shmdt:cp");
93 }
94
95 if (shmdt(cp1) < 0) {
96 perror("shmdt1");
97 tst_resm(TFAIL, "shmdt:cp1");
98 }
99
100 tst_resm(TPASS, "shmdt");
101
102 /*---------------------------------------------------------*/
103 rm_shm(shmid);
104 tst_exit();
105 }
106
rm_shm(int shmid)107 static int rm_shm(int shmid)
108 {
109 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
110 perror("shmctl");
111 tst_brkm(TFAIL,
112 NULL,
113 "shmctl Failed to remove: shmid = %d, errno = %d\n",
114 shmid, errno);
115 }
116 return (0);
117 }
118