blob: 4a7739f75c220b99dc66325c34162aad7f87bfed [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
subrata_modak4bb656a2009-02-26 12:02:09 +00003 *
plars865695b2001-08-27 22:15:12 +00004 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +00007 *
plars865695b2001-08-27 22:15:12 +00008 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000011 *
plars865695b2001-08-27 22:15:12 +000012 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
plars865695b2001-08-27 22:15:12 +000019 * 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.
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
plars865695b2001-08-27 22:15:12 +000023 */
Wanlong Gaof85fea72013-04-26 14:55:25 +080024
25/*
26 * Description:
27 * Basic test for access(2) using F_OK, R_OK, W_OK and X_OK on tmp file
28 * AUTHOR : William Roske
29 */
plars865695b2001-08-27 22:15:12 +000030
31#include <string.h>
plars19d169e2002-09-04 13:34:31 +000032#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000033#include <sys/types.h>
34#include <sys/fcntl.h>
35#include <errno.h>
36#include <signal.h>
37#include <sys/stat.h>
38#include "test.h"
Wanlong Gaof85fea72013-04-26 14:55:25 +080039
plars865695b2001-08-27 22:15:12 +000040
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020041char *TCID = "access01";
plars865695b2001-08-27 22:15:12 +000042
Wanlong Gaof85fea72013-04-26 14:55:25 +080043char fname[255];
plars865695b2001-08-27 22:15:12 +000044
45static struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000046 char *file;
47 int mode;
48 char *string;
49 int experrno;
Wanlong Gaof85fea72013-04-26 14:55:25 +080050} test_cases[] = {
51 {fname, F_OK, "F_OK", 0},
52 {fname, X_OK, "X_OK", 0},
53 {fname, W_OK, "W_OK", 0},
54 {fname, R_OK, "R_OK", 0},
55};
plars865695b2001-08-27 22:15:12 +000056
Wanlong Gaof85fea72013-04-26 14:55:25 +080057int TST_TOTAL = sizeof(test_cases) / sizeof(struct test_case_t);
plars865695b2001-08-27 22:15:12 +000058
Wanlong Gaof85fea72013-04-26 14:55:25 +080059static void setup(void);
60static void cleanup(void);
61
plars865695b2001-08-27 22:15:12 +000062int main(int ac, char **av)
63{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020064 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020065 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000066 int tc;
plars865695b2001-08-27 22:15:12 +000067
Wanlong Gaof85fea72013-04-26 14:55:25 +080068 msg = parse_opts(ac, av, NULL, NULL);
69 if (msg != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000070 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000071
subrata_modak56207ce2009-03-23 13:35:39 +000072 setup();
plars865695b2001-08-27 22:15:12 +000073
subrata_modak56207ce2009-03-23 13:35:39 +000074 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080075 tst_count = 0;
Wanlong Gaof85fea72013-04-26 14:55:25 +080076 for (tc = 0; tc < TST_TOTAL; tc++) {
77 TEST(access(test_cases[tc].file, test_cases[tc].mode));
plars865695b2001-08-27 22:15:12 +000078
Wanlong Gaof85fea72013-04-26 14:55:25 +080079 if (TEST_RETURN == -1 && test_cases[tc].experrno == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 tst_resm(TFAIL | TTERRNO,
81 "access(%s, %s) failed",
Wanlong Gaof85fea72013-04-26 14:55:25 +080082 test_cases[tc].file,
83 test_cases[tc].string);
plars865695b2001-08-27 22:15:12 +000084
subrata_modak56207ce2009-03-23 13:35:39 +000085 } else if (TEST_RETURN != -1
Wanlong Gaof85fea72013-04-26 14:55:25 +080086 && test_cases[tc].experrno != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +000087 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 "access(%s, %s) returned %ld, "
89 "exp -1, errno:%d",
Wanlong Gaof85fea72013-04-26 14:55:25 +080090 test_cases[tc].file,
91 test_cases[tc].string, TEST_RETURN,
92 test_cases[tc].experrno);
subrata_modak56207ce2009-03-23 13:35:39 +000093 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020094 tst_resm(TPASS,
95 "access(%s, %s) returned %ld",
96 test_cases[tc].file,
97 test_cases[tc].string,
98 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +000099 }
100 }
plars865695b2001-08-27 22:15:12 +0000101
Garrett Cooper2c282152010-12-16 00:55:50 -0800102 }
plars865695b2001-08-27 22:15:12 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 cleanup();
Garrett Cooper43088e12010-12-13 23:30:59 -0800105 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800106}
plars865695b2001-08-27 22:15:12 +0000107
Wanlong Gaof85fea72013-04-26 14:55:25 +0800108static void setup(void)
subrata_modak4bb656a2009-02-26 12:02:09 +0000109{
subrata_modak56207ce2009-03-23 13:35:39 +0000110 int fd;
111 struct stat stbuf;
plars865695b2001-08-27 22:15:12 +0000112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000114
Wanlong Gaof85fea72013-04-26 14:55:25 +0800115 umask(0);
subrata_modak56207ce2009-03-23 13:35:39 +0000116 TEST_PAUSE;
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000118
plars865695b2001-08-27 22:15:12 +0000119 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000120 * Since files inherit group ids, make sure our dir has a valid grp
121 * to us.
plars865695b2001-08-27 22:15:12 +0000122 */
yaberauneyadb37adc2009-11-09 05:56:58 +0000123 if (chown(".", -1, getegid()) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 tst_brkm(TBROK | TERRNO, cleanup,
125 "chown(\".\", -1, %d) failed", getegid());
yaberauneyadb37adc2009-11-09 05:56:58 +0000126 }
plars865695b2001-08-27 22:15:12 +0000127
Wanlong Gaof85fea72013-04-26 14:55:25 +0800128 snprintf(fname, sizeof(fname), "accessfile");
plars865695b2001-08-27 22:15:12 +0000129
Wanlong Gaof85fea72013-04-26 14:55:25 +0800130 fd = open(fname, O_RDWR | O_CREAT, 06777);
vapier6f550172009-08-28 10:46:41 +0000131 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_brkm(TBROK | TERRNO, cleanup,
Wanlong Gaof85fea72013-04-26 14:55:25 +0800133 "open(%s, O_RDWR|O_CREAT, 06777) failed", fname);
vapier6f550172009-08-28 10:46:41 +0000134 else if (close(fd) == -1)
Wanlong Gaof85fea72013-04-26 14:55:25 +0800135 tst_resm(TINFO | TERRNO, "close(%s) failed", fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000136
137 /*
138 * force the mode to be set to 6777
139 */
Wanlong Gaof85fea72013-04-26 14:55:25 +0800140 if (chmod(fname, 06777) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 06777) failed",
Wanlong Gaof85fea72013-04-26 14:55:25 +0800142 fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000143
Wanlong Gaof85fea72013-04-26 14:55:25 +0800144 stat(fname, &stbuf);
subrata_modak56207ce2009-03-23 13:35:39 +0000145
146 if ((stbuf.st_mode & 06777) != 06777) {
Wanlong Gaof85fea72013-04-26 14:55:25 +0800147 tst_brkm(TBROK, cleanup, "'%s' can't be properly setup",
148 fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000149 }
Garrett Cooper43088e12010-12-13 23:30:59 -0800150}
plars865695b2001-08-27 22:15:12 +0000151
Wanlong Gaof85fea72013-04-26 14:55:25 +0800152static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000153{
subrata_modak56207ce2009-03-23 13:35:39 +0000154 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700155}