What is tty port in linux

What do pty and tty mean?

I noticed many mentions of pty and tty in some open source projects, could someone tell me what do they mean and what is the difference between them?

One might want to read the pty(7) man page (like in this link), where pty/tty s are described as pre-created pairs of files representing master/slave end of a pseudo-terminal (see the description about BSD style).

In the past I wrote an article about the PTY in the Linux world : rkoucha.fr/tech_corner/pty_pdip.html

5 Answers 5

tty originally meant «teletype» and «pty» means «pseudo-teletype».

In UNIX, /dev/tty* is any device that acts like a «teletype», i.e: a terminal. (Called teletype because that’s what we had for terminals in those benighted days.)

A pty is a pseudotty, a device entry that acts like a terminal to the process reading and writing there, but is managed by something else. They first appeared (as I recall) for X Window and screen and the like, where you needed something that acted like a terminal but could be used from another program.

@CharlieMartin What do you mean the pty is something «that acted like a terminal?» Why would we want something like that? And how does another program make use of this pty? My guess is that the pty relays commands or something to the for-real terminal (tty) for the program. Is this correct? If not, ignore my guess and please answer the first part of my comment.

Think about a terminal as an object: it connects something on one end to stdin and stdout on the other. A real TTY connects to a physical terminal. a PTY connect to a program, eg, xterm, or a command window, or a shell window. It then lies to the program and says it really is so a terminal, honest. Before PTYs you connected programs like this with pipes, but pipes have significant differences, like no flow control. PTYs appeared to solve this.

@CharlieMartin «A real TTY connects to a physical terminal». What about the Linux Console? For example, in an Ubuntu system, there are Linux Consoles on Ctrl-Alt-F <1..6>and they are connected to /dev/tty <1..6>. The Linux Console is not a physical terminal, yet it is connected to a ttyN (not to a ptyN ). Am I missing something here?

Only that you pretty much can’t say anything definite about LINUX that won’t have a weird edge case. In this case the Linux console is a kernel feature that uses a /dev/tty dev entry but then gets connected to a bunch of things to get out to the user. en.wikipedia.org/wiki/Linux_console

A tty is a terminal (it stands for teletype — the original terminals used a line printer for output and a keyboard for input!). A terminal is a basically just a user interface device that uses text for input and output.

A pty is a pseudo-terminal — it’s a software implementation that appears to the attached program like a terminal, but instead of communicating directly with a «real» terminal, it transfers the input and output to another program.

For example, when you ssh in to a machine and run ls , the ls command is sending its output to a pseudo-terminal, the other side of which is attached to the SSH daemon.

Читайте также:  Резервное копирование баз 1с postgresql linux

Could you give a little bit more «precise» example of PTYs ? I still didn’t understand where they come in, and how they even are called. Thanks

@darth_coder: They’re created when an application requests one. That happens when you do things like open a new graphical terminal window or log in remotely.

tty: teletype. Usually refers to the serial ports of a computer, to which terminals were attached.

pty: pseudoteletype. Kernel provided pseudoserial port connected to programs emulating terminals, such as xterm, or screen.

If you run the mount command with no command-line arguments, which displays the file systems mounted on your system, you’ll notice a line that looks something like this:

none on /dev/pts type devpts (rw,gid=5,mode=620) 

This indicates that a special type of file system, devpts, is mounted at /dev/pts . This file system, which isn’t associated with any hardware device, is a “magic” file system that is created by the Linux kernel. It’s similar to the /proc file system

Like the /dev directory, /dev/pts contains entries corresponding to devices. But unlike /dev , which is an ordinary directory, /dev/pts is a special directory that is created dynamically by the Linux kernel. The contents of the directory vary with time and reflect the state of the running system. The entries in /dev/pts correspond to pseudo-terminals (or pseudo-TTYs, or PTYs).

Linux creates a PTY for every new terminal window you open and displays a corresponding entry in /dev/pts . The PTY device acts like a terminal device — it accepts input from the keyboard and displays text output from the programs that run in it. PTYs are numbered, and the PTY number is the name of the corresponding entry in /dev/pts .

For example, if the new terminal window’s PTY number is 7, invoke this command from another window:

echo ‘I am a virtual di ’ > /dev/pts/7 

The output appears in the new terminal window. You can try to exchange the 7 for another number, and, depending on the numbers given to your open terminals, you will see the output on another terminal window. /dev/pts is the bus (the post office) to do this!

Источник

TTY Port¶

The TTY drivers are advised to use struct tty_port helpers as much as possible. If the drivers implement tty_port.ops.activate() and tty_port.ops.shutdown() , they can use tty_port_open() , tty_port_close() , and tty_port_hangup() in respective tty_struct.ops hooks.

The reference and details are contained in the TTY Port Reference and TTY Port Operations Reference sections at the bottom.

TTY Port Functions¶

Init & Destroy¶

Description

Initializes the state of struct tty_port . When a port was initialized using this function, one has to destroy the port by tty_port_destroy() . Either indirectly by using tty_port refcounting ( tty_port_put() ) or directly if refcounting is not used.

Description

When a port was initialized using tty_port_init() , one has to destroy the port by this function. Either indirectly by using tty_port refcounting ( tty_port_put() ) or directly if refcounting is not used.

port to drop a reference of (can be NULL)

Description

The final put will destroy and free up the port using port->ops->destruct() hook, or using kfree() if not provided.

Open/Close/Hangup Helpers¶

internal helper to shutdown the device

Description

It is used by tty_port_hangup() and tty_port_close() . Its task is to shutdown the device if it was initialized (note consoles remain functioning). It lowers DTR/RTS (if tty has HUPCL set) and invokes port->ops->shutdown().

Читайте также:  Username для linux ubuntu

Description

Perform port level tty hangup flag and count changes. Drop the tty reference.

int tty_port_block_til_ready ( struct tty_port * port , struct tty_struct * tty , struct file * filp ) ¶

Waiting logic for tty open

the tty port being opened

the tty device being bound

the file pointer of the opener or NULL

Description

Implement the core POSIX/SuS tty behaviour when opening a tty device. Handles:

  • hangup (both before and during)
  • non blocking open
  • rts/dtr/dcd
  • signals
  • port flags and counts

The passed port must implement the port->ops->carrier_raised method if it can do carrier detect and the port->ops->dtr_rts method if it supports software management of these lines. Note that the dtr/rts raise is done each iteration as a hangup may have previously dropped them while we wait.

May drop and reacquire tty lock when blocking, so tty and port may have changed state (eg., may have been hung up).

helper for tty->ops->close, part 1/2

Description

Decrements and checks open count. Flushes the port if this is the last close. That means, dropping the data from the outpu buffer on the device and waiting for sending logic to finish. The rest of close handling is performed in tty_port_close_end() .

Locking: Caller holds tty lock.

1 if this is the last close, otherwise 0

helper for tty->ops->close, part 2/2

Description

This is a continuation of the first part: tty_port_close_start() . This should be called after turning off the device. It flushes the data from the line discipline and delays the close by port->close_delay.

Locking: Caller holds tty lock.

generic tty->ops->close handler

Description

It is a generic helper to be used in driver’s tty->ops->close. It wraps a sequence of tty_port_close_start() , tty_port_shutdown() , and tty_port_close_end() . The latter two are called only if this is the last close. See the respective functions for the details.

Locking: Caller holds tty lock

int tty_port_install ( struct tty_port * port , struct tty_driver * driver , struct tty_struct * tty ) ¶

generic tty->ops->install handler

tty_driver for this device

Description

It is the same as tty_standard_install() except the provided port is linked to a concrete tty specified by tty. Use this or tty_port_register_device() (or both). Call tty_port_link_device() as a last resort.

generic tty->ops->open handler

Description

It is a generic helper to be used in driver’s tty->ops->open. It activates the devices using port->ops->activate if not active already. And waits for the device to be ready using tty_port_block_til_ready() (e.g. raises DTR/CTS and waits for carrier).

Note that port->ops->shutdown is not called when port->ops->activate returns an error (on the contrary, tty->ops->close is).

Locking: Caller holds tty lock.

may drop and reacquire tty lock (in tty_port_block_til_ready() ) so tty and port may have changed state (eg., may be hung up now).

TTY Refcounting¶

Description

Return a refcount protected tty instance or NULL if the port is not associated with a tty (eg due to close or hangup).

Description

Associate the port and tty pair. Manages any internal refcounts. Pass NULL to deassociate a port.

TTY Helpers¶

hang only ttys with CLOCAL unset?

Modem Signals¶

Description

Wrapper for the carrier detect logic. For the moment this is used to hide some internal details. This will eventually become entirely internal to the tty port.

Читайте также:  Usb stick with linux

Description

Wrapper for the DTR/RTS raise logic. For the moment this is used to hide some internal details. This will eventually become entirely internal to the tty port.

Description

Wrapper for the DTR/RTS raise logic. For the moment this is used to hide some internal details. This will eventually become entirely internal to the tty port.

TTY Port Reference¶

buffer for this port, locked internally

back pointer to struct tty_struct , valid only if the tty is open. Use tty_port_tty_get() to obtain it (and tty_kref_put() to release).

internal back pointer to struct tty_struct . Avoid this. It should be eliminated in the long term.

tty port operations (like activate, shutdown), see struct tty_port_operations

tty port client operations (like receive_buf, write_wakeup). By default, tty_port_default_client_ops is used.

lock protecting tty

open waiters queue (waiting e.g. for a carrier)

modem status change queue (waiting for MSR changes)

internal flags ( TTY_PORT_ )

when set, the port is a console

locking, for open, shutdown and other port operations

xmit_buf alloc lock

optional xmit buffer used by some drivers

optional xmit buffer used by some drivers

delay in jiffies to wait when closing the port

delay in jiffies for output to be sent before closing

set to zero if no pure time based drain is needed else set to size of fifo

references counter. Reaching zero calls ops->destruct() if non- NULL or frees the port otherwise.

pointer to private data, for client_ops

Description

Each device keeps its own port level information. struct tty_port was introduced as a common structure for such information. As every TTY device shall have a backing tty_port structure, every driver can use these members.

The tty port has a different lifetime to the tty so must be kept apart. In addition be careful as tty -> port mappings are valid for the life of the tty object but in many cases port -> tty mappings are valid only until a hangup so don’t use the wrong path.

Tty port shall be initialized by tty_port_init() and shut down either by tty_port_destroy() (refcounting not used), or tty_port_put() (refcounting).

There is a lot of helpers around struct tty_port too. To name the most significant ones: tty_port_open() , tty_port_close() (or tty_port_close_start() and tty_port_close_end() separately if need be), and tty_port_hangup() . These call ops->activate() and ops->shutdown() as needed.

TTY Port Operations Reference¶

struct tty_port_operations < bool (*carrier_raised)(struct tty_port *port); void (*dtr_rts)(struct tty_port *port, bool active); void (*shutdown)(struct tty_port *port); int (*activate)(struct tty_port *port, struct tty_struct *tty); void (*destruct)(struct tty_port *port); >;

return true if the carrier is raised on port

raise the DTR line if active is true, otherwise lower DTR

called when the last close completes or a hangup finishes IFF the port was initialized. Do not use to free resources. Turn off the device only. Called under the port mutex to serialize against activate and shutdown.

called under the port mutex from tty_port_open() , serialized using the port mutex. Supposed to turn on the device.

FIXME: long term getting the tty argument out of this would be good for consoles.

called on the final put of a port. Free resources, possibly incl. the port itself.

Источник

Оцените статью
Adblock
detector