blob: c2c418decfe5efa228f797d66059ce63c72f3102 [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 * shmt08
26 *
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2) shmdt(2)
29 *
30 * ALGORITHM
31 * Create a shared memory segment. Attach it twice at an address
vapier2e2af472007-03-13 20:15:16 +000032 * that is provided by the system. Detach the previously attached
robbiew38f79112002-12-23 17:01:49 +000033 * segments from the process.
34 *
35 */
36
robbiew38f79112002-12-23 17:01:49 +000037#include <stdio.h>
38#include <sys/types.h>
39#include <sys/ipc.h>
40#include <sys/shm.h>
41#include <errno.h>
42
43#define K_1 1024
44
45/** LTP Port **/
46#include "test.h"
robbiew38f79112002-12-23 17:01:49 +000047
vapier2e2af472007-03-13 20:15:16 +000048char *TCID = "shmt08"; /* Test program identifier. */
49int TST_TOTAL = 2; /* Total number of test cases. */
robbiew38f79112002-12-23 17:01:49 +000050/**************/
51
vapier2e2af472007-03-13 20:15:16 +000052key_t key;
robbiew38f79112002-12-23 17:01:49 +000053
54int rm_shm(int);
55
56int main()
57{
vapier2e2af472007-03-13 20:15:16 +000058 char *cp = NULL, *cp1 = NULL;
59 int shmid;
robbiew38f79112002-12-23 17:01:49 +000060
vapier2e2af472007-03-13 20:15:16 +000061 key = (key_t) getpid();
62 errno = 0;
robbiew38f79112002-12-23 17:01:49 +000063/*-------------------------------------------------------*/
64
vapier2e2af472007-03-13 20:15:16 +000065 if ((shmid = shmget(key, 24 * K_1, IPC_CREAT | 0666)) < 0) {
robbiew38f79112002-12-23 17:01:49 +000066 perror("shmget");
Cyril Hrubis526fdf82014-12-04 14:35:01 +010067 tst_brkm(TFAIL, NULL,
68 "Error: shmget: shmid = %d, errno = %d\n",
vapier2e2af472007-03-13 20:15:16 +000069 shmid, errno);
robbiew38f79112002-12-23 17:01:49 +000070 }
71
Cyril Hrubisd2db4802014-09-24 17:08:17 +020072 cp = shmat(shmid, NULL, 0);
robbiew38f79112002-12-23 17:01:49 +000073 if (cp == (char *)-1) {
vapier2e2af472007-03-13 20:15:16 +000074 tst_resm(TFAIL, "shmat1 Failed");
75 rm_shm(shmid);
76 tst_exit();
robbiew38f79112002-12-23 17:01:49 +000077 }
78
Cyril Hrubisd2db4802014-09-24 17:08:17 +020079 cp1 = shmat(shmid, NULL, 0);
robbiew38f79112002-12-23 17:01:49 +000080 if (cp1 == (char *)-1) {
81 perror("shmat2");
vapier2e2af472007-03-13 20:15:16 +000082 rm_shm(shmid);
83 tst_exit();
robbiew38f79112002-12-23 17:01:49 +000084 }
85
vapier2e2af472007-03-13 20:15:16 +000086 tst_resm(TPASS, "shmget,shmat");
robbiew38f79112002-12-23 17:01:49 +000087
88/*--------------------------------------------------------*/
89
robbiew38f79112002-12-23 17:01:49 +000090 if (shmdt(cp) < 0) {
91 perror("shmdt2");
vapier2e2af472007-03-13 20:15:16 +000092 tst_resm(TFAIL, "shmdt:cp");
robbiew38f79112002-12-23 17:01:49 +000093 }
94
vapier2e2af472007-03-13 20:15:16 +000095 if (shmdt(cp1) < 0) {
robbiew38f79112002-12-23 17:01:49 +000096 perror("shmdt1");
vapier2e2af472007-03-13 20:15:16 +000097 tst_resm(TFAIL, "shmdt:cp1");
robbiew38f79112002-12-23 17:01:49 +000098 }
99
vapier2e2af472007-03-13 20:15:16 +0000100 tst_resm(TPASS, "shmdt");
robbiew38f79112002-12-23 17:01:49 +0000101
102/*---------------------------------------------------------*/
vapier2e2af472007-03-13 20:15:16 +0000103 rm_shm(shmid);
104 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000105}
106
107int rm_shm(shmid)
vapier2e2af472007-03-13 20:15:16 +0000108int shmid;
robbiew38f79112002-12-23 17:01:49 +0000109{
vapier2e2af472007-03-13 20:15:16 +0000110 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
111 perror("shmctl");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100112 tst_brkm(TFAIL,
113 NULL,
vapier2e2af472007-03-13 20:15:16 +0000114 "shmctl Failed to remove: shmid = %d, errno = %d\n",
115 shmid, errno);
vapier2e2af472007-03-13 20:15:16 +0000116 }
117 return (0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700118}