xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utimes/utimes01.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) Crackerjack Project., 2007 ,Hitachi, Ltd
4*49cdfc7eSAndroid Build Coastguard Worker  * Author(s): Takahiro Yasui <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Ported to LTP: Manas Kumar Nayak [email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Description:
9*49cdfc7eSAndroid Build Coastguard Worker  *   Verify that,
10*49cdfc7eSAndroid Build Coastguard Worker  *   1) utimes() returns -1 and sets errno to EACCES if times
11*49cdfc7eSAndroid Build Coastguard Worker  *      is NULL, the caller's effective user ID does not match
12*49cdfc7eSAndroid Build Coastguard Worker  *      the owner of the file, the caller does not have write
13*49cdfc7eSAndroid Build Coastguard Worker  *      access to the file, and the caller is not privileged.
14*49cdfc7eSAndroid Build Coastguard Worker  *   2) utimes() returns -1 and sets errno to ENOENT if filename
15*49cdfc7eSAndroid Build Coastguard Worker  *      does not exist.
16*49cdfc7eSAndroid Build Coastguard Worker  *   3) utimes() returns -1 and sets errno to EFAULT if filename
17*49cdfc7eSAndroid Build Coastguard Worker  *      is NULL.
18*49cdfc7eSAndroid Build Coastguard Worker  *   4) utimes() returns -1 and sets errno to EPERM if times is
19*49cdfc7eSAndroid Build Coastguard Worker  *      not NULL, the caller's effective UID does not match the
20*49cdfc7eSAndroid Build Coastguard Worker  *      owner of the file, and the caller is not privileged.
21*49cdfc7eSAndroid Build Coastguard Worker  *   5) utimes() returns -1 and sets errno to EROFS if path resides
22*49cdfc7eSAndroid Build Coastguard Worker  *      on a read-only file system.
23*49cdfc7eSAndroid Build Coastguard Worker  */
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
32*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE1 "testfile1"
33*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE2 "testfile2"
34*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE3 "mntpoint/file"
35*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE (S_IRWXU | S_IRGRP | S_IXGRP | \
36*49cdfc7eSAndroid Build Coastguard Worker 					S_IROTH | S_IXOTH)
37*49cdfc7eSAndroid Build Coastguard Worker #define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static struct timeval a_tv[2] = { {0, 0}, {1000, 0} };
40*49cdfc7eSAndroid Build Coastguard Worker static struct timeval m_tv[2] = { {1000, 0}, {0, 0} };
41*49cdfc7eSAndroid Build Coastguard Worker static struct timeval tv[2] = { {1000, 0}, {2000, 0} };
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
44*49cdfc7eSAndroid Build Coastguard Worker 	char *pathname;
45*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval *times;
46*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
47*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
48*49cdfc7eSAndroid Build Coastguard Worker 	{ TESTFILE1, a_tv, 0 },
49*49cdfc7eSAndroid Build Coastguard Worker 	{ TESTFILE1, m_tv, 0 },
50*49cdfc7eSAndroid Build Coastguard Worker 	{ TESTFILE2, NULL, EACCES },
51*49cdfc7eSAndroid Build Coastguard Worker 	{ "notexistfile", tv, ENOENT },
52*49cdfc7eSAndroid Build Coastguard Worker 	{ NULL, tv, EFAULT },
53*49cdfc7eSAndroid Build Coastguard Worker 	{ TESTFILE2, tv, EPERM },
54*49cdfc7eSAndroid Build Coastguard Worker 	{ TESTFILE3, tv, EROFS },
55*49cdfc7eSAndroid Build Coastguard Worker };
56*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)57*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TESTFILE2, FILE_MODE, NULL);
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(ltpuser->pw_uid);
63*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TESTFILE1, FILE_MODE, NULL);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker 
utimes_verify(unsigned int i)66*49cdfc7eSAndroid Build Coastguard Worker static void utimes_verify(unsigned int i)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker 	struct stat st;
69*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval tmp_tv[2];
70*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == 0) {
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_STAT(tc->pathname, &st);
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 		tmp_tv[0].tv_sec = st.st_atime;
76*49cdfc7eSAndroid Build Coastguard Worker 		tmp_tv[0].tv_usec = 0;
77*49cdfc7eSAndroid Build Coastguard Worker 		tmp_tv[1].tv_sec = st.st_mtime;
78*49cdfc7eSAndroid Build Coastguard Worker 		tmp_tv[1].tv_usec = 0;
79*49cdfc7eSAndroid Build Coastguard Worker 	}
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	TEST(utimes(tc->pathname, tc->times));
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == tc->exp_errno) {
84*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "utimes() worked as expected");
85*49cdfc7eSAndroid Build Coastguard Worker 	} else {
86*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
87*49cdfc7eSAndroid Build Coastguard Worker 			"utimes() failed unexpectedly; expected: %d - %s",
88*49cdfc7eSAndroid Build Coastguard Worker 			tc->exp_errno, tst_strerrno(tc->exp_errno));
89*49cdfc7eSAndroid Build Coastguard Worker 	}
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == 0 && utimes(tc->pathname, tmp_tv) == -1)
92*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "utimes() failed.");
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
96*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
97*49cdfc7eSAndroid Build Coastguard Worker 	.test = utimes_verify,
98*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
99*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
100*49cdfc7eSAndroid Build Coastguard Worker 	.needs_rofs = 1,
101*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
102*49cdfc7eSAndroid Build Coastguard Worker };
103