xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/stat/stat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) International Business Machines  Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 John George
4*49cdfc7eSAndroid Build Coastguard Worker  *		-Ported
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2002-2022
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, stat(2) succeeds to get the status of a file and fills the
12*49cdfc7eSAndroid Build Coastguard Worker  * stat structure elements regardless of whether process has or doesn't
13*49cdfc7eSAndroid Build Coastguard Worker  * have read access to the file.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define FILE_SIZE	 1024
20*49cdfc7eSAndroid Build Coastguard Worker #define TST_FILEREAD     "test_fileread"
21*49cdfc7eSAndroid Build Coastguard Worker #define TST_FILENOREAD   "test_filenoread"
22*49cdfc7eSAndroid Build Coastguard Worker #define READ_MODE        0666
23*49cdfc7eSAndroid Build Coastguard Worker #define NEW_MODE         0222
24*49cdfc7eSAndroid Build Coastguard Worker #define MASK             0777
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static uid_t user_id;
27*49cdfc7eSAndroid Build Coastguard Worker static gid_t group_id;
28*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static struct tcase{
31*49cdfc7eSAndroid Build Coastguard Worker 	char *pathname;
32*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int mode;
33*49cdfc7eSAndroid Build Coastguard Worker } TC[] = {
34*49cdfc7eSAndroid Build Coastguard Worker 	{TST_FILEREAD, READ_MODE},
35*49cdfc7eSAndroid Build Coastguard Worker 	{TST_FILENOREAD, NEW_MODE}
36*49cdfc7eSAndroid Build Coastguard Worker };
37*49cdfc7eSAndroid Build Coastguard Worker 
verify_stat(unsigned int n)38*49cdfc7eSAndroid Build Coastguard Worker static void verify_stat(unsigned int n)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = TC + n;
41*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(stat(tc->pathname, &stat_buf));
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(stat_buf.st_uid, user_id);
46*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(stat_buf.st_gid, group_id);
47*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(stat_buf.st_size, FILE_SIZE);
48*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(stat_buf.st_mode & MASK, tc->mode);
49*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(stat_buf.st_nlink, 1);
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)52*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
57*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETUID(ltpuser->pw_uid);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(TC); i++) {
60*49cdfc7eSAndroid Build Coastguard Worker 		if (tst_fill_file(TC[i].pathname, 'a', 256, 4))
61*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK, "Failed to create tst file %s",
62*49cdfc7eSAndroid Build Coastguard Worker 				TC[i].pathname);
63*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CHMOD(TC[i].pathname, TC[i].mode);
64*49cdfc7eSAndroid Build Coastguard Worker 	}
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	user_id = getuid();
67*49cdfc7eSAndroid Build Coastguard Worker 	group_id = getgid();
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
71*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(TC),
72*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
73*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_stat,
74*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
75*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
76*49cdfc7eSAndroid Build Coastguard Worker };
77