Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 2 Nov 2014 19:56:38 +0100
From: Felix Janda <felix.janda@...teo.de>
To: musl@...ts.openwall.com
Subject: Re: Add login_tty

Rich Felker wrote:
[..]
> There's an approach I use in posix_spawn that could be copied here. My
> hope was to keep forkpty simpler but it's not too bad and it would
> make it so we could handle other arbitrary errors in the child if we
> ever need to, so maybe it's a good idea.

So how about the following?
(I'm not sure whether error checking is necessary for read.)


#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 (pipe2(p, O_CLOEXEC)) return -1;
	if (openpty(m, &s, name, tio, ws) < 0) return -1;

	pid = fork();
	if (!pid) {
		close(*m);
		close(p[0]);
		ec = login_tty(s);
		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);
	close(p[0]);
	if (pid < 0 || ec < 0) {
		close(*m);
		return -1;
	}
	return pid;
}


Felix

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.