1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2002
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/* 03/21/2003   enable ia64     Jacky.Malcles */
21f08c3bdfSopenharmony_ci/* 12/20/2002   Port to LTP     robbiew@us.ibm.com */
22f08c3bdfSopenharmony_ci/* 06/30/2001   Port to Linux   nsharoff@us.ibm.com */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci/*
25f08c3bdfSopenharmony_ci * NAME
26f08c3bdfSopenharmony_ci *		 shmt04
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * CALLS
29f08c3bdfSopenharmony_ci *		 shmctl(2) shmget(2) shmat(2)
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * ALGORITHM
32f08c3bdfSopenharmony_ci * Parent process forks a child. Child pauses until parent has created
33f08c3bdfSopenharmony_ci * a shared memory segment, attached to it and written to it too. At that
34f08c3bdfSopenharmony_ci * time child gets the shared memory segment id, attaches to it and
35f08c3bdfSopenharmony_ci * verifies that its contents are the same as the contents of the
36f08c3bdfSopenharmony_ci * parent attached segment.
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci */
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci#include <stdio.h>
41f08c3bdfSopenharmony_ci#include <sys/types.h>
42f08c3bdfSopenharmony_ci#include <sys/ipc.h>
43f08c3bdfSopenharmony_ci#include <sys/shm.h>
44f08c3bdfSopenharmony_ci#include <sys/wait.h>
45f08c3bdfSopenharmony_ci#include <sys/utsname.h>
46f08c3bdfSopenharmony_ci#include <signal.h>
47f08c3bdfSopenharmony_ci#include <errno.h>
48f08c3bdfSopenharmony_ci#include <stdlib.h>
49f08c3bdfSopenharmony_ci#include <unistd.h>
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci/** LTP Port **/
52f08c3bdfSopenharmony_ci#include "test.h"
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cichar *TCID = "shmt04";		/* Test program identifier.    */
55f08c3bdfSopenharmony_ciint TST_TOTAL = 2;		/* Total number of test cases. */
56f08c3bdfSopenharmony_ci/**************/
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cikey_t key;
59f08c3bdfSopenharmony_cisigset_t set;
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci#define  SIZE  16*1024
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ciint child();
64f08c3bdfSopenharmony_cistatic int rm_shm(int);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ciint main(void)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	char *cp = NULL;
69f08c3bdfSopenharmony_ci	int pid, pid1, shmid;
70f08c3bdfSopenharmony_ci	int status;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	key = (key_t) getpid();
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	sigemptyset(&set);
75f08c3bdfSopenharmony_ci	sigaddset(&set, SIGUSR1);
76f08c3bdfSopenharmony_ci	sigprocmask(SIG_BLOCK, &set, NULL);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	pid = fork();
79f08c3bdfSopenharmony_ci	switch (pid) {
80f08c3bdfSopenharmony_ci	case -1:
81f08c3bdfSopenharmony_ci		tst_brkm(TBROK, NULL, "fork failed");
82f08c3bdfSopenharmony_ci	case 0:
83f08c3bdfSopenharmony_ci		child();
84f08c3bdfSopenharmony_ci	}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci/*----------------------------------------------------------*/
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
89f08c3bdfSopenharmony_ci		perror("shmget");
90f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "Error: shmget: shmid = %d, errno = %d",
91f08c3bdfSopenharmony_ci			 shmid, errno);
92f08c3bdfSopenharmony_ci		/*
93f08c3bdfSopenharmony_ci		 * kill the child if parent failed to do the attach
94f08c3bdfSopenharmony_ci		 */
95f08c3bdfSopenharmony_ci		(void)kill(pid, SIGINT);
96f08c3bdfSopenharmony_ci	} else {
97f08c3bdfSopenharmony_ci		cp = shmat(shmid, NULL, 0);
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci		if (cp == (char *)-1) {
100f08c3bdfSopenharmony_ci			perror("shmat");
101f08c3bdfSopenharmony_ci			tst_resm(TFAIL,
102f08c3bdfSopenharmony_ci				 "Error: shmat: shmid = %d, errno = %d",
103f08c3bdfSopenharmony_ci				 shmid, errno);
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci/* kill the child if parent failed to do the attch */
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci			kill(pid, SIGINT);
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci/* remove shared memory segment */
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci			rm_shm(shmid);
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_ci			tst_exit();
114f08c3bdfSopenharmony_ci		}
115f08c3bdfSopenharmony_ci		*cp = 'A';
116f08c3bdfSopenharmony_ci		*(cp + 1) = 'B';
117f08c3bdfSopenharmony_ci		*(cp + 2) = 'C';
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci		kill(pid, SIGUSR1);
120f08c3bdfSopenharmony_ci		while ((pid1 = wait(&status)) < 0 && (errno == EINTR)) ;
121f08c3bdfSopenharmony_ci		if (pid1 != pid) {
122f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "Waited on the wrong child");
123f08c3bdfSopenharmony_ci			tst_resm(TFAIL,
124f08c3bdfSopenharmony_ci				 "Error: wait_status = %d, pid1= %d", status,
125f08c3bdfSopenharmony_ci				 pid1);
126f08c3bdfSopenharmony_ci		}
127f08c3bdfSopenharmony_ci	}
128f08c3bdfSopenharmony_ci
129f08c3bdfSopenharmony_ci	tst_resm(TPASS, "shmget,shmat");
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci/*----------------------------------------------------------*/
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci	if (shmdt(cp) < 0) {
134f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "shmdt");
135f08c3bdfSopenharmony_ci	}
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	tst_resm(TPASS, "shmdt");
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci/*----------------------------------------------------------*/
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci	rm_shm(shmid);
142f08c3bdfSopenharmony_ci	tst_exit();
143f08c3bdfSopenharmony_ci}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ciint child(void)
146f08c3bdfSopenharmony_ci{
147f08c3bdfSopenharmony_ci	int shmid, chld_pid;
148f08c3bdfSopenharmony_ci	char *cp;
149f08c3bdfSopenharmony_ci	int sig;
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci	sigwait(&set, &sig);
152f08c3bdfSopenharmony_ci	chld_pid = getpid();
153f08c3bdfSopenharmony_ci/*--------------------------------------------------------*/
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	if ((shmid = shmget(key, SIZE, 0)) < 0) {
156f08c3bdfSopenharmony_ci		perror("shmget:child process");
157f08c3bdfSopenharmony_ci		tst_resm(TFAIL,
158f08c3bdfSopenharmony_ci			 "Error: shmget: errno=%d, shmid=%d, child_pid=%d",
159f08c3bdfSopenharmony_ci			 errno, shmid, chld_pid);
160f08c3bdfSopenharmony_ci	} else {
161f08c3bdfSopenharmony_ci		cp = shmat(shmid, NULL, 0);
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci		if (cp == (char *)-1) {
164f08c3bdfSopenharmony_ci			perror("shmat:child process");
165f08c3bdfSopenharmony_ci			tst_resm(TFAIL,
166f08c3bdfSopenharmony_ci				 "Error: shmat: errno=%d, shmid=%d, child_pid=%d",
167f08c3bdfSopenharmony_ci				 errno, shmid, chld_pid);
168f08c3bdfSopenharmony_ci		} else {
169f08c3bdfSopenharmony_ci			if (*cp != 'A') {
170f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "child: not A");
171f08c3bdfSopenharmony_ci			}
172f08c3bdfSopenharmony_ci			if (*(cp + 1) != 'B') {
173f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "child: not B");
174f08c3bdfSopenharmony_ci			}
175f08c3bdfSopenharmony_ci			if (*(cp + 2) != 'C') {
176f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "child: not C");
177f08c3bdfSopenharmony_ci			}
178f08c3bdfSopenharmony_ci			if (*(cp + 8192) != 0) {
179f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "child: not 0");
180f08c3bdfSopenharmony_ci			}
181f08c3bdfSopenharmony_ci		}
182f08c3bdfSopenharmony_ci
183f08c3bdfSopenharmony_ci	}
184f08c3bdfSopenharmony_ci	tst_exit();
185f08c3bdfSopenharmony_ci}
186f08c3bdfSopenharmony_ci
187f08c3bdfSopenharmony_cistatic int rm_shm(int shmid)
188f08c3bdfSopenharmony_ci{
189f08c3bdfSopenharmony_ci	if (shmctl(shmid, IPC_RMID, NULL) == -1) {
190f08c3bdfSopenharmony_ci		perror("shmctl");
191f08c3bdfSopenharmony_ci		tst_brkm(TFAIL,
192f08c3bdfSopenharmony_ci			 NULL,
193f08c3bdfSopenharmony_ci			 "shmctl Failed to remove: shmid = %d, errno = %d",
194f08c3bdfSopenharmony_ci			 shmid, errno);
195f08c3bdfSopenharmony_ci	}
196f08c3bdfSopenharmony_ci	return (0);
197f08c3bdfSopenharmony_ci}
198