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 * shmt07
26 *
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2)
29 *
30 * ALGORITHM
31 * Create and attach a shared memory segment, write to it
32 * and then fork a child. The child verifies that the shared memory segment
33 * that it inherited from the parent contains the same data that was originally
34 * written to it by the parent.
35 *
36 */
37
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43 #include <sys/utsname.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 #define SIZE 16*1024
49
50 /** LTP Port **/
51 #include "test.h"
52
53 char *TCID = "shmt07"; /* Test program identifier. */
54 int TST_TOTAL = 2; /* Total number of test cases. */
55 /**************/
56
57 int child();
58 static int rm_shm(int);
59
main(void)60 int main(void)
61 {
62 char *cp = NULL;
63 int shmid, pid, status;
64 key_t key;
65
66 key = (key_t) getpid();
67
68 /*---------------------------------------------------------*/
69
70 errno = 0;
71
72 if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
73 perror("shmget");
74 tst_brkm(TFAIL, NULL,
75 "Error: shmget: shmid = %d, errno = %d\n",
76 shmid, errno);
77 }
78 cp = shmat(shmid, NULL, 0);
79
80 if (cp == (char *)-1) {
81 perror("shmat");
82 tst_resm(TFAIL,
83 "Error: shmat: shmid = %d, errno = %d\n",
84 shmid, errno);
85 rm_shm(shmid);
86 tst_exit();
87 }
88
89 *cp = '1';
90 *(cp + 1) = '2';
91
92 tst_resm(TPASS, "shmget,shmat");
93
94 /*-------------------------------------------------------*/
95
96 pid = fork();
97 switch (pid) {
98 case -1:
99 tst_brkm(TBROK, NULL, "fork failed");
100
101 case 0:
102 if (*cp != '1') {
103 tst_resm(TFAIL, "Error: not 1\n");
104 }
105 if (*(cp + 1) != '2') {
106 tst_resm(TFAIL, "Error: not 2\n");
107 }
108 tst_exit();
109 }
110
111 /* parent */
112 while (wait(&status) < 0 && errno == EINTR) ;
113
114 tst_resm(TPASS, "cp & cp+1 correct");
115
116 /*-----------------------------------------------------------*/
117 rm_shm(shmid);
118 tst_exit();
119 }
120
rm_shm(int shmid)121 static int rm_shm(int shmid)
122 {
123 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
124 perror("shmctl");
125 tst_brkm(TFAIL,
126 NULL,
127 "shmctl Failed to remove: shmid = %d, errno = %d\n",
128 shmid, errno);
129 }
130 return (0);
131 }
132