99#include <sys/time.h>
1010#include <time.h>
1111#include <unistd.h>
12+ #include <signal.h>
1213
1314#include "getopt_long.h"
1415#include "access/xlogdefs.h"
2930static const char * progname ;
3031
3132static int ops_per_test = 2000 ;
33+ static int needs_unlink = 0 ;
3234static char full_buf [XLOG_SEG_SIZE ],
3335 * buf ,
3436 * filename = FSYNC_FILENAME ;
@@ -44,6 +46,7 @@ static void test_sync(int writes_per_op);
4446static void test_open_syncs (void );
4547static void test_open_sync (const char * msg , int writes_size );
4648static void test_file_descriptor_sync (void );
49+ static void signal_cleanup (int sig );
4750
4851#ifdef HAVE_FSYNC_WRITETHROUGH
4952static int pg_fsync_writethrough (int fd );
@@ -59,6 +62,14 @@ main(int argc, char *argv[])
5962
6063 handle_args (argc , argv );
6164
65+ /* Prevent leaving behind the test file */
66+ signal (SIGINT , signal_cleanup );
67+ signal (SIGTERM , signal_cleanup );
68+ #ifdef SIGHUP
69+ /* Not defined on win32 */
70+ signal (SIGHUP , signal_cleanup );
71+ #endif
72+
6273 prepare_buf ();
6374
6475 test_open ();
@@ -167,6 +178,7 @@ test_open(void)
167178 */
168179 if ((tmpfile = open (filename , O_RDWR | O_CREAT , S_IRUSR | S_IWUSR )) == -1 )
169180 die ("could not open output file" );
181+ needs_unlink = 1 ;
170182 if (write (tmpfile , full_buf , XLOG_SEG_SIZE ) != XLOG_SEG_SIZE )
171183 die ("write failed" );
172184
@@ -490,6 +502,17 @@ test_non_sync(void)
490502 print_elapse (start_t , stop_t );
491503}
492504
505+ static void
506+ signal_cleanup (int signum )
507+ {
508+ /* Delete the file if it exists. Ignore errors */
509+ if (needs_unlink )
510+ unlink (filename );
511+ /* Finish incomplete line on stdout */
512+ puts ("" );
513+ exit (signum );
514+ }
515+
493516#ifdef HAVE_FSYNC_WRITETHROUGH
494517
495518static int
0 commit comments