|
|
Message-ID: <20141221013858.GI4574@brightrain.aerifal.cx>
Date: Sat, 20 Dec 2014 20:38:58 -0500
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: Add login_tty
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
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.