1 /* ************************************************************************* 2 * Copyright (c) International Business Machines Corp., 2009 3 * This program is free software; you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation; either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 11 * the GNU General Public License for more details. 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 15 * 16 * Author: Veerendra C <vechandr (at) in.ibm.com> 17 * 18 * In Parent Process , create semaphore with key 154326L 19 * Now create container by passing 1 of the below flag values.. 20 * clone(NONE), clone(CLONE_NEWIPC), or unshare(CLONE_NEWIPC) 21 * In cloned process, try to access the created semaphore 22 * Test PASS: If the semaphore is readable when flag is None. 23 * Test FAIL: If the semaphore is readable when flag is Unshare or Clone. 24 ***************************************************************************/ 25 26 #define _GNU_SOURCE 1 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <unistd.h> 30 #include <string.h> 31 #include <sys/ipc.h> 32 #include <sys/sem.h> 33 #include <libclone.h> 34 #include "test.h" 35 #include "ipcns_helper.h" 36 37 #define MY_KEY 154326L 38 #define UNSHARESTR "unshare" 39 #define CLONESTR "clone" 40 #define NONESTR "none" 41 42 char *TCID = "sem_nstest"; 43 int TST_TOTAL = 1; 44 int p1[2]; 45 int p2[2]; 46 47 int check_semaphore(void *vtest) 48 { 49 char buf[3]; 50 int id; 51 52 (void) vtest; 53 54 close(p1[1]); 55 close(p2[0]); 56 57 read(p1[0], buf, 3); 58 id = semget(MY_KEY, 1, 0); 59 if (id == -1) 60 write(p2[1], "notfnd", 7); 61 else { 62 write(p2[1], "exists", 7); 63 tst_resm(TINFO, "PID %d: Fetched existing semaphore..id = %d", 64 getpid(), id); 65 } 66 tst_exit(); 67 } 68 69 static void setup(void) 70 { 71 tst_require_root(); 72 check_newipc(); 73 } 74 75 int main(int argc, char *argv[]) 76 { 77 int ret, use_clone = T_NONE, id; 78 char *tsttype = NONESTR; 79 char buf[7]; 80 81 setup(); 82 83 if (argc != 2) { 84 tst_resm(TFAIL, "Usage: %s <clone| unshare| none>", argv[0]); 85 tst_brkm(TFAIL, NULL, " where clone, unshare, or fork specifies" 86 " unshare method."); 87 } 88 89 /* Using PIPE's to sync between container and Parent */ 90 if (pipe(p1) == -1) { 91 perror("pipe"); 92 exit(EXIT_FAILURE); 93 } 94 if (pipe(p2) == -1) { 95 perror("pipe"); 96 exit(EXIT_FAILURE); 97 } 98 99 if (strcmp(argv[1], "clone") == 0) { 100 use_clone = T_CLONE; 101 tsttype = CLONESTR; 102 } else if (strcmp(argv[1], "unshare") == 0) { 103 use_clone = T_UNSHARE; 104 tsttype = UNSHARESTR; 105 } 106 107 /* 1. Create (or fetch if existing) the binary semaphore */ 108 id = semget(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666); 109 if (id == -1) { 110 perror("Semaphore create"); 111 if (errno != EEXIST) { 112 perror("semget failure"); 113 tst_brkm(TBROK, NULL, "Semaphore creation failed"); 114 } 115 id = semget(MY_KEY, 1, 0); 116 if (id == -1) { 117 perror("Semaphore create"); 118 tst_brkm(TBROK, NULL, "Semaphore operation failed"); 119 } 120 } 121 122 tst_resm(TINFO, "Semaphore namespaces Isolation test : %s", tsttype); 123 /* fire off the test */ 124 ret = 125 do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_semaphore, 126 NULL); 127 if (ret < 0) { 128 tst_brkm(TFAIL, NULL, "%s failed", tsttype); 129 } 130 131 close(p1[0]); 132 close(p2[1]); 133 write(p1[1], "go", 3); 134 read(p2[0], buf, 7); 135 136 if (strcmp(buf, "exists") == 0) { 137 if (use_clone == T_NONE) 138 tst_resm(TPASS, "Plain cloned process found semaphore " 139 "inside container"); 140 else 141 tst_resm(TFAIL, 142 "%s: Container init process found semaphore", 143 tsttype); 144 } else { 145 if (use_clone == T_NONE) 146 tst_resm(TFAIL, 147 "Plain cloned process didn't find semaphore"); 148 else 149 tst_resm(TPASS, "%s: Container didn't find semaphore", 150 tsttype); 151 } 152 153 /* Delete the semaphore */ 154 id = semget(MY_KEY, 1, 0); 155 semctl(id, IPC_RMID, 0); 156 157 tst_exit(); 158 } 159