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/* 12/20/2002 Port to LTP robbiew@us.ibm.com */ 21f08c3bdfSopenharmony_ci/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci/* 24f08c3bdfSopenharmony_ci * NAME 25f08c3bdfSopenharmony_ci * shmt3 26f08c3bdfSopenharmony_ci * 27f08c3bdfSopenharmony_ci * CALLS 28f08c3bdfSopenharmony_ci * shmctl(2) shmget(2) shmat(2) 29f08c3bdfSopenharmony_ci * 30f08c3bdfSopenharmony_ci * ALGORITHM 31f08c3bdfSopenharmony_ci * Create one shared memory segment and attach it twice to the same process, 32f08c3bdfSopenharmony_ci * at an address that is chosen by the system. After the first attach has 33f08c3bdfSopenharmony_ci * completed, write to it and then do the second attach. 34f08c3bdfSopenharmony_ci * Verify that the doubly attached segment contains the same data. 35f08c3bdfSopenharmony_ci * 36f08c3bdfSopenharmony_ci */ 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci#include <stdio.h> 39f08c3bdfSopenharmony_ci#include <sys/types.h> 40f08c3bdfSopenharmony_ci#include <sys/ipc.h> 41f08c3bdfSopenharmony_ci#include <sys/shm.h> 42f08c3bdfSopenharmony_ci#include <errno.h> 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci/** LTP Port **/ 45f08c3bdfSopenharmony_ci#include "test.h" 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_cichar *TCID = "shmt03"; /* Test program identifier. */ 48f08c3bdfSopenharmony_ciint TST_TOTAL = 4; /* Total number of test cases. */ 49f08c3bdfSopenharmony_ci/**************/ 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci#define K_1 1024 52f08c3bdfSopenharmony_ci#define SUCCESSFUL 1 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ciint first_attach, second_attach; 55f08c3bdfSopenharmony_cistatic int rm_shm(int); 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ciint main(void) 58f08c3bdfSopenharmony_ci{ 59f08c3bdfSopenharmony_ci char *cp1, *cp2; 60f08c3bdfSopenharmony_ci int shmid; 61f08c3bdfSopenharmony_ci key_t key; 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci key = (key_t) getpid(); 64f08c3bdfSopenharmony_ci errno = 0; 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci/*------------------------------------------------------------*/ 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci if ((shmid = shmget(key, 16 * K_1, IPC_CREAT | 0666)) < 0) { 69f08c3bdfSopenharmony_ci perror("shmget"); 70f08c3bdfSopenharmony_ci tst_brkm(TFAIL, NULL, 71f08c3bdfSopenharmony_ci "shmget Failed: shmid = %d, errno = %d", 72f08c3bdfSopenharmony_ci shmid, errno); 73f08c3bdfSopenharmony_ci } 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci tst_resm(TPASS, "shmget"); 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci/*------------------------------------------------------------*/ 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci if ((cp1 = shmat(shmid, NULL, 0)) == (char *)-1) { 80f08c3bdfSopenharmony_ci perror("shmat"); 81f08c3bdfSopenharmony_ci tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d", 82f08c3bdfSopenharmony_ci shmid, errno); 83f08c3bdfSopenharmony_ci } else { 84f08c3bdfSopenharmony_ci *cp1 = '1'; 85f08c3bdfSopenharmony_ci *(cp1 + 5 * K_1) = '2'; 86f08c3bdfSopenharmony_ci first_attach = SUCCESSFUL; 87f08c3bdfSopenharmony_ci } 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci tst_resm(TPASS, "1st shmat"); 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci/*------------------------------------------------------------*/ 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_ci if ((cp2 = shmat(shmid, NULL, 0)) == (char *)-1) { 94f08c3bdfSopenharmony_ci perror("shmat"); 95f08c3bdfSopenharmony_ci tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d", 96f08c3bdfSopenharmony_ci shmid, errno); 97f08c3bdfSopenharmony_ci } else { 98f08c3bdfSopenharmony_ci second_attach = SUCCESSFUL; 99f08c3bdfSopenharmony_ci if ((*cp2 != '1' || *(cp2 + 5 * K_1) != '2') && 100f08c3bdfSopenharmony_ci first_attach == SUCCESSFUL) { 101f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Error: Shared memory contents"); 102f08c3bdfSopenharmony_ci } 103f08c3bdfSopenharmony_ci } 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci tst_resm(TPASS, "2nd shmat"); 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci/*---------------------------------------------------------------*/ 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci rm_shm(shmid); 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci if (first_attach && second_attach) { 112f08c3bdfSopenharmony_ci if (*cp2 != '1' || *(cp2 + 5 * K_1) != '2' || 113f08c3bdfSopenharmony_ci *cp1 != '1' || *(cp1 + 5 * K_1) != '2') { 114f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Error: Shared memory contents"); 115f08c3bdfSopenharmony_ci } 116f08c3bdfSopenharmony_ci } 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci tst_resm(TPASS, "Correct shared memory contents"); 119f08c3bdfSopenharmony_ci/*-----------------------------------------------------------------*/ 120f08c3bdfSopenharmony_ci tst_exit(); 121f08c3bdfSopenharmony_ci} 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_cistatic int rm_shm(int shmid) 124f08c3bdfSopenharmony_ci{ 125f08c3bdfSopenharmony_ci if (shmctl(shmid, IPC_RMID, NULL) == -1) { 126f08c3bdfSopenharmony_ci perror("shmctl"); 127f08c3bdfSopenharmony_ci tst_brkm(TFAIL, 128f08c3bdfSopenharmony_ci NULL, 129f08c3bdfSopenharmony_ci "shmctl Failed to remove: shmid = %d, errno = %d", 130f08c3bdfSopenharmony_ci shmid, errno); 131f08c3bdfSopenharmony_ci } 132f08c3bdfSopenharmony_ci return (0); 133f08c3bdfSopenharmony_ci} 134