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 struct tcase {
38 int *shmid;
39 int cmd;
40 struct shmid_ds *sbuf;
41 int error;
42 } tcases[] = {
43 /* EFAULT - IPC_SET & buf isn't valid */
44 {&shm_id_2, IPC_SET, (struct shmid_ds *)-1, EFAULT},
45 /* EFAULT - IPC_STAT & buf isn't valid */
46 {&shm_id_2, IPC_STAT, (struct shmid_ds *)-1, EFAULT},
47 /* EINVAL - the shmid is not valid */
48 {&shm_id_3, IPC_STAT, &buf, EINVAL},
49 /* EINVAL - the command is not valid */
50 {&shm_id_2, -1, &buf, EINVAL},
51 };
52
test_hugeshmctl(unsigned int i)53 static void test_hugeshmctl(unsigned int i)
54 {
55 TEST(shmctl(*(tcases[i].shmid), tcases[i].cmd, tcases[i].sbuf));
56 if (TST_RET != -1) {
57 tst_res(TFAIL, "shmctl succeeded unexpectedly");
58 return;
59 }
60
61 if (TST_ERR == tcases[i].error) {
62 tst_res(TPASS | TTERRNO, "shmctl failed as expected");
63 return;
64 }
65
66 tst_res(TFAIL | TTERRNO,
67 "shmctl failed unexpectedly - expect errno = %d, got",
68 tcases[i].error);
69 }
70
setup(void)71 static void setup(void)
72 {
73 long hpage_size;
74
75 if (tst_hugepages == 0)
76 tst_brk(TCONF, "No enough hugepages for testing.");
77
78 hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
79
80 shm_size = hpage_size * (tst_hugepages / 2);
81 update_shm_size(&shm_size);
82 shmkey = getipckey();
83
84 /* create a shared memory segment without read or write permissions */
85 shm_id_1 = shmget(shmkey, shm_size, SHM_HUGETLB | IPC_CREAT | IPC_EXCL);
86 if (shm_id_1 == -1)
87 tst_brk(TBROK | TERRNO, "shmget #1");
88
89 /* create a shared memory segment with read and write permissions */
90 shm_id_2 = shmget(shmkey + 1, shm_size,
91 SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
92 if (shm_id_2 == -1)
93 tst_brk(TBROK | TERRNO, "shmget #2");
94 }
95
cleanup(void)96 static void cleanup(void)
97 {
98 rm_shm(shm_id_1);
99 rm_shm(shm_id_2);
100 }
101
102 static struct tst_test test = {
103 .test = test_hugeshmctl,
104 .tcnt = ARRAY_SIZE(tcases),
105 .needs_root = 1,
106 .needs_tmpdir = 1,
107 .options = (struct tst_option[]) {
108 {"s:", &nr_opt, "Set the number of the been allocated hugepages"},
109 {}
110 },
111 .setup = setup,
112 .cleanup = cleanup,
113 .request_hugepages = 128,
114 };
115