blob: 0ddf73003f5f0e2ace178cf3cbcef4ae0feeb816 [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 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
subrata_modak923b23f2009-11-02 13:57:16 +000033/* $Id: stat06.c,v 1.10 2009/11/02 13:57:19 subrata_modak Exp $ */
plars865695b2001-08-27 22:15:12 +000034/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000035 *
plars865695b2001-08-27 22:15:12 +000036 * OS Test - Silicon Graphics, Inc.
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
plars865695b2001-08-27 22:15:12 +000038 * TEST IDENTIFIER : stat06
subrata_modak4bb656a2009-02-26 12:02:09 +000039 *
plars865695b2001-08-27 22:15:12 +000040 * EXECUTED BY : anyone
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
plars865695b2001-08-27 22:15:12 +000042 * TEST TITLE : stat(2) negative path testcases
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
plars865695b2001-08-27 22:15:12 +000044 * PARENT DOCUMENT : None
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
plars865695b2001-08-27 22:15:12 +000046 * TEST CASE TOTAL : 7
subrata_modak4bb656a2009-02-26 12:02:09 +000047 *
plars865695b2001-08-27 22:15:12 +000048 * WALL CLOCK TIME : 1
subrata_modak4bb656a2009-02-26 12:02:09 +000049 *
plars865695b2001-08-27 22:15:12 +000050 * CPU TYPES : ALL
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
plars865695b2001-08-27 22:15:12 +000052 * AUTHOR : Richard Logan
subrata_modak4bb656a2009-02-26 12:02:09 +000053 *
plars865695b2001-08-27 22:15:12 +000054 * CO-PILOT : William Roske
subrata_modak4bb656a2009-02-26 12:02:09 +000055 *
plars865695b2001-08-27 22:15:12 +000056 * DATE STARTED : 03/30/94
subrata_modak4bb656a2009-02-26 12:02:09 +000057 *
plars865695b2001-08-27 22:15:12 +000058 * INITIAL RELEASE : UNICOS 7.0
subrata_modak4bb656a2009-02-26 12:02:09 +000059 *
plars865695b2001-08-27 22:15:12 +000060 * TEST CASES
subrata_modak4bb656a2009-02-26 12:02:09 +000061 *
plars865695b2001-08-27 22:15:12 +000062 * 1-7) See Testcases structure below.
subrata_modakbdbaec52009-02-26 12:14:51 +000063 *
plars865695b2001-08-27 22:15:12 +000064 * INPUT SPECIFICATIONS
65 * The standard options for system call tests are accepted.
66 * (See the parse_opts(3) man page).
67 * -h : print help and exit
subrata_modak4bb656a2009-02-26 12:02:09 +000068 *
plars865695b2001-08-27 22:15:12 +000069 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000070 *$
plars865695b2001-08-27 22:15:12 +000071 * DURATION
72 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000073 *
plars865695b2001-08-27 22:15:12 +000074 * SIGNALS
75 * Uses SIGUSR1 to pause before test if option set.
76 * (See the parse_opts(3) man page).
77 *
78 * RESOURCES
79 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000080 *
plars865695b2001-08-27 22:15:12 +000081 * ENVIRONMENTAL NEEDS
subrata_modak4bb656a2009-02-26 12:02:09 +000082 * The libcuts.a and libsys.a libraries must be included in
plars865695b2001-08-27 22:15:12 +000083 * the compilation of this test.
subrata_modak4bb656a2009-02-26 12:02:09 +000084 *
plars865695b2001-08-27 22:15:12 +000085 * SPECIAL PROCEDURAL REQUIREMENTS
86 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000087 *
plars865695b2001-08-27 22:15:12 +000088 * INTERCASE DEPENDENCIES
89 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000090 *
plars865695b2001-08-27 22:15:12 +000091 * DETAILED DESCRIPTION
92 * This is a Phase I test for the stat(2) system call. It is intended
93 * to provide a limited exposure of the system call, for now. It
94 * should/will be extended when full functional tests are written for
95 * stat(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000096 *
plars865695b2001-08-27 22:15:12 +000097 * Setup:
98 * Setup signal handling.
99 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +0000100 *
plars865695b2001-08-27 22:15:12 +0000101 * Test:
102 * Loop if the proper options are given.
103 * Execute system call
104 * Check return code, if system call failed (return=-1)
105 * Log the errno and Issue a FAIL message.
106 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +0000107 *
plars865695b2001-08-27 22:15:12 +0000108 * Cleanup:
109 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +0000110 *
111 *
plars865695b2001-08-27 22:15:12 +0000112 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
113
114#include <sys/types.h>
115#include <sys/fcntl.h>
116#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +0000117#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +0000118#include <errno.h>
119#include <string.h>
120#include <signal.h>
121#include <setjmp.h>
122#include <unistd.h>
123#include "test.h"
plars865695b2001-08-27 22:15:12 +0000124
125void setup();
126void cleanup();
127
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200128char *TCID = "stat06";
plars865695b2001-08-27 22:15:12 +0000129
subrata_modak56207ce2009-03-23 13:35:39 +0000130char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000131
robbiewd34d5812005-07-11 22:28:09 +0000132#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +0000133int high_address_setup();
robbiewd34d5812005-07-11 22:28:09 +0000134char High_address[64];
135#endif
plars865695b2001-08-27 22:15:12 +0000136int longpath_setup();
137int no_setup();
138int filepath_setup();
subrata_modak56207ce2009-03-23 13:35:39 +0000139char Longpathname[PATH_MAX + 2];
plars865695b2001-08-27 22:15:12 +0000140struct stat statbuf;
141jmp_buf sig11_recover;
142void sig11_handler(int sig);
143
144struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +0000145 char *pathname;
146 struct stat *stbuf;
147 char *desc;
148 int exp_errno;
149 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000150} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000151 {
152 "nonexistfile", &statbuf, "non-existent file", ENOENT, no_setup}, {
153 "", &statbuf, "path is empty string", ENOENT, no_setup}, {
154 "nefile/file", &statbuf, "path contains a non-existent file",
155 ENOENT, no_setup}, {
156 "file/file", &statbuf, "path contains a regular file",
157 ENOTDIR, filepath_setup}, {
158 Longpathname, &statbuf, "pathname too long", ENAMETOOLONG,
159 longpath_setup},
robbiewd34d5812005-07-11 22:28:09 +0000160#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000161 {
162 High_address, &statbuf, "address beyond address space", EFAULT,
163 high_address_setup}, {
164 (char *)-1, &statbuf, "negative address", EFAULT, no_setup},
vapier62b16cf2007-02-09 20:48:23 +0000165#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000166 {
167 NULL, NULL, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000168};
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169
Cyril Hrubisb863a0b2014-09-24 13:15:29 +0200170int TST_TOTAL = ARRAY_SIZE(Test_cases);
plars865695b2001-08-27 22:15:12 +0000171
172/***********************************************************************
173 * Main
174 ***********************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000175int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000176{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200177 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200178 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000179 char *fname;
180 char *desc;
181 int ind;
182 struct stat *stbuf;
183 struct sigaction sa, osa;
184
plars865695b2001-08-27 22:15:12 +0000185 /***************************************************************
186 * parse standard options
187 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800188 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000189 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000190 }
plars865695b2001-08-27 22:15:12 +0000191
192 /***************************************************************
193 * perform global setup for test
194 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000195 setup();
plars865695b2001-08-27 22:15:12 +0000196
plars865695b2001-08-27 22:15:12 +0000197 /***************************************************************
198 * check looping state if -c option given
199 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000200 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000201
Caspar Zhangd59a6592013-03-07 14:59:12 +0800202 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000203
subrata_modak56207ce2009-03-23 13:35:39 +0000204 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
plars865695b2001-08-27 22:15:12 +0000205
subrata_modak56207ce2009-03-23 13:35:39 +0000206 fname = Test_cases[ind].pathname;
207 desc = Test_cases[ind].desc;
208 stbuf = Test_cases[ind].stbuf;
plars865695b2001-08-27 22:15:12 +0000209
subrata_modak56207ce2009-03-23 13:35:39 +0000210 if (stbuf == (struct stat *)-1) {
211 /* special sig11 case */
212 sa.sa_handler = &sig11_handler;
213 sigemptyset(&sa.sa_mask);
214 sa.sa_flags = 0;
plars865695b2001-08-27 22:15:12 +0000215
subrata_modak56207ce2009-03-23 13:35:39 +0000216 sigaction(SIGSEGV, NULL, &osa);
217 sigaction(SIGSEGV, &sa, NULL);
plars865695b2001-08-27 22:15:12 +0000218
subrata_modak56207ce2009-03-23 13:35:39 +0000219 if (setjmp(sig11_recover)) {
220 TEST_RETURN = -1;
221 TEST_ERRNO = EFAULT;
222 } else {
223 TEST(stat(fname, stbuf));
224 }
225 sigaction(SIGSEGV, &osa, NULL);
226 } else {
227 /*
228 * Call stat(2)
229 */
230
231 TEST(stat(fname, stbuf));
232 }
233
234 /* check return code */
235 if (TEST_RETURN == -1) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200236 if (TEST_ERRNO ==
237 Test_cases[ind].exp_errno)
238 tst_resm(TPASS,
239 "stat(<%s>, &stbuf) Failed, errno=%d",
240 desc, TEST_ERRNO);
241 else
242 tst_resm(TFAIL,
243 "stat(<%s>, &stbuf) Failed, errno=%d, expected errno:%d",
244 desc, TEST_ERRNO,
245 Test_cases
246 [ind].exp_errno);
subrata_modak56207ce2009-03-23 13:35:39 +0000247 } else {
248 tst_resm(TFAIL,
subrata_modak923b23f2009-11-02 13:57:16 +0000249 "stat(<%s>, &stbuf) returned %ld, expected -1, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000250 desc, TEST_RETURN,
251 Test_cases[ind].exp_errno);
252 }
plars865695b2001-08-27 22:15:12 +0000253 }
plars865695b2001-08-27 22:15:12 +0000254
Garrett Cooper2c282152010-12-16 00:55:50 -0800255 }
plars865695b2001-08-27 22:15:12 +0000256
subrata_modak56207ce2009-03-23 13:35:39 +0000257 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800258 tst_exit();
259}
plars865695b2001-08-27 22:15:12 +0000260
261/***************************************************************
262 * setup() - performs all ONE TIME setup for this test.
263 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400264void setup(void)
plars865695b2001-08-27 22:15:12 +0000265{
subrata_modak56207ce2009-03-23 13:35:39 +0000266 int ind;
plars865695b2001-08-27 22:15:12 +0000267
subrata_modak56207ce2009-03-23 13:35:39 +0000268 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000269
subrata_modak56207ce2009-03-23 13:35:39 +0000270 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000271
subrata_modak56207ce2009-03-23 13:35:39 +0000272 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000273
vapier62b16cf2007-02-09 20:48:23 +0000274#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000275 bad_addr = mmap(0, 1, PROT_NONE,
276 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
277 if (bad_addr == MAP_FAILED) {
278 tst_brkm(TBROK, cleanup, "mmap failed");
279 }
280 Test_cases[6].pathname = bad_addr;
vapier62b16cf2007-02-09 20:48:23 +0000281#endif
plars1ad84512002-07-23 13:11:18 +0000282
subrata_modak56207ce2009-03-23 13:35:39 +0000283 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
284 Test_cases[ind].setupfunc();
285 }
plars865695b2001-08-27 22:15:12 +0000286
Garrett Cooper2c282152010-12-16 00:55:50 -0800287}
plars865695b2001-08-27 22:15:12 +0000288
289/***************************************************************
290 * cleanup() - performs all ONE TIME cleanup for this test at
291 * completion or premature exit.
292 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400293void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000294{
plars865695b2001-08-27 22:15:12 +0000295
subrata_modak56207ce2009-03-23 13:35:39 +0000296 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000297
Garrett Cooper2c282152010-12-16 00:55:50 -0800298}
plars865695b2001-08-27 22:15:12 +0000299
300/******************************************************************
301 * no_setup() - does nothing
302 ******************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400303int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000304{
subrata_modak56207ce2009-03-23 13:35:39 +0000305 return 0;
plars865695b2001-08-27 22:15:12 +0000306}
307
robbiewd34d5812005-07-11 22:28:09 +0000308#if !defined(UCLINUX)
309
plars865695b2001-08-27 22:15:12 +0000310/******************************************************************
311 * high_address_setup() - generates an address that should cause a segfault
312 ******************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400313int high_address_setup(void)
plars865695b2001-08-27 22:15:12 +0000314{
subrata_modak56207ce2009-03-23 13:35:39 +0000315 int ind;
plars865695b2001-08-27 22:15:12 +0000316
subrata_modak56207ce2009-03-23 13:35:39 +0000317 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
318 if (Test_cases[ind].pathname == High_address) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800319 /*if (strcmp(Test_cases[ind].pathname, HIGH_ADDRESS) == 0) { ** */
subrata_modak56207ce2009-03-23 13:35:39 +0000320 Test_cases[ind].pathname = (char *)(sbrk(0) + 5);
321 break;
322 }
plars865695b2001-08-27 22:15:12 +0000323 }
subrata_modak56207ce2009-03-23 13:35:39 +0000324 return 0;
plars865695b2001-08-27 22:15:12 +0000325
326}
robbiewd34d5812005-07-11 22:28:09 +0000327#endif
plars865695b2001-08-27 22:15:12 +0000328
329/******************************************************************
330 * longpath_setup() - creates a filename that is too long
331 ******************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400332int longpath_setup(void)
plars865695b2001-08-27 22:15:12 +0000333{
subrata_modak56207ce2009-03-23 13:35:39 +0000334 int ind;
plars865695b2001-08-27 22:15:12 +0000335
subrata_modak56207ce2009-03-23 13:35:39 +0000336 for (ind = 0; ind <= PATH_MAX + 1; ind++) {
337 Longpathname[ind] = 'a';
338 }
339 return 0;
plars865695b2001-08-27 22:15:12 +0000340
341}
342
343/******************************************************************
344 * filepath_setup() creates a file the exists that we will treat as a directory
345 ******************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400346int filepath_setup(void)
plars865695b2001-08-27 22:15:12 +0000347{
subrata_modak56207ce2009-03-23 13:35:39 +0000348 int fd;
plars865695b2001-08-27 22:15:12 +0000349
subrata_modak56207ce2009-03-23 13:35:39 +0000350 if ((fd = creat("file", 0777)) == -1) {
351 tst_brkm(TBROK, cleanup, "creat(file) failed, errno:%d %s",
352 errno, strerror(errno));
353 }
354 close(fd);
355 return 0;
plars865695b2001-08-27 22:15:12 +0000356}
357
358/******************************************************************
359 * sig11_handler() - our segfault recover hack
360 ******************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000361void sig11_handler(int sig)
plars865695b2001-08-27 22:15:12 +0000362{
subrata_modak56207ce2009-03-23 13:35:39 +0000363 longjmp(sig11_recover, 1);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700364}