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