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 * hugeshmat02.c 23 * 24 * DESCRIPTION 25 * hugeshmat02 - check for EINVAL and EACCES errors with hugetlb 26 * 27 * ALGORITHM 28 * loop if that option was specified 29 * call shmat() using three invalid test cases 30 * check the errno value 31 * issue a PASS message if we get EINVAL or EACCES 32 * otherwise, the tests fails 33 * issue a FAIL message 34 * call cleanup 35 * 36 * USAGE: <for command-line> 37 * hugeshmat02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 38 * where, -c n : Run n copies concurrently. 39 * -e : Turn on errno logging. 40 * -i n : Execute test n times. 41 * -I x : Execute test for x seconds. 42 * -P x : Pause for x seconds between iterations. 43 * -t : Turn on syscall timing. 44 * 45 * HISTORY 46 * 03/2001 - Written by Wayne Boyer 47 * 04/2004 - Updated By Robbie Williamson 48 * 49 * RESTRICTIONS 50 * Must be ran as root 51 */ 52 53#include <pwd.h> 54#include "hugetlb.h" 55#include "safe_macros.h" 56#include "mem.h" 57 58char *TCID = "hugeshmat02"; 59int TST_TOTAL = 2; 60 61#if __WORDSIZE == 64 62#define NADDR 0x10000000eef /* a 64bit non alligned address value */ 63#else 64#define NADDR 0x60000eef /* a non alligned address value */ 65#endif 66 67static size_t shm_size; 68static int shm_id_1 = -1; 69static int shm_id_2 = -1; 70static void *addr; 71 72static long hugepages = 128; 73static option_t options[] = { 74 {"s:", &sflag, &nr_opt}, 75 {NULL, NULL, NULL} 76}; 77 78struct test_case_t { 79 int *shmid; 80 void *addr; 81 int error; 82} TC[] = { 83 /* EINVAL - the shared memory ID is not valid */ 84 { 85 &shm_id_1, NULL, EINVAL}, 86 /* EINVAL - the address is not page aligned and SHM_RND is not given */ 87 { 88&shm_id_2, (void *)NADDR, EINVAL},}; 89 90int main(int ac, char **av) 91{ 92 int lc, i; 93 94 tst_parse_opts(ac, av, options, NULL); 95 96 if (sflag) 97 hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX); 98 99 setup(); 100 101 for (lc = 0; TEST_LOOPING(lc); lc++) { 102 tst_count = 0; 103 104 for (i = 0; i < TST_TOTAL; i++) { 105 addr = shmat(*(TC[i].shmid), TC[i].addr, 0); 106 if (addr != (void *)-1) { 107 tst_resm(TFAIL, "shmat suceeded unexpectedly"); 108 continue; 109 } 110 if (errno == TC[i].error) 111 tst_resm(TPASS | TERRNO, "shmat failed as " 112 "expected"); 113 else 114 tst_resm(TFAIL | TERRNO, "shmat failed " 115 "unexpectedly - expect errno=%d, " 116 "got", TC[i].error); 117 } 118 } 119 cleanup(); 120 tst_exit(); 121} 122 123void setup(void) 124{ 125 long hpage_size; 126 127 tst_require_root(); 128 check_hugepage(); 129 tst_sig(NOFORK, DEF_HANDLER, cleanup); 130 tst_tmpdir(); 131 132 orig_hugepages = get_sys_tune("nr_hugepages"); 133 set_sys_tune("nr_hugepages", hugepages, 1); 134 hpage_size = read_meminfo("Hugepagesize:") * 1024; 135 136 shm_size = hpage_size * hugepages / 2; 137 update_shm_size(&shm_size); 138 shmkey = getipckey(cleanup); 139 140 /* create a shared memory resource with read and write permissions */ 141 /* also post increment the shmkey for the next shmget call */ 142 shm_id_2 = shmget(shmkey++, shm_size, 143 SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL); 144 if (shm_id_2 == -1) 145 tst_brkm(TBROK | TERRNO, cleanup, "shmget"); 146 147 TEST_PAUSE; 148} 149 150void cleanup(void) 151{ 152 rm_shm(shm_id_2); 153 154 set_sys_tune("nr_hugepages", orig_hugepages, 0); 155 156 tst_rmdir(); 157} 158