>From 8b60d5f69bf3b199e4050f3b1f24b76657765ea0 Mon Sep 17 00:00:00 2001 From: Felix Janda Date: Sat, 1 Nov 2014 23:08:24 +0100 Subject: [PATCH 1/2] Remove unnecessary allocation of fd 0/1/2 in forkpty() Since there are no error paths in the kernel's dup2() implementation which might be evaded by first allocating fd 0/1/2, remove the corresponding code. --- src/misc/forkpty.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/misc/forkpty.c b/src/misc/forkpty.c index 07f8d01..4a1ea9b 100644 --- a/src/misc/forkpty.c +++ b/src/misc/forkpty.c @@ -5,21 +5,12 @@ int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws) { - int s, t, i, istmp[3]={0}; + int s; pid_t pid; if (openpty(m, &s, name, tio, ws) < 0) return -1; - /* Ensure before forking that we don't exceed fd limit */ - for (i=0; i<3; i++) { - if (fcntl(i, F_GETFL) < 0) { - t = fcntl(s, F_DUPFD, i); - if (t<0) break; - else if (t!=i) close(t); - else istmp[i] = 1; - } - } - pid = i==3 ? fork() : -1; + pid = fork(); if (!pid) { close(*m); setsid(); @@ -30,8 +21,6 @@ int forkpty(int *m, char *name, const struct termios *tio, const struct winsize if (s>2) close(s); return 0; } - for (i=0; i<3; i++) - if (istmp[i]) close(i); close(s); if (pid < 0) close(*m); return pid; -- 2.0.4