From cdf0f53f8ba0e79dedb83c626851597bacec53ca Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Tue, 9 Jul 2013 00:42:09 -0400 Subject: [PATCH] fix fd leak on races and cancellation in ctermid --- src/unistd/ctermid.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/unistd/ctermid.c b/src/unistd/ctermid.c index 21b44ec8..c238905a 100644 --- a/src/unistd/ctermid.c +++ b/src/unistd/ctermid.c @@ -4,6 +4,7 @@ #include #include #include +#include "syscall.h" char *ctermid(char *s) { @@ -13,11 +14,11 @@ char *ctermid(char *s) if (!s2) s2 = malloc(L_ctermid); s = s2; } - fd = open("/dev/tty", O_WRONLY | O_NOCTTY); + fd = open("/dev/tty", O_WRONLY | O_NOCTTY | O_CLOEXEC); if (fd < 0) return strcpy(s, ""); if (ttyname_r(fd, s, L_ctermid)) strcpy(s, ""); - close(fd); + __syscall(SYS_close, fd); return s; } -- 2.25.1