tty_ioctlのヘルプ・マニュアル
日本語 英語
tty_ioctl --help
man tty_ioctl
TTY_IOCTL(4) Linux Programmer’s Manual TTY_IOCTL(4)
名前
tty ioctl - 端末とシリアルラインの ioctl (入出力制御)
書式
#include
int ioctl(int fd, int cmd, ...);
説明
端末とシリアルポートについての ioctl() コールは、多くのコマンド引き数を
受け付ける。多くがいろいろな型の 3 番目の引き数を必要とする。ここ で は
argp または arg と呼ぶ。
ioctl を 使 用 す る と 移 植性のないプログラムになる。可能な場合は、
termios(3) に記述されている POSIX インタフェースを使うこと。
端末属性の取得と設定
TCGETS struct termios *argp
tcgetattr(fd, argp) と同じ。
現在のシリアルポートの設定を取得する。
TCSETS const struct termios *argp
tcsetattr(fd, TCSANOW, argp) と同じ。
現在のシリアルポートの設定を変更する。
TCSETSW const struct termios *argp
tcsetattr(fd, TCSADRAIN, argp) と同じ。
排出 (drain) を行うための出力バッファの使用を許可し、現在のシ リ
アルポートの設定を変更する。
TCSETSF const struct termios *argp
tcsetattr(fd, TCSAFLUSH, argp) と同じ。
排 出 (drain) を行うための出力バッファの使用を許可し、処理してい
ない入力を破棄して、現在のシリアルポートの設定を変更する。
以下の 4 つの ioctl は TCGETS, TCSETS, TCSETSW, TCSETSF と似ている。 た
だし、 struct termios * の代わりに struct termio * を取る。
TCGETA struct termio *argp
TCSETA const struct termio *argp
TCSETAW const struct termio *argp
TCSETAF const struct termio *argp
termios 構造体のロック
端 末 の termios 構造体はロックすることが可能である。このロック自体は
termios 構造体であり、 0 でないビットまたはフィールドはロックされた値を
示す。
TIOCGLCKTRMIOS struct termios *argp
端末の termios 構造体のロック状態を取得する。
TIOCSLCKTRMIOS const struct termios *argp
端 末の termios 構造体のロック状態を設定する。 root のみ (より正
確には、 CAP_SYS_ADMIN ケーパビリティを持つプロセス) がこれを 実
行できる。
ウィンドウサイズの取得と設定
ウ ィンドウサイズはカーネル内に保持されるが、カーネルによって使用されな
い (仮想コンソールの場合は例外であり、新しいフォントを読み込んだ場合 な
ど 、仮想端末のサイズが変更された場合、カーネルはウィンドウサイズを更新
する)。
以下の定数と構造体は で定義されている。
TIOCGWINSZ struct winsize *argp
ウィンドウサイズを取得する。
TIOCSWINSZ const struct winsize *argp
ウィンドウサイズを設定する。
これらの ioctl で使用される構造体は、以下のように定義される。
struct winsize {
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel; /* 未使用 */
unsigned short ws_ypixel; /* 未使用 */
};
ウィンドウサイズが変更された場合、フォアグラウンドプロセスグル ー プ に
SIGWINCH シグナルが送られる。
break の送信
TCSBRK int arg
tcsendbreak(fd, arg) と同じ。
端 末 が非同期シリアルデータ転送を使用しており、 arg が 0 の場合
、0.25 から 0.5 秒の間に break (0 のビット列のストリーム) が送信
される。端末が非同期シリアルデータ転送を使用している場合、 break
が送信されるか、この関数は何もせずに返る。 arg が 0 以外の場合、
何が起こるか分からない。
(SVr4, UnixWare, Solaris, Linux は、 tcsendbreak(fd,arg) の arg
が 0 以外の場合、 tcdrain(fd) のように扱う。 SunOS は arg を倍数
と して扱い、ビットのストリームを arg 回送信する。 arg が 0 の場
合も同じ。 DG/UX と AIX は、(0 以外の場合) arg をミリ秒単位の 時
間間隔として扱う。 HP-UX は arg を無視する。)
TCSBRKP int arg
い わ ゆ る 「POSIX 版」の TCSBRK である。これは 0 以外の arg を
1/10 秒単位の時間間隔として扱う。またドライバが break をサポート
していない場合は、何もしない。
TIOCSBRK void
break をオンにする。つまり 0 のビット列の送信を開始する。
TIOCCBRK void
break をオフにする。つまり 0 のビット列の送信を停止する。
ソフトウェアフロー制御
TCXONC int arg
tcflow(fd, arg) と同じ。
tcflow(3) の引き数 TCOOFF, TCOON, TCIOFF, TCION を参照すること。
バッファのカウントと書き出し (flush)
FIONREAD int *argp
入力バッファにあるバイト数を取得する。
TIOCINQ int *argp
FIONREAD と同じ。
TIOCOUTQ int *argp
出力バッファにあるバイト数を取得する。
TCFLSH int arg
tcflush(fd, arg) と同じ。
tcflush(3) の引き数 TCIFLUSH, TCOFLUSH, TCIOFLUSH を参照すること
。
入力の偽装
TIOCSTI const char *argp
指定されたバイトを入力キューに挿入する。
コンソール出力のリダイレクト
TIOCCONS void
/dev/console または /dev/tty0 に送られる出力を、指定された端末リ
ダイレクトする。指定された端末が疑似端末 (pseudo-terminal) の マ
スタの場合、出力はスレーブに送られる。バージョン 2.6.10 より前の
Linux では、出力がまだリダイレクトされていなければ、誰でもリダイ
レ ク ト を 行 う ことができる。バージョン 2.6.10 以降では、root
(CAP_SYS_ADMIN ケーパビリティを持つプロセス) だけがリダイレク ト
を 行 う こ とができる。出力がすでにリダイレクトされている場合は
EBUSY が返されるが、 /dev/console か /dev/tty0 を指してい る fd
に 対してこの ioctl を使用することで、リダイレクトを止めることが
できる。
端末の制御
TIOCSCTTY int arg
指定された端末を呼び出し元のプロセスの制御端末にする。呼び出し元
のプロセスはセッションリーダでなければならず、かつ既に制御端末を
持っていてはならない。この端末が既に他のセッショングループの制御
端末である場合、 ioctl は EPERM で失敗する。ただし呼び出したユー
ザが root で (より正確には CAP_SYS_ADMIN ケーパビリティを持っ て
い て) 、 かつ arg が 1 である場合を除く。この場合、端末は盗まれ
(stolen)、この端末を制御端末としていた全てのプロセスは端末を失う
。
TIOCNOTTY void
指定された端末が呼び出し元のプロセスの制御端末である場合、この制
御端末を放棄する。プロセスがセッションリーダの場合、フォアグラウ
ン ドプロセスグループに SIGHUP と SIGCONT を送り、カレントセッシ
ョンの全てのプロセスは制御端末を失う。
グループ ID とセッション ID の処理
TIOCGPGRP pid_t *argp
成功した場合、 *argp = tcgetpgrp(fd) と同じ。
この端末上のフォアグラウンドプロセスのプロセスグループ ID を取得
する。
TIOCSPGRP const pid_t *argp
tcsetpgrp(fd, *argp) と同じ。
この端末のフォアグラウンドプロセスのグループ ID を設定する。
TIOCGSID pid_t *argp
指定された端末のセッション ID を取得する。端末がマスタ疑似端末ま
たは制御端末でない場合は、 ENOTTY で失敗する。奇妙だ。
排他モード
TIOCEXCL void
端末を排他モードにする。端末に対して、これ以降の open(2) 操作 を
禁止する。 (root 以外の場合、つまり CAP_SYS_ADMIN ケーパビリティ
を持たないプロセスの場合、これ以降の open(2) は EBUSY で失敗する
。)
TIOCNXCL void
排他モードを無効にする。
ライン制御 (line discipline)
TIOCGETD int *argp
端末のライン制御の情報を取得する。
TIOCSETD const int *argp
端末のライン制御の情報を設定する。
疑似端末の ioctl
TIOCPKT const int *argp
パ ケットモードを有効 (*argp が 0 以外の場合) または無効にする。
疑似端末のマスタ側にのみ適用できる (それ以外の場合は ENOTTY を返
す) 。 パケットモードでは、その後に実行される read(2) は、値が 0
以外の 1 つの制御バイトを含むパケットか、値が 0 の 1 バ イ ト
('\0') に疑似端末のスレーブ側で書き込まれたデータが続くパケット
を返す。最初のバイトが TIOCPKT_DATA (0) でない場合、以下のビット
の 1 つ以上を OR したものである:
TIOCPKT_FLUSHREAD 端末の読み込みキューがフラッシュ (flush) される。
TIOCPKT_FLUSHWRITE 端末の書き出しキューがフラッシュされる。
TIOCPKT_STOP 端末への出力が停止される。
TIOCPKT_START 端末への出力が再開される。
TIOCPKT_DOSTOP 開始文字と終了文字が ^S/^Q である。
TIOCPKT_NOSTOP 開始文字と終了文字が ^S/^Q でない。
このモードが使われている場合、制御状態情報の存在がマスタ側から読
み込めるかは、例外的な条件で select(2) を使うことにより知るこ と
ができる。
こ のモードは rlogin(1) と rlogind(8) で使われ、リモートエコーの
リモートログインとローカルでの ^S/^Q フロー制御のリモートログ イ
ンを実装している。
BSD の ioctl である TIOCSTOP, TIOCSTART, TIOCUCNTL, TIOCREMOTE
は、Linux では実装されていない。
モデム制御
TIOCMGET int *argp
モデムビット列の状態を取得する。
TIOCMSET const int *argp
モデムビット列の状態を設定する。
TIOCMBIC const int *argp
指定されたモデムビット列をクリアする。
TIOCMBIS const int *argp
指定されたモデムビット列を設定する。
これらの 4 つの ioctl で使われるビットは以下の通り:
TIOCM_LE DSR (data set ready/line enable)
TIOCM_DTR DTR (data terminal ready)
TIOCM_RTS RTS (request to send)
TIOCM_ST Secondary TXD (transmit)
TIOCM_SR Secondary RXD (receive)
TIOCM_CTS CTS (clear to send)
TIOCM_CAR DCD (data carrier detect)
TIOCM_CD TIOCM_CAR を参照。
TIOCM_RNG RNG (ring)
TIOCM_RI TIOCM_RNG を参照。
TIOCM_DSR DSR (data set ready)
回線をローカルとしてマークする
TIOCGSOFTCAR int *argp
("ソフトウェアキャリアフラグの取得") termios 構造体の c_cflag フ
ィールドの CLOCAL フラグの状態を取得する。
TIOCSSOFTCAR const int *argp
(" ソ フ ト ウェアキャリアフラグの設定") *argp が 0 以外の場合、
termios 構造体の CLOCAL フラグを設定する。 0 の場合はクリアす る
。
ラ インの CLOCAL フラグがオフの場合、ハードウェアキャリア検出 (hardware
carrier detect, DCD) シグナルが重要であり、 O_NONBLOCK フラグが指定され
な い限り、対応する端末の open(2) は DCD が示されるまでブロックされる。
CLOCAL が設定されている場合、ラインは常に DCD が示されているかのよう に
動 作する。ソフトウェアキャリアフラグは、ローカルデバイスでは通常はオン
になっており、モデムのラインではオフになっている。
Linux 固有の ioctl
TIOCLINUX ioctl については、 console_ioctl(4) を参照すること。
カーネルデバッギング
#include
TIOCTTYGSTRUCT struct tty_struct *argp
fd に対応する tty_struct を取得する。
返り値
ioctl() システムコールは、成功した場合は 0 を返す。エラーの場合は -1 を
返し、 errno を適切に設定する。
エラー
EINVAL 不正なコマンド引き数である。
ENOIOCTLCMD
不明なコマンドである。
ENOTTY fd が不適切である。
EPERM 権限が不足している。
例
シリアルポートの DTR の状態をチェックする。
#include
#include
#include
int
main(void)
{
int fd, serial;
fd = open("/dev/ttyS0", O_RDONLY);
ioctl(fd, TIOCMGET, &serial);
if (serial & TIOCM_DTR)
puts("TIOCM_DTR が設定されていない。");
else
puts("TIOCM_DTR が設定されている。");
close(fd);
}
関連項目
ioctl(2), termios(3), console_ioctl(4), pty(7)
Linux 2008-10-29 TTY_IOCTL(4)
TTY_IOCTL(4) Linux Programmer’s Manual TTY_IOCTL(4)
NAME
tty ioctl - ioctls for terminals and serial lines
SYNOPSIS
#include
int ioctl(int fd, int cmd, ...);
DESCRIPTION
The ioctl() call for terminals and serial ports accepts many possible
command arguments. Most require a third argument, of varying type,
here called argp or arg.
Use of ioctl makes for non-portable programs. Use the POSIX interface
described in termios(3) whenever possible.
Get and Set Terminal Attributes
TCGETS struct termios *argp
Equivalent to tcgetattr(fd, argp).
Get the current serial port settings.
TCSETS const struct termios *argp
Equivalent to tcsetattr(fd, TCSANOW, argp).
Set the current serial port settings.
TCSETSW const struct termios *argp
Equivalent to tcsetattr(fd, TCSADRAIN, argp).
Allow the output buffer to drain, and set the current serial
port settings.
TCSETSF const struct termios *argp
Equivalent to tcsetattr(fd, TCSAFLUSH, argp).
Allow the output buffer to drain, discard pending input, and set
the current serial port settings.
The following four ioctls are just like TCGETS, TCSETS, TCSETSW,
TCSETSF, except that they take a struct termio * instead of a struct
termios *.
TCGETA struct termio *argp
TCSETA const struct termio *argp
TCSETAW const struct termio *argp
TCSETAF const struct termio *argp
Locking the termios structure
The termios structure of a terminal can be locked. The lock is itself
a termios structure, with non-zero bits or fields indicating a locked
value.
TIOCGLCKTRMIOS struct termios *argp
Gets the locking status of the termios structure of the termi-
nal.
TIOCSLCKTRMIOS const struct termios *argp
Sets the locking status of the termios structure of the termi-
nal. Only root (more precisely: a process with the
CAP_SYS_ADMIN capability) can do this.
Get and Set Window Size
Window sizes are kept in the kernel, but not used by the kernel (except
in the case of virtual consoles, where the kernel will update the
window size when the size of the virtual console changes, for example,
by loading a new font).
The following constants and structure are defined in .
TIOCGWINSZ struct winsize *argp
Get window size.
TIOCSWINSZ const struct winsize *argp
Set window size.
The struct used by these ioctls is defined as
struct winsize {
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel; /* unused */
unsigned short ws_ypixel; /* unused */
};
When the window size changes, a SIGWINCH signal is sent to the fore-
ground process group.
Sending a Break
TCSBRK int arg
Equivalent to tcsendbreak(fd, arg).
If the terminal is using asynchronous serial data transmission,
and arg is zero, then send a break (a stream of zero bits) for
between 0.25 and 0.5 seconds. If the terminal is not using
asynchronous serial data transmission, then either a break is
sent, or the function returns without doing anything. When arg
is non-zero, nobody knows what will happen.
(SVr4, UnixWare, Solaris, Linux treat tcsendbreak(fd,arg) with
non-zero arg like tcdrain(fd). SunOS treats arg as a multi-
plier, and sends a stream of bits arg times as long as done for
zero arg. DG/UX and AIX treat arg (when non-zero) as a time
interval measured in milliseconds. HP-UX ignores arg.)
TCSBRKP int arg
So-called "POSIX version" of TCSBRK. It treats non-zero arg as
a timeinterval measured in deciseconds, and does nothing when
the driver does not support breaks.
TIOCSBRK void
Turn break on, that is, start sending zero bits.
TIOCCBRK void
Turn break off, that is, stop sending zero bits.
Software flow control
TCXONC int arg
Equivalent to tcflow(fd, arg).
See tcflow(3) for the argument values TCOOFF, TCOON, TCIOFF,
TCION.
Buffer count and flushing
FIONREAD int *argp
Get the number of bytes in the input buffer.
TIOCINQ int *argp
Same as FIONREAD.
TIOCOUTQ int *argp
Get the number of bytes in the output buffer.
TCFLSH int arg
Equivalent to tcflush(fd, arg).
See tcflush(3) for the argument values TCIFLUSH, TCOFLUSH,
TCIOFLUSH.
Faking input
TIOCSTI const char *argp
Insert the given byte in the input queue.
Redirecting console output
TIOCCONS void
Redirect output that would have gone to /dev/console or
/dev/tty0 to the given terminal. If that was a pseudo-terminal
master, send it to the slave. In Linux before version 2.6.10,
anybody can do this as long as the output was not redirected
yet; since version 2.6.10, only root (a process with the
CAP_SYS_ADMIN capability) may do this. If output was redirected
already EBUSY is returned, but redirection can be stopped by
using this ioctl with fd pointing at /dev/console or /dev/tty0.
Controlling terminal
TIOCSCTTY int arg
Make the given terminal the controlling terminal of the calling
process. The calling process must be a session leader and not
have a controlling terminal already. If this terminal is
already the controlling terminal of a different session group
then the ioctl fails with EPERM, unless the caller is root (more
precisely: has the CAP_SYS_ADMIN capability) and arg equals 1,
in which case the terminal is stolen, and all processes that had
it as controlling terminal lose it.
TIOCNOTTY void
If the given terminal was the controlling terminal of the call-
ing process, give up this controlling terminal. If the process
was session leader, then send SIGHUP and SIGCONT to the fore-
ground process group and all processes in the current session
lose their controlling terminal.
Process group and session ID
TIOCGPGRP pid_t *argp
When successful, equivalent to *argp = tcgetpgrp(fd).
Get the process group ID of the foreground process group on this
terminal.
TIOCSPGRP const pid_t *argp
Equivalent to tcsetpgrp(fd, *argp).
Set the foreground process group ID of this terminal.
TIOCGSID pid_t *argp
Get the session ID of the given terminal. This will fail with
ENOTTY in case the terminal is not a master pseudo-terminal and
not our controlling terminal. Strange.
Exclusive mode
TIOCEXCL void
Put the terminal into exclusive mode. No further open(2) opera-
tions on the terminal are permitted. (They will fail with
EBUSY, except for root, that is, a process with the
CAP_SYS_ADMIN capability.)
TIOCNXCL void
Disable exclusive mode.
Line discipline
TIOCGETD int *argp
Get the line discipline of the terminal.
TIOCSETD const int *argp
Set the line discipline of the terminal.
Pseudo-terminal ioctls
TIOCPKT const int *argp
Enable (when *argp is non-zero) or disable packet mode. Can be
applied to the master side of a pseudo-terminal only (and will
return ENOTTY otherwise). In packet mode, each subsequent
read(2) will return a packet that either contains a single non-
zero control byte, or has a single byte containing zero (' ')
followed by data written on the slave side of the pseudo-termi-
nal. If the first byte is not TIOCPKT_DATA (0), it is an OR of
one or more of the following bits:
TIOCPKT_FLUSHREAD The read queue for the terminal is flushed.
TIOCPKT_FLUSHWRITE The write queue for the terminal is flushed.
TIOCPKT_STOP Output to the terminal is stopped.
TIOCPKT_START Output to the terminal is restarted.
TIOCPKT_DOSTOP The start and stop characters are ^S/^Q.
TIOCPKT_NOSTOP The start and stop characters are not ^S/^Q.
While this mode is in use, the presence of control status infor-
mation to be read from the master side may be detected by a
select(2) for exceptional conditions.
This mode is used by rlogin(1) and rlogind(8) to implement a
remote-echoed, locally ^S/^Q flow-controlled remote login.
The BSD ioctls TIOCSTOP, TIOCSTART, TIOCUCNTL, TIOCREMOTE have
not been implemented under Linux.
Modem control
TIOCMGET int *argp
get the status of modem bits.
TIOCMSET const int *argp
set the status of modem bits.
TIOCMBIC const int *argp
clear the indicated modem bits.
TIOCMBIS const int *argp
set the indicated modem bits.
Bits used by these four ioctls:
TIOCM_LE DSR (data set ready/line enable)
TIOCM_DTR DTR (data terminal ready)
TIOCM_RTS RTS (request to send)
TIOCM_ST Secondary TXD (transmit)
TIOCM_SR Secondary RXD (receive)
TIOCM_CTS CTS (clear to send)
TIOCM_CAR DCD (data carrier detect)
TIOCM_CD see TIOCM_CAR
TIOCM_RNG RNG (ring)
TIOCM_RI see TIOCM_RNG
TIOCM_DSR DSR (data set ready)
Marking a line as local
TIOCGSOFTCAR int *argp
("Get software carrier flag") Get the status of the CLOCAL flag
in the c_cflag field of the termios structure.
TIOCSSOFTCAR const int *argp
("Set software carrier flag") Set the CLOCAL flag in the termios
structure when *argp is non-zero, and clear it otherwise.
If the CLOCAL flag for a line is off, the hardware carrier detect (DCD)
signal is significant, and an open(2) of the corresponding terminal
will block until DCD is asserted, unless the O_NONBLOCK flag is given.
If CLOCAL is set, the line behaves as if DCD is always asserted. The
software carrier flag is usually turned on for local devices, and is
off for lines with modems.
Linux-specific
For the TIOCLINUX ioctl, see console_ioctl(4).
Kernel debugging
#include
TIOCTTYGSTRUCT struct tty_struct *argp
Get the tty_struct corresponding to fd.
RETURN VALUE
The ioctl() system call returns 0 on success. On error it returns -1
and sets errno appropriately.
ERRORS
EINVAL Invalid command parameter.
ENOIOCTLCMD
Unknown command.
ENOTTY Inappropriate fd.
EPERM Insufficient permission.
EXAMPLE
Check the condition of DTR on the serial port.
#include
#include
#include
int
main(void)
{
int fd, serial;
fd = open("/dev/ttyS0", O_RDONLY);
ioctl(fd, TIOCMGET, &serial);
if (serial & TIOCM_DTR)
puts("TIOCM_DTR is not set");
else
puts("TIOCM_DTR is set");
close(fd);
}
SEE ALSO
ioctl(2), termios(3), console_ioctl(4), pty(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-10-29 TTY_IOCTL(4)