xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/truncate/truncate03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Linux Test Project, 2001-2022
4  * Copyright (c) International Business Machines  Corp., 2001
5  * 07/2001 John George
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that:
12  *
13  * - truncate(2) returns -1 and sets errno to EACCES if search/write
14  *   permission denied for the process on the component of the path prefix
15  *   or named file.
16  * - truncate(2) returns -1 and sets errno to ENOTDIR if the component of
17  *   the path prefix is not a directory.
18  * - truncate(2) returns -1 and sets errno to EFAULT if pathname points
19  *   outside user's accessible address space.
20  * - truncate(2) returns -1 and sets errno to ENAMETOOLONG if the component
21  *   of a pathname exceeded 255 characters or entire pathname exceeds 1023
22  *   characters.
23  * - truncate(2) returns -1 and sets errno to ENOENT if the named file
24  *    does not exist.
25  * - truncate(2) returns -1 and sets errno to EISDIR if the named file
26  *   is a directory.
27  * - truncate(2) returns -1 and sets errno to EFBIG if the argument length
28  *   is larger than the maximum file size.
29  * - truncate(2) returns -1 and sets errno to ELOOP if too many symbolic
30  *   links were encountered in translating the pathname.
31  */
32 
33 #define _GNU_SOURCE
34 
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <sys/mman.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <pwd.h>
44 #include <sys/resource.h>
45 
46 #include "tst_test.h"
47 
48 #define TEST_FILE1	"testfile"
49 #define TEST_FILE2	"t_file/testfile"
50 #define TEST_FILE3	"testfile3"
51 #define TEST_SYM1	"testsymlink1"
52 #define TEST_SYM2	"testsymlink2"
53 #define TEST_DIR1	"testdir"
54 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
55 #define NEW_MODE	S_IRUSR | S_IRGRP | S_IROTH
56 #define DIR_MODE	S_IRWXU
57 #define TRUNC_LEN	256
58 #define MAX_FSIZE	(16*1024*1024)
59 
60 static char long_pathname[PATH_MAX + 2];
61 
62 static struct test_case_t {
63 	char *pathname;
64 	off_t length;
65 	int exp_errno;
66 } test_cases[] = {
67 	{ TEST_FILE1, TRUNC_LEN, EACCES },
68 	{ TEST_FILE2, TRUNC_LEN, ENOTDIR },
69 	{ NULL, TRUNC_LEN, EFAULT },
70 	{ long_pathname, TRUNC_LEN, ENAMETOOLONG },
71 	{ "", TRUNC_LEN, ENOENT },
72 	{ TEST_DIR1, TRUNC_LEN, EISDIR },
73 	{ TEST_FILE3, MAX_FSIZE*2, EFBIG },
74 	{ TEST_SYM1, TRUNC_LEN, ELOOP }
75 };
76 
setup(void)77 static void setup(void)
78 {
79 	struct passwd *ltpuser;
80 	struct rlimit rlim = {
81 		.rlim_cur = MAX_FSIZE,
82 		.rlim_max = MAX_FSIZE,
83 	};
84 	sigset_t signalset;
85 	unsigned int n;
86 
87 	ltpuser = SAFE_GETPWNAM("nobody");
88 	SAFE_SETEUID(ltpuser->pw_uid);
89 
90 	SAFE_TOUCH(TEST_FILE1, NEW_MODE, NULL);
91 
92 	SAFE_TOUCH("t_file", FILE_MODE, NULL);
93 
94 	memset(long_pathname, 'a', PATH_MAX + 1);
95 
96 	SAFE_MKDIR(TEST_DIR1, DIR_MODE);
97 
98 	SAFE_TOUCH(TEST_FILE3, FILE_MODE, NULL);
99 
100 	SAFE_SYMLINK(TEST_SYM1, TEST_SYM2);
101 	SAFE_SYMLINK(TEST_SYM2, TEST_SYM1);
102 
103 	SAFE_SETRLIMIT(RLIMIT_FSIZE, &rlim);
104 
105 	SAFE_SIGEMPTYSET(&signalset);
106 	SAFE_SIGADDSET(&signalset, SIGXFSZ);
107 	SAFE_SIGPROCMASK(SIG_BLOCK, &signalset, NULL);
108 
109 	for (n = 0; n < ARRAY_SIZE(test_cases); n++) {
110 		if (!test_cases[n].pathname)
111 			test_cases[n].pathname = tst_get_bad_addr(NULL);
112 	}
113 
114 }
115 
verify_truncate(unsigned int n)116 static void verify_truncate(unsigned int n)
117 {
118 	struct test_case_t *tc = &test_cases[n];
119 
120 	TST_EXP_FAIL(truncate(tc->pathname, tc->length), tc->exp_errno);
121 }
122 
123 static struct tst_test test = {
124 	.needs_root = 1,
125 	.needs_tmpdir = 1,
126 	.setup = setup,
127 	.tcnt = ARRAY_SIZE(test_cases),
128 	.test = verify_truncate,
129 };
130