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/time.h>
39 #include <pwd.h>
40 #include "hugetlb.h"
41
42 key_t shmkey;
43
44 /*
45 * getipckey() - generates and returns a message key used by the "get"
46 * calls to create an IPC resource.
47 */
getipckey(void)48 int getipckey(void)
49 {
50 const char a = 'a';
51 int ascii_a = (int)a;
52 char *curdir = NULL;
53 size_t size = 0;
54 key_t ipc_key;
55 struct timeval time_info;
56
57 curdir = getcwd(curdir, size);
58 if (curdir == NULL)
59 tst_brk(TBROK | TERRNO, "getcwd(curdir)");
60
61 /*
62 * Get a Sys V IPC key
63 *
64 * ftok() requires a character as a second argument. This is
65 * refered to as a "project identifier" in the man page. In
66 * order to maximize the chance of getting a unique key, the
67 * project identifier is a "random character" produced by
68 * generating a random number between 0 and 25 and then adding
69 * that to the ascii value of 'a'. The "seed" for the random
70 * number is the microseconds value that is set in the timeval
71 * structure after calling gettimeofday().
72 */
73 gettimeofday(&time_info, NULL);
74 srandom((unsigned int)time_info.tv_usec);
75
76 ipc_key = ftok(curdir, ascii_a + random() % 26);
77 if (ipc_key == -1)
78 tst_brk(TBROK | TERRNO, __func__);
79
80 return ipc_key;
81 }
82
83 /*
84 * getuserid() - return the integer value for the "user" id
85 */
getuserid(char * user)86 int getuserid(char *user)
87 {
88 struct passwd *ent;
89
90 ent = getpwnam(user);
91 if (ent == NULL)
92 tst_brk(TBROK | TERRNO, "getpwnam");
93
94 return ent->pw_uid;
95 }
96
97 /*
98 * rm_shm() - removes a shared memory segment.
99 */
rm_shm(int shm_id)100 void rm_shm(int shm_id)
101 {
102 if (shm_id == -1)
103 return;
104
105 /*
106 * check for # of attaches ?
107 */
108 if (shmctl(shm_id, IPC_RMID, NULL) == -1) {
109 tst_res(TINFO, "WARNING: shared memory deletion failed.");
110 tst_res(TINFO, "This could lead to IPC resource problems.");
111 tst_res(TINFO, "id = %d", shm_id);
112 }
113 }
114