@@ -268,8 +268,7 @@ seccomp_program_new (int *fd)
268
268
if (data == NULL )
269
269
die_with_error ("Can't read seccomp data" );
270
270
271
- close (* fd );
272
- * fd = -1 ;
271
+ cleanup_fdp (fd );
273
272
274
273
if (len % 8 != 0 )
275
274
die ("Invalid seccomp data, must be multiple of 8" );
@@ -481,8 +480,7 @@ report_child_exit_status (int exitc, int setup_finished_fd)
481
480
482
481
output = xasprintf ("{ \"exit-code\": %i }\n" , exitc );
483
482
dump_info (opt_json_status_fd , output , false);
484
- close (opt_json_status_fd );
485
- opt_json_status_fd = -1 ;
483
+ cleanup_fdp (& opt_json_status_fd );
486
484
close (setup_finished_fd );
487
485
}
488
486
@@ -657,13 +655,7 @@ do_init (int event_fd, pid_t initial_pid)
657
655
658
656
/* Close FDs. */
659
657
for (lock = lock_files ; lock != NULL ; lock = lock -> next )
660
- {
661
- if (lock -> fd >= 0 )
662
- {
663
- close (lock -> fd );
664
- lock -> fd = -1 ;
665
- }
666
- }
658
+ cleanup_fdp (& lock -> fd );
667
659
668
660
return initial_exit_status ;
669
661
}
@@ -1505,8 +1497,7 @@ setup_newroot (bool unshare_pid,
1505
1497
if (copy_file_data (op -> fd , dest_fd ) != 0 )
1506
1498
die_with_error ("Can't write data to file %s" , op -> dest );
1507
1499
1508
- close (op -> fd );
1509
- op -> fd = -1 ;
1500
+ cleanup_fdp (& op -> fd );
1510
1501
}
1511
1502
break ;
1512
1503
@@ -1531,8 +1522,7 @@ setup_newroot (bool unshare_pid,
1531
1522
if (copy_file_data (op -> fd , dest_fd ) != 0 )
1532
1523
die_with_error ("Can't write data to file %s" , op -> dest );
1533
1524
1534
- close (op -> fd );
1535
- op -> fd = -1 ;
1525
+ cleanup_fdp (& op -> fd );
1536
1526
1537
1527
assert (dest != NULL );
1538
1528
@@ -1598,13 +1588,7 @@ close_ops_fd (void)
1598
1588
SetupOp * op ;
1599
1589
1600
1590
for (op = ops ; op != NULL ; op = op -> next )
1601
- {
1602
- if (op -> fd != -1 )
1603
- {
1604
- (void ) close (op -> fd );
1605
- op -> fd = -1 ;
1606
- }
1607
- }
1591
+ cleanup_fdp (& op -> fd );
1608
1592
}
1609
1593
1610
1594
/* We need to resolve relative symlinks in the sandbox before we
@@ -1820,7 +1804,7 @@ parse_args_recurse (int *argcp,
1820
1804
opt_args_data = load_file_data (the_fd , & data_len );
1821
1805
if (opt_args_data == NULL )
1822
1806
die_with_error ("Can't read --args data" );
1823
- ( void ) close ( the_fd );
1807
+ cleanup_fdp ( & the_fd );
1824
1808
1825
1809
data_end = opt_args_data + data_len ;
1826
1810
data_argc = 0 ;
@@ -3186,7 +3170,7 @@ main (int argc,
3186
3170
dump_info (opt_info_fd , output , true);
3187
3171
namespace_ids_write (opt_info_fd , false);
3188
3172
dump_info (opt_info_fd , "\n}\n" , true);
3189
- close ( opt_info_fd );
3173
+ cleanup_fdp ( & opt_info_fd );
3190
3174
}
3191
3175
if (opt_json_status_fd != -1 )
3192
3176
{
@@ -3200,14 +3184,14 @@ main (int argc,
3200
3184
{
3201
3185
char b [1 ];
3202
3186
(void ) TEMP_FAILURE_RETRY (read (opt_userns_block_fd , b , 1 ));
3203
- close ( opt_userns_block_fd );
3187
+ cleanup_fdp ( & opt_userns_block_fd );
3204
3188
}
3205
3189
3206
3190
/* Let child run now that the uid maps are set up */
3207
3191
val = 1 ;
3208
3192
res = TEMP_FAILURE_RETRY (write (child_wait_fd , & val , 8 ));
3209
3193
/* Ignore res, if e.g. the child died and closed child_wait_fd we don't want to error out here */
3210
- close ( child_wait_fd );
3194
+ cleanup_fdp ( & child_wait_fd );
3211
3195
3212
3196
return monitor_child (event_fd , pid , setup_finished_pipe [0 ]);
3213
3197
}
@@ -3251,15 +3235,12 @@ main (int argc,
3251
3235
* sandboxed process from outside the sandbox either.
3252
3236
*/
3253
3237
3254
- if (opt_info_fd != -1 )
3255
- close (opt_info_fd );
3256
-
3257
- if (opt_json_status_fd != -1 )
3258
- close (opt_json_status_fd );
3238
+ cleanup_fdp (& opt_info_fd );
3239
+ cleanup_fdp (& opt_json_status_fd );
3259
3240
3260
3241
/* Wait for the parent to init uid/gid maps and drop caps */
3261
3242
res = read (child_wait_fd , & val , 8 );
3262
- close ( child_wait_fd );
3243
+ cleanup_fdp ( & child_wait_fd );
3263
3244
3264
3245
/* At this point we can completely drop root uid, but retain the
3265
3246
* required permitted caps. This allow us to do full setup as
@@ -3498,7 +3479,7 @@ main (int argc,
3498
3479
{
3499
3480
char b [1 ];
3500
3481
(void ) TEMP_FAILURE_RETRY (read (opt_block_fd , b , 1 ));
3501
- close ( opt_block_fd );
3482
+ cleanup_fdp ( & opt_block_fd );
3502
3483
}
3503
3484
3504
3485
if (opt_seccomp_fd != -1 )
@@ -3580,16 +3561,12 @@ main (int argc,
3580
3561
3581
3562
debug ("launch executable %s" , argv [0 ]);
3582
3563
3583
- if (proc_fd != -1 )
3584
- close (proc_fd );
3564
+ cleanup_fdp (& proc_fd );
3585
3565
3586
3566
/* If we are using --as-pid-1 leak the sync fd into the sandbox.
3587
3567
--sync-fd will still work unless the container process doesn't close this file. */
3588
3568
if (!opt_as_pid_1 )
3589
- {
3590
- if (opt_sync_fd != -1 )
3591
- close (opt_sync_fd );
3592
- }
3569
+ cleanup_fdp (& opt_sync_fd );
3593
3570
3594
3571
/* We want sigchild in the child */
3595
3572
unblock_sigchild ();
0 commit comments