xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utime/utime07.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Authors: David Fenner, Jon Hendrickson
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 Andrea Cervesato <[email protected]>
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  * This test verifies that utime() is working correctly on symlink()
12*49cdfc7eSAndroid Build Coastguard Worker  * generated files.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * Also verify if utime() fails with:
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * - ENOENT when symlink points to nowhere
17*49cdfc7eSAndroid Build Coastguard Worker  * - ELOOP when symlink is looping
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <utime.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define TIME_DIFF 100
24*49cdfc7eSAndroid Build Coastguard Worker 
create_symlink(const char * path,const char * symname)25*49cdfc7eSAndroid Build Coastguard Worker static void create_symlink(const char *path, const char *symname)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	struct stat asymlink;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(path, symname);
30*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSTAT(symname, &asymlink);
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if ((asymlink.st_mode & S_IFMT) != S_IFLNK) {
33*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "symlink generated a non-symbolic link %s to %s",
34*49cdfc7eSAndroid Build Coastguard Worker 			symname, path);
35*49cdfc7eSAndroid Build Coastguard Worker 	}
36*49cdfc7eSAndroid Build Coastguard Worker }
37*49cdfc7eSAndroid Build Coastguard Worker 
test_utime(void)38*49cdfc7eSAndroid Build Coastguard Worker static void test_utime(void)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	char *symname = "my_symlink0";
41*49cdfc7eSAndroid Build Coastguard Worker 	struct stat oldsym_stat;
42*49cdfc7eSAndroid Build Coastguard Worker 	struct stat newsym_stat;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test if utime() changes access time");
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	create_symlink(tst_get_tmpdir(), symname);
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(symname, &oldsym_stat);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	struct utimbuf utimes = {
50*49cdfc7eSAndroid Build Coastguard Worker 		.actime = oldsym_stat.st_atime + TIME_DIFF,
51*49cdfc7eSAndroid Build Coastguard Worker 		.modtime = oldsym_stat.st_mtime + TIME_DIFF
52*49cdfc7eSAndroid Build Coastguard Worker 	};
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(utime(symname, &utimes));
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(symname, &newsym_stat);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(newsym_stat.st_atime - oldsym_stat.st_atime, TIME_DIFF);
58*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(newsym_stat.st_mtime - oldsym_stat.st_mtime, TIME_DIFF);
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(symname);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
test_utime_no_path(void)63*49cdfc7eSAndroid Build Coastguard Worker static void test_utime_no_path(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	char *symname = "my_symlink1";
66*49cdfc7eSAndroid Build Coastguard Worker 	struct utimbuf utimes;
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test if utime() raises ENOENT when symlink points to nowhere");
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 	create_symlink("bc+eFhi!k", symname);
71*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(utime(symname, &utimes), ENOENT);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(symname);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
test_utime_loop(void)76*49cdfc7eSAndroid Build Coastguard Worker static void test_utime_loop(void)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker 	char *symname = "my_symlink2";
79*49cdfc7eSAndroid Build Coastguard Worker 	struct utimbuf utimes;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test if utime() raises ELOOP when symlink is looping");
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	create_symlink(symname, symname);
84*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(utime(symname, &utimes), ELOOP);
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(symname);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
run(void)89*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker 	test_utime();
92*49cdfc7eSAndroid Build Coastguard Worker 	test_utime_no_path();
93*49cdfc7eSAndroid Build Coastguard Worker 	test_utime_loop();
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
97*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
98*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
99*49cdfc7eSAndroid Build Coastguard Worker };
100