A stream is allocated as a controlling terminal for a session if it:
Is acting as a terminal.
Is not already allocated as a controlling terminal.
Is opened by a session leader that does not have a controlling terminal.
Controlling terminals are allocated with open(2). The device must inform the stream head that it is acting as a terminal.