blob: aa161ac57fcb4488f6d46b5c9695dd83b4e5d437 [file] [log] [blame]
Xing Guc2870842014-06-13 10:53:18 +08001/*
2 * Copyright (c) Crackerjack Project., 2007 ,Hitachi, Ltd
3 * Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com>
4 * Ported to LTP: Manas Kumar Nayak maknayak@in.ibm.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20/*
21 * Description:
22 * Verify that,
23 * 1) utimes() returns -1 and sets errno to EACCES if times
24 * is NULL, the caller's effective user ID does not match
25 * the owner of the file, the caller does not have write
26 * access to the file, and the caller is not privileged.
27 * 2) utimes() returns -1 and sets errno to ENOENT if filename
28 * does not exist.
29 * 3) utimes() returns -1 and sets errno to EFAULT if filename
30 * is NULL.
Xing Gu6b22d9d2014-06-13 10:53:19 +080031 * 4) utimes() returns -1 and sets errno to EPERM if times is
32 * not NULL, the caller's effective UID does not match the
33 * owner of the file, and the caller is not privileged.
34 * 5) utimes() returns -1 and sets errno to EROFS if path resides
35 * on a read-only file system.
Xing Guc2870842014-06-13 10:53:18 +080036 */
37
subrata_modak705ba462009-05-29 12:21:34 +000038#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/time.h>
41#include <dirent.h>
42#include <unistd.h>
43#include <getopt.h>
44#include <string.h>
45#include <stdlib.h>
46#include <errno.h>
47#include <stdio.h>
Xing Guc2870842014-06-13 10:53:18 +080048#include <pwd.h>
Xing Gu6b22d9d2014-06-13 10:53:19 +080049#include <sys/mount.h>
subrata_modak705ba462009-05-29 12:21:34 +000050
subrata_modak705ba462009-05-29 12:21:34 +000051#include "test.h"
Xing Guc2870842014-06-13 10:53:18 +080052#include "safe_macros.h"
subrata_modak705ba462009-05-29 12:21:34 +000053#include "linux_syscall_numbers.h"
54
Xing Gu6b22d9d2014-06-13 10:53:19 +080055#define MNTPOINT "mntpoint"
Xing Guc2870842014-06-13 10:53:18 +080056#define TESTFILE1 "testfile1"
57#define TESTFILE2 "testfile2"
Xing Gu6b22d9d2014-06-13 10:53:19 +080058#define TESTFILE3 "mntpoint/testfile"
Xing Guc2870842014-06-13 10:53:18 +080059#define FILE_MODE (S_IRWXU | S_IRGRP | S_IXGRP | \
60 S_IROTH | S_IXOTH)
Xing Gu6b22d9d2014-06-13 10:53:19 +080061#define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
Xing Guc2870842014-06-13 10:53:18 +080062
63#define LTPUSER1 "nobody"
64#define LTPUSER2 "bin"
65
Cyril Hrubis1e8843d2014-06-30 12:17:27 +020066static const char *device;
Xing Gu6b22d9d2014-06-13 10:53:19 +080067static int mount_flag;
68
Xing Guc2870842014-06-13 10:53:18 +080069static struct timeval a_tv[2] = { {0, 0}, {1000, 0} };
70static struct timeval m_tv[2] = { {1000, 0}, {0, 0} };
71static struct timeval tv[2] = { {1000, 0}, {2000, 0} };
72
73static struct test_case_t {
74 char *pathname;
75 struct timeval *times;
76 int exp_errno;
77} test_cases[] = {
78 { TESTFILE1, a_tv, 0 },
79 { TESTFILE1, m_tv, 0 },
80 { TESTFILE2, NULL, EACCES },
81 { "notexistfile", tv, ENOENT },
82 { NULL, tv, EFAULT },
Xing Gu6b22d9d2014-06-13 10:53:19 +080083 { TESTFILE2, tv, EPERM },
84 { TESTFILE3, tv, EROFS },
Xing Guc2870842014-06-13 10:53:18 +080085};
86
87static void setup(void);
88static void cleanup(void);
89static void utimes_verify(const struct test_case_t *);
90
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020091char *TCID = "utimes01";
Xing Guc2870842014-06-13 10:53:18 +080092int TST_TOTAL = ARRAY_SIZE(test_cases);
subrata_modak705ba462009-05-29 12:21:34 +000093
Wanlong Gao354ebb42012-12-07 10:10:04 +080094int main(int ac, char **av)
95{
Xing Guc2870842014-06-13 10:53:18 +080096 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020097 const char *msg;
subrata_modak705ba462009-05-29 12:21:34 +000098
Cyril Hrubis1e8843d2014-06-30 12:17:27 +020099 msg = parse_opts(ac, av, NULL, NULL);
Xing Guc2870842014-06-13 10:53:18 +0800100 if (msg != NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak705ba462009-05-29 12:21:34 +0000102
Garrett Cooper045b2222010-11-22 15:34:12 -0800103 setup();
subrata_modak705ba462009-05-29 12:21:34 +0000104
Garrett Cooper045b2222010-11-22 15:34:12 -0800105 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 tst_count = 0;
subrata_modak705ba462009-05-29 12:21:34 +0000107
Xing Guc2870842014-06-13 10:53:18 +0800108 for (i = 0; i < TST_TOTAL; i++)
109 utimes_verify(&test_cases[i]);
Garrett Cooper2c282152010-12-16 00:55:50 -0800110 }
Xing Guc2870842014-06-13 10:53:18 +0800111
Garrett Cooper045b2222010-11-22 15:34:12 -0800112 cleanup();
subrata_modak705ba462009-05-29 12:21:34 +0000113 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700114}
Xing Guc2870842014-06-13 10:53:18 +0800115
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200116static void setup(void)
Xing Guc2870842014-06-13 10:53:18 +0800117{
118 struct passwd *ltpuser;
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200119 const char *fs_type;
Xing Guc2870842014-06-13 10:53:18 +0800120
121 tst_require_root(NULL);
122
123 tst_sig(NOFORK, DEF_HANDLER, cleanup);
124
125 TEST_PAUSE;
126
127 tst_tmpdir();
128
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200129 fs_type = tst_dev_fs_type();
130 device = tst_acquire_device(cleanup);
131
132 if (!device)
133 tst_brkm(TCONF, cleanup, "Failed to obtain block device");
134
Xing Guc2870842014-06-13 10:53:18 +0800135 SAFE_TOUCH(cleanup, TESTFILE1, FILE_MODE, NULL);
136 ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1);
137 SAFE_CHOWN(cleanup, TESTFILE1, ltpuser->pw_uid,
138 ltpuser->pw_gid);
139
140 SAFE_TOUCH(cleanup, TESTFILE2, FILE_MODE, NULL);
141 ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER2);
142 SAFE_CHOWN(cleanup, TESTFILE2, ltpuser->pw_uid,
143 ltpuser->pw_gid);
144
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200145 tst_mkfs(cleanup, device, fs_type, NULL);
Xing Gu6b22d9d2014-06-13 10:53:19 +0800146 SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200147 if (mount(device, MNTPOINT, fs_type, 0, NULL) == -1) {
Xing Gu6b22d9d2014-06-13 10:53:19 +0800148 tst_brkm(TBROK | TERRNO, cleanup,
149 "mount device:%s failed", device);
150 }
151 mount_flag = 1;
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200152 SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL);
153 ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1);
154 SAFE_CHOWN(cleanup, TESTFILE3, ltpuser->pw_uid,
155 ltpuser->pw_gid);
156 if (mount(device, MNTPOINT, fs_type,
157 MS_REMOUNT | MS_RDONLY, NULL) == -1) {
158 tst_brkm(TBROK | TERRNO, cleanup,
159 "mount device:%s failed", device);
160 }
Xing Gu6b22d9d2014-06-13 10:53:19 +0800161
Xing Guc2870842014-06-13 10:53:18 +0800162 ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1);
163 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
164}
165
166static void utimes_verify(const struct test_case_t *tc)
167{
168 struct stat st;
169 struct timeval tmp_tv[2];
170
171 if (tc->exp_errno == 0) {
172 SAFE_STAT(cleanup, tc->pathname, &st);
173
174 tmp_tv[0].tv_sec = st.st_atime;
175 tmp_tv[0].tv_usec = 0;
176 tmp_tv[1].tv_sec = st.st_mtime;
177 tmp_tv[1].tv_usec = 0;
178 }
179
180 TEST(utimes(tc->pathname, tc->times));
181
Xing Guc2870842014-06-13 10:53:18 +0800182 if (TEST_ERRNO == tc->exp_errno) {
183 tst_resm(TPASS | TTERRNO, "utimes() worked as expected");
184 } else {
185 tst_resm(TFAIL | TTERRNO,
186 "utimes() failed unexpectedly; expected: %d - %s",
187 tc->exp_errno, strerror(tc->exp_errno));
188 }
189
190 if (TEST_ERRNO == 0 && utimes(tc->pathname, tmp_tv) == -1)
191 tst_brkm(TBROK | TERRNO, cleanup, "utimes() failed.");
192}
193
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200194static void cleanup(void)
Xing Guc2870842014-06-13 10:53:18 +0800195{
196 if (seteuid(0) == -1)
197 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
198
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100199 if (mount_flag && tst_umount(MNTPOINT) == -1)
Xing Gu6b22d9d2014-06-13 10:53:19 +0800200 tst_resm(TWARN | TERRNO, "umount %s failed", MNTPOINT);
201
Cyril Hrubis1e8843d2014-06-30 12:17:27 +0200202
203 if (device)
204 tst_release_device(NULL, device);
205
Xing Guc2870842014-06-13 10:53:18 +0800206 tst_rmdir();
207}