From: Rich Felker Date: Tue, 9 Jul 2013 04:42:09 +0000 (-0400) Subject: fix fd leak on races and cancellation in ctermid X-Git-Tag: v0.9.12~79 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=cdf0f53f8ba0e79dedb83c626851597bacec53ca;p=oweals%2Fmusl.git fix fd leak on races and cancellation in ctermid --- 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; }