blob: ab8520da9f267f919d60394a503b512881596f5d [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 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
plars865695b2001-08-27 22:15:12 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
yaberauneyadb37adc2009-11-09 05:56:58 +000032/* $Id: access01.c,v 1.8 2009/11/09 05:56:58 yaberauneya Exp $ */
plars865695b2001-08-27 22:15:12 +000033/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000034 *
Garrett Cooper43088e12010-12-13 23:30:59 -080035 * OS Test - Silicon Graphics, Inc.
subrata_modak4bb656a2009-02-26 12:02:09 +000036 *
Garrett Cooper43088e12010-12-13 23:30:59 -080037 * TEST IDENTIFIER : access01
subrata_modak4bb656a2009-02-26 12:02:09 +000038 *
Garrett Cooper43088e12010-12-13 23:30:59 -080039 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000040 *
Garrett Cooper43088e12010-12-13 23:30:59 -080041 * TEST TITLE : Basic test for access(2) using F_OK,
42 * R_OK, W_OK and X_OK arguments.
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
Garrett Cooper43088e12010-12-13 23:30:59 -080044 * PARENT DOCUMENT : usctpl01
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
Garrett Cooper43088e12010-12-13 23:30:59 -080046 * TEST CASE TOTAL : 6
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
Garrett Cooper43088e12010-12-13 23:30:59 -080048 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
Garrett Cooper43088e12010-12-13 23:30:59 -080050 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
Garrett Cooper43088e12010-12-13 23:30:59 -080052 * AUTHOR : William Roske
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
Garrett Cooper43088e12010-12-13 23:30:59 -080054 * CO-PILOT : Dave Fenner
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
Garrett Cooper43088e12010-12-13 23:30:59 -080056 * DATE STARTED : 03/30/92
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
Garrett Cooper43088e12010-12-13 23:30:59 -080058 * INITIAL RELEASE : UNICOS 7.0
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
Garrett Cooper43088e12010-12-13 23:30:59 -080060 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
subrata_modak56207ce2009-03-23 13:35:39 +000062 * 1.) access(2) returns 0 for F_OK...(See Description)
63 * 2.) access(2) returns 0 for R_OK...(See Description)
64 * 3.) access(2) returns 0 for W_OK...(See Description)
65 * 4.) access(2) returns 0 for X_OK...(See Description)
subrata_modakbdbaec52009-02-26 12:14:51 +000066 *
Garrett Cooper43088e12010-12-13 23:30:59 -080067 * INPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000068 * The standard options for system call tests are accepted.
plars865695b2001-08-27 22:15:12 +000069 * (See the parse_opts(3) man page).
subrata_modak4bb656a2009-02-26 12:02:09 +000070 *
Garrett Cooper43088e12010-12-13 23:30:59 -080071 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000072 *
Garrett Cooper43088e12010-12-13 23:30:59 -080073 * DURATION
subrata_modak56207ce2009-03-23 13:35:39 +000074 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000075 *
Garrett Cooper43088e12010-12-13 23:30:59 -080076 * SIGNALS
subrata_modak56207ce2009-03-23 13:35:39 +000077 * Uses SIGUSR1 to pause before test if option set.
78 * (See the parse_opts(3) man page).
plars865695b2001-08-27 22:15:12 +000079 *
Garrett Cooper43088e12010-12-13 23:30:59 -080080 * RESOURCES
subrata_modak56207ce2009-03-23 13:35:39 +000081 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000082 *
Garrett Cooper43088e12010-12-13 23:30:59 -080083 * ENVIRONMENTAL NEEDS
subrata_modak56207ce2009-03-23 13:35:39 +000084 * The libcuts.a and libsys.a libraries must be included in
plars865695b2001-08-27 22:15:12 +000085 * the compilation of this test.
subrata_modak4bb656a2009-02-26 12:02:09 +000086 *
Garrett Cooper43088e12010-12-13 23:30:59 -080087 * SPECIAL PROCEDURAL REQUIREMENTS
subrata_modak56207ce2009-03-23 13:35:39 +000088 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000089 *
Garrett Cooper43088e12010-12-13 23:30:59 -080090 * INTERCASE DEPENDENCIES
subrata_modak56207ce2009-03-23 13:35:39 +000091 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000092 *
Garrett Cooper43088e12010-12-13 23:30:59 -080093 * DETAILED DESCRIPTION
plars865695b2001-08-27 22:15:12 +000094 * This is a Phase I test for the access(2) system call. It is intended
95 * to provide a limited exposure of the system call, for now. It
96 * should/will be extended when full functional tests are written for
97 * access(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000098 *
subrata_modak56207ce2009-03-23 13:35:39 +000099 * Setup:
100 * Setup signal handling.
plars865695b2001-08-27 22:15:12 +0000101 * Pause for SIGUSR1 if option specified.
102 * Create a temp directory and cd to it.
103 * Creat a temp file wil read, write and execute permissions.
subrata_modak4bb656a2009-02-26 12:02:09 +0000104 *
subrata_modak56207ce2009-03-23 13:35:39 +0000105 * Test:
plars865695b2001-08-27 22:15:12 +0000106 * Loop if the proper options are given.
107 * Execute system call with F_OK on tmp file
108 * Check return code, if system call failed (return=-1)
109 * Log the errno and Issue a FAIL message.
110 * Otherwise, Issue a PASS message.
111 * Execute system call with X_OK on tmp file...
112 * Execute system call with W_OK on tmp file...
subrata_modak56207ce2009-03-23 13:35:39 +0000113 * Execute system call with R_OK on tmp file...
subrata_modak4bb656a2009-02-26 12:02:09 +0000114 *
subrata_modak56207ce2009-03-23 13:35:39 +0000115 * Cleanup:
116 * Print errno log
subrata_modak4bb656a2009-02-26 12:02:09 +0000117 *
118 *
plars865695b2001-08-27 22:15:12 +0000119 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
120
121#include <string.h>
plars19d169e2002-09-04 13:34:31 +0000122#include <unistd.h>
plars865695b2001-08-27 22:15:12 +0000123#include <sys/types.h>
124#include <sys/fcntl.h>
125#include <errno.h>
126#include <signal.h>
127#include <sys/stat.h>
128#include "test.h"
129#include "usctest.h"
130void setup();
131void cleanup();
132
Garrett Cooper43088e12010-12-13 23:30:59 -0800133char *TCID = "access01"; /* Test program identifier. */
subrata_modak56207ce2009-03-23 13:35:39 +0000134int TST_TOTAL = 4; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +0000135
plars865695b2001-08-27 22:15:12 +0000136char Fname[255];
137
138static struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +0000139 char *file;
140 int mode;
141 char *string;
142 int experrno;
plars865695b2001-08-27 22:15:12 +0000143} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000144 { Fname, F_OK, "F_OK", 0},
145 { Fname, X_OK, "X_OK", 0},
146 { Fname, W_OK, "W_OK", 0},
147 { Fname, R_OK, "R_OK", 0},
plars865695b2001-08-27 22:15:12 +0000148};
149
150int Ntc = sizeof(Test_cases) / sizeof(struct test_case_t);
151
152/***********************************************************************
153 * Main
154 ***********************************************************************/
155int main(int ac, char **av)
156{
subrata_modak56207ce2009-03-23 13:35:39 +0000157 int lc; /* loop counter */
158 char *msg; /* message returned from parse_opts */
159 int tc;
plars865695b2001-08-27 22:15:12 +0000160
subrata_modak56207ce2009-03-23 13:35:39 +0000161 TST_TOTAL = Ntc;
162
Garrett Cooper2c282152010-12-16 00:55:50 -0800163 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +0000164 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000165
subrata_modak56207ce2009-03-23 13:35:39 +0000166 setup();
plars865695b2001-08-27 22:15:12 +0000167
subrata_modak56207ce2009-03-23 13:35:39 +0000168 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000169
subrata_modak56207ce2009-03-23 13:35:39 +0000170 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 for (tc = 0; tc < Ntc; tc++) {
173 /*
174 * Call access(2)
175 */
176 TEST(access(Test_cases[tc].file, Test_cases[tc].mode));
subrata_modakbdbaec52009-02-26 12:14:51 +0000177
subrata_modak56207ce2009-03-23 13:35:39 +0000178 /* check return code */
179 if (TEST_RETURN == -1 && Test_cases[tc].experrno == 0) {
vapier6f550172009-08-28 10:46:41 +0000180 tst_resm(TFAIL|TTERRNO,
Garrett Cooper43088e12010-12-13 23:30:59 -0800181 "access(%s, %s) failed",
182 Test_cases[tc].file,
183 Test_cases[tc].string);
plars865695b2001-08-27 22:15:12 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 } else if (TEST_RETURN != -1
186 && Test_cases[tc].experrno != 0) {
187 tst_resm(TFAIL,
Garrett Cooper43088e12010-12-13 23:30:59 -0800188 "access(%s, %s) returned %ld, "
189 "exp -1, errno:%d",
190 Test_cases[tc].file,
191 Test_cases[tc].string, TEST_RETURN,
192 Test_cases[tc].experrno);
subrata_modak56207ce2009-03-23 13:35:39 +0000193 } else {
plars865695b2001-08-27 22:15:12 +0000194
subrata_modak56207ce2009-03-23 13:35:39 +0000195 if (STD_FUNCTIONAL_TEST) {
196 /* No Verification test, yet... */
197 tst_resm(TPASS,
vapier6f550172009-08-28 10:46:41 +0000198 "access(%s, %s) returned %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000199 Test_cases[tc].file,
200 Test_cases[tc].string,
201 TEST_RETURN);
202 }
203 }
204 }
plars865695b2001-08-27 22:15:12 +0000205
Garrett Cooper2c282152010-12-16 00:55:50 -0800206 }
plars865695b2001-08-27 22:15:12 +0000207
subrata_modak56207ce2009-03-23 13:35:39 +0000208 cleanup();
Garrett Cooper43088e12010-12-13 23:30:59 -0800209 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800210}
plars865695b2001-08-27 22:15:12 +0000211
subrata_modak56207ce2009-03-23 13:35:39 +0000212void setup()
subrata_modak4bb656a2009-02-26 12:02:09 +0000213{
subrata_modak56207ce2009-03-23 13:35:39 +0000214 int fd;
215 struct stat stbuf;
plars865695b2001-08-27 22:15:12 +0000216
subrata_modak56207ce2009-03-23 13:35:39 +0000217 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000218
subrata_modak56207ce2009-03-23 13:35:39 +0000219 umask(0); /* reset umask avoid it affects on modes */
plars865695b2001-08-27 22:15:12 +0000220
subrata_modak56207ce2009-03-23 13:35:39 +0000221 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000222
subrata_modak56207ce2009-03-23 13:35:39 +0000223 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000224
plars865695b2001-08-27 22:15:12 +0000225 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000226 * Since files inherit group ids, make sure our dir has a valid grp
227 * to us.
plars865695b2001-08-27 22:15:12 +0000228 */
yaberauneyadb37adc2009-11-09 05:56:58 +0000229 if (chown(".", -1, getegid()) < 0) {
230 tst_brkm(TBROK|TERRNO, cleanup,
Garrett Cooper43088e12010-12-13 23:30:59 -0800231 "chown(\".\", -1, %d) failed", getegid());
yaberauneyadb37adc2009-11-09 05:56:58 +0000232 }
plars865695b2001-08-27 22:15:12 +0000233
subrata_modak56207ce2009-03-23 13:35:39 +0000234 sprintf(Fname, "accessfile");
plars865695b2001-08-27 22:15:12 +0000235
vapier6f550172009-08-28 10:46:41 +0000236 fd = open(Fname, O_RDWR | O_CREAT, 06777);
237 if (fd == -1)
238 tst_brkm(TBROK|TERRNO, cleanup,
239 "open(%s, O_RDWR|O_CREAT, 06777) failed", Fname);
240 else if (close(fd) == -1)
241 tst_resm(TINFO|TERRNO, "close(%s) failed", Fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000242
243 /*
244 * force the mode to be set to 6777
245 */
vapier6f550172009-08-28 10:46:41 +0000246 if (chmod(Fname, 06777) == -1)
Garrett Cooper43088e12010-12-13 23:30:59 -0800247 tst_brkm(TBROK|TERRNO, cleanup, "chmod(%s, 06777) failed",
248 Fname);
subrata_modak56207ce2009-03-23 13:35:39 +0000249
250 stat(Fname, &stbuf);
251
252 if ((stbuf.st_mode & 06777) != 06777) {
253 /*
254 * file can not be properly setup
255 */
256 }
257
Garrett Cooper43088e12010-12-13 23:30:59 -0800258}
plars865695b2001-08-27 22:15:12 +0000259
260/***************************************************************
261 * cleanup() - performs all ONE TIME cleanup for this test at
262 * completion or premature exit.
263 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000264void cleanup()
plars865695b2001-08-27 22:15:12 +0000265{
subrata_modak56207ce2009-03-23 13:35:39 +0000266 /*
267 * print timing stats if that option was specified.
268 */
269 TEST_CLEANUP;
plars865695b2001-08-27 22:15:12 +0000270
subrata_modak56207ce2009-03-23 13:35:39 +0000271 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700272}