1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* 21f08c3bdfSopenharmony_ci * NAME 22f08c3bdfSopenharmony_ci * hugetlb.c 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * DESCRIPTION 25f08c3bdfSopenharmony_ci * common routines for the hugepage tests. 26f08c3bdfSopenharmony_ci * 27f08c3bdfSopenharmony_ci * The library contains the following routines: 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * getipckey() 30f08c3bdfSopenharmony_ci * getuserid() 31f08c3bdfSopenharmony_ci * rm_shm() 32f08c3bdfSopenharmony_ci */ 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_ci#define TST_NO_DEFAULT_MAIN 35f08c3bdfSopenharmony_ci#include <sys/types.h> 36f08c3bdfSopenharmony_ci#include <sys/ipc.h> 37f08c3bdfSopenharmony_ci#include <sys/shm.h> 38f08c3bdfSopenharmony_ci#include <sys/time.h> 39f08c3bdfSopenharmony_ci#include <pwd.h> 40f08c3bdfSopenharmony_ci#include "hugetlb.h" 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cikey_t shmkey; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci/* 45f08c3bdfSopenharmony_ci * getipckey() - generates and returns a message key used by the "get" 46f08c3bdfSopenharmony_ci * calls to create an IPC resource. 47f08c3bdfSopenharmony_ci */ 48f08c3bdfSopenharmony_ciint getipckey(void) 49f08c3bdfSopenharmony_ci{ 50f08c3bdfSopenharmony_ci const char a = 'a'; 51f08c3bdfSopenharmony_ci int ascii_a = (int)a; 52f08c3bdfSopenharmony_ci char *curdir = NULL; 53f08c3bdfSopenharmony_ci size_t size = 0; 54f08c3bdfSopenharmony_ci key_t ipc_key; 55f08c3bdfSopenharmony_ci struct timeval time_info; 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci curdir = getcwd(curdir, size); 58f08c3bdfSopenharmony_ci if (curdir == NULL) 59f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "getcwd(curdir)"); 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci /* 62f08c3bdfSopenharmony_ci * Get a Sys V IPC key 63f08c3bdfSopenharmony_ci * 64f08c3bdfSopenharmony_ci * ftok() requires a character as a second argument. This is 65f08c3bdfSopenharmony_ci * refered to as a "project identifier" in the man page. In 66f08c3bdfSopenharmony_ci * order to maximize the chance of getting a unique key, the 67f08c3bdfSopenharmony_ci * project identifier is a "random character" produced by 68f08c3bdfSopenharmony_ci * generating a random number between 0 and 25 and then adding 69f08c3bdfSopenharmony_ci * that to the ascii value of 'a'. The "seed" for the random 70f08c3bdfSopenharmony_ci * number is the microseconds value that is set in the timeval 71f08c3bdfSopenharmony_ci * structure after calling gettimeofday(). 72f08c3bdfSopenharmony_ci */ 73f08c3bdfSopenharmony_ci gettimeofday(&time_info, NULL); 74f08c3bdfSopenharmony_ci srandom((unsigned int)time_info.tv_usec); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci ipc_key = ftok(curdir, ascii_a + random() % 26); 77f08c3bdfSopenharmony_ci if (ipc_key == -1) 78f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, __func__); 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci return ipc_key; 81f08c3bdfSopenharmony_ci} 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci/* 84f08c3bdfSopenharmony_ci * getuserid() - return the integer value for the "user" id 85f08c3bdfSopenharmony_ci */ 86f08c3bdfSopenharmony_ciint getuserid(char *user) 87f08c3bdfSopenharmony_ci{ 88f08c3bdfSopenharmony_ci struct passwd *ent; 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci ent = getpwnam(user); 91f08c3bdfSopenharmony_ci if (ent == NULL) 92f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "getpwnam"); 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci return ent->pw_uid; 95f08c3bdfSopenharmony_ci} 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci/* 98f08c3bdfSopenharmony_ci * rm_shm() - removes a shared memory segment. 99f08c3bdfSopenharmony_ci */ 100f08c3bdfSopenharmony_civoid rm_shm(int shm_id) 101f08c3bdfSopenharmony_ci{ 102f08c3bdfSopenharmony_ci if (shm_id == -1) 103f08c3bdfSopenharmony_ci return; 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci /* 106f08c3bdfSopenharmony_ci * check for # of attaches ? 107f08c3bdfSopenharmony_ci */ 108f08c3bdfSopenharmony_ci if (shmctl(shm_id, IPC_RMID, NULL) == -1) { 109f08c3bdfSopenharmony_ci tst_res(TINFO, "WARNING: shared memory deletion failed."); 110f08c3bdfSopenharmony_ci tst_res(TINFO, "This could lead to IPC resource problems."); 111f08c3bdfSopenharmony_ci tst_res(TINFO, "id = %d", shm_id); 112f08c3bdfSopenharmony_ci } 113f08c3bdfSopenharmony_ci} 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci#define RANDOM_CONSTANT 0x1234ABCD 116f08c3bdfSopenharmony_ciint do_readback(void *p, size_t size, char *desc) 117f08c3bdfSopenharmony_ci{ 118f08c3bdfSopenharmony_ci unsigned int *q = p; 119f08c3bdfSopenharmony_ci size_t i; 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci for (i = 0; i < (size / sizeof(*q)); i++) 122f08c3bdfSopenharmony_ci q[i] = RANDOM_CONSTANT ^ i; 123f08c3bdfSopenharmony_ci 124f08c3bdfSopenharmony_ci for (i = 0; i < (size / sizeof(*q)); i++) { 125f08c3bdfSopenharmony_ci if (q[i] != (RANDOM_CONSTANT ^ i)) { 126f08c3bdfSopenharmony_ci tst_res(TFAIL, "At \"%s\": Mismatch at offset 0x%lx: 0x%x " 127f08c3bdfSopenharmony_ci "instead of 0x%lx", desc, i, q[i], RANDOM_CONSTANT ^ i); 128f08c3bdfSopenharmony_ci return -1; 129f08c3bdfSopenharmony_ci } 130f08c3bdfSopenharmony_ci } 131f08c3bdfSopenharmony_ci return 0; 132f08c3bdfSopenharmony_ci} 133