blob: 98589028ac9f53f0cfad7f5d16b5551f31626ee9 [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"
53#include "usctest.h"
54
vapier2e2af472007-03-13 20:15:16 +000055char *TCID = "shmt04"; /* Test program identifier. */
56int TST_TOTAL = 2; /* Total number of test cases. */
robbiew38f79112002-12-23 17:01:49 +000057/**************/
58
robbiewb0848882003-03-25 15:05:02 +000059key_t key;
robbiew95157a92003-01-08 21:43:27 +000060sigset_t sigset;
robbiew38f79112002-12-23 17:01:49 +000061
robbiewb0848882003-03-25 15:05:02 +000062#define SIZE 16*1024
robbiew38f79112002-12-23 17:01:49 +000063
robbiew38f79112002-12-23 17:01:49 +000064int child();
65int rm_shm(int);
66
67int main()
68{
vapier2e2af472007-03-13 20:15:16 +000069 char *cp = NULL;
70 int pid, pid1, shmid;
71 int status;
robbiew38f79112002-12-23 17:01:49 +000072
vapier2e2af472007-03-13 20:15:16 +000073 key = (key_t) getpid();
robbiewb0848882003-03-25 15:05:02 +000074
vapier2e2af472007-03-13 20:15:16 +000075 sigemptyset(&sigset);
76 sigaddset(&sigset, SIGUSR1);
77 sigprocmask(SIG_BLOCK, &sigset, NULL);
78
79 pid = fork();
80 switch (pid) {
81 case -1:
82 tst_resm(TBROK, "fork failed");
83 tst_exit();
84 case 0:
85 child();
86 }
robbiew38f79112002-12-23 17:01:49 +000087
88/*----------------------------------------------------------*/
89
vapier2e2af472007-03-13 20:15:16 +000090 if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
91 perror("shmget");
92 tst_resm(TFAIL, "Error: shmget: shmid = %d, errno = %d\n",
93 shmid, errno);
94 /*
95 * kill the child if parent failed to do the attach
96 */
97 (void)kill(pid, SIGINT);
98 } else {
Cyril Hrubisd2db4802014-09-24 17:08:17 +020099 cp = shmat(shmid, NULL, 0);
robbiewc866f1d2003-07-29 19:46:14 +0000100
vapier2e2af472007-03-13 20:15:16 +0000101 if (cp == (char *)-1) {
102 perror("shmat");
103 tst_resm(TFAIL,
104 "Error: shmat: shmid = %d, errno = %d\n",
105 shmid, errno);
robbiew38f79112002-12-23 17:01:49 +0000106
robbiewb0848882003-03-25 15:05:02 +0000107/* kill the child if parent failed to do the attch */
robbiew38f79112002-12-23 17:01:49 +0000108
vapier2e2af472007-03-13 20:15:16 +0000109 kill(pid, SIGINT);
robbiew38f79112002-12-23 17:01:49 +0000110
robbiewb0848882003-03-25 15:05:02 +0000111/* remove shared memory segment */
robbiew38f79112002-12-23 17:01:49 +0000112
vapier2e2af472007-03-13 20:15:16 +0000113 rm_shm(shmid);
robbiew38f79112002-12-23 17:01:49 +0000114
vapier2e2af472007-03-13 20:15:16 +0000115 tst_exit();
116 }
117 *cp = 'A';
118 *(cp + 1) = 'B';
119 *(cp + 2) = 'C';
robbiew38f79112002-12-23 17:01:49 +0000120
vapier2e2af472007-03-13 20:15:16 +0000121 kill(pid, SIGUSR1);
122 while ((pid1 = wait(&status)) < 0 && (errno == EINTR)) ;
123 if (pid1 != pid) {
124 tst_resm(TFAIL, "Waited on the wrong child");
125 tst_resm(TFAIL,
126 "Error: wait_status = %d, pid1= %d\n", status,
127 pid1);
128 }
129 }
robbiew38f79112002-12-23 17:01:49 +0000130
vapier2e2af472007-03-13 20:15:16 +0000131 tst_resm(TPASS, "shmget,shmat");
robbiew38f79112002-12-23 17:01:49 +0000132
133/*----------------------------------------------------------*/
134
vapier2e2af472007-03-13 20:15:16 +0000135 if (shmdt(cp) < 0) {
136 tst_resm(TFAIL, "shmdt");
137 }
robbiew38f79112002-12-23 17:01:49 +0000138
vapier2e2af472007-03-13 20:15:16 +0000139 tst_resm(TPASS, "shmdt");
robbiew38f79112002-12-23 17:01:49 +0000140
141/*----------------------------------------------------------*/
142
vapier2e2af472007-03-13 20:15:16 +0000143 rm_shm(shmid);
144 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000145}
146
147int child()
148{
vapier2e2af472007-03-13 20:15:16 +0000149 int shmid, chld_pid;
150 char *cp;
151 int sig;
robbiew38f79112002-12-23 17:01:49 +0000152
vapier2e2af472007-03-13 20:15:16 +0000153 sigwait(&sigset, &sig);
154 chld_pid = getpid();
robbiew38f79112002-12-23 17:01:49 +0000155/*--------------------------------------------------------*/
156
vapier2e2af472007-03-13 20:15:16 +0000157 if ((shmid = shmget(key, SIZE, 0)) < 0) {
158 perror("shmget:child process");
159 tst_resm(TFAIL,
160 "Error: shmget: errno=%d, shmid=%d, child_pid=%d\n",
161 errno, shmid, chld_pid);
162 } else {
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200163 cp = shmat(shmid, NULL, 0);
subrata_modak78b19122008-05-15 06:54:17 +0000164
vapier2e2af472007-03-13 20:15:16 +0000165 if (cp == (char *)-1) {
166 perror("shmat:child process");
167 tst_resm(TFAIL,
168 "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n",
169 errno, shmid, chld_pid);
170 } else {
171 if (*cp != 'A') {
172 tst_resm(TFAIL, "child: not A\n");
173 }
174 if (*(cp + 1) != 'B') {
175 tst_resm(TFAIL, "child: not B\n");
176 }
177 if (*(cp + 2) != 'C') {
178 tst_resm(TFAIL, "child: not C\n");
179 }
180 if (*(cp + 8192) != 0) {
181 tst_resm(TFAIL, "child: not 0\n");
182 }
183 }
robbiew38f79112002-12-23 17:01:49 +0000184
vapier2e2af472007-03-13 20:15:16 +0000185 }
186 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000187}
188
189int rm_shm(shmid)
vapier2e2af472007-03-13 20:15:16 +0000190int shmid;
robbiew38f79112002-12-23 17:01:49 +0000191{
vapier2e2af472007-03-13 20:15:16 +0000192 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
193 perror("shmctl");
194 tst_resm(TFAIL,
195 "shmctl Failed to remove: shmid = %d, errno = %d\n",
196 shmid, errno);
197 tst_exit();
198 }
199 return (0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700200}