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