@@ -298,12 +298,17 @@ int
298
298
grab_excl_lock_file (const char * root_dir , const char * backup_id , bool strict )
299
299
{
300
300
char lock_file [MAXPGPATH ];
301
- int fd = 0 ;
301
+ FILE * fp = NULL ;
302
302
char buffer [256 ];
303
303
int ntries = LOCK_TIMEOUT ;
304
304
int empty_tries = LOCK_STALE_TIMEOUT ;
305
- int len ;
306
- int encoded_pid ;
305
+ size_t len ;
306
+ pid_t encoded_pid ;
307
+ int save_errno = 0 ;
308
+ enum {
309
+ GELF_FAILED_WRITE = 1 ,
310
+ GELF_FAILED_CLOSE = 2 ,
311
+ } failed_action = 0 ;
307
312
308
313
join_path_components (lock_file , root_dir , BACKUP_LOCK_FILE );
309
314
@@ -314,19 +319,17 @@ grab_excl_lock_file(const char *root_dir, const char *backup_id, bool strict)
314
319
*/
315
320
do
316
321
{
317
- FILE * fp_out = NULL ;
318
-
319
322
if (interrupted )
320
323
elog (ERROR , "Interrupted while locking backup %s" , backup_id );
321
324
322
325
/*
323
- * Try to create the lock file --- O_EXCL makes this atomic.
326
+ * Try to create the lock file --- "wx" makes this atomic.
324
327
*
325
328
* Think not to make the file protection weaker than 0600. See
326
329
* comments below.
327
330
*/
328
- fd = fio_open ( FIO_BACKUP_HOST , lock_file , O_RDWR | O_CREAT | O_EXCL );
329
- if (fd >= 0 )
331
+ fp = fopen ( lock_file , "wx" );
332
+ if (fp != NULL )
330
333
break ; /* Success; exit the retry loop */
331
334
332
335
/* read-only fs is a special case */
@@ -342,7 +345,6 @@ grab_excl_lock_file(const char *root_dir, const char *backup_id, bool strict)
342
345
* If file already exists or we have some permission problem (???),
343
346
* then retry;
344
347
*/
345
- // if ((errno != EEXIST && errno != EACCES))
346
348
if (errno != EEXIST )
347
349
elog (ERROR , "Could not create lock file \"%s\": %s" ,
348
350
lock_file , strerror (errno ));
@@ -352,18 +354,19 @@ grab_excl_lock_file(const char *root_dir, const char *backup_id, bool strict)
352
354
* here: file might have been deleted since we tried to create it.
353
355
*/
354
356
355
- fp_out = fopen (lock_file , "r" );
356
- if (fp_out == NULL )
357
+ fp = fopen (lock_file , "r" );
358
+ if (fp == NULL )
357
359
{
358
360
if (errno == ENOENT )
359
361
continue ; /* race condition; try again */
360
362
elog (ERROR , "Cannot open lock file \"%s\": %s" , lock_file , strerror (errno ));
361
363
}
362
364
363
- len = fread (buffer , 1 , sizeof (buffer ) - 1 , fp_out );
364
- if (ferror (fp_out ))
365
+ len = fread (buffer , 1 , sizeof (buffer ) - 1 , fp );
366
+ if (ferror (fp ))
365
367
elog (ERROR , "Cannot read from lock file: \"%s\"" , lock_file );
366
- fclose (fp_out );
368
+ fclose (fp );
369
+ fp = NULL ;
367
370
368
371
/*
369
372
* There are several possible reasons for lock file
@@ -400,7 +403,7 @@ grab_excl_lock_file(const char *root_dir, const char *backup_id, bool strict)
400
403
continue ;
401
404
}
402
405
403
- encoded_pid = atoi (buffer );
406
+ encoded_pid = ( pid_t ) atoll (buffer );
404
407
405
408
if (encoded_pid <= 0 )
406
409
{
@@ -450,7 +453,7 @@ grab_excl_lock_file(const char *root_dir, const char *backup_id, bool strict)
450
453
* it. Need a loop because of possible race condition against other
451
454
* would-be creators.
452
455
*/
453
- if (fio_remove ( FIO_BACKUP_HOST , lock_file , false ) < 0 )
456
+ if (remove ( lock_file ) < 0 )
454
457
{
455
458
if (errno == ENOENT )
456
459
continue ; /* race condition, again */
@@ -461,40 +464,32 @@ grab_excl_lock_file(const char *root_dir, const char *backup_id, bool strict)
461
464
} while (ntries -- );
462
465
463
466
/* Failed to acquire exclusive lock in time */
464
- if (fd <= 0 )
467
+ if (fp == NULL )
465
468
return LOCK_FAIL_TIMEOUT ;
466
469
467
470
/*
468
471
* Successfully created the file, now fill it.
469
472
*/
470
- snprintf (buffer , sizeof (buffer ), "%lld\n" , (long long )my_pid );
471
-
472
473
errno = 0 ;
473
- if (fio_write (fd , buffer , strlen (buffer )) != strlen (buffer ))
474
- {
475
- int save_errno = errno ;
474
+ fprintf (fp , "%lld\n" , (long long )my_pid );
475
+ fflush (fp );
476
476
477
- fio_close (fd );
478
- if (fio_remove (FIO_BACKUP_HOST , lock_file , false) != 0 )
479
- elog (WARNING , "Cannot remove lock file \"%s\": %s" , lock_file , strerror (errno ));
480
-
481
- /* In lax mode if we failed to grab lock because of 'out of space error',
482
- * then treat backup as locked.
483
- * Only delete command should be run in lax mode.
484
- */
485
- if (!strict && save_errno == ENOSPC )
486
- return LOCK_FAIL_ENOSPC ;
487
- else
488
- elog (ERROR , "Could not write lock file \"%s\": %s" ,
489
- lock_file , strerror (save_errno ));
477
+ if (ferror (fp ))
478
+ {
479
+ failed_action = GELF_FAILED_WRITE ;
480
+ save_errno = errno ;
481
+ clearerr (fp );
490
482
}
491
483
492
- if (fio_flush ( fd ) ! = 0 )
484
+ if (fclose ( fp ) && save_errno = = 0 )
493
485
{
494
- int save_errno = errno ;
486
+ failed_action = GELF_FAILED_CLOSE ;
487
+ save_errno = errno ;
488
+ }
495
489
496
- fio_close (fd );
497
- if (fio_remove (FIO_BACKUP_HOST , lock_file , false) != 0 )
490
+ if (save_errno )
491
+ {
492
+ if (remove (lock_file ) != 0 )
498
493
elog (WARNING , "Cannot remove lock file \"%s\": %s" , lock_file , strerror (errno ));
499
494
500
495
/* In lax mode if we failed to grab lock because of 'out of space error',
@@ -503,21 +498,10 @@ grab_excl_lock_file(const char *root_dir, const char *backup_id, bool strict)
503
498
*/
504
499
if (!strict && save_errno == ENOSPC )
505
500
return LOCK_FAIL_ENOSPC ;
506
- else
507
- elog (ERROR , "Could not flush lock file \"%s\": %s" ,
508
- lock_file , strerror (save_errno ));
509
- }
510
-
511
- if (fio_close (fd ) != 0 )
512
- {
513
- int save_errno = errno ;
514
-
515
- if (fio_remove (FIO_BACKUP_HOST , lock_file , false) != 0 )
516
- elog (WARNING , "Cannot remove lock file \"%s\": %s" , lock_file , strerror (errno ));
517
-
518
- if (!strict && save_errno == ENOSPC )
519
- return LOCK_FAIL_ENOSPC ;
520
- else
501
+ else if (failed_action == GELF_FAILED_WRITE )
502
+ elog (ERROR , "Could not write lock file \"%s\": %s" ,
503
+ lock_file , strerror (save_errno ));
504
+ else if (failed_action == GELF_FAILED_CLOSE )
521
505
elog (ERROR , "Could not close lock file \"%s\": %s" ,
522
506
lock_file , strerror (save_errno ));
523
507
}
0 commit comments