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.