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