Skip to content

Commit ab7d421

Browse files
committed
If pg_test_fsync is interrupted, clean up the temp file.
Marti Raudsepp, with additional paranoia by me.
1 parent 5bcf8ed commit ab7d421

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

contrib/pg_test_fsync/pg_test_fsync.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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"
@@ -29,6 +30,7 @@
2930
static const char *progname;
3031

3132
static int ops_per_test = 2000;
33+
static int needs_unlink = 0;
3234
static char full_buf[XLOG_SEG_SIZE],
3335
*buf,
3436
*filename = FSYNC_FILENAME;
@@ -44,6 +46,7 @@ static void test_sync(int writes_per_op);
4446
static void test_open_syncs(void);
4547
static void test_open_sync(const char *msg, int writes_size);
4648
static void test_file_descriptor_sync(void);
49+
static void signal_cleanup(int sig);
4750

4851
#ifdef HAVE_FSYNC_WRITETHROUGH
4952
static 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

495518
static int

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy