Imported Upstream version 2.8.4
[platform/upstream/man-db.git] / src / tests / fspause.c
1 /*
2  * fspause.c: pause until a file timestamp updates
3  *
4  * Copyright (C) 2014 Colin Watson.
5  *
6  * This file is part of man-db.
7  *
8  * man-db is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * man-db is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with man-db; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <time.h>
33 #include <unistd.h>
34
35 #include "progname.h"
36 #include "stat-time.h"
37 #include "timespec.h"
38 #include "xalloc.h"
39
40 #include "manconfig.h"
41
42 static char *filename;
43 static int fd = -1;
44
45 #define MUST(name, cond) \
46         do { \
47                 if (!(cond)) { \
48                         fprintf (stderr, "fspause: " name " failed\n"); \
49                         abort (); \
50                 } \
51         } while (0)
52
53 static void unlink_tempfile (void)
54 {
55         if (fd >= 0) {
56                 MUST ("close", close (fd) >= 0);
57                 MUST ("unlink", unlink (filename) >= 0);
58         }
59 }
60
61 static void delay (int delay_ns)
62 {
63         struct timespec delay_ts;
64
65         delay_ts.tv_sec = delay_ns / 1000000000;
66         delay_ts.tv_nsec = delay_ns % 1000000000;
67         for (;;) {
68                 errno = 0;
69                 if (nanosleep (&delay_ts, NULL) == 0)
70                         break;
71                 MUST ("nanosleep", errno == 0 || errno == EINTR);
72         }
73 }
74
75 static int try_delay (struct stat *st, int delay_ns)
76 {
77         struct timespec start_ts, end_ts;
78
79         start_ts = get_stat_mtime (st);
80         delay (delay_ns);
81         MUST ("write", write (fd, "\n", 1) == 1);
82         MUST ("fstat", fstat (fd, st) >= 0);
83         end_ts = get_stat_mtime (st);
84         return timespec_cmp (start_ts, end_ts) != 0;
85 }
86
87 int main (int argc ATTRIBUTE_UNUSED, char **argv)
88 {
89         struct stat st;
90         int delay_ns;
91
92         set_program_name (argv[0]);
93
94         filename = xstrdup ("fspause.tmp.XXXXXX");
95         MUST ("mkstemp", (fd = mkstemp (filename)) >= 0);
96         atexit (unlink_tempfile);
97         MUST ("fstat", fstat (fd, &st) >= 0);
98
99         /* 0x40000000 nanoseconds is just over a second.  The effective
100          * maximum delay we will allow is thus about two seconds.  This
101          * saves us having to keep track of anything more complicated than a
102          * single signed 32-bit int.
103          */
104         for (delay_ns = 1; delay_ns < 0x40000000; delay_ns *= 2) {
105                 if (try_delay (&st, delay_ns))
106                         return 0;
107         }
108
109         fprintf (stderr,
110                  "fspause: temporary file timestamp refuses to change!\n");
111         return 1;
112 }