summaryrefslogtreecommitdiffstats
path: root/src/tests/fspause.c
blob: 4f573c62a85572269a729a8746d3bc0f488c27dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * fspause.c: pause until a file timestamp updates
 *
 * Copyright (C) 2014 Colin Watson.
 *
 * This file is part of man-db.
 *
 * man-db is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * man-db is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with man-db; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

#include "attribute.h"
#include "progname.h"
#include "stat-time.h"
#include "timespec.h"
#include "xalloc.h"

#include "manconfig.h"

static char *filename;
static int fd = -1;

#define MUST(name, cond) \
	do { \
		if (!(cond)) { \
			fprintf (stderr, "fspause: " name " failed\n"); \
			abort (); \
		} \
	} while (0)

static void unlink_tempfile (void)
{
	if (fd >= 0) {
		MUST ("close", close (fd) >= 0);
		MUST ("unlink", unlink (filename) >= 0);
	}
}

static void delay (int delay_ns)
{
	struct timespec delay_ts;

	delay_ts.tv_sec = delay_ns / 1000000000;
	delay_ts.tv_nsec = delay_ns % 1000000000;
	for (;;) {
		errno = 0;
		if (nanosleep (&delay_ts, NULL) == 0)
			break;
		MUST ("nanosleep", errno == 0 || errno == EINTR);
	}
}

static int try_delay (struct stat *st, int delay_ns)
{
	struct timespec start_ts, end_ts;

	start_ts = get_stat_mtime (st);
	delay (delay_ns);
	MUST ("write", write (fd, "\n", 1) == 1);
	MUST ("fstat", fstat (fd, st) >= 0);
	end_ts = get_stat_mtime (st);
	return timespec_cmp (start_ts, end_ts) != 0;
}

int main (int argc MAYBE_UNUSED, char **argv)
{
	struct stat st;
	int delay_ns;

	set_program_name (argv[0]);

	filename = xstrdup ("fspause.tmp.XXXXXX");
	MUST ("mkstemp", (fd = mkstemp (filename)) >= 0);
	atexit (unlink_tempfile);
	MUST ("fstat", fstat (fd, &st) >= 0);

	/* 0x40000000 nanoseconds is just over a second.  The effective
	 * maximum delay we will allow is thus about two seconds.  This
	 * saves us having to keep track of anything more complicated than a
	 * single signed 32-bit int.
	 */
	for (delay_ns = 1; delay_ns < 0x40000000; delay_ns *= 2) {
		if (try_delay (&st, delay_ns))
			return 0;
	}

	fprintf (stderr,
		 "fspause: temporary file timestamp refuses to change!\n");
	return 1;
}