From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6743 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 19:58:21 -0500 Message-ID: <20141221005821.GG4574@brightrain.aerifal.cx> References: <20141101214503.GK22465@brightrain.aerifal.cx> <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> 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 1419123523 5271 80.91.229.3 (21 Dec 2014 00:58:43 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Dec 2014 00:58:43 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-6756-gllmg-musl=m.gmane.org@lists.openwall.com Sun Dec 21 01:58:37 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 1Y2Uqy-00061n-UR for gllmg-musl@m.gmane.org; Sun, 21 Dec 2014 01:58:37 +0100 Original-Received: (qmail 7311 invoked by uid 550); 21 Dec 2014 00:58:34 -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 7285 invoked from network); 21 Dec 2014 00:58:34 -0000 Content-Disposition: inline In-Reply-To: <20141103182954.GA4423@euler> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:6743 Archived-At: 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. > while (write(p[1], &ec, sizeof ec) < 0); > 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. > 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. > } > fail: > close(*m); > return -1; > } Otherwise it looks okay now. Rich