blob: d86bf64513a11411a1ea78a72de9bfbe730a819a [file] [log] [blame]
plars6c153562002-06-12 21:24:47 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars6c153562002-06-12 21:24:47 +000018 */
19
20/*
21 * NAME
22 * mlock01.c
23 *
24 * DESCRIPTION
25 * Test to see that mlock works
subrata_modak56207ce2009-03-23 13:35:39 +000026 *$
plars6c153562002-06-12 21:24:47 +000027 * ALGORITHM
28 * test 1:
subrata_modak4bb656a2009-02-26 12:02:09 +000029 * Call mlock with various valid addresses and lengths. No
plars6c153562002-06-12 21:24:47 +000030 * error should be returned
31 *
32 * USAGE: <for command-line>
33 * -c n Run n copies concurrently
34 * -e Turn on errno logging
35 * -f Turn off functional testing
36 * -h Show this help screen
37 * -i n Execute test n times
38 * -I x Execute test for x seconds
39 * -p Pause for SIGUSR1 before starting
40 * -P x Pause for x seconds between iterations
41 * -t Turn on syscall timing
42 *
43 * HISTORY
44 * 06/2002 Written by Paul Larson
45 *
46 * RESTRICTIONS
47 * None
48 */
49#include <errno.h>
50#include <unistd.h>
51#include <sys/mman.h>
52#include "test.h"
plars6c153562002-06-12 21:24:47 +000053
54void setup();
55void setup1(int);
56void cleanup();
57
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020058char *TCID = "mlock01";
59int TST_TOTAL = 4;
plars6c153562002-06-12 21:24:47 +000060
plars6c153562002-06-12 21:24:47 +000061void *addr1;
62
63struct test_case_t {
64 void **addr;
65 int len;
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 void (*setupfunc) ();
plars6c153562002-06-12 21:24:47 +000067} TC[] = {
subrata_modak4bb656a2009-02-26 12:02:09 +000068 /* mlock should return ENOMEM when some or all of the address
plars6c153562002-06-12 21:24:47 +000069 * range pointed to by addr and len are not valid mapped pages
70 * in the address space of the process
71 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080072 {
73 &addr1, 1, setup1}, {
74 &addr1, 1024, setup1}, {
75 &addr1, 1024 * 1024, setup1}, {
76 &addr1, 1024 * 1024 * 10, setup1}
plars6c153562002-06-12 21:24:47 +000077};
78
robbiewd34d5812005-07-11 22:28:09 +000079#if !defined(UCLINUX)
80
plars6c153562002-06-12 21:24:47 +000081int main(int ac, char **av)
82{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020083 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020084 const char *msg;
plars6c153562002-06-12 21:24:47 +000085
Garrett Cooper2f412992010-12-19 06:01:41 -080086 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars6c153562002-06-12 21:24:47 +000087 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars6c153562002-06-12 21:24:47 +000088
plars6c153562002-06-12 21:24:47 +000089 setup();
90
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080091 /*
Garrett Cooper2f412992010-12-19 06:01:41 -080092 * FIXME (garrcoop): this should really test out whether or not the
93 * process's mappable address space is indeed accessible by the
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080094 * current user, instead of needing to be run by root all the time.
plars6c153562002-06-12 21:24:47 +000095 */
Garrett Cooper2f412992010-12-19 06:01:41 -080096 tst_require_root(NULL);
97
plars6c153562002-06-12 21:24:47 +000098 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
Caspar Zhangd59a6592013-03-07 14:59:12 +0800100 tst_count = 0;
plars6c153562002-06-12 21:24:47 +0000101
102 for (i = 0; i < TST_TOTAL; i++) {
103
104 if (TC[i].setupfunc != NULL)
105 TC[i].setupfunc(TC[i].len);
106
107 TEST(mlock(*(TC[i].addr), TC[i].len));
108
Garrett Cooper2f412992010-12-19 06:01:41 -0800109 /* I'm confused -- given the description above this
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800110 * should fail as designed, but this application
Garrett Cooper2f412992010-12-19 06:01:41 -0800111 * */
112 if (TEST_RETURN == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 tst_resm(TFAIL | TTERRNO, "mlock failed");
Garrett Cooper2f412992010-12-19 06:01:41 -0800114 else
115 tst_resm(TPASS, "mlock passed");
plars6c153562002-06-12 21:24:47 +0000116 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800117 }
plars6c153562002-06-12 21:24:47 +0000118
plars6c153562002-06-12 21:24:47 +0000119 cleanup();
120
Garrett Cooper2c282152010-12-16 00:55:50 -0800121 tst_exit();
122}
plars6c153562002-06-12 21:24:47 +0000123
robbiewd34d5812005-07-11 22:28:09 +0000124#else
125
Mike Frysingerc57fba52014-04-09 18:56:30 -0400126int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000127{
Garrett Cooper2f412992010-12-19 06:01:41 -0800128 tst_brkm(TCONF, NULL, "test is not available on uClinux");
robbiewd34d5812005-07-11 22:28:09 +0000129}
130
131#endif /* if !defined(UCLINUX) */
132
Mike Frysingerc57fba52014-04-09 18:56:30 -0400133void setup(void)
plars6c153562002-06-12 21:24:47 +0000134{
135 TEST_PAUSE;
136}
137
138void setup1(int len)
139{
Garrett Cooper2f412992010-12-19 06:01:41 -0800140 addr1 = malloc(len);
plars6c153562002-06-12 21:24:47 +0000141 if (addr1 == NULL)
142 tst_brkm(TFAIL, cleanup, "malloc failed");
143}
144
Mike Frysingerc57fba52014-04-09 18:56:30 -0400145void cleanup(void)
plars6c153562002-06-12 21:24:47 +0000146{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700147}