blob: 5c9eb64a2dcc708eb8f09339160918eedcf33420 [file] [log] [blame]
robbiew38f79112002-12-23 17:01:49 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew38f79112002-12-23 17:01:49 +000018 */
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 * shmt06
26 *
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2)
29 *
30 * ALGORITHM
31 * Parent process forks a child. Child pauses until parent has created
32 * a shared memory segment, attached to it and written to it too. At that
subrata_modakee2924a2008-05-19 06:40:38 +000033 * time child gets the shared memory segment id, attaches to it at two
34 * different addresses than the parents and verifies that their contents
35 * are the same as the contents of the parent attached segment.
robbiew38f79112002-12-23 17:01:49 +000036 *
37 */
38
39#include <stdio.h>
40#include <sys/types.h>
41#include <sys/wait.h>
42#include <sys/ipc.h>
43#include <sys/shm.h>
robbiewb0848882003-03-25 15:05:02 +000044#include <sys/utsname.h>
robbiew38f79112002-12-23 17:01:49 +000045#include <signal.h>
46#include <errno.h>
47#include <stdlib.h>
48#include <unistd.h>
49
robbiew38f79112002-12-23 17:01:49 +000050#define SIZE 16*1024
51
52/** LTP Port **/
53#include "test.h"
robbiew38f79112002-12-23 17:01:49 +000054
vapier2e2af472007-03-13 20:15:16 +000055char *TCID = "shmt06"; /* Test program identifier. */
56int TST_TOTAL = 2; /* Total number of test cases. */
robbiew38f79112002-12-23 17:01:49 +000057/**************/
58
vapier2e2af472007-03-13 20:15:16 +000059key_t key;
robbiew95157a92003-01-08 21:43:27 +000060sigset_t sigset;
robbiew38f79112002-12-23 17:01:49 +000061
62int child();
63int rm_shm(int);
64
65int main()
66{
vapier2e2af472007-03-13 20:15:16 +000067 char *cp = NULL;
68 int pid, pid1, shmid;
69 int status;
70
71 key = (key_t) getpid();
robbiew38f79112002-12-23 17:01:49 +000072
robbiew95157a92003-01-08 21:43:27 +000073 sigemptyset(&sigset);
vapier2e2af472007-03-13 20:15:16 +000074 sigaddset(&sigset, SIGUSR1);
75 sigprocmask(SIG_BLOCK, &sigset, NULL);
robbiew38f79112002-12-23 17:01:49 +000076
77 pid = fork();
78 switch (pid) {
79 case -1:
Cyril Hrubis526fdf82014-12-04 14:35:01 +010080 tst_brkm(TBROK, NULL, "fork failed");
robbiew38f79112002-12-23 17:01:49 +000081 case 0:
82 child();
83 }
84
85/*------------------------------------------------------*/
86
vapier2e2af472007-03-13 20:15:16 +000087 if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
robbiew38f79112002-12-23 17:01:49 +000088 perror("shmget");
vapier2e2af472007-03-13 20:15:16 +000089 tst_resm(TFAIL, "Error: shmget: shmid = %d, errno = %d\n",
90 shmid, errno);
robbiew38f79112002-12-23 17:01:49 +000091 /*
92 * kill the child if parent failed to do the attach
93 */
94 (void)kill(pid, SIGINT);
vapier2e2af472007-03-13 20:15:16 +000095 } else {
Cyril Hrubisd2db4802014-09-24 17:08:17 +020096 cp = shmat(shmid, NULL, 0);
subrata_modak78b19122008-05-15 06:54:17 +000097
robbiew38f79112002-12-23 17:01:49 +000098 if (cp == (char *)-1) {
vapier2e2af472007-03-13 20:15:16 +000099 perror("shmat");
robbiew38f79112002-12-23 17:01:49 +0000100 tst_resm(TFAIL,
vapier2e2af472007-03-13 20:15:16 +0000101 "Error: shmat: shmid = %d, errno = %d\n",
102 shmid, errno);
robbiew38f79112002-12-23 17:01:49 +0000103
vapier2e2af472007-03-13 20:15:16 +0000104 /* kill the child if parent failed to do the attch */
robbiew38f79112002-12-23 17:01:49 +0000105
vapier2e2af472007-03-13 20:15:16 +0000106 kill(pid, SIGINT);
robbiew38f79112002-12-23 17:01:49 +0000107
vapier2e2af472007-03-13 20:15:16 +0000108 /* remove shared memory segment */
robbiew38f79112002-12-23 17:01:49 +0000109
vapier2e2af472007-03-13 20:15:16 +0000110 rm_shm(shmid);
robbiew38f79112002-12-23 17:01:49 +0000111
vapier2e2af472007-03-13 20:15:16 +0000112 tst_exit();
113 }
114 *cp = 'A';
115 *(cp + 1) = 'B';
116 *(cp + 2) = 'C';
robbiew38f79112002-12-23 17:01:49 +0000117
118 kill(pid, SIGUSR1);
vapier2e2af472007-03-13 20:15:16 +0000119 while ((pid1 = wait(&status)) < 0 && (errno == EINTR)) ;
robbiew38f79112002-12-23 17:01:49 +0000120 if (pid1 != pid) {
vapier2e2af472007-03-13 20:15:16 +0000121 tst_resm(TFAIL, "Waited on the wrong child");
robbiew38f79112002-12-23 17:01:49 +0000122 tst_resm(TFAIL,
vapier2e2af472007-03-13 20:15:16 +0000123 "Error: wait_status = %d, pid1= %d\n", status,
124 pid1);
robbiew38f79112002-12-23 17:01:49 +0000125 }
126 }
127
vapier2e2af472007-03-13 20:15:16 +0000128 tst_resm(TPASS, "shmget,shmat");
robbiew38f79112002-12-23 17:01:49 +0000129
130/*---------------------------------------------------------------*/
131
robbiew38f79112002-12-23 17:01:49 +0000132 if (shmdt(cp) < 0) {
vapier2e2af472007-03-13 20:15:16 +0000133 tst_resm(TFAIL, "shmdt");
robbiew38f79112002-12-23 17:01:49 +0000134 }
135
vapier2e2af472007-03-13 20:15:16 +0000136 tst_resm(TPASS, "shmdt");
robbiew38f79112002-12-23 17:01:49 +0000137
138/*-------------------------------------------------------------*/
139
vapier2e2af472007-03-13 20:15:16 +0000140 rm_shm(shmid);
141 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000142}
143
144int child()
145{
vapier2e2af472007-03-13 20:15:16 +0000146 int shmid, chld_pid;
147 char *cp;
robbiew95157a92003-01-08 21:43:27 +0000148 int sig;
robbiew38f79112002-12-23 17:01:49 +0000149
robbiew95157a92003-01-08 21:43:27 +0000150 sigwait(&sigset, &sig);
vapier2e2af472007-03-13 20:15:16 +0000151 chld_pid = getpid();
152
robbiew38f79112002-12-23 17:01:49 +0000153 if ((shmid = shmget(key, SIZE, 0)) < 0) {
154 perror("shmget:child process");
155 tst_resm(TFAIL,
vapier2e2af472007-03-13 20:15:16 +0000156 "Error: shmget: errno=%d, shmid=%d, child_pid=%d\n",
157 errno, shmid, chld_pid);
158 } else {
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200159 cp = shmat(shmid, NULL, 0);
subrata_modak78b19122008-05-15 06:54:17 +0000160
robbiew38f79112002-12-23 17:01:49 +0000161 if (cp == (char *)-1) {
162 perror("shmat:child process");
163 tst_resm(TFAIL,
vapier2e2af472007-03-13 20:15:16 +0000164 "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n",
165 errno, shmid, chld_pid);
robbiew38f79112002-12-23 17:01:49 +0000166 } else {
167 if (*cp != 'A') {
vapier2e2af472007-03-13 20:15:16 +0000168 tst_resm(TFAIL, "child: not A\n");
robbiew38f79112002-12-23 17:01:49 +0000169 }
vapier2e2af472007-03-13 20:15:16 +0000170 if (*(cp + 1) != 'B') {
171 tst_resm(TFAIL, "child: not B\n");
robbiew38f79112002-12-23 17:01:49 +0000172 }
vapier2e2af472007-03-13 20:15:16 +0000173 if (*(cp + 2) != 'C') {
174 tst_resm(TFAIL, "child: not C\n");
robbiew38f79112002-12-23 17:01:49 +0000175 }
vapier2e2af472007-03-13 20:15:16 +0000176 if (*(cp + 8192) != 0) {
177 tst_resm(TFAIL, "child: not 0\n");
robbiew38f79112002-12-23 17:01:49 +0000178 }
179 }
180
subrata_modakee2924a2008-05-19 06:40:38 +0000181 /*
182 * Attach the segment to a different addresse
183 * and verify it's contents again.
184 */
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200185 cp = shmat(shmid, NULL, 0);
subrata_modakee2924a2008-05-19 06:40:38 +0000186
187 if (cp == (char *)-1) {
188 perror("shmat:child process");
189 tst_resm(TFAIL,
190 "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n",
191 errno, shmid, chld_pid);
192 } else {
193 if (*cp != 'A') {
194 tst_resm(TFAIL, "child: not A\n");
195 }
196 if (*(cp + 1) != 'B') {
197 tst_resm(TFAIL, "child: not B\n");
198 }
199 if (*(cp + 2) != 'C') {
200 tst_resm(TFAIL, "child: not C\n");
201 }
202 if (*(cp + 8192) != 0) {
203 tst_resm(TFAIL, "child: not 0\n");
204 }
205 }
robbiew38f79112002-12-23 17:01:49 +0000206 }
vapier2e2af472007-03-13 20:15:16 +0000207 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000208}
209
210int rm_shm(shmid)
vapier2e2af472007-03-13 20:15:16 +0000211int shmid;
robbiew38f79112002-12-23 17:01:49 +0000212{
vapier2e2af472007-03-13 20:15:16 +0000213 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
214 perror("shmctl");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100215 tst_brkm(TFAIL,
216 NULL,
vapier2e2af472007-03-13 20:15:16 +0000217 "shmctl Failed to remove: shmid = %d, errno = %d\n",
218 shmid, errno);
vapier2e2af472007-03-13 20:15:16 +0000219 }
220 return (0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700221}