• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
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  *	hugetlb.c
23  *
24  * DESCRIPTION
25  *	common routines for the hugepage tests.
26  *
27  *	The library contains the following routines:
28  *
29  *	getipckey()
30  *	getuserid()
31  *	rm_shm()
32  */
33 
34 #define TST_NO_DEFAULT_MAIN
35 #include <sys/types.h>
36 #include <sys/ipc.h>
37 #include <sys/shm.h>
38 #include <sys/timeb.h>
39 #include <pwd.h>
40 #include "hugetlb.h"
41 
42 static long orig_hugepages = -1;
43 
save_nr_hugepages(void)44 long save_nr_hugepages(void)
45 {
46 	check_hugepage();
47 
48 	orig_hugepages = get_sys_tune("nr_hugepages");
49 
50 	return orig_hugepages;
51 }
52 
restore_nr_hugepages(void)53 void restore_nr_hugepages(void)
54 {
55 	if (orig_hugepages != -1)
56 		set_sys_tune("nr_hugepages", orig_hugepages, 0);
57 }
58 
59 /*
60  * getipckey() - generates and returns a message key used by the "get"
61  *		 calls to create an IPC resource.
62  */
getipckey(void)63 int getipckey(void)
64 {
65 	const char a = 'a';
66 	int ascii_a = (int)a;
67 	char *curdir = NULL;
68 	size_t size = 0;
69 	key_t ipc_key;
70 	struct timeb time_info;
71 
72 	curdir = getcwd(curdir, size);
73 	if (curdir == NULL)
74 		tst_brk(TBROK | TERRNO, "getcwd(curdir)");
75 
76 	/*
77 	 * Get a Sys V IPC key
78 	 *
79 	 * ftok() requires a character as a second argument.  This is
80 	 * refered to as a "project identifier" in the man page.  In
81 	 * order to maximize the chance of getting a unique key, the
82 	 * project identifier is a "random character" produced by
83 	 * generating a random number between 0 and 25 and then adding
84 	 * that to the ascii value of 'a'.  The "seed" for the random
85 	 * number is the millisecond value that is set in the timeb
86 	 * structure after calling ftime().
87 	 */
88 	ftime(&time_info);
89 	srandom((unsigned int)time_info.millitm);
90 
91 	ipc_key = ftok(curdir, ascii_a + random() % 26);
92 	if (ipc_key == -1)
93 		tst_brk(TBROK | TERRNO, __func__);
94 
95 	return ipc_key;
96 }
97 
98 /*
99  * getuserid() - return the integer value for the "user" id
100  */
getuserid(char * user)101 int getuserid(char *user)
102 {
103 	struct passwd *ent;
104 
105 	ent = getpwnam(user);
106 	if (ent == NULL)
107 		tst_brk(TBROK | TERRNO, "getpwnam");
108 
109 	return ent->pw_uid;
110 }
111 
112 /*
113  * rm_shm() - removes a shared memory segment.
114  */
rm_shm(int shm_id)115 void rm_shm(int shm_id)
116 {
117 	if (shm_id == -1)
118 		return;
119 
120 	/*
121 	 * check for # of attaches ?
122 	 */
123 	if (shmctl(shm_id, IPC_RMID, NULL) == -1) {
124 		tst_res(TINFO, "WARNING: shared memory deletion failed.");
125 		tst_res(TINFO, "This could lead to IPC resource problems.");
126 		tst_res(TINFO, "id = %d", shm_id);
127 	}
128 }
129