N_TTY
The default (and fallback) TTY line discipline. It tries to handle characters as per POSIX.
External Functions #
n_tty_inherit_ops() #
void inherit N_TTY methods
Parameters
struct tty_ldisc_ops *ops: struct tty_ldisc_ops where to save N_TTY methods
Description
Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
Internal Functions #
n_tty_kick_worker() #
void start input worker (if required)
Parameters
const struct tty_struct *tty: terminal
Description
Re-schedules the flip buffer work if it may have stopped.
- Locking:
- Caller holds exclusive
termios_rwsem, or n_tty_read()/consumer path: holds non-exclusivetermios_rwsem
- Caller holds exclusive
n_tty_write_wakeup() #
void asynchronous I/O notifier
Parameters
struct tty_struct *tty: tty device
Description
Required for the ptys, serial driver etc. since processes that attach themselves to the master and rely on ASYNC IO must be woken up.
put_tty_queue() #
void add character to tty
Parameters
u8 c: characterstruct n_tty_data *ldata: n_tty data
Description
Add a character to the tty read_buf queue.
- Locking:
n_tty_receive_buf()/producer path: caller holds non-exclusivetermios_rwsem
reset_buffer_flags() #
void reset buffer state
Parameters
struct n_tty_data *ldata: line disc data to reset
Description
Reset the read buffer counters and clear the flags. Called from
n_tty_open() and n_tty_flush_buffer().
- Locking:
- caller holds exclusive
termios_rwsem, or - (locking is not required)
- caller holds exclusive
n_tty_flush_buffer() #
void clean input queue
Parameters
struct tty_struct *tty: terminal device
Description
Flush the input buffer. Called when the tty layer wants the buffer flushed
(eg at hangup) or when the N_TTY line discipline internally has to clean
the pending queue (for example some signals).
Holds termios_rwsem to exclude producer/consumer while buffer indices are
reset.
Locking: ctrl.lock, exclusive termios_rwsem
is_utf8_continuation() #
int utf8 multibyte check
Parameters
u8 c: byte to check
Return
true if the utf8 character c is a multibyte continuation character. We use this to correctly compute the on-screen size of the character when printing.
is_continuation() #
int multibyte check
Parameters
u8 c: byte to checkconst struct tty_struct *tty: terminal device
Return
true if the utf8 character c is a multibyte continuation character and the terminal is in unicode mode.
do_output_char() #
int output one character
Parameters
u8 c: character (or partial unicode symbol)struct tty_struct *tty: terminal deviceint space: space available in tty driver write buffer
Description
This is a helper function that handles one output character (including special characters like TAB, CR, LF, etc.), doing OPOST processing and putting the results in the tty driver's write buffer.
Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't relevant in the world today. If you ever need them, add them here.
Locking: should be called under the output_lock to protect the column state
and space left in the buffer.
Return
the number of bytes of buffer space used or -1 if no space left.
process_output() #
int output post processor
Parameters
u8 c: character (or partial unicode symbol)struct tty_struct *tty: terminal device
Description
Output one character with OPOST processing.
Locking: output_lock to protect column state and space left (also, this is
called from n_tty_write() under the tty layer write lock).
Return
-1 when the output device is full and the character must be retried.
process_output_block() #
ssize_t block post processor
Parameters
struct tty_struct *tty: terminal deviceconst u8 *buf: character bufferunsigned int nr: number of bytes to output
Description
Output a block of characters with OPOST processing.
This path is used to speed up block console writes, among other things when processing blocks of output data. It handles only the simple cases normally found and helps to generate blocks of symbols for the console driver and thus improve performance.
Locking: output_lock to protect column state and space left (also, this is
called from n_tty_write() under the tty layer write lock).
Return
the number of characters output.
__process_echoes() #
size_t write pending echo characters
Parameters
struct tty_struct *tty: terminal device
Description
Write previously buffered echo (and other ldisc-generated) characters to the tty.
Characters generated by the ldisc (including echoes) need to be buffered because the driver's write buffer can fill during heavy program output. Echoing straight to the driver will often fail under these conditions, causing lost characters and resulting mismatches of ldisc state information.
Since the ldisc state must represent the characters actually sent to the driver at the time of the write, operations like certain changes in column state are also saved in the buffer and executed here.
A circular fifo buffer is used so that the most recent characters are prioritized. Also, when control characters are echoed with a prefixed "^", the pair is treated atomically and thus not separated.
Locking: callers must hold output_lock.
add_echo_byte() #
void add a byte to the echo buffer
Parameters
u8 c: unicode byte to echostruct n_tty_data *ldata: n_tty data
Description
Add a character or operation byte to the echo buffer.
echo_move_back_col() #
void add operation to move back a column
Parameters
struct n_tty_data *ldata: n_tty data
Description
Add an operation to the echo buffer to move back one column.
echo_set_canon_col() #
void add operation to set the canon column
Parameters
struct n_tty_data *ldata: n_tty data
Description
Add an operation to the echo buffer to set the canon column to the current column.
echo_erase_tab() #
void add operation to erase a tab
Parameters
unsigned int num_chars: number of character columns already usedint after_tab: true if num_chars starts after a previous tabstruct n_tty_data *ldata: n_tty data
Description
Add an operation to the echo buffer to erase a tab.
Called by the eraser function, which knows how many character columns have been used since either a previous tab or the start of input. This information will be used later, along with canon column (if applicable), to go back the correct number of columns.
echo_char_raw() #
void echo a character raw
Parameters
u8 c: unicode byte to echostruct n_tty_data *ldata: line disc data
Description
Echo user input back onto the screen. This must be called only when
L_ECHO(tty) is true. Called from the tty_driver.receive_buf() path.
This variant does not treat control characters specially.
echo_char() #
void echo a character
Parameters
u8 c: unicode byte to echoconst struct tty_struct *tty: terminal device
Description
Echo user input back onto the screen. This must be called only when
L_ECHO(tty) is true. Called from the tty_driver.receive_buf() path.
This variant tags control characters to be echoed as "^X" (where X is the letter representing the control char).
finish_erasing() #
void complete erase
Parameters
struct n_tty_data *ldata: n_tty data
eraser() #
void handle erase function
Parameters
u8 c: character inputconst struct tty_struct *tty: terminal device
Description
Perform erase and necessary output when an erase character is present in the stream from the driver layer. Handles the complexities of UTF-8 multibyte symbols.
- Locking:
n_tty_receive_buf()/producer path: caller holds non-exclusivetermios_rwsem
isig() #
void handle the ISIG optio
Parameters
int sig: signalstruct tty_struct *tty: terminal
Description
Called when a signal is being sent due to terminal input. Called from the
tty_driver.receive_buf() path, so serialized.
Performs input and output flush if !NOFLSH. In this context, the echo buffer is 'output'. The signal is processed first to alert any current readers or writers to discontinue and exit their i/o loops.
Locking: ctrl.lock
n_tty_receive_break() #
void handle break
Parameters
struct tty_struct *tty: terminal
Description
An RS232 break event has been hit in the incoming bitstream. This can cause a variety of events depending upon the termios settings.
- Locking:
n_tty_receive_buf()/producer path: caller holds non-exclusive termios_rwsem
Note
may get exclusive termios_rwsem if flushing input buffer
n_tty_receive_overrun() #
void handle overrun reporting
Parameters
const struct tty_struct *tty: terminal
Description
Data arrived faster than we could process it. While the tty driver has flagged this the bits that were missed are gone forever.
Called from the receive_buf path so single threaded. Does not need locking as num_overrun and overrun_time are function private.
n_tty_receive_parity_error() #
void error notifier
Parameters
const struct tty_struct *tty: terminal deviceu8 c: character
Description
Process a parity error and queue the right data to indicate the error case if necessary.
- Locking:
n_tty_receive_buf()/producer path: caller holds non-exclusivetermios_rwsem
n_tty_receive_char_flow_ctrl() #
bool receive flow control chars
Parameters
struct tty_struct *tty: terminal deviceu8 c: characterbool lookahead_done: lookahead has processed this character already
Description
Receive and process flow control character actions.
In case lookahead for flow control chars already handled the character in advance to the normal receive, the actions are skipped during normal receive.
Returns true if c is consumed as flow-control character, the character must not be treated as normal character.
n_tty_receive_char() #
void perform processing
Parameters
struct tty_struct *tty: terminal deviceu8 c: character
Description
Process an individual character of input received from the driver. This is serialized with respect to itself by the rules for the driver above.
- Locking:
n_tty_receive_buf()/producer path: caller holds non-exclusivetermios_rwsempublishes canon_head if canonical mode is active
n_tty_receive_buf_common() #
size_t process input
Parameters
struct tty_struct *tty: device to receive inputconst u8 *cp: input charsconst u8 *fp: flags for each char (ifNULL, all chars areTTY_NORMAL)size_t count: number of input chars in cpbool flow: enable flow control
Description
Called by the terminal driver when a block of characters has been received.
This function must be called from soft contexts not from interrupt context.
The driver is responsible for making calls one at a time and in order (or
using flush_to_ldisc()).
In canonical mode, the maximum line length is 4096 chars (including the line termination char); lines longer than 4096 chars are truncated. After 4095 chars, input data is still processed but not stored. Overflow processing ensures the tty can always receive more input until at least one line can be read.
In non-canonical mode, the read buffer will only accept 4095 chars; this provides the necessary space for a newline char if the input mode is switched to canonical.
Note it is possible for the read buffer to contain 4096 chars in non-canonical mode: the read buffer could already contain the maximum canon line of 4096 chars when the mode is switched to non-canonical.
- Locking:
n_tty_receive_buf()/producer path: claims non-exclusivetermios_rwsempublishes commit_head or canon_head
Return
the # of input chars from cp which were processed.
n_tty_set_termios() #
void termios data changed
Parameters
struct tty_struct *tty: terminalconst struct ktermios *old: previous data
Description
Called by the tty layer when the user changes termios flags so that the line discipline can plan ahead. This function cannot sleep and is protected from re-entry by the tty layer. The user is guaranteed that this function will not be re-entered or in progress when the ldisc is closed.
Locking: Caller holds tty->termios_rwsem
n_tty_close() #
void close the ldisc for this tty
Parameters
struct tty_struct *tty: device
Description
Called from the terminal layer when this line discipline is being shut down, either because of a close or becsuse of a discipline change. The function will not be called while other ldisc methods are in progress.
n_tty_open() #
int open an ldisc
Parameters
struct tty_struct *tty: terminal to open
Description
Called when this line discipline is being attached to the terminal device. Can sleep. Called serialized so that no other events will occur in parallel. No further open will occur until a close.
copy_from_read_buf() #
bool copy read data directly
Parameters
const struct tty_struct *tty: terminal deviceu8 **kbp: datasize_t *nr: size of data
Description
Helper function to speed up n_tty_read(). It is only called when ICANON is
off; it copies characters straight from the tty queue.
- Locking:
- called under the ldata->atomic_read_lock sem
n_tty_read()/consumer path: caller holds non-exclusivetermios_rwsem; read_tail published
Return
true if it successfully copied data, but there is still more data to be had.
canon_copy_from_read_buf() #
bool copy read data in canonical mode
Parameters
const struct tty_struct *tty: terminal deviceu8 **kbp: datasize_t *nr: size of data
Description
Helper function for n_tty_read(). It is only called when ICANON is on; it
copies one line of input up to and including the line-delimiting character
into the result buffer.
Note
When termios is changed from non-canonical to canonical mode and the
read buffer contains data, n_tty_set_termios() simulates an EOF push (as if
C-d were input) without the DISABLED_CHAR in the buffer. This causes data
already processed as input to be immediately available as input although a
newline has not been received.
- Locking:
- called under the
atomic_read_lockmutex n_tty_read()/consumer path: caller holds non-exclusivetermios_rwsem; read_tail published
- called under the
job_control() #
int check job control
Parameters
struct tty_struct *tty: ttystruct file *file: file handle
Description
Perform job control management checks on this file/tty descriptor and if appropriate send any needed signals and return a negative error code if action should be taken.
- Locking:
- redirected write test is safe
- current->signal->tty check is safe
- ctrl.lock to safely reference tty->ctrl.pgrp
n_tty_read() #
ssize_t read function for tty
Parameters
struct tty_struct *tty: tty devicestruct file *file: file objectu8 *kbuf: kernelspace buffer pointersize_t nr: size of I/Ovoid **cookie: if non-NULL, this is a continuation readunsigned long offset: where to continue reading from (unused in n_tty)
Description
Perform reads for the line discipline. We are guaranteed that the line discipline will not be closed under us but we may get multiple parallel readers and must handle this ourselves. We may also get a hangup. Always called in user context, may sleep.
This code must be sure never to sleep through a hangup.
- Locking:
n_tty_read()/consumer path: claims non-exclusive termios_rwsem; publishes read_tail
n_tty_write() #
ssize_t write function for tty
Parameters
struct tty_struct *tty: tty devicestruct file *file: file objectconst u8 *buf: userspace buffer pointersize_t nr: size of I/O
Description
Write function of the terminal device. This is serialized with respect to
other write callers but not to termios changes, reads and other such events.
Since the receive code will echo characters, thus calling driver write
methods, the output_lock is used in the output processing functions called
here as well as in the echo processing function to protect the column state
and space left in the buffer.
This code must be sure never to sleep through a hangup.
-
Locking: output_lock to protect column state and space left
(note that the process_output*() functions take this lock themselves)
n_tty_poll() #
__poll_t poll method for N_TTY
Parameters
struct tty_struct *tty: terminal devicestruct file *file: file accessing itpoll_table *wait: poll table
Description
Called when the line discipline is asked to poll() for data or for special
events. This code is not serialized with respect to other events save
open/close.
This code must be sure never to sleep through a hangup.
Locking: called without the kernel lock held -- fine.