blob: 0e34bc14f8f255af57779f4ce93938a4642d56b4 [file] [log] [blame]
subrata_modake63f96b2009-05-29 10:29:58 +00001/******************************************************************************/
yaberauneyac2365082009-11-04 06:18:27 +00002/* Copyright (c) Crackerjack Project., 2007-2008 ,Hitachi, Ltd */
yaberauneyaa95028f2009-11-04 04:28:20 +00003/* Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com>, */
subrata_modake63f96b2009-05-29 10:29:58 +00004/* Yumiko Sugita <yumiko.sugita.yf@hitachi.com>, */
5/* Satoshi Fujiwara <sa-fuji@sdl.hitachi.co.jp> */
yaberauneyaa95028f2009-11-04 04:28:20 +00006/* */
subrata_modake63f96b2009-05-29 10:29:58 +00007/* This program is free software; you can redistribute it and/or modify */
8/* it under the terms of the GNU General Public License as published by */
yaberauneyac2365082009-11-04 06:18:27 +00009/* the Free Software Foundation; either version 2 of the License, or */
10/* (at your option) any later version. */
11/* */
12/* This program is distributed in the hope that it will be useful, */
13/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
14/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
15/* the GNU General Public License for more details. */
16/* */
17/* You should have received a copy of the GNU General Public License */
18/* along with this program; if not, write to the Free Software */
subrata_modake63f96b2009-05-29 10:29:58 +000019/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
yaberauneyac2365082009-11-04 06:18:27 +000020/* */
subrata_modake63f96b2009-05-29 10:29:58 +000021/******************************************************************************/
22/******************************************************************************/
yaberauneyac2365082009-11-04 06:18:27 +000023/* */
24/* File: get_mempolicy01.c */
25/* */
26/* Description: This tests the get_mempolicy() syscall */
subrata_modake63f96b2009-05-29 10:29:58 +000027/* */
28/* */
29/* */
30/* */
31/* */
yaberauneyac2365082009-11-04 06:18:27 +000032/* */
33/* Usage: <for command-line> */
34/* get_mempolicy01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
35/* where, -c n : Run n copies concurrently. */
36/* -e : Turn on errno logging. */
37/* -i n : Execute test n times. */
38/* -I x : Execute test for x seconds. */
39/* -P x : Pause for x seconds between iterations. */
40/* -t : Turn on syscall timing. */
41/* */
42/* Total Tests: 1 */
43/* */
44/* Test Name: get_mempolicy01 */
45/* History: Porting from Crackerjack to LTP is done by */
46/* Manas Kumar Nayak maknayak@in.ibm.com> */
subrata_modake63f96b2009-05-29 10:29:58 +000047/******************************************************************************/
48#include <sys/syscall.h>
49#include <sys/types.h>
50#include <sys/mman.h>
51#include <getopt.h>
52#include <string.h>
53#include <stdlib.h>
54#include <errno.h>
55#include <stdio.h>
56#include <unistd.h>
yaberauneyaa95028f2009-11-04 04:28:20 +000057#include <libgen.h>
yaberauneyac2365082009-11-04 06:18:27 +000058#include "numaif.h"
59#include "include_j_h.h"
60#include "common_j_h.c"
subrata_modake63f96b2009-05-29 10:29:58 +000061
62/* Harness Specific Include Files. */
63#include "test.h"
64#include "usctest.h"
subrata_modake63f96b2009-05-29 10:29:58 +000065
66/* Extern Global Variables */
yaberauneyaa95028f2009-11-04 04:28:20 +000067extern int Tst_count; /* counter for tst_xxx routines. */
68extern char *TESTDIR; /* temporary dir created by tst_tmpdir() */
subrata_modake63f96b2009-05-29 10:29:58 +000069
70/* Global Variables */
71char *TCID = "get_mempolicy01"; /* Test program identifier.*/
72int testno;
yaberauneyaa95028f2009-11-04 04:28:20 +000073int TST_TOTAL = 1; /* total number of tests in this file. */
subrata_modake63f96b2009-05-29 10:29:58 +000074
75/*
76 * Macros
77 */
78#define SYSCALL_NAME "get_mempolicy"
79
yaberauneyaa95028f2009-11-04 04:28:20 +000080/* Extern Global Functions */
81/******************************************************************************/
82/* */
83/* Function: cleanup */
84/* */
85/* Description: Performs all one time clean up for this test on successful */
86/* completion, premature exit or failure. Closes all temporary */
87/* files, removes all temporary directories exits the test with */
88/* appropriate return code by calling tst_exit() function. */
89/* */
90/* Input: None. */
91/* */
92/* Output: None. */
93/* */
94/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
95/* On success - Exits calling tst_exit(). With '0' return code. */
96/* */
97/******************************************************************************/
98extern void cleanup() {
99 /* Remove tmp dir and all files in it */
100 TEST_CLEANUP;
101 tst_rmdir();
102
103 /* Exit with appropriate return code. */
104 tst_exit();
105}
106
107/* Local Functions */
108/******************************************************************************/
109/* */
110/* Function: setup */
111/* */
112/* Description: Performs all one time setup for this test. This function is */
113/* typically used to capture signals, create temporary dirs */
114/* and temporary files that may be used in the course of this */
115/* test. */
116/* */
117/* Input: None. */
118/* */
119/* Output: None. */
120/* */
121/* Return: On failure - Exits by calling cleanup(). */
122/* On success - returns 0. */
123/* */
124/******************************************************************************/
125void setup() {
126 /* Capture signals if any */
127 /* Create temporary directories */
128 TEST_PAUSE;
129 tst_tmpdir();
130}
subrata_modake63f96b2009-05-29 10:29:58 +0000131
132/*
133 * Global variables
134 */
135static int opt_debug;
136static char *progname;
137
138enum test_type {
yaberauneyaa95028f2009-11-04 04:28:20 +0000139 DEFAULT, /* get default policy */
140 ADDR, /* get policy of memory which include mapped address */
141 INVALID_POINTER,
142 INVALID_FLAGS,
subrata_modake63f96b2009-05-29 10:29:58 +0000143};
144
145enum from_node {
yaberauneyaa95028f2009-11-04 04:28:20 +0000146 NONE,
147 SELF,
subrata_modake63f96b2009-05-29 10:29:58 +0000148};
149
150
151/*
152 * Data Structure
153 */
154struct test_case {
yaberauneyaa95028f2009-11-04 04:28:20 +0000155 int ttype;
156 int policy;
157 int from_node;
158 int ret;
159 int err;
subrata_modake63f96b2009-05-29 10:29:58 +0000160};
161
162/* Test cases
163 *
164 * test status of errors on man page
165 *
yaberauneyaa95028f2009-11-04 04:28:20 +0000166 * (NONE) man page hadn't been completed.
subrata_modake63f96b2009-05-29 10:29:58 +0000167 *
168 * test status of errors NOT on man page
169 *
yaberauneyaa95028f2009-11-04 04:28:20 +0000170 * EFAULT v (invalid address)
171 * EINVAL v (invalid parameters)
subrata_modake63f96b2009-05-29 10:29:58 +0000172 */
173
174static struct test_case tcase[] = {
yaberauneyaa95028f2009-11-04 04:28:20 +0000175 { /* case00 */
176 .ttype = DEFAULT,
177 .policy = MPOL_DEFAULT,
178 .from_node = NONE,
179 .ret = 0,
180 .err = 0,
181 },
182 { /* case01 */
183 .ttype = DEFAULT,
184 .policy = MPOL_BIND,
185 .from_node = SELF,
186 .ret = 0,
187 .err = 0,
188 },
189 { /* case02 */
190 .ttype = DEFAULT,
191 .policy = MPOL_INTERLEAVE,
192 .from_node = SELF,
193 .ret = 0,
194 .err = 0,
195 },
196{ /* case03 */
197 .ttype = DEFAULT,
198 .policy = MPOL_PREFERRED,
199 .from_node = NONE,
200 .ret = 0,
201 .err = 0,
202 },
203 { /* case04 */
204 .ttype = DEFAULT,
205 .policy = MPOL_PREFERRED,
206 .from_node = SELF,
207 .ret = 0,
208 .err = 0,
209 },
210 { /* case05 */
211 .ttype = ADDR,
212 .policy = MPOL_DEFAULT,
213 .from_node = NONE,
214 .ret = 0,
215 .err = 0,
216 },
217{ /* case06 */
218 .ttype = ADDR,
219 .policy = MPOL_BIND,
220 .from_node = SELF,
221 .ret = 0,
222 .err = 0,
223 },
224 { /* case07 */
225 .ttype = ADDR,
226 .policy = MPOL_INTERLEAVE,
227 .from_node = SELF,
228 .ret = 0,
229 .err = 0,
230 },
231 { /* case08 */
232 .ttype = ADDR,
233 .policy = MPOL_PREFERRED,
234 .from_node = NONE,
235 .ret = 0,
236 .err = 0,
237 },
238{ /* case09 */
239 .ttype = ADDR,
240 .policy = MPOL_PREFERRED,
241 .from_node = SELF,
242 .ret = 0,
243 .err = 0,
244 },
245 { /* case10 */
246 .ttype = INVALID_POINTER,
247 .policy = MPOL_DEFAULT,
248 .from_node = NONE,
249 .ret = -1,
250 .err = EFAULT,
251 },
252 { /* case11 */
253 .ttype = INVALID_FLAGS,
254 .policy = MPOL_DEFAULT,
255 .from_node = NONE,
256 .ret = -1,
257 .err = EINVAL,
258 },
subrata_modake63f96b2009-05-29 10:29:58 +0000259};
260
yaberauneyaa95028f2009-11-04 04:28:20 +0000261#define MEM_LENGTH (4 * 1024 * 1024)
subrata_modake63f96b2009-05-29 10:29:58 +0000262/*
263 * do_test()
264 *
265 * Input : TestCase Data
266 * Return : RESULT_OK(0), RESULT_NG(1)
267 *
268 */
269
yaberauneyaa95028f2009-11-04 04:28:20 +0000270static int do_test (struct test_case *tc)
subrata_modake63f96b2009-05-29 10:29:58 +0000271{
yaberauneyaa95028f2009-11-04 04:28:20 +0000272 int sys_ret;
273 int sys_errno;
274 int result = RESULT_OK;
275 int rc, policy, flags, cmp_ok = 1;
276 nodemask_t nodemask, getnodemask;
277 unsigned long maxnode = NUMA_NUM_NODES;
278 char *p = NULL;
279 unsigned long len = MEM_LENGTH;
subrata_modake63f96b2009-05-29 10:29:58 +0000280
yaberauneyaa95028f2009-11-04 04:28:20 +0000281 /* We assume that there is only one node(node0). */
282 nodemask_zero(&nodemask);
283 nodemask_set(&nodemask, 0);
284 nodemask_zero(&getnodemask);
subrata_modake63f96b2009-05-29 10:29:58 +0000285
yaberauneyaa95028f2009-11-04 04:28:20 +0000286 switch (tc->ttype) {
287 case DEFAULT:
288 flags = 0;
289 p = NULL;
290 /* set memory policy */
291 if (tc->from_node == NONE)
292 TEST(rc = syscall(__NR_set_mempolicy, tc->policy, NULL, 0));
293 else
294 TEST(rc = syscall(__NR_set_mempolicy, tc->policy, &nodemask, maxnode));
subrata_modake63f96b2009-05-29 10:29:58 +0000295
296 if (TEST_RETURN < 0) {
vapierd0c9e5f2009-08-28 13:06:55 +0000297 tst_resm(TFAIL|TTERRNO, "set_mempolicy() failed");
yaberauneyaa95028f2009-11-04 04:28:20 +0000298 result = 1;
subrata_modake63f96b2009-05-29 10:29:58 +0000299 cleanup();
yaberauneyaa95028f2009-11-04 04:28:20 +0000300 tst_exit();
301 }
302 break;
303 default:
304 flags = MPOL_F_ADDR;
305 /* mmap memory */
306 p = mmap(NULL, len, PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
307 if (p == (void*)-1) {
vapierd0c9e5f2009-08-28 13:06:55 +0000308 tst_resm(TFAIL|TERRNO, "mmap() failed");
yaberauneyaa95028f2009-11-04 04:28:20 +0000309 result = 1;
subrata_modake63f96b2009-05-29 10:29:58 +0000310 cleanup();
yaberauneyaa95028f2009-11-04 04:28:20 +0000311 tst_exit();
312 }
313 /* set memory policy */
314 if (tc->from_node == NONE)
315 TEST(rc = syscall(__NR_mbind, p, len, tc->policy,NULL, 0, 0));
316 else
317 TEST(rc = syscall(__NR_mbind, p, len, tc->policy,&nodemask, maxnode, 0));
318 if (tc->ttype == INVALID_POINTER)
319 p = NULL;
subrata_modake63f96b2009-05-29 10:29:58 +0000320
321 if (tc->ttype == INVALID_FLAGS){
yaberauneyaa95028f2009-11-04 04:28:20 +0000322 flags = -1;
323 break;
324 }
subrata_modake63f96b2009-05-29 10:29:58 +0000325 }
yaberauneyaa95028f2009-11-04 04:28:20 +0000326 /*
327 * Execute system call
328 */
329 errno = 0;
330 TEST(sys_ret = syscall(__NR_get_mempolicy, &policy, &getnodemask, maxnode,p, flags));
331 sys_errno = errno;
332 if (sys_ret < 0)
333 goto TEST_END;
subrata_modake63f96b2009-05-29 10:29:58 +0000334
yaberauneyaa95028f2009-11-04 04:28:20 +0000335 // When policy equals MPOL_DEFAULT, then get_mempolicy not return node
336 if (tc->policy == MPOL_DEFAULT)
337 nodemask_zero(&nodemask);
338 cmp_ok = tc->policy == policy && (tc->from_node == NONE || nodemask_equal(&nodemask,&getnodemask));
339 if (opt_debug) {
340 nodemask_dump("nodemask Expect:", &nodemask);
341 nodemask_dump("nodemask Result:", &getnodemask);
342 tst_resm(TINFO,"policy E:%d R:%d", tc->policy, policy);
343 }
subrata_modake63f96b2009-05-29 10:29:58 +0000344TEST_END:
yaberauneyaa95028f2009-11-04 04:28:20 +0000345 /*
346 * Check results
347 */
348 result |= (sys_errno != tc->err) || !cmp_ok;
349 PRINT_RESULT_CMP(0, tc->ret, tc->err, sys_ret, sys_errno, cmp_ok);
350 return result;
subrata_modake63f96b2009-05-29 10:29:58 +0000351}
352
353
354/*
355 * usage()
356 */
357
358void usage(const char *progname)
359{
yaberauneyaa95028f2009-11-04 04:28:20 +0000360 tst_resm(TINFO, "usage: %s [options]", progname);
361 tst_resm(TINFO, "This is a regression test program of %s system call.", SYSCALL_NAME);
362 tst_resm(TINFO, "options:");
363 tst_resm(TINFO, " -d --debug Show debug messages");
364 tst_resm(TINFO, " -h --help Show this message");
subrata_modake63f96b2009-05-29 10:29:58 +0000365
yaberauneyaa95028f2009-11-04 04:28:20 +0000366 tst_resm(TINFO,"NG");
367 exit(1);
subrata_modake63f96b2009-05-29 10:29:58 +0000368}
369
370
371/*
372 * main()
373 */
374
375
376
yaberauneyaa95028f2009-11-04 04:28:20 +0000377int main(int argc, char **argv) {
378 int c, i, lc, ret;
379 char *msg; /* message returned from parse_opts */
subrata_modake63f96b2009-05-29 10:29:58 +0000380
381 struct option long_options[] = {
yaberauneyaa95028f2009-11-04 04:28:20 +0000382 { "debug", no_argument, 0, 'd' },
383 { "help", no_argument, 0, 'h' },
384 { NULL, 0, NULL, 0 }
385 };
subrata_modake63f96b2009-05-29 10:29:58 +0000386
yaberauneyaa95028f2009-11-04 04:28:20 +0000387 progname = basename(argv[0]);
subrata_modake63f96b2009-05-29 10:29:58 +0000388
yaberauneyaa95028f2009-11-04 04:28:20 +0000389 /* parse standard options */
390 if ((msg = parse_opts(argc, argv, (option_t *)NULL, NULL)) != (char *)NULL){
391 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
392 tst_exit();
393 }
subrata_modake63f96b2009-05-29 10:29:58 +0000394
yaberauneyaa95028f2009-11-04 04:28:20 +0000395 setup();
subrata_modake63f96b2009-05-29 10:29:58 +0000396
yaberauneyaa95028f2009-11-04 04:28:20 +0000397 /* Check looping state if -i option given */
398 for (lc = 0; TEST_LOOPING(lc); lc++) {
399
400 Tst_count = 0;
401
402 for (testno = 0; testno < TST_TOTAL; ++testno) {
403
404 TEST(c = getopt_long(argc, argv, "dh", long_options, NULL));
405
406 while (TEST_RETURN != -1) {
407 switch (c) {
408 case 'd':
409 opt_debug = 1;
410 break;
411 default:
412 usage(progname);
413 // NOTREACHED
414 }
415 }
416
417 /*
418 * Execute test
419 */
420 for (i = 0; i < (int)(sizeof(tcase) / sizeof(*tcase)); i++) {
421 tst_resm(TINFO, "(case%02d) START", i);
422 ret = do_test(&tcase[i]);
423 tst_resm((ret == 0 ? TPASS : TFAIL | TERRNO),
424 "(case%02d) END", i);
425 }
subrata_modake63f96b2009-05-29 10:29:58 +0000426
yaberauneyaa95028f2009-11-04 04:28:20 +0000427 }
subrata_modake63f96b2009-05-29 10:29:58 +0000428
yaberauneyaa95028f2009-11-04 04:28:20 +0000429 }
subrata_modake63f96b2009-05-29 10:29:58 +0000430
yaberauneyaa95028f2009-11-04 04:28:20 +0000431 cleanup();
subrata_modake63f96b2009-05-29 10:29:58 +0000432 tst_exit();
yaberauneyaa95028f2009-11-04 04:28:20 +0000433
subrata_modake63f96b2009-05-29 10:29:58 +0000434}