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