1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/major.h>
7 #include <linux/termios.h>
8 #include <linux/workqueue.h>
9 #include <linux/tty_buffer.h>
10 #include <linux/tty_driver.h>
11 #include <linux/tty_ldisc.h>
12 #include <linux/tty_port.h>
13 #include <linux/mutex.h>
14 #include <linux/tty_flags.h>
15 #include <uapi/linux/tty.h>
16 #include <linux/rwsem.h>
17 #include <linux/llist.h>
21 * (Note: the *_driver.minor_start values 1, 64, 128, 192 are
22 * hardcoded at present.)
24 #define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */
25 #define NR_UNIX98_PTY_RESERVE 1024 /* Default reserve for main devpts */
26 #define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */
29 * This character is the same as _POSIX_VDISABLE: it cannot be used as
30 * a c_cc[] character, but indicates that a particular special character
31 * isn't in use (eg VINTR has no character etc)
33 #define __DISABLED_CHAR '\0'
35 #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR])
36 #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT])
37 #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE])
38 #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL])
39 #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF])
40 #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME])
41 #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN])
42 #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC])
43 #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART])
44 #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP])
45 #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP])
46 #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL])
47 #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT])
48 #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD])
49 #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE])
50 #define LNEXT_CHAR(tty) ((tty)->termios.c_cc[VLNEXT])
51 #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2])
53 #define _I_FLAG(tty, f) ((tty)->termios.c_iflag & (f))
54 #define _O_FLAG(tty, f) ((tty)->termios.c_oflag & (f))
55 #define _C_FLAG(tty, f) ((tty)->termios.c_cflag & (f))
56 #define _L_FLAG(tty, f) ((tty)->termios.c_lflag & (f))
58 #define I_IGNBRK(tty) _I_FLAG((tty), IGNBRK)
59 #define I_BRKINT(tty) _I_FLAG((tty), BRKINT)
60 #define I_IGNPAR(tty) _I_FLAG((tty), IGNPAR)
61 #define I_PARMRK(tty) _I_FLAG((tty), PARMRK)
62 #define I_INPCK(tty) _I_FLAG((tty), INPCK)
63 #define I_ISTRIP(tty) _I_FLAG((tty), ISTRIP)
64 #define I_INLCR(tty) _I_FLAG((tty), INLCR)
65 #define I_IGNCR(tty) _I_FLAG((tty), IGNCR)
66 #define I_ICRNL(tty) _I_FLAG((tty), ICRNL)
67 #define I_IUCLC(tty) _I_FLAG((tty), IUCLC)
68 #define I_IXON(tty) _I_FLAG((tty), IXON)
69 #define I_IXANY(tty) _I_FLAG((tty), IXANY)
70 #define I_IXOFF(tty) _I_FLAG((tty), IXOFF)
71 #define I_IMAXBEL(tty) _I_FLAG((tty), IMAXBEL)
72 #define I_IUTF8(tty) _I_FLAG((tty), IUTF8)
74 #define O_OPOST(tty) _O_FLAG((tty), OPOST)
75 #define O_OLCUC(tty) _O_FLAG((tty), OLCUC)
76 #define O_ONLCR(tty) _O_FLAG((tty), ONLCR)
77 #define O_OCRNL(tty) _O_FLAG((tty), OCRNL)
78 #define O_ONOCR(tty) _O_FLAG((tty), ONOCR)
79 #define O_ONLRET(tty) _O_FLAG((tty), ONLRET)
80 #define O_OFILL(tty) _O_FLAG((tty), OFILL)
81 #define O_OFDEL(tty) _O_FLAG((tty), OFDEL)
82 #define O_NLDLY(tty) _O_FLAG((tty), NLDLY)
83 #define O_CRDLY(tty) _O_FLAG((tty), CRDLY)
84 #define O_TABDLY(tty) _O_FLAG((tty), TABDLY)
85 #define O_BSDLY(tty) _O_FLAG((tty), BSDLY)
86 #define O_VTDLY(tty) _O_FLAG((tty), VTDLY)
87 #define O_FFDLY(tty) _O_FLAG((tty), FFDLY)
89 #define C_BAUD(tty) _C_FLAG((tty), CBAUD)
90 #define C_CSIZE(tty) _C_FLAG((tty), CSIZE)
91 #define C_CSTOPB(tty) _C_FLAG((tty), CSTOPB)
92 #define C_CREAD(tty) _C_FLAG((tty), CREAD)
93 #define C_PARENB(tty) _C_FLAG((tty), PARENB)
94 #define C_PARODD(tty) _C_FLAG((tty), PARODD)
95 #define C_HUPCL(tty) _C_FLAG((tty), HUPCL)
96 #define C_CLOCAL(tty) _C_FLAG((tty), CLOCAL)
97 #define C_CIBAUD(tty) _C_FLAG((tty), CIBAUD)
98 #define C_CRTSCTS(tty) _C_FLAG((tty), CRTSCTS)
99 #define C_CMSPAR(tty) _C_FLAG((tty), CMSPAR)
101 #define L_ISIG(tty) _L_FLAG((tty), ISIG)
102 #define L_ICANON(tty) _L_FLAG((tty), ICANON)
103 #define L_XCASE(tty) _L_FLAG((tty), XCASE)
104 #define L_ECHO(tty) _L_FLAG((tty), ECHO)
105 #define L_ECHOE(tty) _L_FLAG((tty), ECHOE)
106 #define L_ECHOK(tty) _L_FLAG((tty), ECHOK)
107 #define L_ECHONL(tty) _L_FLAG((tty), ECHONL)
108 #define L_NOFLSH(tty) _L_FLAG((tty), NOFLSH)
109 #define L_TOSTOP(tty) _L_FLAG((tty), TOSTOP)
110 #define L_ECHOCTL(tty) _L_FLAG((tty), ECHOCTL)
111 #define L_ECHOPRT(tty) _L_FLAG((tty), ECHOPRT)
112 #define L_ECHOKE(tty) _L_FLAG((tty), ECHOKE)
113 #define L_FLUSHO(tty) _L_FLAG((tty), FLUSHO)
114 #define L_PENDIN(tty) _L_FLAG((tty), PENDIN)
115 #define L_IEXTEN(tty) _L_FLAG((tty), IEXTEN)
116 #define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC)
119 struct signal_struct;
120 struct tty_operations;
123 * struct tty_struct - state associated with a tty while open
125 * @magic: magic value set early in @alloc_tty_struct to %TTY_MAGIC, for
127 * @kref: reference counting by tty_kref_get() and tty_kref_put(), reaching zero
128 * frees the structure
129 * @dev: class device or %NULL (e.g. ptys, serdev)
130 * @driver: &struct tty_driver operating this tty
131 * @ops: &struct tty_operations of @driver for this tty (open, close, etc.)
132 * @index: index of this tty (e.g. to construct @name like tty12)
133 * @ldisc_sem: protects line discipline changes (@ldisc) -- lock tty not pty
134 * @ldisc: the current line discipline for this tty (n_tty by default)
135 * @atomic_write_lock: protects against concurrent writers, i.e. locks
136 * @write_cnt, @write_buf and similar
137 * @legacy_mutex: leftover from history (BKL -> BTM -> @legacy_mutex),
138 * protecting several operations on this tty
139 * @throttle_mutex: protects against concurrent tty_throttle_safe() and
140 * tty_unthrottle_safe() (but not tty_unthrottle())
141 * @termios_rwsem: protects @termios and @termios_locked
142 * @winsize_mutex: protects @winsize
143 * @termios: termios for the current tty, copied from/to @driver.termios
144 * @termios_locked: locked termios (by %TIOCGLCKTRMIOS and %TIOCSLCKTRMIOS
146 * @name: name of the tty constructed by tty_line_name() (e.g. ttyS3)
147 * @flags: bitwise OR of %TTY_THROTTLED, %TTY_IO_ERROR, ...
148 * @count: count of open processes, reaching zero cancels all the work for
149 * this tty and drops a @kref too (but does not free this tty)
150 * @winsize: size of the terminal "window" (cf. @winsize_mutex)
151 * @flow: flow settings grouped together, see also @flow.unused
152 * @flow.lock: lock for @flow members
153 * @flow.stopped: tty stopped/started by stop_tty()/start_tty()
154 * @flow.tco_stopped: tty stopped/started by %TCOOFF/%TCOON ioctls (it has
155 * precedence over @flow.stopped)
156 * @flow.unused: alignment for Alpha, so that no members other than @flow.* are
157 * modified by the same 64b word store. The @flow's __aligned is
158 * there for the very same reason.
159 * @ctrl: control settings grouped together, see also @ctrl.unused
160 * @ctrl.lock: lock for @ctrl members
161 * @ctrl.pgrp: process group of this tty (setpgrp(2))
162 * @ctrl.session: session of this tty (setsid(2)). Writes are protected by both
163 * @ctrl.lock and @legacy_mutex, readers must use at least one of
165 * @ctrl.pktstatus: packet mode status (bitwise OR of %TIOCPKT_ constants)
166 * @ctrl.packet: packet mode enabled
167 * @ctrl.unused: alignment for Alpha, see @flow.unused for explanation
168 * @hw_stopped: not controlled by the tty layer, under @driver's control for CTS
170 * @receive_room: bytes permitted to feed to @ldisc without any being lost
171 * @flow_change: controls behavior of throttling, see tty_throttle_safe() and
172 * tty_unthrottle_safe()
173 * @link: link to another pty (master -> slave and vice versa)
174 * @fasync: state for %O_ASYNC (for %SIGIO); managed by fasync_helper()
175 * @write_wait: concurrent writers are waiting in this queue until they are
177 * @read_wait: readers wait for data in this queue
178 * @hangup_work: normally a work to perform a hangup (do_tty_hangup()); while
179 * freeing the tty, (re)used to release_one_tty()
180 * @disc_data: pointer to @ldisc's private data (e.g. to &struct n_tty_data)
181 * @driver_data: pointer to @driver's private data (e.g. &struct uart_state)
182 * @files_lock: protects @tty_files list
183 * @tty_files: list of (re)openers of this tty (i.e. linked &struct
185 * @closing: when set during close, n_tty processes only START & STOP chars
186 * @write_buf: temporary buffer used during tty_write() to copy user data to
187 * @write_cnt: count of bytes written in tty_write() to @write_buf
188 * @SAK_work: if the tty has a pending do_SAK, it is queued here
189 * @port: persistent storage for this device (i.e. &struct tty_port)
191 * All of the state associated with a tty while the tty is open. Persistent
192 * storage for tty devices is referenced here as @port and is documented in
199 struct tty_driver *driver;
200 const struct tty_operations *ops;
203 struct ld_semaphore ldisc_sem;
204 struct tty_ldisc *ldisc;
206 struct mutex atomic_write_lock;
207 struct mutex legacy_mutex;
208 struct mutex throttle_mutex;
209 struct rw_semaphore termios_rwsem;
210 struct mutex winsize_mutex;
211 struct ktermios termios, termios_locked;
215 struct winsize winsize;
221 unsigned long unused[0];
222 } __aligned(sizeof(unsigned long)) flow;
228 unsigned char pktstatus;
230 unsigned long unused[0];
231 } __aligned(sizeof(unsigned long)) ctrl;
234 unsigned int receive_room;
237 struct tty_struct *link;
238 struct fasync_struct *fasync;
239 wait_queue_head_t write_wait;
240 wait_queue_head_t read_wait;
241 struct work_struct hangup_work;
244 spinlock_t files_lock;
245 struct list_head tty_files;
247 #define N_TTY_BUF_SIZE 4096
250 unsigned char *write_buf;
252 struct work_struct SAK_work;
253 struct tty_port *port;
254 } __randomize_layout;
256 /* Each of a tty's open files has private_data pointing to tty_file_private */
257 struct tty_file_private {
258 struct tty_struct *tty;
260 struct list_head list;
263 /* tty magic number */
264 #define TTY_MAGIC 0x5401
267 * DOC: TTY Struct Flags
269 * These bits are used in the :c:member:`tty_struct.flags` field.
271 * So that interrupts won't be able to mess up the queues,
272 * copy_to_cooked must be atomic with respect to itself, as must
273 * tty->write. Thus, you must use the inline functions set_bit() and
274 * clear_bit() to make things atomic.
277 * Driver input is throttled. The ldisc should call
278 * :c:member:`tty_driver.unthrottle()` in order to resume reception when
279 * it is ready to process more data (at threshold min).
282 * If set, causes all subsequent userspace read/write calls on the tty to
283 * fail, returning -%EIO. (May be no ldisc too.)
286 * Device is a pty and the other side has closed.
289 * Exclusive open mode (a single opener).
291 * TTY_DO_WRITE_WAKEUP
292 * If set, causes the driver to call the
293 * :c:member:`tty_ldisc_ops.write_wakeup()` method in order to resume
294 * transmission when it can accept more data to transmit.
297 * Indicates that a line discipline is open. For debugging purposes only.
300 * A flag private to pty code to implement %TIOCSPTLCK/%TIOCGPTLCK logic.
303 * Prevent driver from splitting up writes into smaller chunks (preserve
304 * write boundaries to driver).
307 * The TTY was hung up. This is set post :c:member:`tty_driver.hangup()`.
310 * The TTY is in the process of hanging up to abort potential readers.
313 * Line discipline for this TTY is being changed. I/O should not block
314 * when this is set. Use tty_io_nonblock() to check.
317 * Line discipline for this TTY was stopped. No work should be queued to
320 #define TTY_THROTTLED 0
321 #define TTY_IO_ERROR 1
322 #define TTY_OTHER_CLOSED 2
323 #define TTY_EXCLUSIVE 3
324 #define TTY_DO_WRITE_WAKEUP 5
325 #define TTY_LDISC_OPEN 11
326 #define TTY_PTY_LOCK 16
327 #define TTY_NO_WRITE_SPLIT 17
328 #define TTY_HUPPED 18
329 #define TTY_HUPPING 19
330 #define TTY_LDISC_CHANGING 20
331 #define TTY_LDISC_HALTED 22
333 static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file)
335 return file->f_flags & O_NONBLOCK ||
336 test_bit(TTY_LDISC_CHANGING, &tty->flags);
339 static inline bool tty_io_error(struct tty_struct *tty)
341 return test_bit(TTY_IO_ERROR, &tty->flags);
344 static inline bool tty_throttled(struct tty_struct *tty)
346 return test_bit(TTY_THROTTLED, &tty->flags);
350 void tty_kref_put(struct tty_struct *tty);
351 struct pid *tty_get_pgrp(struct tty_struct *tty);
352 void tty_vhangup_self(void);
353 void disassociate_ctty(int priv);
354 dev_t tty_devnum(struct tty_struct *tty);
355 void proc_clear_tty(struct task_struct *p);
356 struct tty_struct *get_current_tty(void);
358 int __init tty_init(void);
359 const char *tty_name(const struct tty_struct *tty);
360 struct tty_struct *tty_kopen_exclusive(dev_t device);
361 struct tty_struct *tty_kopen_shared(dev_t device);
362 void tty_kclose(struct tty_struct *tty);
363 int tty_dev_name_to_number(const char *name, dev_t *number);
365 static inline void tty_kref_put(struct tty_struct *tty)
367 static inline struct pid *tty_get_pgrp(struct tty_struct *tty)
369 static inline void tty_vhangup_self(void)
371 static inline void disassociate_ctty(int priv)
373 static inline dev_t tty_devnum(struct tty_struct *tty)
375 static inline void proc_clear_tty(struct task_struct *p)
377 static inline struct tty_struct *get_current_tty(void)
380 static inline int __init tty_init(void)
382 static inline const char *tty_name(const struct tty_struct *tty)
384 static inline struct tty_struct *tty_kopen_exclusive(dev_t device)
385 { return ERR_PTR(-ENODEV); }
386 static inline void tty_kclose(struct tty_struct *tty)
388 static inline int tty_dev_name_to_number(const char *name, dev_t *number)
389 { return -ENOTSUPP; }
392 extern struct ktermios tty_std_termios;
396 extern struct class *tty_class;
399 * tty_kref_get - get a tty reference
402 * Return a new reference to a tty object. The caller must hold
403 * sufficient locks/counts to ensure that their existing reference cannot
407 static inline struct tty_struct *tty_kref_get(struct tty_struct *tty)
410 kref_get(&tty->kref);
414 const char *tty_driver_name(const struct tty_struct *tty);
415 void tty_wait_until_sent(struct tty_struct *tty, long timeout);
416 void stop_tty(struct tty_struct *tty);
417 void start_tty(struct tty_struct *tty);
418 void tty_write_message(struct tty_struct *tty, char *msg);
419 int tty_send_xchar(struct tty_struct *tty, char ch);
420 int tty_put_char(struct tty_struct *tty, unsigned char c);
421 unsigned int tty_chars_in_buffer(struct tty_struct *tty);
422 unsigned int tty_write_room(struct tty_struct *tty);
423 void tty_driver_flush_buffer(struct tty_struct *tty);
424 void tty_unthrottle(struct tty_struct *tty);
425 int tty_throttle_safe(struct tty_struct *tty);
426 int tty_unthrottle_safe(struct tty_struct *tty);
427 int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
428 int tty_get_icount(struct tty_struct *tty,
429 struct serial_icounter_struct *icount);
430 int is_current_pgrp_orphaned(void);
431 void tty_hangup(struct tty_struct *tty);
432 void tty_vhangup(struct tty_struct *tty);
433 int tty_hung_up_p(struct file *filp);
434 void do_SAK(struct tty_struct *tty);
435 void __do_SAK(struct tty_struct *tty);
437 speed_t tty_termios_baud_rate(struct ktermios *termios);
438 void tty_termios_encode_baud_rate(struct ktermios *termios, speed_t ibaud,
440 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud,
444 * tty_get_baud_rate - get tty bit rates
447 * Returns the baud rate as an integer for this terminal. The
448 * termios lock must be held by the caller and the terminal bit
449 * flags may be updated.
453 static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
455 return tty_termios_baud_rate(&tty->termios);
458 unsigned char tty_get_char_size(unsigned int cflag);
459 unsigned char tty_get_frame_size(unsigned int cflag);
461 void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old);
462 int tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b);
463 int tty_set_termios(struct tty_struct *tty, struct ktermios *kt);
465 void tty_wakeup(struct tty_struct *tty);
467 int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
468 int tty_perform_flush(struct tty_struct *tty, unsigned long arg);
469 struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx);
470 void tty_release_struct(struct tty_struct *tty, int idx);
471 void tty_init_termios(struct tty_struct *tty);
472 void tty_save_termios(struct tty_struct *tty);
473 int tty_standard_install(struct tty_driver *driver,
474 struct tty_struct *tty);
476 extern struct mutex tty_mutex;
479 void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
481 void __init n_tty_init(void);
483 static inline void n_tty_init(void) { }
488 void tty_audit_exit(void);
489 void tty_audit_fork(struct signal_struct *sig);
490 int tty_audit_push(void);
492 static inline void tty_audit_exit(void)
495 static inline void tty_audit_fork(struct signal_struct *sig)
498 static inline int tty_audit_push(void)
505 int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd,
510 int vt_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
512 long vt_compat_ioctl(struct tty_struct *tty, unsigned int cmd,
516 /* functions for preparation of BKL removal */
517 void tty_lock(struct tty_struct *tty);
518 int tty_lock_interruptible(struct tty_struct *tty);
519 void tty_unlock(struct tty_struct *tty);
520 void tty_lock_slave(struct tty_struct *tty);
521 void tty_unlock_slave(struct tty_struct *tty);
522 void tty_set_lock_subclass(struct tty_struct *tty);