robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 18 | */ |
| 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 |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 32 | * that is provided by the system. Detach the previously attached |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 33 | * segments from the process. |
| 34 | * |
| 35 | */ |
| 36 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 37 | #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" |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 47 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 48 | char *TCID = "shmt08"; /* Test program identifier. */ |
| 49 | int TST_TOTAL = 2; /* Total number of test cases. */ |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 50 | /**************/ |
| 51 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 52 | key_t key; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 53 | |
| 54 | int rm_shm(int); |
| 55 | |
| 56 | int main() |
| 57 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 58 | char *cp = NULL, *cp1 = NULL; |
| 59 | int shmid; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 60 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 61 | key = (key_t) getpid(); |
| 62 | errno = 0; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 63 | /*-------------------------------------------------------*/ |
| 64 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 65 | if ((shmid = shmget(key, 24 * K_1, IPC_CREAT | 0666)) < 0) { |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 66 | perror("shmget"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 67 | tst_brkm(TFAIL, NULL, |
| 68 | "Error: shmget: shmid = %d, errno = %d\n", |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 69 | shmid, errno); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 72 | cp = shmat(shmid, NULL, 0); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 73 | if (cp == (char *)-1) { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 74 | tst_resm(TFAIL, "shmat1 Failed"); |
| 75 | rm_shm(shmid); |
| 76 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 79 | cp1 = shmat(shmid, NULL, 0); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 80 | if (cp1 == (char *)-1) { |
| 81 | perror("shmat2"); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 82 | rm_shm(shmid); |
| 83 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 84 | } |
| 85 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 86 | tst_resm(TPASS, "shmget,shmat"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 87 | |
| 88 | /*--------------------------------------------------------*/ |
| 89 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 90 | if (shmdt(cp) < 0) { |
| 91 | perror("shmdt2"); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 92 | tst_resm(TFAIL, "shmdt:cp"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 93 | } |
| 94 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 95 | if (shmdt(cp1) < 0) { |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 96 | perror("shmdt1"); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 97 | tst_resm(TFAIL, "shmdt:cp1"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 98 | } |
| 99 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 100 | tst_resm(TPASS, "shmdt"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 101 | |
| 102 | /*---------------------------------------------------------*/ |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 103 | rm_shm(shmid); |
| 104 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int rm_shm(shmid) |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 108 | int shmid; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 109 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 110 | if (shmctl(shmid, IPC_RMID, NULL) == -1) { |
| 111 | perror("shmctl"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 112 | tst_brkm(TFAIL, |
| 113 | NULL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 114 | "shmctl Failed to remove: shmid = %d, errno = %d\n", |
| 115 | shmid, errno); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 116 | } |
| 117 | return (0); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 118 | } |