Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions | Variables
tty_io.c File Reference
#include <linux/types.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/devpts_fs.h>
#include <linux/file.h>
#include <linux/fdtable.h>
#include <linux/console.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/kd.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/seq_file.h>
#include <linux/serial.h>
#include <linux/ratelimit.h>
#include <linux/uaccess.h>
#include <linux/kbd_kern.h>
#include <linux/vt_kern.h>
#include <linux/selection.h>
#include <linux/kmod.h>
#include <linux/nsproxy.h>

Go to the source code of this file.

Macros

#define TTY_PARANOIA_CHECK   1
 
#define CHECK_TTY_COUNT   1
 
#define tty_compat_ioctl   NULL
 
#define TTY_NUMBER(tty)   ((tty)->index + (tty)->driver->name_base)
 

Functions

 EXPORT_SYMBOL (tty_std_termios)
 
 LIST_HEAD (tty_drivers)
 
 DEFINE_MUTEX (tty_mutex)
 
 EXPORT_SYMBOL (tty_mutex)
 
 DEFINE_SPINLOCK (tty_files_lock)
 
ssize_t redirected_tty_write (struct file *, const char __user *, size_t, loff_t *)
 
long tty_ioctl (struct file *file, unsigned int cmd, unsigned long arg)
 
struct tty_structalloc_tty_struct (void)
 
void free_tty_struct (struct tty_struct *tty)
 
int tty_alloc_file (struct file *file)
 
void tty_add_file (struct tty_struct *tty, struct file *file)
 
void tty_free_file (struct file *file)
 
void tty_del_file (struct file *file)
 
chartty_name (struct tty_struct *tty, char *buf)
 
 EXPORT_SYMBOL (tty_name)
 
int tty_paranoia_check (struct tty_struct *tty, struct inode *inode, const char *routine)
 
int tty_check_change (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_check_change)
 
void tty_wakeup (struct tty_struct *tty)
 
 EXPORT_SYMBOL_GPL (tty_wakeup)
 
void __tty_hangup (struct tty_struct *tty)
 
void tty_hangup (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_hangup)
 
void tty_vhangup (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_vhangup)
 
void tty_vhangup_self (void)
 
int tty_hung_up_p (struct file *filp)
 
 EXPORT_SYMBOL (tty_hung_up_p)
 
void disassociate_ctty (int on_exit)
 
void no_tty (void)
 
void stop_tty (struct tty_struct *tty)
 
 EXPORT_SYMBOL (stop_tty)
 
void start_tty (struct tty_struct *tty)
 
 EXPORT_SYMBOL (start_tty)
 
void tty_write_unlock (struct tty_struct *tty) __releases(&tty-> atomic_write_lock)
 
int tty_write_lock (struct tty_struct *tty, int ndelay) __acquires(&tty-> atomic_write_lock)
 
void tty_write_message (struct tty_struct *tty, char *msg)
 
int tty_init_termios (struct tty_struct *tty)
 
 EXPORT_SYMBOL_GPL (tty_init_termios)
 
int tty_standard_install (struct tty_driver *driver, struct tty_struct *tty)
 
 EXPORT_SYMBOL_GPL (tty_standard_install)
 
void tty_driver_remove_tty (struct tty_driver *driver, struct tty_struct *tty)
 
struct tty_structtty_init_dev (struct tty_driver *driver, int idx)
 
void tty_free_termios (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_free_termios)
 
void tty_kref_put (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_kref_put)
 
int tty_release (struct inode *inode, struct file *filp)
 
int tty_do_resize (struct tty_struct *tty, struct winsize *ws)
 
struct pidtty_get_pgrp (struct tty_struct *tty)
 
 EXPORT_SYMBOL_GPL (tty_get_pgrp)
 
struct tty_structtty_pair_get_tty (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_pair_get_tty)
 
struct tty_structtty_pair_get_pty (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_pair_get_pty)
 
void __do_SAK (struct tty_struct *tty)
 
void do_SAK (struct tty_struct *tty)
 
 EXPORT_SYMBOL (do_SAK)
 
void initialize_tty_struct (struct tty_struct *tty, struct tty_driver *driver, int idx)
 
void deinitialize_tty_struct (struct tty_struct *tty)
 
int tty_put_char (struct tty_struct *tty, unsigned char ch)
 
 EXPORT_SYMBOL_GPL (tty_put_char)
 
struct devicetty_register_device (struct tty_driver *driver, unsigned index, struct device *device)
 
 EXPORT_SYMBOL (tty_register_device)
 
struct devicetty_register_device_attr (struct tty_driver *driver, unsigned index, struct device *device, void *drvdata, const struct attribute_group **attr_grp)
 
 EXPORT_SYMBOL_GPL (tty_register_device_attr)
 
void tty_unregister_device (struct tty_driver *driver, unsigned index)
 
 EXPORT_SYMBOL (tty_unregister_device)
 
struct tty_driver__tty_alloc_driver (unsigned int lines, struct module *owner, unsigned long flags)
 
 EXPORT_SYMBOL (__tty_alloc_driver)
 
void tty_driver_kref_put (struct tty_driver *driver)
 
 EXPORT_SYMBOL (tty_driver_kref_put)
 
void tty_set_operations (struct tty_driver *driver, const struct tty_operations *op)
 
 EXPORT_SYMBOL (tty_set_operations)
 
void put_tty_driver (struct tty_driver *d)
 
 EXPORT_SYMBOL (put_tty_driver)
 
int tty_register_driver (struct tty_driver *driver)
 
 EXPORT_SYMBOL (tty_register_driver)
 
int tty_unregister_driver (struct tty_driver *driver)
 
 EXPORT_SYMBOL (tty_unregister_driver)
 
dev_t tty_devnum (struct tty_struct *tty)
 
 EXPORT_SYMBOL (tty_devnum)
 
void proc_clear_tty (struct task_struct *p)
 
struct tty_structget_current_tty (void)
 
 EXPORT_SYMBOL_GPL (get_current_tty)
 
void tty_default_fops (struct file_operations *fops)
 
void __init console_init (void)
 
 postcore_initcall (tty_class_init)
 
void console_sysfs_notify (void)
 
int __init tty_init (void)
 

Variables

struct ktermios tty_std_termios
 
struct classtty_class
 

Macro Definition Documentation

#define CHECK_TTY_COUNT   1

Definition at line 111 of file tty_io.c.

#define tty_compat_ioctl   NULL

Definition at line 151 of file tty_io.c.

#define TTY_NUMBER (   tty)    ((tty)->index + (tty)->driver->name_base)

Definition at line 251 of file tty_io.c.

#define TTY_PARANOIA_CHECK   1

Definition at line 110 of file tty_io.c.

Function Documentation

void __do_SAK ( struct tty_struct tty)

Definition at line 2835 of file tty_io.c.

struct tty_driver* __tty_alloc_driver ( unsigned int  lines,
struct module owner,
unsigned long  flags 
)
read

__tty_alloc_driver – allocate tty driver : count of lines this driver can handle at most : module which is repsonsible for this driver : some of TTY_DRIVER_* flags, will be set in driver->flags

This should not be called directly, some of the provided macros should be used instead. Use IS_ERR and friends on

Return values

Definition at line 3147 of file tty_io.c.

void __tty_hangup ( struct tty_struct tty)

__tty_hangup - actual handler for hangup events : tty device

This can be called by the "eventd" kernel thread. That is process synchronous but doesn't hold any locks, so we need to make sure we have the appropriate locks for what we're doing.

The hangup event clears any pending redirections onto the hung up device. It ensures future writes will error and it does the needed line discipline hangup and signal delivery. The tty object itself remains intact.

Locking: BTM redirect lock for undoing redirection file list lock for manipulating list of ttys tty_ldisc_lock from called functions termios_mutex resetting termios data tasklist_lock to walk task list for hangup event ->siglock to protect ->signal/->sighand

Definition at line 558 of file tty_io.c.

struct tty_struct* alloc_tty_struct ( void  )
read

alloc_tty_struct - allocate a tty object

Return a new empty tty structure. The data fields have not been initialized in any way but has been zeroed

Locking: none

Definition at line 168 of file tty_io.c.

void __init console_init ( void  )

Definition at line 3407 of file tty_io.c.

void console_sysfs_notify ( void  )

Definition at line 3480 of file tty_io.c.

DEFINE_MUTEX ( tty_mutex  )
DEFINE_SPINLOCK ( tty_files_lock  )
void deinitialize_tty_struct ( struct tty_struct tty)

deinitialize_tty_struct : tty to deinitialize

This subroutine deinitializes a tty structure that has been newly allocated but tty_release cannot be called on that yet.

Locking: none - tty in question must not be exposed at this point

Definition at line 2973 of file tty_io.c.

void disassociate_ctty ( int  on_exit)

disassociate_ctty - disconnect controlling tty : true if exiting so need to "hang up" the session

This function is typically called only by the session leader, when it wants to disassociate itself from its controlling tty.

It performs the following functions: (1) Sends a SIGHUP and SIGCONT to the foreground process group (2) Clears the tty from being controlling the session (3) Clears the controlling tty for all processes in the session group.

The argument on_exit is set to 1 if called when a process is exiting; it is 0 if called by the ioctl TIOCNOTTY.

Locking: BTM is taken for hysterical raisins, and held when called from no_tty(). tty_mutex is taken to protect tty ->siglock is taken to protect ->signal/->sighand tasklist_lock is taken to walk process list for sessions ->siglock is taken to protect ->signal/->sighand

Definition at line 792 of file tty_io.c.

void do_SAK ( struct tty_struct tty)

Definition at line 2898 of file tty_io.c.

EXPORT_SYMBOL ( tty_std_termios  )
EXPORT_SYMBOL ( tty_mutex  )
EXPORT_SYMBOL ( tty_name  )
EXPORT_SYMBOL ( tty_check_change  )
EXPORT_SYMBOL ( tty_hangup  )
EXPORT_SYMBOL ( tty_vhangup  )
EXPORT_SYMBOL ( tty_hung_up_p  )
EXPORT_SYMBOL ( stop_tty  )
EXPORT_SYMBOL ( start_tty  )
EXPORT_SYMBOL ( tty_free_termios  )
EXPORT_SYMBOL ( tty_kref_put  )
EXPORT_SYMBOL ( tty_pair_get_tty  )
EXPORT_SYMBOL ( tty_pair_get_pty  )
EXPORT_SYMBOL ( do_SAK  )
EXPORT_SYMBOL ( tty_register_device  )
EXPORT_SYMBOL ( tty_unregister_device  )
EXPORT_SYMBOL ( __tty_alloc_driver  )
EXPORT_SYMBOL ( tty_driver_kref_put  )
EXPORT_SYMBOL ( tty_set_operations  )
EXPORT_SYMBOL ( put_tty_driver  )
EXPORT_SYMBOL ( tty_register_driver  )
EXPORT_SYMBOL ( tty_unregister_driver  )
EXPORT_SYMBOL ( tty_devnum  )
EXPORT_SYMBOL_GPL ( tty_wakeup  )
EXPORT_SYMBOL_GPL ( tty_init_termios  )
EXPORT_SYMBOL_GPL ( tty_standard_install  )
EXPORT_SYMBOL_GPL ( tty_get_pgrp  )
EXPORT_SYMBOL_GPL ( tty_put_char  )
EXPORT_SYMBOL_GPL ( tty_register_device_attr  )
EXPORT_SYMBOL_GPL ( get_current_tty  )
void free_tty_struct ( struct tty_struct tty)

free_tty_struct - free a disused tty : tty struct to free

Free the write buffers, tty queue and tty memory itself.

Locking: none. Must be called after tty is definitely unused

Definition at line 182 of file tty_io.c.

struct tty_struct* get_current_tty ( void  )
read

Definition at line 3384 of file tty_io.c.

void initialize_tty_struct ( struct tty_struct tty,
struct tty_driver driver,
int  idx 
)

initialize_tty_struct : tty to initialize

This subroutine initializes a tty structure that has been newly allocated.

Locking: none - tty in question must not be exposed at this point

Definition at line 2931 of file tty_io.c.

LIST_HEAD ( tty_drivers  )
void no_tty ( void  )

no_tty - Ensure the current process does not have a controlling tty

Definition at line 859 of file tty_io.c.

postcore_initcall ( tty_class_init  )
void proc_clear_tty ( struct task_struct p)

Definition at line 3343 of file tty_io.c.

void put_tty_driver ( struct tty_driver d)

Definition at line 3249 of file tty_io.c.

ssize_t redirected_tty_write ( struct file file,
const char __user buf,
size_t  count,
loff_t *  ppos 
)

Definition at line 1163 of file tty_io.c.

void start_tty ( struct tty_struct tty)

start_tty - propagate flow control : tty to start

Start a tty that has been stopped if at all possible. Perform any necessary wakeups and propagate the TIOCPKT status. If this is the tty was previous stopped and is being started then the driver start method is invoked and the line discipline woken.

Locking: ctrl_lock

Definition at line 922 of file tty_io.c.

void stop_tty ( struct tty_struct tty)

stop_tty - propagate flow control : tty to stop

Perform flow control to the driver. For PTY/TTY pairs we must also propagate the TIOCKPKT status. May be called on an already stopped device and will not re-call the driver method.

This functionality is used by both the line disciplines for halting incoming flow and by the driver. It may therefore be called from any context, may be under the tty atomic_write_lock but not always.

Locking: Uses the tty control lock internally

Definition at line 888 of file tty_io.c.

void tty_add_file ( struct tty_struct tty,
struct file file 
)

Definition at line 213 of file tty_io.c.

int tty_alloc_file ( struct file file)

Definition at line 199 of file tty_io.c.

int tty_check_change ( struct tty_struct tty)

tty_check_change - check for POSIX terminal changes : tty to check

If we try to write to, or set the state of, a terminal and we're not in the foreground, send a SIGTTOU. If the signal is blocked or ignored, go ahead and perform the operation. (POSIX 7.2)

Locking: ctrl_lock

Definition at line 408 of file tty_io.c.

void tty_default_fops ( struct file_operations fops)

Definition at line 3396 of file tty_io.c.

void tty_del_file ( struct file file)

Definition at line 240 of file tty_io.c.

dev_t tty_devnum ( struct tty_struct tty)

Definition at line 3337 of file tty_io.c.

int tty_do_resize ( struct tty_struct tty,
struct winsize ws 
)

tty_do_resize - resize event : tty being resized : rows (character) : cols (character)

Update the termios variables and send the necessary signals to peform a terminal resize correctly

Definition at line 2185 of file tty_io.c.

void tty_driver_kref_put ( struct tty_driver driver)

Definition at line 3236 of file tty_io.c.

void tty_driver_remove_tty ( struct tty_driver driver,
struct tty_struct tty 
)

tty_driver_remove_tty() - remove a tty from the driver tables : the driver for the tty : the minor number

Remvoe a tty object from the driver tables. The tty->index field will be set by the time this is called.

Locking: tty_mutex for now

Definition at line 1315 of file tty_io.c.

void tty_free_file ( struct file file)

tty_free_file - free file->private_data

This shall be used only for fail path handling when tty_add_file was not called yet.

Definition at line 231 of file tty_io.c.

void tty_free_termios ( struct tty_struct tty)

Definition at line 1448 of file tty_io.c.

struct pid* tty_get_pgrp ( struct tty_struct tty)
read

tty_get_pgrp - return a ref counted pgrp pid : tty to read

Returns a refcounted instance of the pid struct for the process group controlling the tty.

Definition at line 2359 of file tty_io.c.

void tty_hangup ( struct tty_struct tty)

tty_hangup - trigger a hangup event : tty to hangup

A carrier loss (virtual or otherwise) has occurred on this like schedule a hangup sequence to run after this event.

Definition at line 694 of file tty_io.c.

int tty_hung_up_p ( struct file filp)

tty_hung_up_p - was tty hung up : file pointer of tty

Return true if the tty has been subject to a vhangup or a carrier loss

Definition at line 752 of file tty_io.c.

int __init tty_init ( void  )

Definition at line 3490 of file tty_io.c.

struct tty_struct* tty_init_dev ( struct tty_driver driver,
int  idx 
)
read

tty_init_dev - initialise a tty device : tty driver we are opening a device on : device index : returned tty structure

Prepare a tty device. This may not be a "new" clean device but could also be an active device. The pty drivers require special handling because of this.

Locking: The function is called under the tty_mutex, which protects us from the tty struct or driver itself going away.

On exit the tty device has the line discipline attached and a reference count of 1. If a pair was created for pty/tty use and the other was a pty master then it too has a reference count of 1.

WSH 06/09/97: Rewritten to remove races and properly clean up after a failed open. The new code protects the open with a mutex, so it's really quite straightforward. The mutex locking can probably be relaxed for the (most common) case of reopening a tty.

Definition at line 1385 of file tty_io.c.

int tty_init_termios ( struct tty_struct tty)

tty_init_termios - helper for termios setup : the tty to set up

Initialise the termios structures for this tty. Thus runs under the tty_mutex currently so we can be relaxed about ordering.

Definition at line 1251 of file tty_io.c.

long tty_ioctl ( struct file file,
unsigned int  cmd,
unsigned long  arg 
)

Definition at line 2639 of file tty_io.c.

void tty_kref_put ( struct tty_struct tty)

tty_kref_put - release a tty kref : tty device

Release a reference to a tty device and if need be let the kref layer destruct the object for us

Definition at line 1527 of file tty_io.c.

char* tty_name ( struct tty_struct tty,
char buf 
)

tty_name - return tty naming : tty structure : buffer for output

Convert a tty structure into a name. The name reflects the kernel naming policy and if udev is in use may not reflect user space

Locking: none

Definition at line 264 of file tty_io.c.

struct tty_struct* tty_pair_get_pty ( struct tty_struct tty)
read

Definition at line 2627 of file tty_io.c.

struct tty_struct* tty_pair_get_tty ( struct tty_struct tty)
read

Definition at line 2618 of file tty_io.c.

int tty_paranoia_check ( struct tty_struct tty,
struct inode inode,
const char routine 
)

Definition at line 275 of file tty_io.c.

int tty_put_char ( struct tty_struct tty,
unsigned char  ch 
)

tty_put_char - write one character to a tty : tty : character

Write one byte to the tty using the provided put_char method if present. Returns the number of characters successfully output.

Note: the specific put_char operation in the driver layer may go away soon. Don't call it directly, use this method

Definition at line 2990 of file tty_io.c.

struct device* tty_register_device ( struct tty_driver driver,
unsigned  index,
struct device device 
)
read

tty_register_device - register a tty device : the tty driver that describes the tty device : the index in the tty driver for this tty device : a struct device that is associated with this tty device. This field is optional, if there is no known struct device for this tty device it can be set to NULL safely.

Returns a pointer to the struct device for this tty device (or ERR_PTR(-EFOO) on error).

This call is required to be made to register an individual tty device if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If that bit is not set, this function should not be called by a tty driver.

Locking: ??

Definition at line 3028 of file tty_io.c.

struct device* tty_register_device_attr ( struct tty_driver driver,
unsigned  index,
struct device device,
void drvdata,
const struct attribute_group **  attr_grp 
)
read

tty_register_device_attr - register a tty device : the tty driver that describes the tty device : the index in the tty driver for this tty device : a struct device that is associated with this tty device. This field is optional, if there is no known struct device for this tty device it can be set to NULL safely. : Driver data to be set to device. : Attribute group to be set on device.

Returns a pointer to the struct device for this tty device (or ERR_PTR(-EFOO) on error).

This call is required to be made to register an individual tty device if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If that bit is not set, this function should not be called by a tty driver.

Locking: ??

Definition at line 3061 of file tty_io.c.

int tty_register_driver ( struct tty_driver driver)

Definition at line 3258 of file tty_io.c.

int tty_release ( struct inode inode,
struct file filp 
)

tty_release - vfs callback for close : inode of tty : file pointer for handle to tty

Called the last time each file handle is closed that references this tty. There may however be several such references.

Locking: Takes bkl. See tty_release_dev

Even releasing the tty structures is a tricky business.. We have to be very careful that the structures are all released at the same time, as interrupts might otherwise get the wrong pointers.

WSH 09/09/97: rewritten to avoid some nasty race conditions that could lead to double frees or releasing memory still in use.

Definition at line 1623 of file tty_io.c.

void tty_set_operations ( struct tty_driver driver,
const struct tty_operations op 
)

Definition at line 3242 of file tty_io.c.

int tty_standard_install ( struct tty_driver driver,
struct tty_struct tty 
)

Definition at line 1273 of file tty_io.c.

void tty_unregister_device ( struct tty_driver driver,
unsigned  index 
)

tty_unregister_device - unregister a tty device : the tty driver that describes the tty device : the index in the tty driver for this tty device

If a tty device is registered with a call to tty_register_device() then this function must be called when the tty device is gone.

Locking: ??

Definition at line 3129 of file tty_io.c.

int tty_unregister_driver ( struct tty_driver driver)

Definition at line 3320 of file tty_io.c.

void tty_vhangup ( struct tty_struct tty)

tty_vhangup - process vhangup : tty to hangup

The user has asked via system call for the terminal to be hung up. We do this synchronously so that when the syscall returns the process is complete. That guarantee is necessary for security reasons.

Definition at line 714 of file tty_io.c.

void tty_vhangup_self ( void  )

tty_vhangup_self - process vhangup for own ctty

Perform a vhangup on the current controlling tty

Definition at line 733 of file tty_io.c.

void tty_wakeup ( struct tty_struct tty)

tty_wakeup - request more data : terminal

Internal and external helper for wakeups of tty. This function informs the line discipline if present that the driver is ready to receive more output data.

Definition at line 519 of file tty_io.c.

int tty_write_lock ( struct tty_struct tty,
int  ndelay 
) -> atomic_write_lock)

Definition at line 993 of file tty_io.c.

void tty_write_message ( struct tty_struct tty,
char msg 
)

tty_write_message - write a message to a certain tty, not just the console. : the destination tty_struct : the message to write

This is used for messages that need to be redirected to a specific tty. We don't put it into the syslog queue right now maybe in the future if really needed.

We must still hold the BTM and test the CLOSING flag for the moment.

Definition at line 1105 of file tty_io.c.

void tty_write_unlock ( struct tty_struct tty) -> atomic_write_lock)

Definition at line 986 of file tty_io.c.

Variable Documentation

struct class* tty_class

Definition at line 2998 of file tty_io.c.

struct ktermios tty_std_termios
Initial value:
= {
.c_iflag = ICRNL | IXON,
.c_oflag = OPOST | ONLCR,
.c_cflag = B38400 | CS8 | CREAD | HUPCL,
.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
.c_cc = INIT_C_CC,
.c_ispeed = 38400,
.c_ospeed = 38400
}

Definition at line 113 of file tty_io.c.