Skip to content

Commit a7e9059

Browse files
committed
Open fstream files in O_CLOEXEC mode when possible.
Reviewers: EricWF, mclow.lists, ldionne Reviewed By: ldionne Subscribers: smeenai, dexonsmith, christof, ldionne, libcxx-commits Tags: #libc Differential Revision: https://reviews.llvm.org/D59839 llvm-svn: 372027
1 parent a507a5e commit a7e9059

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

libcxx/include/__config

+11
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,17 @@ _LIBCPP_FUNC_VIS extern "C" void __sanitizer_annotate_contiguous_container(
14471447

14481448
#define _LIBCPP_UNUSED_VAR(x) ((void)(x))
14491449

1450+
// Configures the fopen close-on-exec mode character, if any. This string will
1451+
// be appended to any mode string used by fstream for fopen/fdopen.
1452+
//
1453+
// Not all platforms support this, but it helps avoid fd-leaks on platforms that
1454+
// do.
1455+
#if defined(__BIONIC__)
1456+
# define _LIBCPP_FOPEN_CLOEXEC_MODE "e"
1457+
#else
1458+
# define _LIBCPP_FOPEN_CLOEXEC_MODE
1459+
#endif
1460+
14501461
#endif // __cplusplus
14511462

14521463
#endif // _LIBCPP_CONFIG

libcxx/include/fstream

+12-12
Original file line numberDiff line numberDiff line change
@@ -508,34 +508,34 @@ const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(
508508
switch (__mode & ~ios_base::ate) {
509509
case ios_base::out:
510510
case ios_base::out | ios_base::trunc:
511-
return "w";
511+
return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
512512
case ios_base::out | ios_base::app:
513513
case ios_base::app:
514-
return "a";
514+
return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
515515
case ios_base::in:
516-
return "r";
516+
return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
517517
case ios_base::in | ios_base::out:
518-
return "r+";
518+
return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
519519
case ios_base::in | ios_base::out | ios_base::trunc:
520-
return "w+";
520+
return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
521521
case ios_base::in | ios_base::out | ios_base::app:
522522
case ios_base::in | ios_base::app:
523-
return "a+";
523+
return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
524524
case ios_base::out | ios_base::binary:
525525
case ios_base::out | ios_base::trunc | ios_base::binary:
526-
return "wb";
526+
return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
527527
case ios_base::out | ios_base::app | ios_base::binary:
528528
case ios_base::app | ios_base::binary:
529-
return "ab";
529+
return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
530530
case ios_base::in | ios_base::binary:
531-
return "rb";
531+
return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
532532
case ios_base::in | ios_base::out | ios_base::binary:
533-
return "r+b";
533+
return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
534534
case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
535-
return "w+b";
535+
return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
536536
case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
537537
case ios_base::in | ios_base::app | ios_base::binary:
538-
return "a+b";
538+
return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
539539
default:
540540
return nullptr;
541541
}

0 commit comments

Comments
 (0)