@@ -157,6 +157,12 @@ static int create_ghost(struct ghost_file *gf, GhostFileEntry *gfe, struct cr_im
157
157
return ret ;
158
158
}
159
159
160
+ static inline void ghost_path (char * path , int plen ,
161
+ struct reg_file_info * rfi , RemapFilePathEntry * rfe )
162
+ {
163
+ snprintf (path , plen , "%s.cr.%x.ghost" , rfi -> path , rfe -> remap_id );
164
+ }
165
+
160
166
static int open_remap_ghost (struct reg_file_info * rfi ,
161
167
RemapFilePathEntry * rfe )
162
168
{
@@ -203,7 +209,7 @@ static int open_remap_ghost(struct reg_file_info *rfi,
203
209
if (S_ISDIR (gfe -> mode ))
204
210
strncpy (gf -> remap .rpath , rfi -> path , PATH_MAX );
205
211
else
206
- snprintf (gf -> remap .rpath , PATH_MAX , "%s.cr.%x.ghost" , rfi -> path , rfe -> remap_id );
212
+ ghost_path (gf -> remap .rpath , PATH_MAX , rfi , rfe );
207
213
208
214
if (create_ghost (gf , gfe , img ))
209
215
goto close_ifd ;
@@ -379,6 +385,78 @@ int prepare_remaps(void)
379
385
return ret ;
380
386
}
381
387
388
+ static void try_clean_ghost (struct remap_info * ri )
389
+ {
390
+ char path [PATH_MAX ];
391
+ int mnt_id , ret ;
392
+
393
+ mnt_id = ri -> rfi -> rfe -> mnt_id ; /* rirfirfe %) */
394
+ ret = rst_get_mnt_root (mnt_id , path , sizeof (path ));
395
+ if (ret < 0 )
396
+ return ;
397
+
398
+ ghost_path (path + ret , sizeof (path ) - 1 , ri -> rfi , ri -> rfe );
399
+ if (!unlink (path )) {
400
+ pr_info (" `- X [%s] ghost\n" , path );
401
+ return ;
402
+ }
403
+
404
+ /*
405
+ * We can also find out the ghost type by stat()-ing
406
+ * it or by reading the ghost image, but this way
407
+ * is the fastest one.
408
+ */
409
+
410
+ if ((errno == EISDIR )) {
411
+ strncpy (path + ret , ri -> rfi -> path , sizeof (path ) - 1 );
412
+ if (!rmdir (path )) {
413
+ pr_info (" `- Xd [%s] ghost\n" , path );
414
+ return ;
415
+ }
416
+ }
417
+
418
+ pr_perror (" `- XFail [%s] ghost" , path );
419
+ }
420
+
421
+ void try_clean_remaps (int ns_fd )
422
+ {
423
+ struct remap_info * ri ;
424
+ int old_ns = -1 ;
425
+
426
+ if (list_empty (& remaps ))
427
+ goto out ;
428
+
429
+ if (ns_fd >= 0 ) {
430
+ pr_info ("Switching to new ns to clean ghosts\n" );
431
+
432
+ old_ns = open_proc (PROC_SELF , "ns/mnt" );
433
+ if (old_ns < 0 ) {
434
+ pr_perror ("`- Can't keep old ns" );
435
+ return ;
436
+ }
437
+
438
+ if (setns (ns_fd , CLONE_NEWNS ) < 0 ) {
439
+ close (old_ns );
440
+ pr_perror ("`- Can't switch" );
441
+ return ;
442
+ }
443
+ }
444
+
445
+ list_for_each_entry (ri , & remaps , list )
446
+ if (ri -> rfe -> remap_type == REMAP_TYPE__GHOST )
447
+ try_clean_ghost (ri );
448
+
449
+ if (old_ns >= 0 ) {
450
+ if (setns (old_ns , CLONE_NEWNS ) < 0 )
451
+ pr_perror ("Fail to switch back!" );
452
+ close (old_ns );
453
+ }
454
+
455
+ out :
456
+ if (ns_fd >= 0 )
457
+ close (ns_fd );
458
+ }
459
+
382
460
static struct collect_image_info remap_cinfo = {
383
461
.fd_type = CR_FD_REMAP_FPATH ,
384
462
.pb_type = PB_REMAP_FPATH ,
0 commit comments