blob: fd7c8f84cb27260dfca96c3788103b3c9d83e483 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
plars865695b2001-08-27 22:15:12 +000023 */
Wanlong Gao41dae892013-04-26 14:55:25 +080024
25/*
26 * access(2) test for errno(s) EFAULT.
27 */
plars865695b2001-08-27 22:15:12 +000028
29#include <errno.h>
30#include <string.h>
31#include <signal.h>
32
plars865695b2001-08-27 22:15:12 +000033#include <unistd.h>
plars1ad84512002-07-23 13:11:18 +000034#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000035#include "test.h"
plars865695b2001-08-27 22:15:12 +000036
Garrett Cooper43088e12010-12-13 23:30:59 -080037static void setup(void);
38static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000039
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020040char *TCID = "access03";
41int TST_TOTAL = 8;
plars865695b2001-08-27 22:15:12 +000042
Garrett Cooper187e27c2010-12-17 13:30:26 -080043/* XXX (garrcoop): uh, this isn't a bad address yo. */
Wanlong Gao41dae892013-04-26 14:55:25 +080044static void *low_addr;
45static void *high_addr;
plars1ad84512002-07-23 13:11:18 +000046
robbiewd34d5812005-07-11 22:28:09 +000047#if !defined(UCLINUX)
48
plars865695b2001-08-27 22:15:12 +000049int main(int ac, char **av)
50{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020051 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020052 const char *msg;
plars865695b2001-08-27 22:15:12 +000053
Wanlong Gao41dae892013-04-26 14:55:25 +080054 msg = parse_opts(ac, av, NULL, NULL);
55 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080056 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000057
subrata_modak56207ce2009-03-23 13:35:39 +000058 setup();
plars865695b2001-08-27 22:15:12 +000059
Garrett Cooper187e27c2010-12-17 13:30:26 -080060#define TEST_ACCESS(addr, mode) \
Wanlong Gao41dae892013-04-26 14:55:25 +080061{ \
Garrett Cooper187e27c2010-12-17 13:30:26 -080062 if (access(low_addr, mode) == -1) { \
63 if (errno == EFAULT) { \
64 tst_resm(TPASS, \
65 "access(%p, %s) failed as expected with EFAULT", \
66 addr, #mode); \
67 } else { \
68 tst_resm(TFAIL|TERRNO, \
69 "access(%p, %s) failed unexpectedly; " \
70 "expected (EFAULT)", addr, #mode); \
71 } \
72 } else { \
73 tst_resm(TFAIL, \
74 "access(%p, %s) succeeded unexpectedly", addr, #mode); \
Wanlong Gao41dae892013-04-26 14:55:25 +080075 } \
76}
Garrett Cooper187e27c2010-12-17 13:30:26 -080077
subrata_modak56207ce2009-03-23 13:35:39 +000078 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080079 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000080
Garrett Cooper187e27c2010-12-17 13:30:26 -080081 TEST_ACCESS(low_addr, R_OK);
82 TEST_ACCESS(low_addr, W_OK);
83 TEST_ACCESS(low_addr, X_OK);
84 TEST_ACCESS(low_addr, F_OK);
subrata_modakbdbaec52009-02-26 12:14:51 +000085
Garrett Cooper187e27c2010-12-17 13:30:26 -080086 TEST_ACCESS(high_addr, R_OK);
87 TEST_ACCESS(high_addr, W_OK);
88 TEST_ACCESS(high_addr, X_OK);
89 TEST_ACCESS(high_addr, F_OK);
Garrett Cooper2c282152010-12-16 00:55:50 -080090 }
plars865695b2001-08-27 22:15:12 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -080093 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +000094}
95
Wanlong Gao41dae892013-04-26 14:55:25 +080096static void setup(void)
plars865695b2001-08-27 22:15:12 +000097{
subrata_modak56207ce2009-03-23 13:35:39 +000098 tst_sig(NOFORK, DEF_HANDLER, cleanup);
subrata_modak56207ce2009-03-23 13:35:39 +000099 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000100
Garrett Cooper187e27c2010-12-17 13:30:26 -0800101 low_addr = mmap(0, 1, PROT_NONE,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
Garrett Cooper187e27c2010-12-17 13:30:26 -0800103 if (low_addr == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_brkm(TBROK | TERRNO, NULL, "mmap failed");
Wanlong Gao41dae892013-04-26 14:55:25 +0800105
Garrett Cooper187e27c2010-12-17 13:30:26 -0800106 high_addr = get_high_address();
107 if (high_addr == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 tst_brkm(TBROK | TERRNO, NULL, "get_high_address failed");
Garrett Cooper187e27c2010-12-17 13:30:26 -0800109 high_addr++;
plars1ad84512002-07-23 13:11:18 +0000110
Garrett Cooper187e27c2010-12-17 13:30:26 -0800111 tst_tmpdir();
Garrett Cooper43088e12010-12-13 23:30:59 -0800112}
plars865695b2001-08-27 22:15:12 +0000113
Wanlong Gao41dae892013-04-26 14:55:25 +0800114static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000115{
subrata_modak56207ce2009-03-23 13:35:39 +0000116 tst_rmdir();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800117}
Garrett Cooper187e27c2010-12-17 13:30:26 -0800118
119#else
120
Mike Frysingerc57fba52014-04-09 18:56:30 -0400121int main(void)
Garrett Cooper187e27c2010-12-17 13:30:26 -0800122{
123 tst_brkm(TCONF, NULL, "test not available on UCLINUX");
124}
125
Chris Dearmanec6edca2012-10-17 19:54:01 -0700126#endif /* if !defined(UCLINUX) */