From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5905 Path: news.gmane.org!not-for-mail From: Felix Janda Newsgroups: gmane.linux.lib.musl.general Subject: Re: Add login_tty Date: Tue, 26 Aug 2014 18:56:28 +0200 Message-ID: <20140826165627.GA1208@euler> References: <20140825185756.GA6077@euler> <20140825224333.GX12888@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="fdj2RfSjLxBAspz7" X-Trace: ger.gmane.org 1409072272 2734 80.91.229.3 (26 Aug 2014 16:57:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 26 Aug 2014 16:57:52 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-5911-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 26 18:57:46 2014 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1XMK41-000068-LC for gllmg-musl@plane.gmane.org; Tue, 26 Aug 2014 18:57:45 +0200 Original-Received: (qmail 5978 invoked by uid 550); 26 Aug 2014 16:57:45 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 5967 invoked from network); 26 Aug 2014 16:57:44 -0000 X-Virus-Scanned: amavisd-new at posteo.de Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <20140825224333.GX12888@brightrain.aerifal.cx> User-Agent: Mutt/1.5.22 (2013-10-16) Xref: news.gmane.org gmane.linux.lib.musl.general:5905 Archived-At: --fdj2RfSjLxBAspz7 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Rich Felker wrote: [..] > I don't have any fundamental objection to this. It might be nice to > review the forkpty code for errors it should be checking and make > these improvements at the same time, though. Ok, attached a proposed patch. Felix --fdj2RfSjLxBAspz7 Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-split-off-login_tty-from-forkpty-and-clean-up-the-la.patch" >From f1d88438a6d00defcf96562ef536a4af71827ee7 Mon Sep 17 00:00:00 2001 From: Felix Janda Date: Tue, 26 Aug 2014 18:36:23 +0200 Subject: [PATCH] split off login_tty() from forkpty() and clean up the latter since after calling openpty() no new fds are needed, an fd limit causes no problems. --- include/utmp.h | 2 ++ src/misc/forkpty.c | 25 ++++--------------------- src/misc/login_tty.c | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 src/misc/login_tty.c diff --git a/include/utmp.h b/include/utmp.h index e9ba23e..d3dcee7 100644 --- a/include/utmp.h +++ b/include/utmp.h @@ -43,6 +43,8 @@ void updwtmp(const char *, const struct utmp *); #define UTMP_FILENAME _PATH_UTMP #define WTMP_FILENAME _PATH_WTMP +int login_tty(int); + #ifdef __cplusplus } #endif diff --git a/src/misc/forkpty.c b/src/misc/forkpty.c index 07f8d01..007b369 100644 --- a/src/misc/forkpty.c +++ b/src/misc/forkpty.c @@ -1,37 +1,20 @@ #include +#include #include -#include -#include 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(); - ioctl(s, TIOCSCTTY, (char *)0); - dup2(s, 0); - dup2(s, 1); - dup2(s, 2); - if (s>2) close(s); + login_tty(s); return 0; } - for (i=0; i<3; i++) - if (istmp[i]) close(i); close(s); if (pid < 0) close(*m); return pid; diff --git a/src/misc/login_tty.c b/src/misc/login_tty.c new file mode 100644 index 0000000..f0be0a0 --- /dev/null +++ b/src/misc/login_tty.c @@ -0,0 +1,14 @@ +#include +#include +#include + +int login_tty(int fd) +{ + setsid(); + if (ioctl(fd, TIOCSCTTY, (char *)0)) return -1; + dup2(fd, 0); + dup2(fd, 1); + dup2(fd, 2); + if (fd>2) close(fd); + return 0; +} -- 1.8.5.5 --fdj2RfSjLxBAspz7--