From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6745 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Add login_tty Date: Sat, 20 Dec 2014 20:38:58 -0500 Message-ID: <20141221013858.GI4574@brightrain.aerifal.cx> References: <20141101222729.GB5949@euler> <20141101224325.GM22465@brightrain.aerifal.cx> <20141101225643.GA8817@euler> <20141102000944.GN22465@brightrain.aerifal.cx> <20141102141912.GA3637@euler> <20141102162841.GP22465@brightrain.aerifal.cx> <20141102185638.GA21712@euler> <20141102222818.GR22465@brightrain.aerifal.cx> <20141103182954.GA4423@euler> <20141221005821.GG4574@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1419125959 4187 80.91.229.3 (21 Dec 2014 01:39:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Dec 2014 01:39:19 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-6758-gllmg-musl=m.gmane.org@lists.openwall.com Sun Dec 21 02:39:12 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 1Y2VUG-0005cq-1d for gllmg-musl@m.gmane.org; Sun, 21 Dec 2014 02:39:12 +0100 Original-Received: (qmail 3926 invoked by uid 550); 21 Dec 2014 01:39:10 -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 3908 invoked from network); 21 Dec 2014 01:39:10 -0000 Content-Disposition: inline In-Reply-To: <20141221005821.GG4574@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:6745 Archived-At: On Sat, Dec 20, 2014 at 07:58:21PM -0500, Rich Felker wrote: > On Mon, Nov 03, 2014 at 07:29:54PM +0100, Felix Janda wrote: > > Thanks for the review. Below a new version. > > Sorry I didn't get around to reviewing this right away. > > > #include > > #include > > #include > > > > int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws) > > { > > int s, ec, p[2]; > > pid_t pid; > > > > if (openpty(m, &s, name, tio, ws) < 0) return -1; > > if (pipe2(p, O_CLOEXEC)) { > > close(s); > > goto fail; > > } > > > > pid = fork(); > > if (!pid) { > > close(*m); > > close(p[0]); > > ec = login_tty(s); > > login_tty could end up closing the pipe if stdin/out/err were > initially closed in the parent, since p[1] might be 0/1/2 in that > case. I think we need to check for this and move p[1] to a new fd in > that case (and fail if that fails) before calling login_tty. Actually this is a non-issue, since login_tty has committed itself to returning success by the time it dup2's over top of file descriptors 0/1/2. However I noticed another small issue: > > while (write(p[1], &ec, sizeof ec) < 0); This is writing -1, not the errno value. > > if (ec) _exit(127); > > close(p[1]); > > return 0; > > } > > close(s); > > close(p[1]); > > if (pid > 0) read(p[0], &ec, sizeof ec); > > This read probably needs to retry-loop, in case the parent has > interrupting signal handlers. I'm working on an improvement and I think it's better to just block signals for the whole function. Then the retry loop wouldn't be needed. The reason is that we don't want to allow a signal handler to run in a child process that "never existed" from the application's perspective. > > > close(p[0]); > > if (pid > 0) { > > if (!ec) return pid; > > waitpid(pid, &(int){0}, 0); > > I think waitpid could in principle fail too, but it probably shouldn't > since the process is already dead at the time waitpid is called. Then the retry is unneeded here too. I've got a draft based on these comments that I'll post soon for review. Rich