blob: 2a97cce3b13f8d5d5139d9ef815be2db4382e7ca [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis3ae6e392014-06-24 14:57:07 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 John George
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis3ae6e392014-06-24 14:57:07 +02005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * Test Description:
22 * 1. Verify that the system call utime() fails to set the modification
23 * and access times of a file to the current time, under the following
24 * constraints,
25 * - The times argument is null.
26 * - The user ID of the process is not "root".
plars865695b2001-08-27 22:15:12 +000027 * 2. Verify that the system call utime() fails to set the modification
28 * and access times of a file if the specified file doesn't exist.
Zeng Linggang42e86002014-06-19 10:21:13 +080029 * 3. Verify that the system call utime() fails to set the modification
30 * and access times of a file to the current time, under the following
31 * constraints,
32 * - The times argument is not null.
33 * - The user ID of the process is not "root".
34 * 4. Verify that the system call utime() fails to set the modification
35 * and access times of a file that resides on a read-only file system.
plars865695b2001-08-27 22:15:12 +000036 */
37
38#include <errno.h>
39#include <fcntl.h>
40#include <pwd.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <signal.h>
44#include <string.h>
45#include <unistd.h>
46#include <utime.h>
Steven Jackson249f4052016-12-13 16:16:00 +000047#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000048#include <sys/stat.h>
49#include <sys/types.h>
Zeng Linggang42e86002014-06-19 10:21:13 +080050#include <sys/mount.h>
plars865695b2001-08-27 22:15:12 +000051
52#include "test.h"
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020053#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000054
plars865695b2001-08-27 22:15:12 +000055#define TEMP_FILE "tmp_file"
Zeng Linggang42e86002014-06-19 10:21:13 +080056#define MNT_POINT "mntpoint"
plars865695b2001-08-27 22:15:12 +000057
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020058char *TCID = "utime06";
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020059static struct passwd *ltpuser;
Zeng Linggang42e86002014-06-19 10:21:13 +080060static const struct utimbuf times;
61static const char *dev;
62static int mount_flag;
63static void setup_nobody(void);
64static void cleanup_nobody(void);
plars865695b2001-08-27 22:15:12 +000065
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020066struct test_case_t {
plars865695b2001-08-27 22:15:12 +000067 char *pathname;
plars865695b2001-08-27 22:15:12 +000068 int exp_errno;
Zeng Linggang42e86002014-06-19 10:21:13 +080069 const struct utimbuf *times;
70 void (*setup_func)(void);
71 void (*cleanup_func)(void);
plars865695b2001-08-27 22:15:12 +000072} Test_cases[] = {
Zeng Linggang42e86002014-06-19 10:21:13 +080073 {TEMP_FILE, EACCES, NULL, setup_nobody, cleanup_nobody},
74 {"", ENOENT, NULL, NULL, NULL},
75 {TEMP_FILE, EPERM, &times, setup_nobody, cleanup_nobody},
76 {MNT_POINT, EROFS, NULL, NULL, NULL},
plars865695b2001-08-27 22:15:12 +000077};
78
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020079int TST_TOTAL = ARRAY_SIZE(Test_cases);
80static void setup(void);
81static void utime_verify(const struct test_case_t *);
82static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000083
subrata_modak56207ce2009-03-23 13:35:39 +000084int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000085{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020086 int lc;
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020087 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000088
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010089 tst_parse_opts(ac, av, NULL, NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -080090
plars865695b2001-08-27 22:15:12 +000091 setup();
92
Cyril Hrubis3ae6e392014-06-24 14:57:07 +020093 for (lc = 0; TEST_LOOPING(lc); lc++) {
94 tst_count = 0;
95 for (i = 0; i < TST_TOTAL; i++)
96 utime_verify(&Test_cases[i]);
plars865695b2001-08-27 22:15:12 +000097 }
Garrett Cooper2c282152010-12-16 00:55:50 -080098
plars865695b2001-08-27 22:15:12 +000099 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800100 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800101}
plars865695b2001-08-27 22:15:12 +0000102
Cyril Hrubis3ae6e392014-06-24 14:57:07 +0200103static void setup(void)
plars865695b2001-08-27 22:15:12 +0000104{
Zeng Linggang42e86002014-06-19 10:21:13 +0800105 const char *fs_type;
106
Cyril Hrubis3ae6e392014-06-24 14:57:07 +0200107 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000108
Cyril Hrubisd1e794d2015-07-30 23:52:51 +0200109 tst_require_root();
plars865695b2001-08-27 22:15:12 +0000110
plars865695b2001-08-27 22:15:12 +0000111 TEST_PAUSE;
112
plars865695b2001-08-27 22:15:12 +0000113 tst_tmpdir();
114
Cyril Hrubis3ae6e392014-06-24 14:57:07 +0200115 SAFE_TOUCH(cleanup, TEMP_FILE, 0644, NULL);
116
Zeng Linggang42e86002014-06-19 10:21:13 +0800117 fs_type = tst_dev_fs_type();
118 dev = tst_acquire_device(cleanup);
119 if (!dev)
120 tst_brkm(TCONF, cleanup, "Failed to acquire test device");
Cyril Hrubis3ae6e392014-06-24 14:57:07 +0200121
Zorro Langd5e17882016-03-15 11:37:48 +0800122 tst_mkfs(cleanup, dev, fs_type, NULL, NULL);
Zeng Linggang42e86002014-06-19 10:21:13 +0800123
124 SAFE_MKDIR(cleanup, MNT_POINT, 0644);
125 if (mount(dev, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
126 tst_brkm(TBROK | TERRNO, cleanup,
127 "mount device:%s failed", dev);
128 }
129 mount_flag = 1;
130
131 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
Cyril Hrubis3ae6e392014-06-24 14:57:07 +0200132}
133
134static void utime_verify(const struct test_case_t *test)
135{
Zeng Linggang42e86002014-06-19 10:21:13 +0800136 if (test->setup_func != NULL)
137 test->setup_func();
138
139 TEST(utime(test->pathname, test->times));
140
141 if (test->cleanup_func != NULL)
142 test->cleanup_func();
Cyril Hrubis3ae6e392014-06-24 14:57:07 +0200143
144 if (TEST_RETURN != -1) {
145 tst_resm(TFAIL, "utime succeeded unexpectedly");
146 return;
147 }
148
149 if (TEST_ERRNO == test->exp_errno) {
150 tst_resm(TPASS | TTERRNO, "utime failed as expected");
151 } else {
152 tst_resm(TFAIL | TTERRNO,
153 "utime failed unexpectedly; expected: %d - %s",
154 test->exp_errno, strerror(test->exp_errno));
plars865695b2001-08-27 22:15:12 +0000155 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800156}
plars865695b2001-08-27 22:15:12 +0000157
Zeng Linggang42e86002014-06-19 10:21:13 +0800158static void setup_nobody(void)
159{
160 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
161}
162
163static void cleanup_nobody(void)
164{
165 SAFE_SETEUID(cleanup, 0);
166}
167
Cyril Hrubis3ae6e392014-06-24 14:57:07 +0200168static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000169{
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100170 if (mount_flag && tst_umount(MNT_POINT) < 0)
Zeng Linggang42e86002014-06-19 10:21:13 +0800171 tst_resm(TWARN | TERRNO, "umount device:%s failed", dev);
172
173 if (dev)
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100174 tst_release_device(dev);
Zeng Linggang42e86002014-06-19 10:21:13 +0800175
plars865695b2001-08-27 22:15:12 +0000176 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700177}