• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2004
4  * Copyright (c) Linux Test Project, 2004-2020
5  */
6 
7 /*
8  * DESCRIPTION
9  *	hugeshmctl01 - test the IPC_STAT, IPC_SET and IPC_RMID commands as
10  *		   they are used with shmctl()
11  *
12  * ALGORITHM
13  *	loop if that option was specified
14  *	create a large shared memory segment with read and write permission
15  *	set up any test case specific conditions
16  *	call shmctl() using the TEST macro
17  *	check the return code
18  *	  if failure, issue a FAIL message.
19  *	otherwise,
20  *	  if doing functionality testing
21  *		call the correct test function
22  *		if the conditions are correct,
23  *			issue a PASS message
24  *		otherwise
25  *			issue a FAIL message
26  *	  otherwise
27  *	    issue a PASS message
28  *	call cleanup
29  *
30  * HISTORY
31  *	03/2001 - Written by Wayne Boyer
32  *	04/2004 - Updated by Robbie Williamson
33  */
34 
35 #include <limits.h>
36 #include "hugetlb.h"
37 
38 #define N_ATTACH	4U
39 #define NEWMODE		0066
40 
41 static size_t shm_size;
42 static int shm_id_1 = -1;
43 static struct shmid_ds buf;
44 static time_t save_time;
45 static void *attach_to_parent;
46 
47 static void stat_setup_1(void);
48 static void stat_cleanup(void);
49 static void stat_setup_2(void);
50 static void set_setup(void);
51 static void func_stat(void);
52 static void func_set(void);
53 static void func_rmid(void);
54 static void *set_shmat(void);
55 
56 static struct tst_option options[] = {
57 	{"s:", &nr_opt, "-s num   Set the number of the been allocated hugepages"},
58 	{NULL, NULL, NULL}
59 };
60 
61 struct tcase {
62 	int cmd;
63 	void (*func_test) (void);
64 	void (*func_setup) (void);
65 } tcases[] = {
66 	{IPC_STAT, func_stat, stat_setup_1},
67 	{IPC_STAT, func_stat, stat_setup_2},
68 	{IPC_SET,  func_set,  set_setup},
69 	{IPC_RMID, func_rmid, NULL}
70 };
71 
test_hugeshmctl(unsigned int i)72 static void test_hugeshmctl(unsigned int i)
73 {
74 	/*
75 	 * Create a shared memory segment with read and write
76 	 * permissions.  Do this here instead of in setup()
77 	 * so that looping (-i) will work correctly.
78 	 */
79 	if (i == 0)
80 		shm_id_1 = shmget(shmkey, shm_size,
81 			SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
82 	if (shm_id_1 == -1)
83 		tst_brk(TBROK | TERRNO, "shmget #main");
84 
85 	if (tcases[i].func_setup != NULL)
86 		(*tcases[i].func_setup) ();
87 
88 	if (shmctl(shm_id_1, tcases[i].cmd, &buf) == -1) {
89 		tst_res(TFAIL | TERRNO, "shmctl #main");
90 		return;
91 	}
92 	(*tcases[i].func_test)();
93 }
94 
95 /*
96  * set_shmat() - Attach the shared memory and return the pointer.
97  */
set_shmat(void)98 void *set_shmat(void)
99 {
100 	void *rval;
101 
102 	rval = shmat(shm_id_1, 0, 0);
103 	if (rval == (void *)-1)
104 		tst_brk(TBROK | TERRNO, "set shmat");
105 
106 	return rval;
107 }
108 
109 /*
110  * stat_setup_2() - Set up for the IPC_STAT command with shmctl().
111  * 		  Attach the shared memory to parent process and
112  * 		  some children will inherit the shared memory.
113  */
stat_setup_2(void)114 static void stat_setup_2(void)
115 {
116 	if (!attach_to_parent)
117 		attach_to_parent = set_shmat();
118 	stat_setup_1();
119 }
120 
121 /*
122  * stat_setup_1() - Set up for the IPC_STAT command with shmctl().
123  *                some children will inherit or attatch the shared memory.
124  *                It deponds on whther we attach the shared memory
125  *                to parent process.
126  */
stat_setup_1(void)127 static void stat_setup_1(void)
128 {
129 	unsigned int i;
130 	void *test;
131 	pid_t pid;
132 
133 	for (i = 0; i < N_ATTACH; i++) {
134 		switch (pid = SAFE_FORK()) {
135 		case 0:
136 			test = (attach_to_parent == NULL) ? set_shmat() : attach_to_parent;
137 			/* do an assignement for fun */
138 			*(int *)test = i;
139 
140 			TST_CHECKPOINT_WAKE(0);
141 
142 			TST_CHECKPOINT_WAIT(1);
143 
144 			/* now we're back - detach the memory and exit */
145 			if (shmdt(test) == -1)
146 				tst_brk(TBROK | TERRNO,
147 					 "shmdt in stat_setup()");
148 
149 			exit(0);
150 		default:
151 			TST_CHECKPOINT_WAIT(0);
152 		}
153 	}
154 }
155 
156 
157 /*
158  * func_stat() - check the functionality of the IPC_STAT command with shmctl()
159  *		 by looking at the pid of the creator, the segement size,
160  *		 the number of attaches and the mode.
161  */
func_stat(void)162 static void func_stat(void)
163 {
164 	pid_t pid;
165 	unsigned int num;
166 
167 	/* check perm, pid, nattach and size */
168 	pid = getpid();
169 
170 	if (buf.shm_cpid != pid) {
171 		tst_res(TFAIL, "creator pid is incorrect");
172 		goto fail;
173 	}
174 
175 	if (buf.shm_segsz != shm_size) {
176 		tst_res(TFAIL, "segment size is incorrect");
177 		goto fail;
178 	}
179 
180 	/*
181 	 * The first case, only the children attach the memory, so
182 	 * the attaches equal N_ATTACH. The second case, the parent
183 	 * attaches the memory and the children inherit that memory
184 	 * so the attaches equal N_ATTACH + 1.
185 	 */
186 	num = (attach_to_parent == NULL) ? 0 : 1;
187 	if (buf.shm_nattch != N_ATTACH + num) {
188 		tst_res(TFAIL, "# of attaches is incorrect - %lu",
189 			 (unsigned long)buf.shm_nattch);
190 		goto fail;
191 	}
192 
193 	/* use MODE_MASK to make sure we are comparing the last 9 bits */
194 	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
195 		tst_res(TFAIL, "segment mode is incorrect");
196 		goto fail;
197 	}
198 
199 	tst_res(TPASS, "pid, size, # of attaches and mode are correct "
200 		 "- pass #%d", num);
201 
202 fail:
203 	stat_cleanup();
204 
205 	/* save the change time for use in the next test */
206 	save_time = buf.shm_ctime;
207 }
208 
209 /*
210  * stat_cleanup() - signal the children to clean up after themselves and
211  *		    have the parent make dessert, er, um, make that remove
212  *		    the shared memory that is no longer needed.
213  */
stat_cleanup(void)214 static void stat_cleanup(void)
215 {
216 	unsigned int i;
217 	int status;
218 
219 	/* wake up the childern so they can detach the memory and exit */
220 	TST_CHECKPOINT_WAKE2(1, N_ATTACH);
221 
222 	for (i = 0; i < N_ATTACH; i++)
223 		SAFE_WAIT(&status);
224 
225 	/* remove the parent's shared memory if we set*/
226 	if (attach_to_parent) {
227 		if (shmdt(attach_to_parent) == -1)
228 			tst_res(TFAIL | TERRNO, "shmdt in stat_cleanup()");
229 		attach_to_parent = NULL;
230 	}
231 }
232 
233 /*
234  * set_setup() - set up for the IPC_SET command with shmctl()
235  */
set_setup(void)236 static void set_setup(void)
237 {
238 	/* set up a new mode for the shared memory segment */
239 	buf.shm_perm.mode = SHM_RW | NEWMODE;
240 
241 	/* sleep for one second to get a different shm_ctime value */
242 	sleep(1);
243 }
244 
245 /*
246  * func_set() - check the functionality of the IPC_SET command with shmctl()
247  */
func_set(void)248 static void func_set(void)
249 {
250 	/* first stat the shared memory to get the new data */
251 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
252 		tst_res(TFAIL | TERRNO, "shmctl in func_set()");
253 		return;
254 	}
255 
256 	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW | NEWMODE) & MODE_MASK)) {
257 		tst_res(TFAIL, "new mode is incorrect");
258 		return;
259 	}
260 
261 	if (save_time >= buf.shm_ctime) {
262 		tst_res(TFAIL, "change time is incorrect");
263 		return;
264 	}
265 
266 	tst_res(TPASS, "new mode and change time are correct");
267 }
268 
269 /*
270  * func_rmid() - check the functionality of the IPC_RMID command with shmctl()
271  */
func_rmid(void)272 static void func_rmid(void)
273 {
274 	/* Do another shmctl() - we should get EINVAL */
275 	if (shmctl(shm_id_1, IPC_STAT, &buf) != -1)
276 		tst_brk(TBROK, "shmctl in func_rmid() "
277 			 "succeeded unexpectedly");
278 	if (errno != EINVAL)
279 		tst_res(TFAIL | TERRNO, "shmctl in func_rmid() failed "
280 			 "unexpectedly - expect errno=EINVAL, got");
281 	else
282 		tst_res(TPASS, "shmctl in func_rmid() failed as expected, "
283 			 "shared memory appears to be removed");
284 	shm_id_1 = -1;
285 }
286 
setup(void)287 void setup(void)
288 {
289 	long hpage_size;
290 
291 	if (tst_hugepages == 0)
292 		tst_brk(TCONF, "No enough hugepages for testing.");
293 
294 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
295 
296 	shm_size = hpage_size * tst_hugepages / 2;
297 	update_shm_size(&shm_size);
298 	shmkey = getipckey();
299 }
300 
cleanup(void)301 void cleanup(void)
302 {
303 	rm_shm(shm_id_1);
304 }
305 
306 static struct tst_test test = {
307 	.tcnt = ARRAY_SIZE(tcases),
308 	.needs_root = 1,
309 	.forks_child = 1,
310 	.options = options,
311 	.setup = setup,
312 	.cleanup = cleanup,
313 	.test = test_hugeshmctl,
314 	.needs_checkpoints = 1,
315 	.request_hugepages = 128,
316 };
317