Matus Marhefka | 8243c77 | 2014-10-02 16:47:10 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2014 Red Hat, Inc. |
| 2 | * |
| 3 | * This program is free software: you can redistribute it and/or modify |
| 4 | * it under the terms of version 2 the GNU General Public License as |
| 5 | * published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | *********************************************************************** |
| 15 | * File: shm_comm.c |
| 16 | * |
| 17 | * Description: |
| 18 | * 1. Clones two child processes with CLONE_NEWIPC flag, each child |
| 19 | * allocates System V shared memory segment (shm) with the _identical_ |
| 20 | * key and attaches that segment into its address space. |
| 21 | * 2. Child1 writes into the shared memory segment. |
| 22 | * 3. Child2 writes into the shared memory segment. |
| 23 | * 4. Writes to the shared memory segment with the identical key but from |
| 24 | * two different IPC namespaces should not interfere with each other |
| 25 | * and so child1 checks whether its shared segment wasn't changed |
| 26 | * by child2, if it wasn't test passes, otherwise test fails. |
| 27 | */ |
| 28 | |
| 29 | #define _GNU_SOURCE |
| 30 | #include <sys/ipc.h> |
| 31 | #include <sys/shm.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <stdio.h> |
| 35 | #include <errno.h> |
Matus Marhefka | 8243c77 | 2014-10-02 16:47:10 +0200 | [diff] [blame] | 36 | #include "test.h" |
| 37 | #include "safe_macros.h" |
| 38 | #include "libclone.h" |
| 39 | #include "ipcns_helper.h" |
| 40 | |
| 41 | |
| 42 | #define TESTKEY 124426L |
| 43 | #define SHMSIZE 50 |
| 44 | char *TCID = "shm_comm"; |
| 45 | int TST_TOTAL = 1; |
| 46 | struct tst_checkpoint checkpoint1; |
| 47 | struct tst_checkpoint checkpoint2; |
| 48 | |
| 49 | |
| 50 | static void cleanup(void) |
| 51 | { |
| 52 | tst_rmdir(); |
| 53 | } |
| 54 | |
| 55 | static void setup(void) |
| 56 | { |
| 57 | tst_require_root(NULL); |
| 58 | check_newipc(); |
| 59 | tst_tmpdir(); |
| 60 | TST_CHECKPOINT_CREATE(&checkpoint1); |
| 61 | TST_CHECKPOINT_CREATE(&checkpoint2); |
| 62 | } |
| 63 | |
| 64 | int chld1_shm(void *arg) |
| 65 | { |
| 66 | int id, rval = 0; |
| 67 | char *shmem; |
| 68 | |
| 69 | id = shmget(TESTKEY, SHMSIZE, IPC_CREAT); |
| 70 | if (id == -1) { |
| 71 | perror("shmget"); |
| 72 | return 2; |
| 73 | } |
| 74 | |
| 75 | if ((shmem = shmat(id, NULL, 0)) == (char *) -1) { |
| 76 | perror("shmat"); |
| 77 | shmctl(id, IPC_RMID, NULL); |
| 78 | return 2; |
| 79 | } |
| 80 | |
| 81 | *shmem = 'A'; |
| 82 | |
| 83 | /* tell child2 to continue */ |
| 84 | TST_CHECKPOINT_SIGNAL_CHILD(NULL, &checkpoint1); |
| 85 | |
| 86 | /* wait for child2 */ |
| 87 | TST_CHECKPOINT_CHILD_WAIT(&checkpoint2); |
| 88 | |
| 89 | /* if child1 shared segment has changed (by child2) report fail */ |
| 90 | if (*shmem != 'A') |
| 91 | rval = 1; |
| 92 | |
| 93 | /* tell child2 to continue */ |
| 94 | TST_CHECKPOINT_SIGNAL_CHILD(NULL, &checkpoint1); |
| 95 | |
| 96 | shmdt(shmem); |
| 97 | shmctl(id, IPC_RMID, NULL); |
| 98 | return rval; |
| 99 | } |
| 100 | |
| 101 | int chld2_shm(void *arg) |
| 102 | { |
| 103 | int id; |
| 104 | char *shmem; |
| 105 | |
| 106 | id = shmget(TESTKEY, SHMSIZE, IPC_CREAT); |
| 107 | if (id == -1) { |
| 108 | perror("shmget"); |
| 109 | return 2; |
| 110 | } |
| 111 | |
| 112 | if ((shmem = shmat(id, NULL, 0)) == (char *) -1) { |
| 113 | perror("shmat"); |
| 114 | shmctl(id, IPC_RMID, NULL); |
| 115 | return 2; |
| 116 | } |
| 117 | |
| 118 | /* wait for child1 to write to his segment */ |
| 119 | TST_CHECKPOINT_CHILD_WAIT(&checkpoint1); |
| 120 | |
| 121 | *shmem = 'B'; |
| 122 | |
| 123 | /* tell child1 to continue */ |
| 124 | TST_CHECKPOINT_SIGNAL_CHILD(NULL, &checkpoint2); |
| 125 | |
| 126 | /* wait for child1 */ |
| 127 | TST_CHECKPOINT_CHILD_WAIT(&checkpoint1); |
| 128 | |
| 129 | shmdt(shmem); |
| 130 | shmctl(id, IPC_RMID, NULL); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static void test(void) |
| 135 | { |
| 136 | int status, ret = 0; |
| 137 | |
| 138 | ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_shm, NULL); |
| 139 | if (ret == -1) |
| 140 | tst_brkm(TBROK | TERRNO, cleanup, "clone failed"); |
| 141 | |
| 142 | ret = do_clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_shm, NULL); |
| 143 | if (ret == -1) |
| 144 | tst_brkm(TBROK | TERRNO, cleanup, "clone failed"); |
| 145 | |
| 146 | |
| 147 | while (wait(&status) > 0) { |
| 148 | if (WIFEXITED(status) && WEXITSTATUS(status) == 1) |
| 149 | ret = 1; |
| 150 | if (WIFEXITED(status) && WEXITSTATUS(status) == 2) |
| 151 | tst_brkm(TBROK | TERRNO, cleanup, "error in child"); |
| 152 | if (WIFSIGNALED(status)) { |
| 153 | tst_resm(TFAIL, "child was killed with signal %s", |
| 154 | tst_strsig(WTERMSIG(status))); |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (ret) |
| 160 | tst_resm(TFAIL, "SysV shm: communication with identical keys" |
| 161 | " between namespaces"); |
| 162 | else |
| 163 | tst_resm(TPASS, "SysV shm: communication with identical keys" |
| 164 | " between namespaces"); |
| 165 | } |
| 166 | |
| 167 | int main(int argc, char *argv[]) |
| 168 | { |
| 169 | const char *msg; |
| 170 | int lc; |
| 171 | |
| 172 | msg = parse_opts(argc, argv, NULL, NULL); |
| 173 | if (msg != NULL) |
| 174 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 175 | |
| 176 | setup(); |
| 177 | |
| 178 | for (lc = 0; TEST_LOOPING(lc); lc++) |
| 179 | test(); |
| 180 | |
| 181 | cleanup(); |
| 182 | tst_exit(); |
| 183 | } |