blob: 9771ea10e78b5c6a1016087fb62c5db30dd8d42a [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
robbiewb0848882003-03-25 15:05:02 +000020/* 03/21/2003 enable ia64 Jacky.Malcles */
robbiew38f79112002-12-23 17:01:49 +000021/* 12/20/2002 Port to LTP robbiew@us.ibm.com */
22/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
23
24/*
25 * NAME
robbiewb0848882003-03-25 15:05:02 +000026 * shmt04
robbiew38f79112002-12-23 17:01:49 +000027 *
28 * CALLS
robbiewb0848882003-03-25 15:05:02 +000029 * shmctl(2) shmget(2) shmat(2)
robbiew38f79112002-12-23 17:01:49 +000030 *
31 * ALGORITHM
32 * Parent process forks a child. Child pauses until parent has created
33 * a shared memory segment, attached to it and written to it too. At that
vapier2e2af472007-03-13 20:15:16 +000034 * time child gets the shared memory segment id, attaches to it and
robbiew38f79112002-12-23 17:01:49 +000035 * verifies that its contents are the same as the contents of the
36 * parent attached segment.
37 *
38 */
39
robbiew38f79112002-12-23 17:01:49 +000040#include <stdio.h>
41#include <sys/types.h>
42#include <sys/ipc.h>
43#include <sys/shm.h>
44#include <sys/wait.h>
robbiewb0848882003-03-25 15:05:02 +000045#include <sys/utsname.h>
robbiew38f79112002-12-23 17:01:49 +000046#include <signal.h>
47#include <errno.h>
48#include <stdlib.h>
49#include <unistd.h>
50
51/** LTP Port **/
52#include "test.h"
robbiew38f79112002-12-23 17:01:49 +000053
vapier2e2af472007-03-13 20:15:16 +000054char *TCID = "shmt04"; /* Test program identifier. */
55int TST_TOTAL = 2; /* Total number of test cases. */
robbiew38f79112002-12-23 17:01:49 +000056/**************/
57
robbiewb0848882003-03-25 15:05:02 +000058key_t key;
robbiew95157a92003-01-08 21:43:27 +000059sigset_t sigset;
robbiew38f79112002-12-23 17:01:49 +000060
robbiewb0848882003-03-25 15:05:02 +000061#define SIZE 16*1024
robbiew38f79112002-12-23 17:01:49 +000062
robbiew38f79112002-12-23 17:01:49 +000063int child();
64int rm_shm(int);
65
66int main()
67{
vapier2e2af472007-03-13 20:15:16 +000068 char *cp = NULL;
69 int pid, pid1, shmid;
70 int status;
robbiew38f79112002-12-23 17:01:49 +000071
vapier2e2af472007-03-13 20:15:16 +000072 key = (key_t) getpid();
robbiewb0848882003-03-25 15:05:02 +000073
vapier2e2af472007-03-13 20:15:16 +000074 sigemptyset(&sigset);
75 sigaddset(&sigset, SIGUSR1);
76 sigprocmask(SIG_BLOCK, &sigset, NULL);
77
78 pid = fork();
79 switch (pid) {
80 case -1:
Cyril Hrubis526fdf82014-12-04 14:35:01 +010081 tst_brkm(TBROK, NULL, "fork failed");
vapier2e2af472007-03-13 20:15:16 +000082 case 0:
83 child();
84 }
robbiew38f79112002-12-23 17:01:49 +000085
86/*----------------------------------------------------------*/
87
vapier2e2af472007-03-13 20:15:16 +000088 if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
89 perror("shmget");
90 tst_resm(TFAIL, "Error: shmget: shmid = %d, errno = %d\n",
91 shmid, errno);
92 /*
93 * kill the child if parent failed to do the attach
94 */
95 (void)kill(pid, SIGINT);
96 } else {
Cyril Hrubisd2db4802014-09-24 17:08:17 +020097 cp = shmat(shmid, NULL, 0);
robbiewc866f1d2003-07-29 19:46:14 +000098
vapier2e2af472007-03-13 20:15:16 +000099 if (cp == (char *)-1) {
100 perror("shmat");
101 tst_resm(TFAIL,
102 "Error: shmat: shmid = %d, errno = %d\n",
103 shmid, errno);
robbiew38f79112002-12-23 17:01:49 +0000104
robbiewb0848882003-03-25 15:05:02 +0000105/* kill the child if parent failed to do the attch */
robbiew38f79112002-12-23 17:01:49 +0000106
vapier2e2af472007-03-13 20:15:16 +0000107 kill(pid, SIGINT);
robbiew38f79112002-12-23 17:01:49 +0000108
robbiewb0848882003-03-25 15:05:02 +0000109/* remove shared memory segment */
robbiew38f79112002-12-23 17:01:49 +0000110
vapier2e2af472007-03-13 20:15:16 +0000111 rm_shm(shmid);
robbiew38f79112002-12-23 17:01:49 +0000112
vapier2e2af472007-03-13 20:15:16 +0000113 tst_exit();
114 }
115 *cp = 'A';
116 *(cp + 1) = 'B';
117 *(cp + 2) = 'C';
robbiew38f79112002-12-23 17:01:49 +0000118
vapier2e2af472007-03-13 20:15:16 +0000119 kill(pid, SIGUSR1);
120 while ((pid1 = wait(&status)) < 0 && (errno == EINTR)) ;
121 if (pid1 != pid) {
122 tst_resm(TFAIL, "Waited on the wrong child");
123 tst_resm(TFAIL,
124 "Error: wait_status = %d, pid1= %d\n", status,
125 pid1);
126 }
127 }
robbiew38f79112002-12-23 17:01:49 +0000128
vapier2e2af472007-03-13 20:15:16 +0000129 tst_resm(TPASS, "shmget,shmat");
robbiew38f79112002-12-23 17:01:49 +0000130
131/*----------------------------------------------------------*/
132
vapier2e2af472007-03-13 20:15:16 +0000133 if (shmdt(cp) < 0) {
134 tst_resm(TFAIL, "shmdt");
135 }
robbiew38f79112002-12-23 17:01:49 +0000136
vapier2e2af472007-03-13 20:15:16 +0000137 tst_resm(TPASS, "shmdt");
robbiew38f79112002-12-23 17:01:49 +0000138
139/*----------------------------------------------------------*/
140
vapier2e2af472007-03-13 20:15:16 +0000141 rm_shm(shmid);
142 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000143}
144
145int child()
146{
vapier2e2af472007-03-13 20:15:16 +0000147 int shmid, chld_pid;
148 char *cp;
149 int sig;
robbiew38f79112002-12-23 17:01:49 +0000150
vapier2e2af472007-03-13 20:15:16 +0000151 sigwait(&sigset, &sig);
152 chld_pid = getpid();
robbiew38f79112002-12-23 17:01:49 +0000153/*--------------------------------------------------------*/
154
vapier2e2af472007-03-13 20:15:16 +0000155 if ((shmid = shmget(key, SIZE, 0)) < 0) {
156 perror("shmget:child process");
157 tst_resm(TFAIL,
158 "Error: shmget: errno=%d, shmid=%d, child_pid=%d\n",
159 errno, shmid, chld_pid);
160 } else {
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200161 cp = shmat(shmid, NULL, 0);
subrata_modak78b19122008-05-15 06:54:17 +0000162
vapier2e2af472007-03-13 20:15:16 +0000163 if (cp == (char *)-1) {
164 perror("shmat:child process");
165 tst_resm(TFAIL,
166 "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n",
167 errno, shmid, chld_pid);
168 } else {
169 if (*cp != 'A') {
170 tst_resm(TFAIL, "child: not A\n");
171 }
172 if (*(cp + 1) != 'B') {
173 tst_resm(TFAIL, "child: not B\n");
174 }
175 if (*(cp + 2) != 'C') {
176 tst_resm(TFAIL, "child: not C\n");
177 }
178 if (*(cp + 8192) != 0) {
179 tst_resm(TFAIL, "child: not 0\n");
180 }
181 }
robbiew38f79112002-12-23 17:01:49 +0000182
vapier2e2af472007-03-13 20:15:16 +0000183 }
184 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000185}
186
187int rm_shm(shmid)
vapier2e2af472007-03-13 20:15:16 +0000188int shmid;
robbiew38f79112002-12-23 17:01:49 +0000189{
vapier2e2af472007-03-13 20:15:16 +0000190 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
191 perror("shmctl");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100192 tst_brkm(TFAIL,
193 NULL,
vapier2e2af472007-03-13 20:15:16 +0000194 "shmctl Failed to remove: shmid = %d, errno = %d\n",
195 shmid, errno);
vapier2e2af472007-03-13 20:15:16 +0000196 }
197 return (0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700198}