blob: 42fce0f7a2cc3773961aa492fdb7adcee1173c47 [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
robbiew1b8d8e62003-03-20 15:21:48 +000020/* 12/20/2002 Port to LTP robbiew@us.ibm.com */
21/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
robbiew38f79112002-12-23 17:01:49 +000022
23/*
24 * NAME
robbiew1b8d8e62003-03-20 15:21:48 +000025 * shmt02
robbiew38f79112002-12-23 17:01:49 +000026 *
27 * CALLS
robbiew1b8d8e62003-03-20 15:21:48 +000028 * shmctl(2) shmget(2)
robbiew38f79112002-12-23 17:01:49 +000029 *
30 * ALGORITHM
31 * Create and attach a shared memory segment, write to it
robbiew1b8d8e62003-03-20 15:21:48 +000032 * and then remove it. Verify that the shared memory segment
robbiew38f79112002-12-23 17:01:49 +000033 * is accessible as long as the process is still alive.
34 *
35 */
36
37#include <stdio.h>
38#include <sys/types.h>
39#include <sys/ipc.h>
40#include <sys/shm.h>
robbiew1b8d8e62003-03-20 15:21:48 +000041#include <sys/utsname.h>
robbiewc0325f72003-02-10 18:46:26 +000042#include <errno.h>
robbiew38f79112002-12-23 17:01:49 +000043
44/** LTP Port **/
45#include "test.h"
robbiew38f79112002-12-23 17:01:49 +000046
vapier2e2af472007-03-13 20:15:16 +000047char *TCID = "shmt02"; /* Test program identifier. */
48int TST_TOTAL = 3; /* Total number of test cases. */
robbiew1b8d8e62003-03-20 15:21:48 +000049
robbiew38f79112002-12-23 17:01:49 +000050/**************/
51
52#define K_1 1024
53
54int rm_shm(int);
55
56int main()
57{
vapier2e2af472007-03-13 20:15:16 +000058 register int shmid;
59 char *cp;
60 key_t key;
robbiew38f79112002-12-23 17:01:49 +000061
vapier2e2af472007-03-13 20:15:16 +000062 errno = 0;
63 key = (key_t) getpid();
robbiew38f79112002-12-23 17:01:49 +000064
65/*----------------------------------------------------------------*/
66
vapier2e2af472007-03-13 20:15:16 +000067 if ((shmid = shmget(key, 16 * K_1, IPC_CREAT | 0666)) < 0) {
68 perror("shmget");
Cyril Hrubis526fdf82014-12-04 14:35:01 +010069 tst_brkm(TFAIL, NULL,
70 "shmget Failed: shmid = %d, errno = %d\n",
vapier2e2af472007-03-13 20:15:16 +000071 shmid, errno);
vapier2e2af472007-03-13 20:15:16 +000072 }
robbiew38f79112002-12-23 17:01:49 +000073
vapier2e2af472007-03-13 20:15:16 +000074 tst_resm(TPASS, "shmget");
robbiew38f79112002-12-23 17:01:49 +000075
76/*----------------------------------------------------------------*/
77
Cyril Hrubisd2db4802014-09-24 17:08:17 +020078 cp = shmat(shmid, NULL, 0);
subrata_modak78b19122008-05-15 06:54:17 +000079
vapier2e2af472007-03-13 20:15:16 +000080 if (cp == (char *)-1) {
81 perror("shmat");
82 tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d\n",
83 shmid, errno);
84 rm_shm(shmid);
85 tst_exit();
86 }
robbiew38f79112002-12-23 17:01:49 +000087
vapier2e2af472007-03-13 20:15:16 +000088 *cp = '1';
89 *(cp + 1) = '2';
robbiew38f79112002-12-23 17:01:49 +000090
vapier2e2af472007-03-13 20:15:16 +000091 tst_resm(TPASS, "shmat");
robbiew38f79112002-12-23 17:01:49 +000092
93/*----------------------------------------------------------------*/
94
vapier2e2af472007-03-13 20:15:16 +000095 rm_shm(shmid);
robbiew38f79112002-12-23 17:01:49 +000096
vapier2e2af472007-03-13 20:15:16 +000097 if (*cp != '1' || *(cp + 1) != '2') {
98 tst_resm(TFAIL,
99 "Error in shared memory contents: shmid = %d\n",
100 shmid);
101 }
robbiew38f79112002-12-23 17:01:49 +0000102
vapier2e2af472007-03-13 20:15:16 +0000103 tst_resm(TPASS, "Correct shared memory contents");
robbiew38f79112002-12-23 17:01:49 +0000104
105/*------------------------------------------------------------------*/
106
vapier2e2af472007-03-13 20:15:16 +0000107 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000108}
109
110int rm_shm(shmid)
vapier2e2af472007-03-13 20:15:16 +0000111int shmid;
robbiew38f79112002-12-23 17:01:49 +0000112{
vapier2e2af472007-03-13 20:15:16 +0000113 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
114 perror("shmctl");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100115 tst_brkm(TFAIL,
116 NULL,
vapier2e2af472007-03-13 20:15:16 +0000117 "shmctl Failed to remove: shmid = %d, errno = %d\n",
118 shmid, errno);
vapier2e2af472007-03-13 20:15:16 +0000119 }
120 return (0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700121}