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 * shmt02
26 *
27 * CALLS
28 * shmctl(2) shmget(2)
29 *
30 * ALGORITHM
31 * Create and attach a shared memory segment, write to it
32 * and then remove it. Verify that the shared memory segment
33 * is accessible as long as the process is still alive.
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
44 /** LTP Port **/
45 #include "test.h"
46
47 char *TCID = "shmt02"; /* Test program identifier. */
48 int TST_TOTAL = 3; /* Total number of test cases. */
49
50 /**************/
51
52 #define K_1 1024
53
54 static int rm_shm(int);
55
main(void)56 int main(void)
57 {
58 register int shmid;
59 char *cp;
60 key_t key;
61
62 errno = 0;
63 key = (key_t) getpid();
64
65 /*----------------------------------------------------------------*/
66
67 if ((shmid = shmget(key, 16 * K_1, IPC_CREAT | 0666)) < 0) {
68 perror("shmget");
69 tst_brkm(TFAIL, NULL,
70 "shmget Failed: shmid = %d, errno = %d\n",
71 shmid, errno);
72 }
73
74 tst_resm(TPASS, "shmget");
75
76 /*----------------------------------------------------------------*/
77
78 cp = shmat(shmid, NULL, 0);
79
80 if (cp == (char *)-1) {
81 perror("shmat");
82 tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d\n",
83 shmid, errno);
84 rm_shm(shmid);
85 tst_exit();
86 }
87
88 *cp = '1';
89 *(cp + 1) = '2';
90
91 tst_resm(TPASS, "shmat");
92
93 /*----------------------------------------------------------------*/
94
95 rm_shm(shmid);
96
97 if (*cp != '1' || *(cp + 1) != '2') {
98 tst_resm(TFAIL,
99 "Error in shared memory contents: shmid = %d\n",
100 shmid);
101 }
102
103 tst_resm(TPASS, "Correct shared memory contents");
104
105 /*------------------------------------------------------------------*/
106
107 tst_exit();
108 }
109
rm_shm(int shmid)110 static int rm_shm(int shmid)
111 {
112 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
113 perror("shmctl");
114 tst_brkm(TFAIL,
115 NULL,
116 "shmctl Failed to remove: shmid = %d, errno = %d\n",
117 shmid, errno);
118 }
119 return (0);
120 }
121