1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * hugeshmget02.c
23 *
24 * DESCRIPTION
25 * hugeshmget02 - check for ENOENT, EEXIST and EINVAL errors
26 *
27 * ALGORITHM
28 * create a large shared memory segment with read & write permissions
29 * loop if that option was specified
30 * call shmget() using five different invalid cases
31 * check the errno value
32 * issue a PASS message if we get ENOENT, EEXIST or EINVAL
33 * otherwise, the tests fails
34 * issue a FAIL message
35 * call cleanup
36 *
37 * USAGE: <for command-line>
38 * hugeshmget02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39 * where, -c n : Run n copies concurrently.
40 * -e : Turn on errno logging.
41 * -i n : Execute test n times.
42 * -I x : Execute test for x seconds.
43 * -P x : Pause for x seconds between iterations.
44 * -t : Turn on syscall timing.
45 *
46 * HISTORY
47 * 03/2001 - Written by Wayne Boyer
48 * 04/2004 - Updated by Robbie Williamson
49 *
50 * RESTRICTIONS
51 * none
52 */
53
54 #include "hugetlb.h"
55 #include "safe_macros.h"
56 #include "mem.h"
57
58 char *TCID = "hugeshmget02";
59 int TST_TOTAL = 4;
60
61 static size_t shm_size;
62 static int shm_id_1 = -1;
63 static int shm_nonexistent_key = -1;
64 static key_t shmkey2;
65
66 static long hugepages = 128;
67 static option_t options[] = {
68 {"s:", &sflag, &nr_opt},
69 {NULL, NULL, NULL}
70 };
71
72 struct test_case_t {
73 int *skey;
74 int size_coe;
75 int flags;
76 int error;
77 } TC[] = {
78 /* EINVAL - size is 0 */
79 {
80 &shmkey2, 0, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
81 /* EINVAL - size is larger than created segment */
82 {
83 &shmkey, 2, SHM_HUGETLB | SHM_RW, EINVAL},
84 /* EEXIST - the segment exists and IPC_CREAT | IPC_EXCL is given */
85 {
86 &shmkey, 1, SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW, EEXIST},
87 /* ENOENT - no segment exists for the key and IPC_CREAT is not given */
88 /* use shm_nonexistend_key (-1) as the key */
89 {
90 &shm_nonexistent_key, 1, SHM_HUGETLB | SHM_RW, ENOENT}
91 };
92
main(int ac,char ** av)93 int main(int ac, char **av)
94 {
95 int lc, i;
96 int shm_id_2 = -1;
97
98 tst_parse_opts(ac, av, options, NULL);
99
100 if (sflag)
101 hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);
102
103 setup();
104
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
106 tst_count = 0;
107
108 for (i = 0; i < TST_TOTAL; i++) {
109 /* If this key is existent, just remove it */
110 if (*TC[i].skey == -1) {
111 shm_id_2 = shmget(*(TC[i].skey), 0, 0);
112 if (shm_id_2 != -1)
113 shmctl(shm_id_2, IPC_RMID, NULL);
114 }
115
116 TEST(shmget(*(TC[i].skey), TC[i].size_coe * shm_size,
117 TC[i].flags));
118 if (TEST_RETURN != -1) {
119 tst_resm(TFAIL, "shmget succeeded "
120 "unexpectedly");
121 continue;
122 }
123 if (TEST_ERRNO == TC[i].error)
124 tst_resm(TPASS | TTERRNO, "shmget failed "
125 "as expected");
126 else
127 tst_resm(TFAIL | TTERRNO, "shmget failed "
128 "unexpectedly - expect errno=%d, "
129 "got", TC[i].error);
130 }
131 }
132 cleanup();
133 tst_exit();
134 }
135
setup(void)136 void setup(void)
137 {
138 long hpage_size;
139
140 tst_require_root();
141 check_hugepage();
142 tst_sig(NOFORK, DEF_HANDLER, cleanup);
143 tst_tmpdir();
144
145 orig_hugepages = get_sys_tune("nr_hugepages");
146 set_sys_tune("nr_hugepages", hugepages, 1);
147 hpage_size = read_meminfo("Hugepagesize:") * 1024;
148
149 shm_size = hpage_size * hugepages / 2;
150 update_shm_size(&shm_size);
151
152 shmkey = getipckey(cleanup);
153 shmkey2 = shmkey + 1;
154 shm_id_1 = shmget(shmkey, shm_size, IPC_CREAT | IPC_EXCL | SHM_RW);
155 if (shm_id_1 == -1)
156 tst_brkm(TBROK | TERRNO, cleanup, "shmget #setup");
157
158 TEST_PAUSE;
159 }
160
cleanup(void)161 void cleanup(void)
162 {
163 rm_shm(shm_id_1);
164
165 set_sys_tune("nr_hugepages", orig_hugepages, 0);
166
167 tst_rmdir();
168 }
169