blob: 25ca4de384438f64a5129820fd48dc7d3463c094 [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: unlink07.c,v 1.8 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 : unlink07
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 : unlink(2) negative testcases
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
plars865695b2001-08-27 22:15:12 +000044 * PARENT DOCUMENT : usctpl01
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
plars865695b2001-08-27 22:15:12 +000046 * TEST CASE TOTAL : 1
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-8) 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).
subrata_modak4bb656a2009-02-26 12:02:09 +000067 *
plars865695b2001-08-27 22:15:12 +000068 * OUTPUT SPECIFICATIONS
subrata_modak56207ce2009-03-23 13:35:39 +000069 *$
plars865695b2001-08-27 22:15:12 +000070 * DURATION
71 * Terminates - with frequency and infinite modes.
subrata_modak4bb656a2009-02-26 12:02:09 +000072 *
plars865695b2001-08-27 22:15:12 +000073 * SIGNALS
74 * Uses SIGUSR1 to pause before test if option set.
75 * (See the parse_opts(3) man page).
76 *
77 * RESOURCES
78 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000079 *
plars865695b2001-08-27 22:15:12 +000080 * ENVIRONMENTAL NEEDS
81 * No run-time environmental needs.
subrata_modak4bb656a2009-02-26 12:02:09 +000082 *
plars865695b2001-08-27 22:15:12 +000083 * SPECIAL PROCEDURAL REQUIREMENTS
84 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000085 *
plars865695b2001-08-27 22:15:12 +000086 * INTERCASE DEPENDENCIES
87 * None
subrata_modak4bb656a2009-02-26 12:02:09 +000088 *
plars865695b2001-08-27 22:15:12 +000089 * DETAILED DESCRIPTION
90 * This is a Phase I test for the unlink(2) system call. It is intended
91 * to provide a limited exposure of the system call, for now. It
92 * should/will be extended when full functional tests are written for
93 * unlink(2).
subrata_modak4bb656a2009-02-26 12:02:09 +000094 *
plars865695b2001-08-27 22:15:12 +000095 * Setup:
96 * Setup signal handling.
97 * Pause for SIGUSR1 if option specified.
subrata_modak4bb656a2009-02-26 12:02:09 +000098 *
plars865695b2001-08-27 22:15:12 +000099 * Test:
100 * Loop if the proper options are given.
101 * Execute system call
102 * Check return code, if system call failed (return=-1)
103 * Log the errno and Issue a FAIL message.
104 * Otherwise, Issue a PASS message.
subrata_modak4bb656a2009-02-26 12:02:09 +0000105 *
plars865695b2001-08-27 22:15:12 +0000106 * Cleanup:
107 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +0000108 *
109 *
plars865695b2001-08-27 22:15:12 +0000110 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
111
112#include <sys/types.h>
113#include <sys/fcntl.h>
114#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +0000115#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +0000116#include <errno.h>
117#include <string.h>
118#include <signal.h>
119#include <unistd.h>
120#include <sys/param.h> /* for PATH_MAX */
121#include "test.h"
plars865695b2001-08-27 22:15:12 +0000122
123void setup();
124void cleanup();
125
plars865695b2001-08-27 22:15:12 +0000126extern char *get_high_address();
127
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200128char *TCID = "unlink07";
129int TST_TOTAL = 6;
plars865695b2001-08-27 22:15:12 +0000130
subrata_modak56207ce2009-03-23 13:35:39 +0000131char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000132
plars865695b2001-08-27 22:15:12 +0000133int longpath_setup();
134int no_setup();
135int filepath_setup();
subrata_modak56207ce2009-03-23 13:35:39 +0000136char Longpathname[PATH_MAX + 2];
plars865695b2001-08-27 22:15:12 +0000137char High_address[64];
138
139struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +0000140 char *pathname;
141 char *desc;
142 int exp_errno;
143 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +0000144} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000145 {
146 "nonexistfile", "non-existent file", ENOENT, no_setup}, {
147 "", "path is empty string", ENOENT, no_setup}, {
148 "nefile/file", "path contains a non-existent file",
149 ENOENT, no_setup},
robbiewd34d5812005-07-11 22:28:09 +0000150#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000151 {
152 High_address, "address beyond address space", EFAULT, no_setup},
robbiewd34d5812005-07-11 22:28:09 +0000153#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000154 {
155 "file/file", "path contains a regular file",
156 ENOTDIR, filepath_setup},
robbiewd34d5812005-07-11 22:28:09 +0000157#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000158 {
159 High_address, "address beyond address space", EFAULT, no_setup},
robbiewd34d5812005-07-11 22:28:09 +0000160#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000161 {
162 Longpathname, "pathname too long", ENAMETOOLONG, longpath_setup}, {
163 (char *)-1, "negative address", EFAULT, no_setup}, {
164 NULL, NULL, 0, no_setup}
plars865695b2001-08-27 22:15:12 +0000165};
166
167/***********************************************************************
168 * Main
169 ***********************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000170int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000171{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200172 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200173 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000174 char *fname;
175 char *desc;
176 int ind;
177
plars865695b2001-08-27 22:15:12 +0000178 /***************************************************************
179 * parse standard options
180 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800181 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000182 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000183 }
plars865695b2001-08-27 22:15:12 +0000184
185 /***************************************************************
186 * perform global setup for test
187 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000188 setup();
plars865695b2001-08-27 22:15:12 +0000189
plars865695b2001-08-27 22:15:12 +0000190 /***************************************************************
191 * check looping state if -c option given
192 ***************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000193 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000194
Caspar Zhangd59a6592013-03-07 14:59:12 +0800195 tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000196
subrata_modak56207ce2009-03-23 13:35:39 +0000197 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
plars865695b2001-08-27 22:15:12 +0000198
subrata_modak56207ce2009-03-23 13:35:39 +0000199 fname = Test_cases[ind].pathname;
200 desc = Test_cases[ind].desc;
plars865695b2001-08-27 22:15:12 +0000201
robbiewd34d5812005-07-11 22:28:09 +0000202#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000203 if (fname == High_address)
204 fname = get_high_address();
205#endif
206 /*
207 * Call unlink(2)
208 */
209 TEST(unlink(fname));
subrata_modakbdbaec52009-02-26 12:14:51 +0000210
subrata_modak56207ce2009-03-23 13:35:39 +0000211 /* check return code */
212 if (TEST_RETURN == -1) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200213 if (TEST_ERRNO ==
214 Test_cases[ind].exp_errno)
215 tst_resm(TPASS,
216 "unlink(<%s>) Failed, errno=%d",
217 desc, TEST_ERRNO);
218 else
219 tst_resm(TFAIL,
220 "unlink(<%s>) Failed, errno=%d, expected errno:%d",
221 desc, TEST_ERRNO,
222 Test_cases
223 [ind].exp_errno);
subrata_modak56207ce2009-03-23 13:35:39 +0000224 } else {
225 tst_resm(TFAIL,
subrata_modak923b23f2009-11-02 13:57:16 +0000226 "unlink(<%s>) returned %ld, expected -1, errno:%d",
subrata_modak56207ce2009-03-23 13:35:39 +0000227 desc, TEST_RETURN,
228 Test_cases[ind].exp_errno);
229 }
230 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000231
Garrett Cooper2c282152010-12-16 00:55:50 -0800232 }
plars865695b2001-08-27 22:15:12 +0000233
subrata_modak56207ce2009-03-23 13:35:39 +0000234 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800235 tst_exit();
236}
plars865695b2001-08-27 22:15:12 +0000237
238/***************************************************************
239 * setup() - performs all ONE TIME setup for this test.
240 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400241void setup(void)
plars865695b2001-08-27 22:15:12 +0000242{
subrata_modak56207ce2009-03-23 13:35:39 +0000243 int ind;
plars865695b2001-08-27 22:15:12 +0000244
subrata_modak56207ce2009-03-23 13:35:39 +0000245 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000246
subrata_modak56207ce2009-03-23 13:35:39 +0000247 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000248
subrata_modak56207ce2009-03-23 13:35:39 +0000249 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000250
subrata_modak56207ce2009-03-23 13:35:39 +0000251 bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
252 if (bad_addr == MAP_FAILED) {
253 tst_brkm(TBROK, cleanup, "mmap failed");
254 }
255 Test_cases[7].pathname = bad_addr;
plars1ad84512002-07-23 13:11:18 +0000256
subrata_modak56207ce2009-03-23 13:35:39 +0000257 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
258 Test_cases[ind].setupfunc();
259 }
plars865695b2001-08-27 22:15:12 +0000260
Garrett Cooper2c282152010-12-16 00:55:50 -0800261}
plars865695b2001-08-27 22:15:12 +0000262
263/***************************************************************
264 * cleanup() - performs all ONE TIME cleanup for this test at
265 * completion or premature exit.
266 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400267void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000268{
subrata_modak56207ce2009-03-23 13:35:39 +0000269 chmod("unwrite_dir", 0777);
270 chmod("unsearch_dir", 0777);
plars865695b2001-08-27 22:15:12 +0000271
subrata_modak56207ce2009-03-23 13:35:39 +0000272 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000273
Garrett Cooper2c282152010-12-16 00:55:50 -0800274}
plars865695b2001-08-27 22:15:12 +0000275
276/******************************************************************
277 *
278 ******************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400279int no_setup(void)
plars865695b2001-08-27 22:15:12 +0000280{
subrata_modak56207ce2009-03-23 13:35:39 +0000281 return 0;
plars865695b2001-08-27 22:15:12 +0000282}
283
284/******************************************************************
285 *
286 ******************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400287int longpath_setup(void)
plars865695b2001-08-27 22:15:12 +0000288{
subrata_modak56207ce2009-03-23 13:35:39 +0000289 int ind;
plars865695b2001-08-27 22:15:12 +0000290
subrata_modak56207ce2009-03-23 13:35:39 +0000291 for (ind = 0; ind <= PATH_MAX + 1; ind++) {
292 Longpathname[ind] = 'a';
293 }
294 return 0;
plars865695b2001-08-27 22:15:12 +0000295
296}
subrata_modak56207ce2009-03-23 13:35:39 +0000297
plars865695b2001-08-27 22:15:12 +0000298/******************************************************************
299 *
300 ******************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400301int filepath_setup(void)
plars865695b2001-08-27 22:15:12 +0000302{
subrata_modak56207ce2009-03-23 13:35:39 +0000303 int fd;
plars865695b2001-08-27 22:15:12 +0000304
subrata_modak56207ce2009-03-23 13:35:39 +0000305 if ((fd = creat("file", 0777)) == -1) {
306 tst_brkm(TBROK, cleanup, "creat(file) failed, errno:%d %s",
307 errno, strerror(errno));
308 }
309 close(fd);
310 return 0;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700311}