login_tty: Rewrite following FreeBSD's traces

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2016-11-24 12:06:29 +01:00
parent b56179f83a
commit d3becf4318

View File

@ -77,22 +77,15 @@ daemon (int nochdir, int noclose)
extern "C" int extern "C" int
login_tty (int fd) login_tty (int fd)
{ {
char *fdname; /* If setsid fails, FreeBSD uses the current sid returned by getsid(0),
int newfd; then calls tcsetsid, which we don't provide (just as Linux doesn't).
tcsetsid only uses the sid to check against the value returned by
if (setsid () == -1) getsid(0). So, either way, that check will not fail and we can
simply ignore the return value from setsid and just perform the
ioctl call tcsetsid does. */
setsid ();
if (ioctl (fd, TIOCSCTTY, NULL) == -1)
return -1; return -1;
if ((fdname = ttyname (fd)))
{
if (fd != STDIN_FILENO)
close (STDIN_FILENO);
if (fd != STDOUT_FILENO)
close (STDOUT_FILENO);
if (fd != STDERR_FILENO)
close (STDERR_FILENO);
newfd = open (fdname, O_RDWR);
close (newfd);
}
dup2 (fd, STDIN_FILENO); dup2 (fd, STDIN_FILENO);
dup2 (fd, STDOUT_FILENO); dup2 (fd, STDOUT_FILENO);
dup2 (fd, STDERR_FILENO); dup2 (fd, STDERR_FILENO);