blob: ceb894bfb46fa6eaa7d2da63fb818ff3e99861d5 [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 * shmt10.c - test simultaneous shmat/shmdt
26 *
27 * CALLS
28 * shmget, shmat, shmdt, shmctl
29 *
30 * ALGORITHM
31 * Create a shared memory segment and fork a child. Both
32 * parent and child spin in a loop attaching and detaching
33 * the segment. After completing the specified number of
34 * iterations, the child exits and the parent deletes the
35 * segment.
36 *
37 * USAGE
38 * shmt10 [-i 500]
39 * -i # of iterations, default 500
40 *
41 */
42
robbiew38f79112002-12-23 17:01:49 +000043#include <stdio.h>
44#include <sys/types.h>
45#include <sys/wait.h>
46#include <sys/ipc.h>
47#include <sys/shm.h>
48#include <stdlib.h>
49#include <unistd.h>
50#include <signal.h>
51#include <errno.h>
52
53#define SIZE 0x32768
54
55/** LTP Port **/
56#include "test.h"
robbiew38f79112002-12-23 17:01:49 +000057
vapier2e2af472007-03-13 20:15:16 +000058char *TCID = "shmt10"; /* Test program identifier. */
59int TST_TOTAL = 2; /* Total number of test cases. */
robbiew38f79112002-12-23 17:01:49 +000060/**************/
61
62int shmid;
63key_t key;
64
65int child(int);
66int rm_shm(int);
67void fini();
68
69int main(argc, argv)
vapier2e2af472007-03-13 20:15:16 +000070int argc;
robbiew38f79112002-12-23 17:01:49 +000071char *argv[];
72{
vapier2e2af472007-03-13 20:15:16 +000073 char *c1 = NULL;
robbiew38f79112002-12-23 17:01:49 +000074 int pid, st;
75 register int i;
76 int iter = 500;
77 int c;
78 extern char *optarg;
robbiew38f79112002-12-23 17:01:49 +000079
vapier2e2af472007-03-13 20:15:16 +000080 key = (key_t) getpid();
robbiew38f79112002-12-23 17:01:49 +000081 signal(SIGTERM, fini);
82
83/*--------------------------------------------------------*/
84
85 while ((c = getopt(argc, argv, "i:")) != EOF) {
86 switch (c) {
87 case 'i':
88 iter = atoi(optarg);
89 break;
90 default:
Cyril Hrubis526fdf82014-12-04 14:35:01 +010091 tst_brkm(TCONF, NULL, "usage: %s [-i <# iterations>]",
vapier2e2af472007-03-13 20:15:16 +000092 argv[0]);
robbiew38f79112002-12-23 17:01:49 +000093 }
94 }
95
robbiew38f79112002-12-23 17:01:49 +000096/*------------------------------------------------------------------------*/
97
vapier2e2af472007-03-13 20:15:16 +000098 if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
99 tst_resm(TFAIL, "shmget");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100100 tst_brkm(TFAIL, NULL, "Error: shmid = %d\n", shmid);
robbiew38f79112002-12-23 17:01:49 +0000101 }
102
103 pid = fork();
vapier2e2af472007-03-13 20:15:16 +0000104 switch (pid) {
robbiew38f79112002-12-23 17:01:49 +0000105 case -1:
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100106 tst_brkm(TBROK, NULL, "fork failed");
robbiew38f79112002-12-23 17:01:49 +0000107 case 0:
108 child(iter);
109 tst_exit();
110 }
111
112 for (i = 0; i < iter; i++) {
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200113 if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) {
vapier2e2af472007-03-13 20:15:16 +0000114 tst_resm(TFAIL,
115 "Error shmat: iter %d, shmid = %d\n", i,
116 shmid);
robbiew38f79112002-12-23 17:01:49 +0000117 break;
118 }
119 if (shmdt(c1) < 0) {
vapier2e2af472007-03-13 20:15:16 +0000120 tst_resm(TFAIL, "Error: shmdt: iter %d ", i);
robbiew38f79112002-12-23 17:01:49 +0000121 break;
122 }
123 }
vapier2e2af472007-03-13 20:15:16 +0000124 while (wait(&st) < 0 && errno == EINTR) ;
125 tst_resm(TPASS, "shmat,shmdt");
robbiew38f79112002-12-23 17:01:49 +0000126/*------------------------------------------------------------------------*/
127
128 rm_shm(shmid);
129 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000130}
131
132int rm_shm(shmid)
vapier2e2af472007-03-13 20:15:16 +0000133int shmid;
robbiew38f79112002-12-23 17:01:49 +0000134{
vapier2e2af472007-03-13 20:15:16 +0000135 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
136 perror("shmctl");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100137 tst_brkm(TFAIL,
138 NULL,
vapier2e2af472007-03-13 20:15:16 +0000139 "shmctl Failed to remove: shmid = %d, errno = %d\n",
140 shmid, errno);
vapier2e2af472007-03-13 20:15:16 +0000141 }
142 return (0);
robbiew38f79112002-12-23 17:01:49 +0000143}
144
145int child(iter)
146int iter;
147{
148 register int i;
149 char *c1;
150
151 for (i = 0; i < iter; i++) {
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200152 if ((c1 = shmat(shmid, NULL, 0)) == (char *)-1) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100153 tst_brkm(TFAIL,
154 NULL,
vapier2e2af472007-03-13 20:15:16 +0000155 "Error:child proc: shmat: iter %d, shmid = %d\n",
156 i, shmid);
robbiew38f79112002-12-23 17:01:49 +0000157 }
158 if (shmdt(c1) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100159 tst_brkm(TFAIL,
160 NULL, "Error: child proc: shmdt: iter %d ",
161 i);
robbiew38f79112002-12-23 17:01:49 +0000162 }
163 }
vapier2e2af472007-03-13 20:15:16 +0000164 return (0);
robbiew38f79112002-12-23 17:01:49 +0000165}
166
vapier2e2af472007-03-13 20:15:16 +0000167void fini()
robbiew38f79112002-12-23 17:01:49 +0000168{
169 rm_shm(shmid);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700170}