From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6746 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 21:59:07 -0500 Message-ID: <20141221025906.GJ4574@brightrain.aerifal.cx> References: <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> <20141221013858.GI4574@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="L6iaP+gRLNZHKoI4" X-Trace: ger.gmane.org 1419130768 1650 80.91.229.3 (21 Dec 2014 02:59:28 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 21 Dec 2014 02:59:28 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-6759-gllmg-musl=m.gmane.org@lists.openwall.com Sun Dec 21 03:59:21 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 1Y2Wjp-0002ps-E4 for gllmg-musl@m.gmane.org; Sun, 21 Dec 2014 03:59:21 +0100 Original-Received: (qmail 9373 invoked by uid 550); 21 Dec 2014 02:59:20 -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 9365 invoked from network); 21 Dec 2014 02:59:19 -0000 Content-Disposition: inline In-Reply-To: <20141221013858.GI4574@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:6746 Archived-At: --L6iaP+gRLNZHKoI4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Dec 20, 2014 at 08:38:58PM -0500, Rich Felker wrote: > I've got a draft based on these comments that I'll post soon for > review. Here's the draft. While I was at it I added protection against cancellation. openpty should have this too but I'll do that separately. Rich --L6iaP+gRLNZHKoI4 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="forkpty.c" #include #include #include #include #include #include #include int forkpty(int *pm, char *name, const struct termios *tio, const struct winsize *ws) { int m, s, ec=0, p[2], cs; pid_t pid=-1; sigset_t set, oldset; if (openpty(&m, &s, name, tio, ws) < 0) return -1; sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); if (pipe2(p, O_CLOEXEC)) { close(s); goto out; } pid = fork(); if (!pid) { close(m); close(p[0]); if (login_tty(s)) { write(p[1], &errno, sizeof errno); _exit(127); } close(p[1]); pthread_setcancelstate(cs, 0); pthread_sigmask(SIG_SETMASK, &oldset, 0); return 0; } close(s); close(p[1]); if (read(p[0], &ec, sizeof ec) > 0) { int status; waitpid(pid, &status, 0); pid = -1; errno = ec; } close(p[0]); out: if (pid > 0) *pm = m; else close(m); pthread_setcancelstate(cs, 0); pthread_sigmask(SIG_SETMASK, &oldset, 0); return pid; } --L6iaP+gRLNZHKoI4--