Skip to content

Commit 896bdce

Browse files
aescolarkartben
authored andcommitted
subsys/tracing/tracing_backend_posix: Set file to be closed on exec
If the process does an exec() (or fork, or..) all descriptors are kept open by default, unless O_CLOEXEC is set when opening them. This is usefull for stdin/out/err so that new process is connected to them, but it is very rare for it to be usefull for any other descriptor. In general this leads to descriptors being kept open unnecessarily, which either will block other process from getting them (for example if the child survives the parent but it does something else). Or for a "leak" which unnecessarily uses descriptors and memory in the child process. Let's ensure we do not leak it for this component as we do not need it. Note glibc fopen supports the "e" modifier as an extension to pass O_CLOEXEC to the open syscall. Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent d83ebce commit 896bdce

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

subsys/tracing/tracing_backend_posix_bottom.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ void *tracing_backend_posix_init_bottom(const char *file_name)
1212
{
1313
FILE *f;
1414

15+
#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 7)
16+
f = fopen(file_name, "wbe"); /* Set O_CLOEXEC flag which is supported in glibc >= 2.7 */
17+
#else
1518
f = fopen(file_name, "wb");
19+
#endif
20+
1621
if (f == NULL) {
1722
nsi_print_error_and_exit("%s: Could not open CTF backend file %s\n",
1823
__func__, file_name);

0 commit comments

Comments
 (0)