1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 * 07/2001 ported by John George 5 * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com> 6 */ 7 8/*\ 9 * [Description] 10 * 11 * Verify that the system call utime() successfully changes the last 12 * access and modification times of a file to the current time, 13 * under the following constraints: 14 * 15 * - The times argument is NULL. 16 * - The user ID of the process is not "root". 17 * - The file is owned by the user ID of the process. 18 */ 19 20#include <utime.h> 21#include <pwd.h> 22 23#include "tst_test.h" 24#include "tst_clocks.h" 25 26#define MNT_POINT "mntpoint" 27#define TEMP_FILE MNT_POINT"/tmp_file" 28#define FILE_MODE 0444 29 30#define TEST_USERNAME "nobody" 31 32 33static void setup(void) 34{ 35 struct passwd *pw; 36 37 pw = SAFE_GETPWNAM(TEST_USERNAME); 38 39 SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL); 40 SAFE_CHOWN(TEMP_FILE, pw->pw_uid, pw->pw_gid); 41 42 tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name); 43 44 SAFE_SETEUID(pw->pw_uid); 45} 46 47static void run(void) 48{ 49 struct utimbuf utbuf; 50 struct stat stat_buf; 51 time_t pre_time, post_time; 52 53 utbuf.modtime = tst_get_fs_timestamp() - 5; 54 utbuf.actime = utbuf.modtime + 1; 55 TST_EXP_PASS_SILENT(utime(TEMP_FILE, &utbuf)); 56 SAFE_STAT(TEMP_FILE, &stat_buf); 57 58 TST_EXP_EQ_LI(stat_buf.st_atime, utbuf.actime); 59 TST_EXP_EQ_LI(stat_buf.st_mtime, utbuf.modtime); 60 61 pre_time = tst_get_fs_timestamp(); 62 TST_EXP_PASS(utime(TEMP_FILE, NULL), "utime(%s, NULL)", TEMP_FILE); 63 if (!TST_PASS) 64 return; 65 post_time = tst_get_fs_timestamp(); 66 SAFE_STAT(TEMP_FILE, &stat_buf); 67 68 if (stat_buf.st_mtime < pre_time || stat_buf.st_mtime > post_time) 69 tst_res(TFAIL, "utime() did not set expected mtime, " 70 "pre_time: %ld, post_time: %ld, st_mtime: %ld", 71 pre_time, post_time, stat_buf.st_mtime); 72 73 if (stat_buf.st_atime < pre_time || stat_buf.st_atime > post_time) 74 tst_res(TFAIL, "utime() did not set expected atime, " 75 "pre_time: %ld, post_time: %ld, st_atime: %ld", 76 pre_time, post_time, stat_buf.st_atime); 77} 78 79static struct tst_test test = { 80 .test_all = run, 81 .setup = setup, 82 .needs_root = 1, 83 .mntpoint = MNT_POINT, 84 .mount_device = 1, 85 .all_filesystems = 1, 86 .skip_filesystems = (const char *const[]) { 87 "vfat", 88 "exfat", 89 NULL 90 } 91}; 92