blob: dd5ad49052c44eeb106e65fe433f4fc4bbe7bf2e [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * Copyright (c) International Business Machines Corp., 2001
Xing Guf38713b2014-06-04 17:43:26 +08003 * 07/2001 John George
plars865695b2001-08-27 22:15:12 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Xing Guf38713b2014-06-04 17:43:26 +080016 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
subrata_modak4bb656a2009-02-26 12:02:09 +000019/*
Xing Guf38713b2014-06-04 17:43:26 +080020 * Test Description:
21 * Verify that,
22 * 1) truncate(2) returns -1 and sets errno to EACCES if search/write
23 * permission denied for the process on the component of the path prefix
24 * or named file.
25 * 2) truncate(2) returns -1 and sets errno to ENOTDIR if the component of
26 * the path prefix is not a directory.
27 * 3) truncate(2) returns -1 and sets errno to EFAULT if pathname points
28 * outside user's accessible address space.
29 * 4) truncate(2) returns -1 and sets errno to ENAMETOOLONG if the component
30 * of a pathname exceeded 255 characters or entire pathname exceeds 1023
31 * characters.
32 * 5) truncate(2) returns -1 and sets errno to ENOENT if the named file
33 * does not exist.
34 * 6) truncate(2) returns -1 and sets errno to EISDIR if the named file
35 * is a directory.
Xing Gud301beb2014-06-04 17:43:27 +080036 * 7) truncate(2) returns -1 and sets errno to EFBIG if the argument length
37 * is larger than the maximum file size.
38 * 8) truncate(2) returns -1 and sets errno to ELOOP if too many symbolic
39 * links were encountered in translating the pathname.
plars865695b2001-08-27 22:15:12 +000040 */
41
Xing Gud301beb2014-06-04 17:43:27 +080042#define _GNU_SOURCE
43
plars865695b2001-08-27 22:15:12 +000044#include <stdio.h>
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <sys/fcntl.h>
plars1ad84512002-07-23 13:11:18 +000048#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000049#include <errno.h>
50#include <string.h>
51#include <signal.h>
robbiew5ed4d212001-08-28 16:10:11 +000052#include <pwd.h>
Jan Stancek1b7edcd2014-08-20 11:28:59 +020053#include <sys/resource.h>
plars865695b2001-08-27 22:15:12 +000054
55#include "test.h"
Xing Guf38713b2014-06-04 17:43:26 +080056#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000057
Xing Guf38713b2014-06-04 17:43:26 +080058#define TEST_FILE1 "testfile"
59#define TEST_FILE2 "t_file/testfile"
Xing Gud301beb2014-06-04 17:43:27 +080060#define TEST_FILE3 "testfile3"
61#define TEST_SYM1 "testsymlink1"
62#define TEST_SYM2 "testsymlink2"
Xing Guf38713b2014-06-04 17:43:26 +080063#define TEST_DIR1 "testdir"
plars865695b2001-08-27 22:15:12 +000064#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
65#define NEW_MODE S_IRUSR | S_IRGRP | S_IROTH
Xing Guf38713b2014-06-04 17:43:26 +080066#define DIR_MODE S_IRWXU
67#define TRUNC_LEN 256
Jan Stancek1b7edcd2014-08-20 11:28:59 +020068#define MAX_FSIZE (16*1024*1024)
plars865695b2001-08-27 22:15:12 +000069
Xing Guf38713b2014-06-04 17:43:26 +080070static char long_pathname[PATH_MAX + 2];
plars865695b2001-08-27 22:15:12 +000071
Xing Guf38713b2014-06-04 17:43:26 +080072static struct test_case_t {
plars865695b2001-08-27 22:15:12 +000073 char *pathname;
Xing Guf38713b2014-06-04 17:43:26 +080074 off_t length;
plars865695b2001-08-27 22:15:12 +000075 int exp_errno;
Xing Guf38713b2014-06-04 17:43:26 +080076} test_cases[] = {
77 { TEST_FILE1, TRUNC_LEN, EACCES },
78 { TEST_FILE2, TRUNC_LEN, ENOTDIR },
robbiewd34d5812005-07-11 22:28:09 +000079#if !defined(UCLINUX)
Xing Guf38713b2014-06-04 17:43:26 +080080 { NULL, TRUNC_LEN, EFAULT },
81 { (char *)-1, TRUNC_LEN, EFAULT },
vapier62b16cf2007-02-09 20:48:23 +000082#endif
Xing Guf38713b2014-06-04 17:43:26 +080083 { long_pathname, TRUNC_LEN, ENAMETOOLONG },
84 { "", TRUNC_LEN, ENOENT },
Xing Gud301beb2014-06-04 17:43:27 +080085 { TEST_DIR1, TRUNC_LEN, EISDIR },
Jan Stancek1b7edcd2014-08-20 11:28:59 +020086 { TEST_FILE3, MAX_FSIZE*2, EFBIG },
Xing Gud301beb2014-06-04 17:43:27 +080087 { TEST_SYM1, TRUNC_LEN, ELOOP }
plars865695b2001-08-27 22:15:12 +000088};
Wanlong Gao354ebb42012-12-07 10:10:04 +080089
Xing Guf38713b2014-06-04 17:43:26 +080090static void setup(void);
91static void cleanup(void);
92static void truncate_verify(struct test_case_t *);
plars865695b2001-08-27 22:15:12 +000093
Xing Guf38713b2014-06-04 17:43:26 +080094char *TCID = "truncate03";
95int TST_TOTAL = ARRAY_SIZE(test_cases);
plars865695b2001-08-27 22:15:12 +000096
subrata_modak56207ce2009-03-23 13:35:39 +000097int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000098{
Xing Guf38713b2014-06-04 17:43:26 +080099 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200100 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000101
Garrett Cooper45e285d2010-11-22 12:19:25 -0800102 msg = parse_opts(ac, av, NULL, NULL);
Xing Guf38713b2014-06-04 17:43:26 +0800103 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +0000104 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800105
plars865695b2001-08-27 22:15:12 +0000106 setup();
107
plars865695b2001-08-27 22:15:12 +0000108 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800109 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000110
Xing Guf38713b2014-06-04 17:43:26 +0800111 for (i = 0; i < TST_TOTAL; i++)
112 truncate_verify(&test_cases[i]);
plars865695b2001-08-27 22:15:12 +0000113
Garrett Cooper2c282152010-12-16 00:55:50 -0800114 }
plars865695b2001-08-27 22:15:12 +0000115
plars865695b2001-08-27 22:15:12 +0000116 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800117 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800118}
plars865695b2001-08-27 22:15:12 +0000119
Mike Frysingerc57fba52014-04-09 18:56:30 -0400120void setup(void)
plars865695b2001-08-27 22:15:12 +0000121{
Xing Guf38713b2014-06-04 17:43:26 +0800122 struct passwd *ltpuser;
123 char *bad_addr;
Jan Stancek1b7edcd2014-08-20 11:28:59 +0200124 struct rlimit rlim;
125 sigset_t sigset;
plars865695b2001-08-27 22:15:12 +0000126
Xing Guf38713b2014-06-04 17:43:26 +0800127 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000128
Xing Guf38713b2014-06-04 17:43:26 +0800129 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000130
Xing Guf38713b2014-06-04 17:43:26 +0800131 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
132 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
133
plars865695b2001-08-27 22:15:12 +0000134 TEST_PAUSE;
135
plars865695b2001-08-27 22:15:12 +0000136 tst_tmpdir();
137
Xing Guf38713b2014-06-04 17:43:26 +0800138 SAFE_TOUCH(cleanup, TEST_FILE1, NEW_MODE, NULL);
plars865695b2001-08-27 22:15:12 +0000139
Xing Guf38713b2014-06-04 17:43:26 +0800140 SAFE_TOUCH(cleanup, "t_file", FILE_MODE, NULL);
plars865695b2001-08-27 22:15:12 +0000141
vapier62b16cf2007-02-09 20:48:23 +0000142#if !defined(UCLINUX)
Xing Guf38713b2014-06-04 17:43:26 +0800143 test_cases[2].pathname = (char *)get_high_address();
144
145 bad_addr = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000146 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
Xing Guf38713b2014-06-04 17:43:26 +0800147 test_cases[3].pathname = bad_addr;
mreed1081534c32006-08-03 05:21:24 +0000148#endif
vapier62b16cf2007-02-09 20:48:23 +0000149
Xing Guf38713b2014-06-04 17:43:26 +0800150 memset(long_pathname, 'a', PATH_MAX + 1);
151
152 SAFE_MKDIR(cleanup, TEST_DIR1, DIR_MODE);
Xing Gud301beb2014-06-04 17:43:27 +0800153
154 SAFE_TOUCH(cleanup, TEST_FILE3, FILE_MODE, NULL);
155
156 SAFE_SYMLINK(cleanup, TEST_SYM1, TEST_SYM2);
157 SAFE_SYMLINK(cleanup, TEST_SYM2, TEST_SYM1);
Jan Stancek1b7edcd2014-08-20 11:28:59 +0200158
159 rlim.rlim_cur = MAX_FSIZE;
160 rlim.rlim_max = MAX_FSIZE;
161 SAFE_SETRLIMIT(cleanup, RLIMIT_FSIZE, &rlim);
162
163 sigemptyset(&sigset);
164 sigaddset(&sigset, SIGXFSZ);
165 TEST(sigprocmask(SIG_BLOCK, &sigset, NULL));
166 if (TEST_RETURN != 0)
167 tst_brkm(TBROK | TTERRNO, cleanup, "sigprocmask");
Garrett Cooper2c282152010-12-16 00:55:50 -0800168}
plars865695b2001-08-27 22:15:12 +0000169
Xing Guf38713b2014-06-04 17:43:26 +0800170void truncate_verify(struct test_case_t *tc)
plars865695b2001-08-27 22:15:12 +0000171{
Xing Guf38713b2014-06-04 17:43:26 +0800172 TEST(truncate(tc->pathname, tc->length));
plars865695b2001-08-27 22:15:12 +0000173
Xing Guf38713b2014-06-04 17:43:26 +0800174 if (TEST_RETURN != -1) {
175 tst_resm(TFAIL, "truncate() returned %ld, "
176 "expected -1, errno:%d", TEST_RETURN,
177 tc->exp_errno);
178 return;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 }
plars865695b2001-08-27 22:15:12 +0000180
Xing Guf38713b2014-06-04 17:43:26 +0800181 if (TEST_ERRNO == tc->exp_errno) {
182 tst_resm(TPASS | TTERRNO, "truncate() failed as expected");
183 } else {
184 tst_resm(TFAIL | TTERRNO,
185 "truncate() failed unexpectedly; expected: %d - %s",
186 tc->exp_errno, strerror(tc->exp_errno));
187 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800188}
plars865695b2001-08-27 22:15:12 +0000189
Mike Frysingerc57fba52014-04-09 18:56:30 -0400190void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000191{
plars865695b2001-08-27 22:15:12 +0000192 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700193}