xref: /netbsd-src/tests/fs/vfs/t_mtime_otrunc.c (revision 06d5dce16174034ea212cecec2ca221e6edaffd4)
1*06d5dce1Smartin /*-
2*06d5dce1Smartin  * Copyright (c) 2017 The NetBSD Foundation, Inc.
3*06d5dce1Smartin  * All rights reserved.
4*06d5dce1Smartin  *
5*06d5dce1Smartin  * Redistribution and use in source and binary forms, with or without
6*06d5dce1Smartin  * modification, are permitted provided that the following conditions
7*06d5dce1Smartin  * are met:
8*06d5dce1Smartin  * 1. Redistributions of source code must retain the above copyright
9*06d5dce1Smartin  *    notice, this list of conditions and the following disclaimer.
10*06d5dce1Smartin  * 2. Redistributions in binary form must reproduce the above copyright
11*06d5dce1Smartin  *    notice, this list of conditions and the following disclaimer in the
12*06d5dce1Smartin  *    documentation and/or other materials provided with the distribution.
13*06d5dce1Smartin  *
14*06d5dce1Smartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15*06d5dce1Smartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16*06d5dce1Smartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17*06d5dce1Smartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18*06d5dce1Smartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19*06d5dce1Smartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20*06d5dce1Smartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21*06d5dce1Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22*06d5dce1Smartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23*06d5dce1Smartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24*06d5dce1Smartin  * POSSIBILITY OF SUCH DAMAGE.
25*06d5dce1Smartin  */
26*06d5dce1Smartin 
27*06d5dce1Smartin #include <sys/cdefs.h>
28*06d5dce1Smartin __COPYRIGHT("@(#) Copyright (c) 2013\
29*06d5dce1Smartin  The NetBSD Foundation, inc. All rights reserved.");
30*06d5dce1Smartin __RCSID("$NetBSD: t_mtime_otrunc.c,v 1.1 2017/02/02 22:07:05 martin Exp $");
31*06d5dce1Smartin 
32*06d5dce1Smartin #include <errno.h>
33*06d5dce1Smartin #include <unistd.h>
34*06d5dce1Smartin #include <stdio.h>
35*06d5dce1Smartin #include <stdlib.h>
36*06d5dce1Smartin #include <sys/stat.h>
37*06d5dce1Smartin #include <atf-c.h>
38*06d5dce1Smartin 
39*06d5dce1Smartin #include <rump/rump_syscalls.h>
40*06d5dce1Smartin #include <rump/rump.h>
41*06d5dce1Smartin 
42*06d5dce1Smartin #include "../common/h_fsmacros.h"
43*06d5dce1Smartin 
44*06d5dce1Smartin #define LOCKFILE	"lock"
45*06d5dce1Smartin 
46*06d5dce1Smartin static time_t
lock_it(void)47*06d5dce1Smartin lock_it(void)
48*06d5dce1Smartin {
49*06d5dce1Smartin 	struct stat st;
50*06d5dce1Smartin 
51*06d5dce1Smartin 	if (rump_sys_stat(LOCKFILE, &st) != 0)
52*06d5dce1Smartin 		st.st_mtime = 0;
53*06d5dce1Smartin 
54*06d5dce1Smartin 	int f = rump_sys_open(LOCKFILE, O_WRONLY|O_CREAT|O_TRUNC, 0666);
55*06d5dce1Smartin 	if (f == -1) return 0;
56*06d5dce1Smartin 	rump_sys_close(f);
57*06d5dce1Smartin 
58*06d5dce1Smartin 	return st.st_mtime;
59*06d5dce1Smartin }
60*06d5dce1Smartin 
61*06d5dce1Smartin static void
otrunc_mtime_update(const atf_tc_t * tc,const char * path)62*06d5dce1Smartin otrunc_mtime_update(const atf_tc_t *tc, const char *path)
63*06d5dce1Smartin {
64*06d5dce1Smartin 	time_t last_ts = 0;
65*06d5dce1Smartin 	int res;
66*06d5dce1Smartin 
67*06d5dce1Smartin 	/* atf_tc_expect_fail("PR kern/51762"); */
68*06d5dce1Smartin 
69*06d5dce1Smartin 	res = rump_sys_chdir(path);
70*06d5dce1Smartin 	if (res == -1)
71*06d5dce1Smartin 		atf_tc_fail("chdir failed");
72*06d5dce1Smartin 
73*06d5dce1Smartin 	for (int i = 0; i < 5; i++) {
74*06d5dce1Smartin 		time_t l = lock_it();
75*06d5dce1Smartin 		printf("last lock: %ld\n", (long)l);
76*06d5dce1Smartin 		ATF_REQUIRE_MSG(i == 0 || l > last_ts,
77*06d5dce1Smartin 		    "iteration %d: lock time did not increase, old time %lu, "
78*06d5dce1Smartin 		    "new time %lu", i,
79*06d5dce1Smartin 		    (unsigned long)last_ts, (unsigned long)l);
80*06d5dce1Smartin 		last_ts = l;
81*06d5dce1Smartin 		sleep(2);
82*06d5dce1Smartin 	}
83*06d5dce1Smartin 	rump_sys_chdir("/");
84*06d5dce1Smartin }
85*06d5dce1Smartin 
86*06d5dce1Smartin ATF_FSAPPLY(otrunc_mtime_update, "Checks for mtime updates by open(O_TRUNC) (PR kern/51762)");
87