• 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  *	hugeshmctl02 - check for EACCES, EFAULT and EINVAL errors
10  *
11  * ALGORITHM
12  *	create a large shared memory segment without read or write permissions
13  *	create a large shared memory segment with read & write permissions
14  *	loop if that option was specified
15  *	  call shmctl() using five different invalid cases
16  *	  check the errno value
17  *	    issue a PASS message if we get EACCES, EFAULT or EINVAL
18  *	  otherwise, the tests fails
19  *	    issue a FAIL message
20  *	call cleanup
21  *
22  * HISTORY
23  *	03/2001 - Written by Wayne Boyer
24  *	04/2004 - Updated by Robbie Williamson
25  */
26 
27 #include <pwd.h>
28 #include <limits.h>
29 #include "hugetlb.h"
30 
31 static size_t shm_size;
32 static int shm_id_1 = -1;
33 static int shm_id_2 = -1;
34 static int shm_id_3 = -1;
35 static struct shmid_ds buf;
36 
37 static struct tst_option options[] = {
38 	{"s:", &nr_opt, "-s num   Set the number of the been allocated hugepages"},
39 	{NULL, NULL, NULL}
40 };
41 
42 struct tcase {
43 	int *shmid;
44 	int cmd;
45 	struct shmid_ds *sbuf;
46 	int error;
47 } tcases[] = {
48 	/* EFAULT - IPC_SET & buf isn't valid */
49 	{&shm_id_2, IPC_SET, (struct shmid_ds *)-1, EFAULT},
50 	/* EFAULT - IPC_STAT & buf isn't valid */
51 	{&shm_id_2, IPC_STAT, (struct shmid_ds *)-1, EFAULT},
52 	/* EINVAL - the shmid is not valid */
53 	{&shm_id_3, IPC_STAT, &buf, EINVAL},
54 	/* EINVAL - the command is not valid */
55 	{&shm_id_2, -1, &buf, EINVAL},
56 };
57 
test_hugeshmctl(unsigned int i)58 static void test_hugeshmctl(unsigned int i)
59 {
60 	TEST(shmctl(*(tcases[i].shmid), tcases[i].cmd, tcases[i].sbuf));
61 	if (TST_RET != -1) {
62 		tst_res(TFAIL, "shmctl succeeded unexpectedly");
63 		return;
64 	}
65 
66 	if (TST_ERR == tcases[i].error) {
67 		tst_res(TPASS | TTERRNO, "shmctl failed as expected");
68 		return;
69 	}
70 
71 	tst_res(TFAIL | TTERRNO,
72 			"shmctl failed unexpectedly - expect errno = %d, got",
73 			tcases[i].error);
74 }
75 
setup(void)76 static void setup(void)
77 {
78 	long hpage_size;
79 
80 	if (tst_hugepages == 0)
81 		tst_brk(TCONF, "No enough hugepages for testing.");
82 
83 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
84 
85 	shm_size = hpage_size * (tst_hugepages / 2);
86 	update_shm_size(&shm_size);
87 	shmkey = getipckey();
88 
89 	/* create a shared memory segment without read or write permissions */
90 	shm_id_1 = shmget(shmkey, shm_size, SHM_HUGETLB | IPC_CREAT | IPC_EXCL);
91 	if (shm_id_1 == -1)
92 		tst_brk(TBROK | TERRNO, "shmget #1");
93 
94 	/* create a shared memory segment with read and write permissions */
95 	shm_id_2 = shmget(shmkey + 1, shm_size,
96 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
97 	if (shm_id_2 == -1)
98 		tst_brk(TBROK | TERRNO, "shmget #2");
99 }
100 
cleanup(void)101 static void cleanup(void)
102 {
103 	rm_shm(shm_id_1);
104 	rm_shm(shm_id_2);
105 }
106 
107 static struct tst_test test = {
108 	.test = test_hugeshmctl,
109 	.tcnt = ARRAY_SIZE(tcases),
110 	.needs_root = 1,
111 	.needs_tmpdir = 1,
112 	.options = options,
113 	.setup = setup,
114 	.cleanup = cleanup,
115 	.request_hugepages = 128,
116 };
117