A stream is allocated as a controlling terminal for a session if:
The stream is acting as a terminal.
The stream is not already allocated as a controlling terminal.
The stream 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.