@@ -268,8 +268,7 @@ seccomp_program_new (int *fd)
268268 if (data == NULL )
269269 die_with_error ("Can't read seccomp data" );
270270
271- close (* fd );
272- * fd = -1 ;
271+ cleanup_fdp (fd );
273272
274273 if (len % 8 != 0 )
275274 die ("Invalid seccomp data, must be multiple of 8" );
@@ -481,8 +480,7 @@ report_child_exit_status (int exitc, int setup_finished_fd)
481480
482481 output = xasprintf ("{ \"exit-code\": %i }\n" , exitc );
483482 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 );
486484 close (setup_finished_fd );
487485}
488486
@@ -657,13 +655,7 @@ do_init (int event_fd, pid_t initial_pid)
657655
658656 /* Close FDs. */
659657 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 );
667659
668660 return initial_exit_status ;
669661}
@@ -1505,8 +1497,7 @@ setup_newroot (bool unshare_pid,
15051497 if (copy_file_data (op -> fd , dest_fd ) != 0 )
15061498 die_with_error ("Can't write data to file %s" , op -> dest );
15071499
1508- close (op -> fd );
1509- op -> fd = -1 ;
1500+ cleanup_fdp (& op -> fd );
15101501 }
15111502 break ;
15121503
@@ -1531,8 +1522,7 @@ setup_newroot (bool unshare_pid,
15311522 if (copy_file_data (op -> fd , dest_fd ) != 0 )
15321523 die_with_error ("Can't write data to file %s" , op -> dest );
15331524
1534- close (op -> fd );
1535- op -> fd = -1 ;
1525+ cleanup_fdp (& op -> fd );
15361526
15371527 assert (dest != NULL );
15381528
@@ -1598,13 +1588,7 @@ close_ops_fd (void)
15981588 SetupOp * op ;
15991589
16001590 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 );
16081592}
16091593
16101594/* We need to resolve relative symlinks in the sandbox before we
@@ -1820,7 +1804,7 @@ parse_args_recurse (int *argcp,
18201804 opt_args_data = load_file_data (the_fd , & data_len );
18211805 if (opt_args_data == NULL )
18221806 die_with_error ("Can't read --args data" );
1823- ( void ) close ( the_fd );
1807+ cleanup_fdp ( & the_fd );
18241808
18251809 data_end = opt_args_data + data_len ;
18261810 data_argc = 0 ;
@@ -3186,7 +3170,7 @@ main (int argc,
31863170 dump_info (opt_info_fd , output , true);
31873171 namespace_ids_write (opt_info_fd , false);
31883172 dump_info (opt_info_fd , "\n}\n" , true);
3189- close ( opt_info_fd );
3173+ cleanup_fdp ( & opt_info_fd );
31903174 }
31913175 if (opt_json_status_fd != -1 )
31923176 {
@@ -3200,14 +3184,14 @@ main (int argc,
32003184 {
32013185 char b [1 ];
32023186 (void ) TEMP_FAILURE_RETRY (read (opt_userns_block_fd , b , 1 ));
3203- close ( opt_userns_block_fd );
3187+ cleanup_fdp ( & opt_userns_block_fd );
32043188 }
32053189
32063190 /* Let child run now that the uid maps are set up */
32073191 val = 1 ;
32083192 res = TEMP_FAILURE_RETRY (write (child_wait_fd , & val , 8 ));
32093193 /* 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 );
32113195
32123196 return monitor_child (event_fd , pid , setup_finished_pipe [0 ]);
32133197 }
@@ -3251,15 +3235,12 @@ main (int argc,
32513235 * sandboxed process from outside the sandbox either.
32523236 */
32533237
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 );
32593240
32603241 /* Wait for the parent to init uid/gid maps and drop caps */
32613242 res = read (child_wait_fd , & val , 8 );
3262- close ( child_wait_fd );
3243+ cleanup_fdp ( & child_wait_fd );
32633244
32643245 /* At this point we can completely drop root uid, but retain the
32653246 * required permitted caps. This allow us to do full setup as
@@ -3498,7 +3479,7 @@ main (int argc,
34983479 {
34993480 char b [1 ];
35003481 (void ) TEMP_FAILURE_RETRY (read (opt_block_fd , b , 1 ));
3501- close ( opt_block_fd );
3482+ cleanup_fdp ( & opt_block_fd );
35023483 }
35033484
35043485 if (opt_seccomp_fd != -1 )
@@ -3580,16 +3561,12 @@ main (int argc,
35803561
35813562 debug ("launch executable %s" , argv [0 ]);
35823563
3583- if (proc_fd != -1 )
3584- close (proc_fd );
3564+ cleanup_fdp (& proc_fd );
35853565
35863566 /* If we are using --as-pid-1 leak the sync fd into the sandbox.
35873567 --sync-fd will still work unless the container process doesn't close this file. */
35883568 if (!opt_as_pid_1 )
3589- {
3590- if (opt_sync_fd != -1 )
3591- close (opt_sync_fd );
3592- }
3569+ cleanup_fdp (& opt_sync_fd );
35933570
35943571 /* We want sigchild in the child */
35953572 unblock_sigchild ();
0 commit comments