@@ -69,12 +69,13 @@ read_backup(time_t timestamp)
6969 * status.
7070 */
7171void
72- write_backup_status (pgBackup * backup )
72+ write_backup_status (pgBackup * backup , BackupStatus status )
7373{
7474 pgBackup * tmp ;
7575
7676 tmp = read_backup (backup -> start_time );
7777
78+ backup -> status = status ;
7879 tmp -> status = backup -> status ;
7980 write_backup (tmp );
8081
@@ -84,7 +85,7 @@ write_backup_status(pgBackup *backup)
8485/*
8586 * Create exclusive lockfile in the backup's directory.
8687 */
87- void
88+ bool
8889lock_backup (pgBackup * backup )
8990{
9091 char lock_file [MAXPGPATH ];
@@ -149,7 +150,7 @@ lock_backup(pgBackup *backup)
149150 * Couldn't create the pid file. Probably it already exists.
150151 */
151152 if ((errno != EEXIST && errno != EACCES ) || ntries > 100 )
152- elog (ERROR , "could not create lock file \"%s\": %s" ,
153+ elog (ERROR , "Could not create lock file \"%s\": %s" ,
153154 lock_file , strerror (errno ));
154155
155156 /*
@@ -161,22 +162,22 @@ lock_backup(pgBackup *backup)
161162 {
162163 if (errno == ENOENT )
163164 continue ; /* race condition; try again */
164- elog (ERROR , "could not open lock file \"%s\": %s" ,
165+ elog (ERROR , "Could not open lock file \"%s\": %s" ,
165166 lock_file , strerror (errno ));
166167 }
167168 if ((len = read (fd , buffer , sizeof (buffer ) - 1 )) < 0 )
168- elog (ERROR , "could not read lock file \"%s\": %s" ,
169+ elog (ERROR , "Could not read lock file \"%s\": %s" ,
169170 lock_file , strerror (errno ));
170171 close (fd );
171172
172173 if (len == 0 )
173- elog (ERROR , "lock file \"%s\" is empty" , lock_file );
174+ elog (ERROR , "Lock file \"%s\" is empty" , lock_file );
174175
175176 buffer [len ] = '\0' ;
176177 encoded_pid = atoi (buffer );
177178
178179 if (encoded_pid <= 0 )
179- elog (ERROR , "bogus data in lock file \"%s\": \"%s\"" ,
180+ elog (ERROR , "Bogus data in lock file \"%s\": \"%s\"" ,
180181 lock_file , buffer );
181182
182183 /*
@@ -190,9 +191,21 @@ lock_backup(pgBackup *backup)
190191 */
191192 if (encoded_pid != my_pid && encoded_pid != my_p_pid )
192193 {
193- if (kill (encoded_pid , 0 ) == 0 ||
194- (errno != ESRCH && errno != EPERM ))
195- elog (ERROR , "lock file \"%s\" already exists" , lock_file );
194+ if (kill (encoded_pid , 0 ) == 0 )
195+ {
196+ elog (WARNING , "Process %d is using backup %s and still is running" ,
197+ encoded_pid , base36enc (backup -> start_time ));
198+ return false;
199+ }
200+ else
201+ {
202+ if (errno == ESRCH )
203+ elog (WARNING , "Process %d which used backup %s no longer exists" ,
204+ encoded_pid , base36enc (backup -> start_time ));
205+ else
206+ elog (ERROR , "Failed to send signal 0 to a process %d: %s" ,
207+ encoded_pid , strerror (errno ));
208+ }
196209 }
197210
198211 /*
@@ -201,7 +214,7 @@ lock_backup(pgBackup *backup)
201214 * would-be creators.
202215 */
203216 if (unlink (lock_file ) < 0 )
204- elog (ERROR , "could not remove old lock file \"%s\": %s" ,
217+ elog (ERROR , "Could not remove old lock file \"%s\": %s" ,
205218 lock_file , strerror (errno ));
206219 }
207220
@@ -219,7 +232,7 @@ lock_backup(pgBackup *backup)
219232 unlink (lock_file );
220233 /* if write didn't set errno, assume problem is no disk space */
221234 errno = save_errno ? save_errno : ENOSPC ;
222- elog (ERROR , "could not write lock file \"%s\": %s" ,
235+ elog (ERROR , "Could not write lock file \"%s\": %s" ,
223236 lock_file , strerror (errno ));
224237 }
225238 if (fsync (fd ) != 0 )
@@ -229,7 +242,7 @@ lock_backup(pgBackup *backup)
229242 close (fd );
230243 unlink (lock_file );
231244 errno = save_errno ;
232- elog (ERROR , "could not write lock file \"%s\": %s" ,
245+ elog (ERROR , "Could not write lock file \"%s\": %s" ,
233246 lock_file , strerror (errno ));
234247 }
235248 if (close (fd ) != 0 )
@@ -238,7 +251,7 @@ lock_backup(pgBackup *backup)
238251
239252 unlink (lock_file );
240253 errno = save_errno ;
241- elog (ERROR , "could not write lock file \"%s\": %s" ,
254+ elog (ERROR , "Culd not write lock file \"%s\": %s" ,
242255 lock_file , strerror (errno ));
243256 }
244257
@@ -255,6 +268,8 @@ lock_backup(pgBackup *backup)
255268 if (lock_files == NULL )
256269 lock_files = parray_new ();
257270 parray_append (lock_files , pgut_strdup (lock_file ));
271+
272+ return true;
258273}
259274
260275/*
@@ -418,7 +433,8 @@ catalog_lock_backup_list(parray *backup_list, int from_idx, int to_idx)
418433 end_idx = Min (from_idx , to_idx );
419434
420435 for (i = start_idx ; i >= end_idx ; i -- )
421- lock_backup ((pgBackup * ) parray_get (backup_list , i ));
436+ if (!lock_backup ((pgBackup * ) parray_get (backup_list , i )))
437+ elog (ERROR , "Cannot lock backup directory" );
422438}
423439
424440/*
0 commit comments