TTY 內部結構

Kopen

這些函式用於從核心空間開啟 TTY

void tty_kclose(struct tty_struct *tty)

關閉由 tty_kopen 開啟的 tty

引數

struct tty_struct *tty

tty 裝置

描述

執行釋放和釋放 tty 裝置的最後步驟。 它與 tty_release_struct() 相同,只是它還在 tty->port 上重置 TTY_PORT_KOPENED 標誌。

struct tty_struct *tty_kopen_exclusive(dev_t device)

為核心開啟 tty 裝置

引數

dev_t device

要開啟的裝置的 dev_t

描述

專門為核心開啟 tty。 執行驅動程式查詢,確保它尚未開啟並執行首次 tty 初始化。

宣告全域性 tty_mutex 以序列化
  • 併發首次 tty 初始化

  • 併發 tty 驅動程式刪除與查詢

  • 併發從驅動程式表中刪除 tty

返回值

鎖定的初始化 tty_struct

struct tty_struct *tty_kopen_shared(dev_t device)

為核心內共享使用開啟 tty 裝置

引數

dev_t device

要開啟的裝置的 dev_t

描述

為核心內使用開啟一個已經存在的 tty。 與上面的 tty_kopen_exclusive() 相比,它不確保是唯一使用者。

鎖定:與上面的 tty_kopen() 相同。


匯出的內部函式

int tty_dev_name_to_number(const char *name, dev_t *number)

返回裝置名稱的 dev_t

引數

const char *name

使用者空間中 /dev 下的裝置名稱

dev_t *number

指向此函式將填充的 dev_t 的指標

描述

此函式將裝置名稱(如 ttyS0 或 ttyUSB1)轉換為 dev_t(如 (4, 64) 或 (188, 1))。 如果未註冊相應的驅動程式,則該函式返回 -ENODEV

鎖定:這會獲取 tty_mutex 以保護 tty_drivers 列表免受

在我們遍歷它時被修改,並確保在退出前釋放它。

void tty_release_struct(struct tty_struct *tty, int idx)

釋放 tty 結構

引數

struct tty_struct *tty

tty 裝置

int idx

tty 的索引

描述

執行釋放和釋放 tty 裝置的最後步驟。 它大致是 tty_init_dev() 的逆過程。

int tty_get_icount(struct tty_struct *tty, struct serial_icounter_struct *icount)

獲取 tty 統計資訊

引數

struct tty_struct *tty

tty 裝置

struct serial_icounter_struct *icount

輸出引數

描述

獲取 tty 的 icount 統計資訊的副本。

鎖定:無(取決於驅動程式)


內部函式

void free_tty_struct(struct tty_struct *tty)

釋放不再使用的 tty

引數

struct tty_struct *tty

要釋放的 tty 結構

描述

釋放寫入緩衝區、tty 佇列和 tty 記憶體本身。

鎖定:無。 必須在 tty 確定未使用後呼叫

void tty_free_file(struct file *file)

釋放 file->private_data

引數

struct file *file

要釋放 private_data 的

描述

這應僅用於故障路徑處理,當 tty_add_file 尚未呼叫時。

struct tty_driver *get_tty_driver(dev_t device, int *index)

查詢 tty 的裝置

引數

dev_t device

裝置識別符號

int *index

返回 tty 的索引

描述

此例程返回 tty 驅動程式結構,給定裝置編號,並傳回索引號。

鎖定:呼叫方必須持有 tty_mutex

struct file *tty_release_redirect(struct tty_struct *tty)

如果存在,則釋放 pty 上的重定向

引數

struct tty_struct *tty

tty 裝置

描述

這適用於 pty 程式碼,因此如果主裝置關閉,如果從裝置是重定向,則它可以釋放重定向。

void __tty_hangup(struct tty_struct *tty, int exit_session)

結束通話事件的實際處理程式

引數

struct tty_struct *tty

tty 裝置

int exit_session

如果非零,則向所有前臺組程序傳送訊號

描述

這可以由“kworker”核心執行緒呼叫。 這是程序同步的,但不持有任何鎖,因此我們需要確保我們擁有執行操作的相應鎖。

結束通話事件會清除結束通話裝置上的任何掛起的重定向。 它確保未來的寫入將出錯,並且它執行所需的行規程結束通話和訊號傳遞。 tty 物件本身保持不變。

鎖定
  • BTM

  • 用於撤消重定向的重定向鎖

  • 用於操作 tty 列表的檔案列表鎖

  • 來自呼叫函式的 tty_ldiscs_lock

  • termios_rwsem 重置 termios 資料

  • tasklist_lock 用於遍歷結束通話事件的任務列表

  • ->siglock 用於保護 ->signal/->sighand

void tty_vhangup_self(void)

為自己的 ctty 執行程序 vhangup

引數

void

無引數

描述

對當前控制 tty 執行 vhangup

void tty_vhangup_session(struct tty_struct *tty)

結束通話會話領導者退出

引數

struct tty_struct *tty

要結束通話的 tty

描述

會話領導者正在退出並結束通話其控制終端。 前臺程序組中的每個程序都會收到 SIGHUP 訊號。

我們同步地執行此操作,以便當系統呼叫返回時,該程序已完成。 出於安全原因,此保證是必要的。

ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)

tty 裝置檔案的讀取方法

引數

struct kiocb *iocb

核心 I/O 控制塊

struct iov_iter *to

讀取資料的目標

描述

在此終端裝置上執行讀取系統呼叫函式。 在呼叫線路規程方法之前檢查結束通話的裝置。

鎖定

在需要時內部鎖定線路規程。 多個讀取呼叫可能會並行掛起。

void tty_write_message(struct tty_struct *tty, char *msg)

向某個 tty 寫入訊息,而不僅僅是控制檯。

引數

struct tty_struct *tty

目標 tty_struct

char *msg

要寫入的訊息

描述

這用於需要重定向到特定 tty 的訊息。 我們現在沒有將其放入 syslog 佇列,如果真的需要,可能會在將來放入。

目前我們仍然必須持有 BTM 並測試 CLOSING 標誌。

此函式已棄用,請勿在新程式碼中使用。

ssize_t tty_write(struct kiocb *iocb, struct iov_iter *from)

tty 裝置檔案的寫入方法

引數

struct kiocb *iocb

核心 I/O 控制塊

struct iov_iter *from

帶有要寫入資料的 iov_iter

描述

透過線路規程將資料寫入 tty 裝置。

鎖定

根據需要鎖定線路規程。 對 tty 驅動程式的寫入由 atomic_write_lock 序列化,然後以塊的形式處理到裝置。 對於每個裝置,不會並行呼叫線路規程寫入方法。

int tty_send_xchar(struct tty_struct *tty, u8 ch)

傳送優先順序字元

引數

struct tty_struct *tty

要傳送到的 tty

u8 ch

要傳送的 xchar

描述

即使已停止,也將高優先順序字元傳送到 tty。

鎖定:xchar 方法沒有鎖定,write 方法有寫入排序。

void pty_line_name(struct tty_driver *driver, int index, char *p)

生成 pty 的名稱

引數

struct tty_driver *driver

使用的 tty 驅動程式

int index

次要數字

char *p

至少 6 個位元組的輸出緩衝區

描述

driver 引用生成一個名稱並將其寫入輸出緩衝區 p

鎖定:無

ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)

生成 tty 的名稱

引數

struct tty_driver *driver

使用的 tty 驅動程式

int index

次要數字

char *p

至少 7 個位元組的輸出緩衝區

描述

driver 引用生成一個名稱並將其寫入輸出緩衝區 p

鎖定:無

struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, struct file *file, int idx)

查詢現有的 tty(如果有)

引數

struct tty_driver *driver

tty 的驅動程式

struct file *file

檔案物件

int idx

次要數字

返回值

tty(如果找到)。 如果未找到,則返回 NULLERR_PTR()(如果驅動程式的 lookup() 方法返回錯誤)。

描述

鎖定:必須持有 tty_mutex。 如果找到 tty,則增加 tty kref。

int tty_driver_install_tty(struct tty_driver *driver, struct tty_struct *tty)

在驅動程式中安裝 tty 條目

引數

struct tty_driver *driver

tty 的驅動程式

struct tty_struct *tty

tty

描述

將 tty 物件安裝到驅動程式表中。 tty->index 欄位將在呼叫此方法時設定。 此方法負責確保分配和配置任何需要的其他結構。

鎖定:目前為 tty_mutex

void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)

從驅動程式表中刪除 tty

引數

struct tty_driver *driver

tty 的驅動程式

struct tty_struct *tty

要刪除的 tty

描述

從驅動程式表中刪除 tty 物件。 tty->index 欄位將在呼叫此方法時設定。

鎖定:目前為 tty_mutex

int tty_reopen(struct tty_struct *tty)

快速重新開啟開啟的 tty

引數

struct tty_struct *tty

要開啟的 tty

描述

不允許在主 ptys 上重新開啟,並返回 -EIO

鎖定:呼叫方必須持有 tty_lock

返回值

成功時為 0,出錯時為 -errno。

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

初始化 tty 裝置

引數

struct tty_driver *driver

我們正在開啟裝置的 tty 驅動程式

int idx

裝置索引

描述

準備 tty 裝置。 這可能不是“新的”乾淨裝置,也可能是活動裝置。 pty 驅動程式需要特殊處理,因為這一點。

鎖定

該函式在 tty_mutex 下呼叫,這可以保護我們免受 tty 結構或驅動程式本身消失的影響。

在退出時,tty 裝置已連線線路規程,引用計數為 1。 如果為 pty/tty 用途建立了一個對,而另一個是對 pty 主裝置,則它也具有 1 的引用計數。

WSH 06/09/97:重寫以消除競爭並在開啟失敗後正確清理。 新程式碼使用互斥鎖保護開啟操作,因此非常簡單。 對於重新開啟 tty 的(最常見)情況,可以放寬互斥鎖鎖定。

返回值

新的 tty 結構

void tty_flush_works(struct tty_struct *tty)

重新整理 tty/pty 對的所有工作

引數

struct tty_struct *tty

要重新整理工作的 tty 裝置(或 pty 對的任何一端)

描述

同步重新整理屬於 tty(以及“另一個”tty)的所有工作。

void release_one_tty(struct work_struct *work)

釋放 tty 結構記憶體

引數

struct work_struct *work

我們正在刪除的 tty 的工作

描述

釋放與 tty 結構關聯的記憶體,並清除驅動程式表槽。 當裝置不再使用時,會呼叫此函式。 當裝置的設定失敗時,也會呼叫此函式。

鎖定

在處理驅動程式保留的 tty 列表時,會在內部獲取檔案列表鎖。

此方法從工作佇列中呼叫,以便驅動程式專用清理操作可以休眠(至少 USB 需要這樣做)

void release_tty(struct tty_struct *tty, int idx)

釋放 tty 結構記憶體

引數

struct tty_struct *tty

tty 裝置釋放

int idx

tty 裝置的索引釋放

描述

釋放 tty 和可能的連結夥伴(考慮 pty 對),並減少後備模組的引用計數。

鎖定

tty_mutex 在處理驅動程式保留的 tty 列表時,會在內部獲取檔案列表鎖。

int tty_release_checks(struct tty_struct *tty, int idx)

在真正釋放 tty 之前進行檢查

引數

struct tty_struct *tty

要檢查的 tty

int idx

tty 的索引

描述

在真正釋放 tty 之前執行一些偏執檢查。除非定義了 TTY_PARANOIA_CHECK,否則這是一個空操作。

int tty_release(struct inode *inode, struct file *filp)

關閉的 vfs 回撥

引數

struct inode *inode

tty 的 inode

struct file *filp

用於處理 tty 的檔案指標

描述

每次關閉引用此 tty 的檔案控制代碼時,都會最後一次呼叫此函式。但是,可能存在多個此類引用。

鎖定

獲取 BKL。請參閱 tty_release_dev()。

即使釋放 tty 結構也是一件棘手的事情。我們必須非常小心,確保所有結構同時釋放,否則中斷可能會得到錯誤的指標。

WSH 09/09/97:重寫以避免一些可能導致雙重釋放或釋放仍在使用的記憶體的糟糕的競爭條件。

struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)

獲取當前任務的鎖定 tty

引數

dev_t device

裝置號

struct file *filp

指向 tty 的檔案指標

返回值

僅當 device 是 /dev/tty 時,當前任務的鎖定 tty

描述

執行當前任務的控制 tty 的重新開啟。

我們無法像其他節點一樣返回驅動程式和索引,因為 devpts 將無法工作。它期望 inode 來自 devpts FS。

struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp, int *index)

查詢給定裝置檔案的 tty 驅動程式

引數

dev_t device

裝置號

struct file *filp

指向 tty 的檔案指標

int *index

return 驅動程式中裝置的索引

描述

如果返回值沒有錯誤,則呼叫者有責任透過 tty_driver_kref_put() 來減少引用計數。

鎖定:tty_mutex 保護 get_tty_driver()

返回值

此 inode 的驅動程式(引用計數增加)

struct tty_struct *tty_open_by_driver(dev_t device, struct file *filp)

開啟 tty 裝置

引數

dev_t device

要開啟的裝置的 dev_t

struct file *filp

指向 tty 的檔案指標

描述

執行驅動程式查詢,檢查重新開啟,或者執行首次 tty 初始化。

宣告全域性 tty_mutex 進行序列化
  • 併發首次 tty 初始化

  • 併發 tty 驅動程式刪除與查詢

  • 併發從驅動程式表中刪除 tty

返回值

鎖定初始化的或重新開啟的 tty_struct

int tty_open(struct inode *inode, struct file *filp)

開啟 tty 裝置

引數

struct inode *inode

裝置檔案的 inode

struct file *filp

指向 tty 的檔案指標

描述

tty_open()tty_release() 會保持 tty 計數,其中包含在 tty 上完成的開啟次數。我們不能使用 inode 計數,因為不同的 inode 可能指向同一個 tty。

pty 主裝置以及跟蹤序列線路需要開啟計數:當發生最後一次關閉時,DTR 會被丟棄。(現在不只是透過 tty->count 完成。 - Ted 1/27/92)

pty 的 termios 狀態在第一次開啟時被重置,以便設定不會跨重用而持續存在。

鎖定

注意

由於 tty_mutex,沒有引用的 tty_unlock/lock 情況才是安全的

__poll_t tty_poll(struct file *filp, poll_table *wait)

檢查 tty 狀態

引數

struct file *filp

正在輪詢的檔案

poll_table *wait

要更新的輪詢等待結構

描述

呼叫線路規程輪詢方法以獲取裝置的輪詢狀態。

鎖定:鎖定的線路規程,但其他呼叫者可以自由地重新進入 ldisc 輪詢方法。

int tiocsti(struct tty_struct *tty, u8 __user *p)

偽造輸入字元

引數

struct tty_struct *tty

偽造輸入到 tty 中

u8 __user *p

指向字元的指標

描述

偽造到 tty 裝置的輸入。執行必要的鎖定和輸入管理。

FIXME:不遵守流量控制?

鎖定
  • 呼叫的函式獲取 tty_ldiscs_lock

  • current->signal->tty 檢查在沒有鎖的情況下是安全的

int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)

實現視窗查詢 ioctl

引數

struct tty_struct *tty

tty

struct winsize __user *arg

用於結果的使用者緩衝區

描述

將視窗大小的核心概念複製到使用者緩衝區中。

鎖定:獲取 tty->winsize_mutex 以確保 winsize 資料的一致性。

int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)

實現視窗大小設定 ioctl

引數

struct tty_struct *tty

tty 側的 tty

struct winsize __user *arg

用於結果的使用者緩衝區

描述

將使用者對視窗大小的看法複製到核心。傳統上,這只是諮詢資訊,但對於 Linux 控制檯,它實際上具有驅動程式級別的含義並觸發 VC 大小調整。

鎖定

與驅動程式相關。預設的 do_resize 方法採用 tty termios 互斥鎖和 ctrl.lock。控制檯獲取自己的鎖,然後呼叫預設方法。

int tioccons(struct file *file)

允許管理員移動邏輯控制檯

引數

struct file *file

要成為控制檯的檔案

描述

允許管理員移動重定向的控制檯裝置。

鎖定:使用 redirect_lock 來保護重定向資訊

int tiocsetd(struct tty_struct *tty, int __user *p)

設定線路規程

引數

struct tty_struct *tty

tty 裝置

int __user *p

指向使用者資料的指標

描述

根據使用者請求設定線路規程。

鎖定:請參閱 tty_set_ldisc(),此函式只是一個輔助函式

int tiocgetd(struct tty_struct *tty, int __user *p)

獲取線路規程

引數

struct tty_struct *tty

tty 裝置

int __user *p

指向使用者資料的指標

描述

直接從 ldisc 中檢索線路規程 ID。

鎖定:等待 ldisc 引用(以防線路規程正在更改或 tty 正在結束通話)

int send_break(struct tty_struct *tty, unsigned int duration)

執行的定時中斷

引數

struct tty_struct *tty

要中斷的裝置

unsigned int duration

超時(毫秒)

描述

在缺乏自身驅動程式級別定時中斷功能的硬體上執行定時中斷。

鎖定

tty->atomic_write_lock 序列化

int tty_tiocmget(struct tty_struct *tty, int __user *p)

獲取調變解調器狀態

引數

struct tty_struct *tty

tty 裝置

int __user *p

指向結果的指標

描述

如果支援此功能,則從 tty 驅動程式獲取調變解調器狀態位。如果不可用,則返回 -ENOTTY

鎖定:無(取決於驅動程式)

int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, unsigned __user *p)

設定調變解調器狀態

引數

struct tty_struct *tty

tty 裝置

unsigned int cmd

命令 - 清除位、設定位或設定所有位

unsigned __user *p

指向所需位的指標

描述

如果支援此功能,則從 tty 驅動程式設定調變解調器狀態位。如果不可用,則返回 -ENOTTY

鎖定:無(取決於驅動程式)

struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)

分配新的 tty

引數

struct tty_driver *driver

將處理返回的 tty 的驅動程式

int idx

tty 的次要裝置號

描述

此子程式分配並初始化一個 tty 結構。

鎖定:無 - 此刻不會公開相關 tty