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 | * 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 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/wait.h> |
| 41 | #include <sys/ipc.h> |
| 42 | #include <sys/shm.h> |
robbiew | b084888 | 2003-03-25 15:05:02 +0000 | [diff] [blame] | 43 | #include <sys/utsname.h> |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 44 | #include <errno.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <unistd.h> |
| 47 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 48 | #define SIZE 16*1024 |
| 49 | |
| 50 | /** LTP Port **/ |
| 51 | #include "test.h" |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 52 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 53 | char *TCID = "shmt07"; /* Test program identifier. */ |
| 54 | int TST_TOTAL = 2; /* Total number of test cases. */ |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 55 | /**************/ |
| 56 | |
| 57 | int child(); |
| 58 | int rm_shm(int); |
| 59 | |
| 60 | int main() |
| 61 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 62 | char *cp = NULL; |
| 63 | int shmid, pid, status; |
| 64 | key_t key; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 65 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 66 | key = (key_t) getpid(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 67 | |
| 68 | /*---------------------------------------------------------*/ |
| 69 | |
| 70 | errno = 0; |
| 71 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 72 | if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) { |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 73 | perror("shmget"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 74 | tst_brkm(TFAIL, NULL, |
| 75 | "Error: shmget: shmid = %d, errno = %d\n", |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 76 | shmid, errno); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 77 | } |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 78 | cp = shmat(shmid, NULL, 0); |
subrata_modak | 78b1912 | 2008-05-15 06:54:17 +0000 | [diff] [blame] | 79 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 80 | if (cp == (char *)-1) { |
| 81 | perror("shmat"); |
| 82 | tst_resm(TFAIL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 83 | "Error: shmat: shmid = %d, errno = %d\n", |
| 84 | shmid, errno); |
| 85 | rm_shm(shmid); |
| 86 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 87 | } |
| 88 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 89 | *cp = '1'; |
| 90 | *(cp + 1) = '2'; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 91 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 92 | tst_resm(TPASS, "shmget,shmat"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 93 | |
| 94 | /*-------------------------------------------------------*/ |
| 95 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 96 | pid = fork(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 97 | switch (pid) { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 98 | case -1: |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "fork failed"); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 100 | |
| 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(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* parent */ |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 112 | while (wait(&status) < 0 && errno == EINTR) ; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 113 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 114 | tst_resm(TPASS, "cp & cp+1 correct"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 115 | |
| 116 | /*-----------------------------------------------------------*/ |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 117 | rm_shm(shmid); |
| 118 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int rm_shm(shmid) |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 122 | int shmid; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 123 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 124 | if (shmctl(shmid, IPC_RMID, NULL) == -1) { |
| 125 | perror("shmctl"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 126 | tst_brkm(TFAIL, |
| 127 | NULL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 128 | "shmctl Failed to remove: shmid = %d, errno = %d\n", |
| 129 | shmid, errno); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 130 | } |
| 131 | return (0); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 132 | } |