blob: b36ec0ba46449ddf047c2ffe67785670185219b4 [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 * shmt07
26 *
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2)
29 *
30 * ALGORITHM
31 * Create and attach a shared memory segment, write to it
32 * and then fork a child. The child Verifies that the shared memory segment
33 * that it inherited from the parent conatins the same data that was originally
34 * written to it by the parent.
35 *
36 */
37
robbiew38f79112002-12-23 17:01:49 +000038#include <stdio.h>
39#include <sys/types.h>
40#include <sys/wait.h>
41#include <sys/ipc.h>
42#include <sys/shm.h>
robbiewb0848882003-03-25 15:05:02 +000043#include <sys/utsname.h>
robbiew38f79112002-12-23 17:01:49 +000044#include <errno.h>
45#include <stdlib.h>
46#include <unistd.h>
47
robbiew38f79112002-12-23 17:01:49 +000048#define SIZE 16*1024
49
50/** LTP Port **/
51#include "test.h"
robbiew38f79112002-12-23 17:01:49 +000052
vapier2e2af472007-03-13 20:15:16 +000053char *TCID = "shmt07"; /* Test program identifier. */
54int TST_TOTAL = 2; /* Total number of test cases. */
robbiew38f79112002-12-23 17:01:49 +000055/**************/
56
57int child();
58int rm_shm(int);
59
60int main()
61{
vapier2e2af472007-03-13 20:15:16 +000062 char *cp = NULL;
63 int shmid, pid, status;
64 key_t key;
robbiew38f79112002-12-23 17:01:49 +000065
vapier2e2af472007-03-13 20:15:16 +000066 key = (key_t) getpid();
robbiew38f79112002-12-23 17:01:49 +000067
68/*---------------------------------------------------------*/
69
70 errno = 0;
71
vapier2e2af472007-03-13 20:15:16 +000072 if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) {
robbiew38f79112002-12-23 17:01:49 +000073 perror("shmget");
Cyril Hrubis526fdf82014-12-04 14:35:01 +010074 tst_brkm(TFAIL, NULL,
75 "Error: shmget: shmid = %d, errno = %d\n",
vapier2e2af472007-03-13 20:15:16 +000076 shmid, errno);
robbiew38f79112002-12-23 17:01:49 +000077 }
Cyril Hrubisd2db4802014-09-24 17:08:17 +020078 cp = shmat(shmid, NULL, 0);
subrata_modak78b19122008-05-15 06:54:17 +000079
robbiew38f79112002-12-23 17:01:49 +000080 if (cp == (char *)-1) {
81 perror("shmat");
82 tst_resm(TFAIL,
vapier2e2af472007-03-13 20:15:16 +000083 "Error: shmat: shmid = %d, errno = %d\n",
84 shmid, errno);
85 rm_shm(shmid);
86 tst_exit();
robbiew38f79112002-12-23 17:01:49 +000087 }
88
vapier2e2af472007-03-13 20:15:16 +000089 *cp = '1';
90 *(cp + 1) = '2';
robbiew38f79112002-12-23 17:01:49 +000091
vapier2e2af472007-03-13 20:15:16 +000092 tst_resm(TPASS, "shmget,shmat");
robbiew38f79112002-12-23 17:01:49 +000093
94/*-------------------------------------------------------*/
95
vapier2e2af472007-03-13 20:15:16 +000096 pid = fork();
robbiew38f79112002-12-23 17:01:49 +000097 switch (pid) {
vapier2e2af472007-03-13 20:15:16 +000098 case -1:
Cyril Hrubis526fdf82014-12-04 14:35:01 +010099 tst_brkm(TBROK, NULL, "fork failed");
vapier2e2af472007-03-13 20:15:16 +0000100
101 case 0:
102 if (*cp != '1') {
103 tst_resm(TFAIL, "Error: not 1\n");
104 }
105 if (*(cp + 1) != '2') {
106 tst_resm(TFAIL, "Error: not 2\n");
107 }
108 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000109 }
110
111 /* parent */
vapier2e2af472007-03-13 20:15:16 +0000112 while (wait(&status) < 0 && errno == EINTR) ;
robbiew38f79112002-12-23 17:01:49 +0000113
vapier2e2af472007-03-13 20:15:16 +0000114 tst_resm(TPASS, "cp & cp+1 correct");
robbiew38f79112002-12-23 17:01:49 +0000115
116/*-----------------------------------------------------------*/
vapier2e2af472007-03-13 20:15:16 +0000117 rm_shm(shmid);
118 tst_exit();
robbiew38f79112002-12-23 17:01:49 +0000119}
120
121int rm_shm(shmid)
vapier2e2af472007-03-13 20:15:16 +0000122int shmid;
robbiew38f79112002-12-23 17:01:49 +0000123{
vapier2e2af472007-03-13 20:15:16 +0000124 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
125 perror("shmctl");
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100126 tst_brkm(TFAIL,
127 NULL,
vapier2e2af472007-03-13 20:15:16 +0000128 "shmctl Failed to remove: shmid = %d, errno = %d\n",
129 shmid, errno);
vapier2e2af472007-03-13 20:15:16 +0000130 }
131 return (0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700132}