mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: Add login_tty
Date: Sat, 20 Dec 2014 20:38:58 -0500	[thread overview]
Message-ID: <20141221013858.GI4574@brightrain.aerifal.cx> (raw)
In-Reply-To: <20141221005821.GG4574@brightrain.aerifal.cx>

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 <pty.h>
> > #include <utmp.h>
> > #include <unistd.h>
> > 
> > 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


  parent reply	other threads:[~2014-12-21  1:38 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-25 18:57 Felix Janda
2014-08-25 22:43 ` Rich Felker
2014-08-26 16:56   ` Felix Janda
2014-08-29 18:44     ` Felix Janda
2014-09-04 21:22     ` Szabolcs Nagy
2014-09-04 21:33       ` Rich Felker
2014-09-04 22:31         ` Justin Cormack
2014-09-05 17:23       ` Felix Janda
2014-09-05 17:29         ` Rich Felker
2014-09-05 18:52           ` Felix Janda
2014-10-31 16:19     ` Rich Felker
2014-11-01 21:15       ` Felix Janda
2014-11-01 21:45         ` Rich Felker
2014-11-01 22:07           ` Szabolcs Nagy
2014-11-01 22:27           ` Felix Janda
2014-11-01 22:43             ` Rich Felker
2014-11-01 22:56               ` Felix Janda
2014-11-02  0:09                 ` Rich Felker
2014-11-02 14:19                   ` Felix Janda
2014-11-02 16:28                     ` Rich Felker
2014-11-02 18:56                       ` Felix Janda
2014-11-02 22:28                         ` Rich Felker
2014-11-03 18:29                           ` Felix Janda
2014-12-21  0:58                             ` Rich Felker
2014-12-21  1:15                               ` Rich Felker
2014-12-21  1:38                               ` Rich Felker [this message]
2014-12-21  2:59                                 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20141221013858.GI4574@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).