From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6420 Path: news.gmane.org!not-for-mail From: Felix Janda Newsgroups: gmane.linux.lib.musl.general Subject: Re: Add login_tty Date: Sat, 1 Nov 2014 23:27:29 +0100 Message-ID: <20141101222729.GB5949@euler> References: <20140825185756.GA6077@euler> <20140825224333.GX12888@brightrain.aerifal.cx> <20140826165627.GA1208@euler> <20141031161907.GD22465@brightrain.aerifal.cx> <20141101211523.GA13145@euler> <20141101214503.GK22465@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="KsGdsel6WgEHnImy" X-Trace: ger.gmane.org 1414880890 4276 80.91.229.3 (1 Nov 2014 22:28:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 1 Nov 2014 22:28:10 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-6433-gllmg-musl=m.gmane.org@lists.openwall.com Sat Nov 01 23:28:04 2014 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1Xkh9M-0004Fq-AK for gllmg-musl@m.gmane.org; Sat, 01 Nov 2014 23:28:00 +0100 Original-Received: (qmail 26607 invoked by uid 550); 1 Nov 2014 22:27:58 -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 26593 invoked from network); 1 Nov 2014 22:27:58 -0000 X-Virus-Scanned: amavisd-new at posteo.de Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <20141101214503.GK22465@brightrain.aerifal.cx> User-Agent: Mutt/1.5.22 (2013-10-16) Xref: news.gmane.org gmane.linux.lib.musl.general:6420 Archived-At: --KsGdsel6WgEHnImy Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Rich Felker wrote: [..] > In that case maybe your patch is okay as-is, aside from needing to be > factored into two changes -- one for removing useless code and the > other for separating-out login_tty. Both of them are attached. Felix --KsGdsel6WgEHnImy Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Remove-unnecessary-allocation-of-fd-0-1-2-in-forkpty.patch" >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 --KsGdsel6WgEHnImy Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0002-Split-off-login_tty-form-forkpty.patch" >From 00f65eed60d4e805bb03bf638289d45409b93f60 Mon Sep 17 00:00:00 2001 From: Felix Janda Date: Sat, 1 Nov 2014 23:20:27 +0100 Subject: [PATCH 2/2] Split off login_tty form forkpty --- include/utmp.h | 2 ++ src/misc/forkpty.c | 10 ++-------- src/misc/login_tty.c | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 8 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 4a1ea9b..007b369 100644 --- a/src/misc/forkpty.c +++ b/src/misc/forkpty.c @@ -1,7 +1,6 @@ #include +#include #include -#include -#include int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws) { @@ -13,12 +12,7 @@ int forkpty(int *m, char *name, const struct termios *tio, const struct winsize 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; } close(s); 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; +} -- 2.0.4 --KsGdsel6WgEHnImy--