sem_nstest.c revision 2c28215423293e443469a07ae7011135d058b671
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15*
16* Author: Veerendra C <vechandr@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
36#define MY_KEY     154326L
37#define UNSHARESTR "unshare"
38#define CLONESTR   "clone"
39#define NONESTR    "none"
40
41char *TCID = "sem_nstest";
42int TST_TOTAL = 1;
43int p1[2];
44int p2[2];
45
46int check_semaphore(void *vtest)
47{
48	char buf[3];
49	int id;
50
51	close(p1[1]);
52	close(p2[0]);
53
54	read(p1[0], buf, 3);
55	id = semget(MY_KEY, 1, 0);
56	if (id == -1)
57		write(p2[1], "notfnd", 7);
58	else {
59		write(p2[1], "exists", 7);
60		tst_resm(TINFO, "PID %d: Fetched existing semaphore..id = %d\n",
61						getpid(), id );
62	}
63	tst_exit();
64
65	return 0;
66}
67
68int main(int argc, char *argv[])
69{
70	int ret, use_clone = T_NONE, id;
71	char *tsttype = NONESTR;
72	char buf[7];
73
74	if (argc != 2) {
75		tst_resm(TFAIL, "Usage: %s <clone| unshare| none>\n", argv[0]);
76		tst_resm(TFAIL, " where clone, unshare, or fork specifies"
77				" unshare method.");
78		tst_exit();
79	}
80
81	/* Using PIPE's to sync between container and Parent */
82	if (pipe(p1) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
83	if (pipe(p2) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
84
85	if (strcmp(argv[1], "clone") == 0) {
86		use_clone = T_CLONE;
87		tsttype = CLONESTR;
88	} else if (strcmp(argv[1], "unshare") == 0) {
89		use_clone = T_UNSHARE;
90		tsttype = UNSHARESTR;
91	}
92
93	/* 1. Create (or fetch if existing) the binary semaphore */
94	id = semget(MY_KEY, 1, IPC_CREAT | IPC_EXCL | 0666);
95	if (id == -1) {
96		perror( "Semaphore create" );
97		if (errno != EEXIST) {
98			perror("semget failure");
99			tst_resm(TBROK, "Semaphore creation failed\n");
100			tst_exit();
101		}
102		id = semget(MY_KEY, 1, 0);
103		if (id == -1) {
104			perror( "Semaphore create" );
105			tst_resm(TBROK, "Semaphore operation failed\n");
106			tst_exit();
107		}
108	}
109
110	tst_resm(TINFO, "Semaphore namespaces Isolation test : %s\n", tsttype);
111	/* fire off the test */
112	ret = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_semaphore, NULL);
113	if (ret < 0) {
114		tst_resm(TFAIL, "%s failed\n", tsttype);
115		tst_exit();
116	}
117
118	close(p1[0]);
119	close(p2[1]);
120	write(p1[1], "go", 3);
121	read(p2[0], buf, 7);
122
123	if (strcmp(buf, "exists") == 0) {
124		if (use_clone == T_NONE)
125			tst_resm(TPASS, "Plain cloned process found semaphore "
126						    "inside container\n");
127		else
128			tst_resm(TFAIL, "%s: Container init process found semaphore\n",
129							tsttype);
130	} else {
131		if (use_clone == T_NONE)
132			tst_resm(TFAIL, "Plain cloned process didn't find semaphore\n");
133		else
134			tst_resm(TPASS, "%s: Container didn't find semaphore", tsttype);
135	}
136
137	/* Delete the semaphore */
138	id = semget(MY_KEY, 1, 0);
139	semctl(id, IPC_RMID, 0);
140
141	tst_exit();
142
143	tst_exit();
144}