plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 3 | * 07/2001 John George |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
| 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 Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 16 | * along with this program; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 19 | /* |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 20 | * 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 Gu | d301beb | 2014-06-04 17:43:27 +0800 | [diff] [blame] | 36 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | */ |
| 41 | |
Xing Gu | d301beb | 2014-06-04 17:43:27 +0800 | [diff] [blame] | 42 | #define _GNU_SOURCE |
| 43 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <sys/fcntl.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 48 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | #include <errno.h> |
| 50 | #include <string.h> |
| 51 | #include <signal.h> |
robbiew | 5ed4d21 | 2001-08-28 16:10:11 +0000 | [diff] [blame] | 52 | #include <pwd.h> |
Jan Stancek | 1b7edcd | 2014-08-20 11:28:59 +0200 | [diff] [blame] | 53 | #include <sys/resource.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | #include "test.h" |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 56 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 58 | #define TEST_FILE1 "testfile" |
| 59 | #define TEST_FILE2 "t_file/testfile" |
Xing Gu | d301beb | 2014-06-04 17:43:27 +0800 | [diff] [blame] | 60 | #define TEST_FILE3 "testfile3" |
| 61 | #define TEST_SYM1 "testsymlink1" |
| 62 | #define TEST_SYM2 "testsymlink2" |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 63 | #define TEST_DIR1 "testdir" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 65 | #define NEW_MODE S_IRUSR | S_IRGRP | S_IROTH |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 66 | #define DIR_MODE S_IRWXU |
| 67 | #define TRUNC_LEN 256 |
Jan Stancek | 1b7edcd | 2014-08-20 11:28:59 +0200 | [diff] [blame] | 68 | #define MAX_FSIZE (16*1024*1024) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 70 | static char long_pathname[PATH_MAX + 2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 72 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | char *pathname; |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 74 | off_t length; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | int exp_errno; |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 76 | } test_cases[] = { |
| 77 | { TEST_FILE1, TRUNC_LEN, EACCES }, |
| 78 | { TEST_FILE2, TRUNC_LEN, ENOTDIR }, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 79 | #if !defined(UCLINUX) |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 80 | { NULL, TRUNC_LEN, EFAULT }, |
| 81 | { (char *)-1, TRUNC_LEN, EFAULT }, |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 82 | #endif |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 83 | { long_pathname, TRUNC_LEN, ENAMETOOLONG }, |
| 84 | { "", TRUNC_LEN, ENOENT }, |
Xing Gu | d301beb | 2014-06-04 17:43:27 +0800 | [diff] [blame] | 85 | { TEST_DIR1, TRUNC_LEN, EISDIR }, |
Jan Stancek | 1b7edcd | 2014-08-20 11:28:59 +0200 | [diff] [blame] | 86 | { TEST_FILE3, MAX_FSIZE*2, EFBIG }, |
Xing Gu | d301beb | 2014-06-04 17:43:27 +0800 | [diff] [blame] | 87 | { TEST_SYM1, TRUNC_LEN, ELOOP } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 90 | static void setup(void); |
| 91 | static void cleanup(void); |
| 92 | static void truncate_verify(struct test_case_t *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 94 | char *TCID = "truncate03"; |
| 95 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | { |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 99 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 100 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 102 | msg = parse_opts(ac, av, NULL, NULL); |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 103 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | setup(); |
| 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 109 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 111 | for (i = 0; i < TST_TOTAL; i++) |
| 112 | truncate_verify(&test_cases[i]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 114 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 117 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 118 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 120 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | { |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 122 | struct passwd *ltpuser; |
| 123 | char *bad_addr; |
Jan Stancek | 1b7edcd | 2014-08-20 11:28:59 +0200 | [diff] [blame] | 124 | struct rlimit rlim; |
| 125 | sigset_t sigset; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 127 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 129 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 131 | ltpuser = SAFE_GETPWNAM(cleanup, "nobody"); |
| 132 | SAFE_SETEUID(cleanup, ltpuser->pw_uid); |
| 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | TEST_PAUSE; |
| 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | tst_tmpdir(); |
| 137 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 138 | SAFE_TOUCH(cleanup, TEST_FILE1, NEW_MODE, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 140 | SAFE_TOUCH(cleanup, "t_file", FILE_MODE, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 142 | #if !defined(UCLINUX) |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 143 | test_cases[2].pathname = (char *)get_high_address(); |
| 144 | |
| 145 | bad_addr = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 147 | test_cases[3].pathname = bad_addr; |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 148 | #endif |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 149 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 150 | memset(long_pathname, 'a', PATH_MAX + 1); |
| 151 | |
| 152 | SAFE_MKDIR(cleanup, TEST_DIR1, DIR_MODE); |
Xing Gu | d301beb | 2014-06-04 17:43:27 +0800 | [diff] [blame] | 153 | |
| 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 Stancek | 1b7edcd | 2014-08-20 11:28:59 +0200 | [diff] [blame] | 158 | |
| 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 168 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 170 | void truncate_verify(struct test_case_t *tc) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | { |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 172 | TEST(truncate(tc->pathname, tc->length)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 174 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 179 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | |
Xing Gu | f38713b | 2014-06-04 17:43:26 +0800 | [diff] [blame] | 181 | 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 188 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 190 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 193 | } |