*This* is what's losing the tty. According to the Fine Man Page:
DESCRIPTION
The setsid function creates a new session. The calling process is the
session leader of the new session, is the process group leader of a new
process group and has no controlling terminal.
^^^^^^^^^^^^^^^^^^^^^^^^^^^
The above description is incomplete. This version has more detail:
DESCRIPTION
setsid() creates a new session if the calling process is not a process
group leader. The calling process is the leader of the new session,
the process group leader of the new process group, and has no control-
ling tty. The process group ID and session ID of the calling process
are set to the PID of the calling process. The calling process will be
the only process in this new process group and in this new session.
The key point is "... if the calling process is not a process group leader".
The easiest way to achieve this condition is to fork(). This ensures that
the child process won't be a process group leader unless it explicitly
makes itself one with setpgid() or setpgrp().
A process launched from a shell typically /will/ be a process group leader
unless it's part of a pipeline (the shell creates a new process group for
each pipeline; one of the processes in that group will be its leader).
setsid() will fail with EPERM if the calling process is a process group
leader.