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 | * shmt06 |
| 26 | * |
| 27 | * CALLS |
| 28 | * shmctl(2) shmget(2) shmat(2) |
| 29 | * |
| 30 | * ALGORITHM |
| 31 | * Parent process forks a child. Child pauses until parent has created |
| 32 | * a shared memory segment, attached to it and written to it too. At that |
subrata_modak | ee2924a | 2008-05-19 06:40:38 +0000 | [diff] [blame] | 33 | * time child gets the shared memory segment id, attaches to it at two |
| 34 | * different addresses than the parents and verifies that their contents |
| 35 | * are the same as the contents of the parent attached segment. |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/wait.h> |
| 42 | #include <sys/ipc.h> |
| 43 | #include <sys/shm.h> |
robbiew | b084888 | 2003-03-25 15:05:02 +0000 | [diff] [blame] | 44 | #include <sys/utsname.h> |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 45 | #include <signal.h> |
| 46 | #include <errno.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <unistd.h> |
| 49 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 50 | #define SIZE 16*1024 |
| 51 | |
| 52 | /** LTP Port **/ |
| 53 | #include "test.h" |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 54 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 55 | char *TCID = "shmt06"; /* Test program identifier. */ |
| 56 | int TST_TOTAL = 2; /* Total number of test cases. */ |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 57 | /**************/ |
| 58 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 59 | key_t key; |
robbiew | 95157a9 | 2003-01-08 21:43:27 +0000 | [diff] [blame] | 60 | sigset_t sigset; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 61 | |
| 62 | int child(); |
| 63 | int rm_shm(int); |
| 64 | |
| 65 | int main() |
| 66 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 67 | char *cp = NULL; |
| 68 | int pid, pid1, shmid; |
| 69 | int status; |
| 70 | |
| 71 | key = (key_t) getpid(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 72 | |
robbiew | 95157a9 | 2003-01-08 21:43:27 +0000 | [diff] [blame] | 73 | sigemptyset(&sigset); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 74 | sigaddset(&sigset, SIGUSR1); |
| 75 | sigprocmask(SIG_BLOCK, &sigset, NULL); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 76 | |
| 77 | pid = fork(); |
| 78 | switch (pid) { |
| 79 | case -1: |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 80 | tst_brkm(TBROK, NULL, "fork failed"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 81 | case 0: |
| 82 | child(); |
| 83 | } |
| 84 | |
| 85 | /*------------------------------------------------------*/ |
| 86 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 87 | if ((shmid = shmget(key, SIZE, IPC_CREAT | 0666)) < 0) { |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 88 | perror("shmget"); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 89 | tst_resm(TFAIL, "Error: shmget: shmid = %d, errno = %d\n", |
| 90 | shmid, errno); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 91 | /* |
| 92 | * kill the child if parent failed to do the attach |
| 93 | */ |
| 94 | (void)kill(pid, SIGINT); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 95 | } else { |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 96 | cp = shmat(shmid, NULL, 0); |
subrata_modak | 78b1912 | 2008-05-15 06:54:17 +0000 | [diff] [blame] | 97 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 98 | if (cp == (char *)-1) { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 99 | perror("shmat"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 100 | tst_resm(TFAIL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 101 | "Error: shmat: shmid = %d, errno = %d\n", |
| 102 | shmid, errno); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 103 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 104 | /* kill the child if parent failed to do the attch */ |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 105 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 106 | kill(pid, SIGINT); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 107 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 108 | /* remove shared memory segment */ |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 109 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 110 | rm_shm(shmid); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 111 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 112 | tst_exit(); |
| 113 | } |
| 114 | *cp = 'A'; |
| 115 | *(cp + 1) = 'B'; |
| 116 | *(cp + 2) = 'C'; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 117 | |
| 118 | kill(pid, SIGUSR1); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 119 | while ((pid1 = wait(&status)) < 0 && (errno == EINTR)) ; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 120 | if (pid1 != pid) { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 121 | tst_resm(TFAIL, "Waited on the wrong child"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 122 | tst_resm(TFAIL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 123 | "Error: wait_status = %d, pid1= %d\n", status, |
| 124 | pid1); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 128 | tst_resm(TPASS, "shmget,shmat"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 129 | |
| 130 | /*---------------------------------------------------------------*/ |
| 131 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 132 | if (shmdt(cp) < 0) { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 133 | tst_resm(TFAIL, "shmdt"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 134 | } |
| 135 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 136 | tst_resm(TPASS, "shmdt"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 137 | |
| 138 | /*-------------------------------------------------------------*/ |
| 139 | |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 140 | rm_shm(shmid); |
| 141 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int child() |
| 145 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 146 | int shmid, chld_pid; |
| 147 | char *cp; |
robbiew | 95157a9 | 2003-01-08 21:43:27 +0000 | [diff] [blame] | 148 | int sig; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 149 | |
robbiew | 95157a9 | 2003-01-08 21:43:27 +0000 | [diff] [blame] | 150 | sigwait(&sigset, &sig); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 151 | chld_pid = getpid(); |
| 152 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 153 | if ((shmid = shmget(key, SIZE, 0)) < 0) { |
| 154 | perror("shmget:child process"); |
| 155 | tst_resm(TFAIL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 156 | "Error: shmget: errno=%d, shmid=%d, child_pid=%d\n", |
| 157 | errno, shmid, chld_pid); |
| 158 | } else { |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 159 | cp = shmat(shmid, NULL, 0); |
subrata_modak | 78b1912 | 2008-05-15 06:54:17 +0000 | [diff] [blame] | 160 | |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 161 | if (cp == (char *)-1) { |
| 162 | perror("shmat:child process"); |
| 163 | tst_resm(TFAIL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 164 | "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n", |
| 165 | errno, shmid, chld_pid); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 166 | } else { |
| 167 | if (*cp != 'A') { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 168 | tst_resm(TFAIL, "child: not A\n"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 169 | } |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 170 | if (*(cp + 1) != 'B') { |
| 171 | tst_resm(TFAIL, "child: not B\n"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 172 | } |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 173 | if (*(cp + 2) != 'C') { |
| 174 | tst_resm(TFAIL, "child: not C\n"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 175 | } |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 176 | if (*(cp + 8192) != 0) { |
| 177 | tst_resm(TFAIL, "child: not 0\n"); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
subrata_modak | ee2924a | 2008-05-19 06:40:38 +0000 | [diff] [blame] | 181 | /* |
| 182 | * Attach the segment to a different addresse |
| 183 | * and verify it's contents again. |
| 184 | */ |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 185 | cp = shmat(shmid, NULL, 0); |
subrata_modak | ee2924a | 2008-05-19 06:40:38 +0000 | [diff] [blame] | 186 | |
| 187 | if (cp == (char *)-1) { |
| 188 | perror("shmat:child process"); |
| 189 | tst_resm(TFAIL, |
| 190 | "Error: shmat: errno=%d, shmid=%d, child_pid=%d\n", |
| 191 | errno, shmid, chld_pid); |
| 192 | } else { |
| 193 | if (*cp != 'A') { |
| 194 | tst_resm(TFAIL, "child: not A\n"); |
| 195 | } |
| 196 | if (*(cp + 1) != 'B') { |
| 197 | tst_resm(TFAIL, "child: not B\n"); |
| 198 | } |
| 199 | if (*(cp + 2) != 'C') { |
| 200 | tst_resm(TFAIL, "child: not C\n"); |
| 201 | } |
| 202 | if (*(cp + 8192) != 0) { |
| 203 | tst_resm(TFAIL, "child: not 0\n"); |
| 204 | } |
| 205 | } |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 206 | } |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 207 | tst_exit(); |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | int rm_shm(shmid) |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 211 | int shmid; |
robbiew | 38f7911 | 2002-12-23 17:01:49 +0000 | [diff] [blame] | 212 | { |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 213 | if (shmctl(shmid, IPC_RMID, NULL) == -1) { |
| 214 | perror("shmctl"); |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 215 | tst_brkm(TFAIL, |
| 216 | NULL, |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 217 | "shmctl Failed to remove: shmid = %d, errno = %d\n", |
| 218 | shmid, errno); |
vapier | 2e2af47 | 2007-03-13 20:15:16 +0000 | [diff] [blame] | 219 | } |
| 220 | return (0); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 221 | } |