tty.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* $OpenBSD: tty.h,v 1.35 2015/07/20 22:28:57 sf Exp $ */
  2. /* $NetBSD: tty.h,v 1.30.4.1 1996/06/02 09:08:13 mrg Exp $ */
  3. /*-
  4. * Copyright (c) 1982, 1986, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. * (c) UNIX System Laboratories, Inc.
  7. * All or some portions of this file are derived from material licensed
  8. * to the University of California by American Telephone and Telegraph
  9. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  10. * the permission of UNIX System Laboratories, Inc.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)tty.h 8.6 (Berkeley) 1/21/94
  37. */
  38. #include <sys/termios.h>
  39. #include <sys/queue.h>
  40. #include <sys/selinfo.h> /* For struct selinfo. */
  41. #include <sys/timeout.h>
  42. #define KERN_TTY_TKNIN 1 /* quad: input chars */
  43. #define KERN_TTY_TKNOUT 2 /* quad: output chars */
  44. #define KERN_TTY_TKRAWCC 3 /* quad: input chars, raw mode */
  45. #define KERN_TTY_TKCANCC 4 /* quad: input char, cooked mode */
  46. #define KERN_TTY_INFO 5 /* struct: tty stats */
  47. #define KERN_TTY_MAXPTYS 6 /* int: max ptys */
  48. #define KERN_TTY_NPTYS 7 /* int: number of allocated ptys */
  49. #define KERN_TTY_MAXID 8
  50. #define CTL_KERN_TTY_NAMES { \
  51. { 0, 0 }, \
  52. { "tk_nin", CTLTYPE_QUAD }, \
  53. { "tk_nout", CTLTYPE_QUAD }, \
  54. { "tk_rawcc", CTLTYPE_QUAD }, \
  55. { "tk_cancc", CTLTYPE_QUAD }, \
  56. { "ttyinfo", CTLTYPE_STRUCT }, \
  57. { "maxptys", CTLTYPE_INT }, \
  58. { "nptys", CTLTYPE_INT }, \
  59. }
  60. /* ptmget, for /dev/ptm pty getting ioctl PTMGET */
  61. struct ptmget {
  62. int cfd;
  63. int sfd;
  64. char cn[16];
  65. char sn[16];
  66. };
  67. #define PTMGET _IOR('t', 1, struct ptmget) /* get ptys */
  68. #define PATH_PTMDEV "/dev/ptm"
  69. #define TTY_GID 4 /* XXX evil hardcoding of tty gid */
  70. /*
  71. * Clists are actually ring buffers. The c_cc, c_cf, c_cl fields have
  72. * exactly the same behaviour as in true clists.
  73. * if c_cq is NULL, the ring buffer has no TTY_QUOTE functionality
  74. * (but, saves memory and cpu time)
  75. *
  76. * *DON'T* play with c_cs, c_ce, c_cq, or c_cl outside tty_subr.c!!!
  77. */
  78. struct clist {
  79. int c_cc; /* count of characters in queue */
  80. int c_cn; /* total ring buffer length */
  81. u_char *c_cf; /* points to first character */
  82. u_char *c_cl; /* points to next open character */
  83. u_char *c_cs; /* start of ring buffer */
  84. u_char *c_ce; /* c_ce + c_len */
  85. u_char *c_cq; /* N bits/bytes long, see tty_subr.c */
  86. };
  87. /*
  88. * Per-tty structure.
  89. *
  90. * Should be split in two, into device and tty drivers.
  91. * Glue could be masks of what to echo and circular buffer
  92. * (low, high, timeout).
  93. */
  94. struct tty {
  95. TAILQ_ENTRY(tty) tty_link; /* Link in global tty list. */
  96. struct clist t_rawq; /* Device raw input queue. */
  97. long t_rawcc; /* Raw input queue statistics. */
  98. struct clist t_canq; /* Device canonical queue. */
  99. long t_cancc; /* Canonical queue statistics. */
  100. struct clist t_outq; /* Device output queue. */
  101. long t_outcc; /* Output queue statistics. */
  102. int t_qlen; /* Length of above queues */
  103. u_char t_line; /* Interface to device drivers. */
  104. dev_t t_dev; /* Device. */
  105. int t_state; /* Device and driver (TS*) state. */
  106. int t_flags; /* Tty flags. */
  107. struct pgrp *t_pgrp; /* Foreground process group. */
  108. struct session *t_session; /* Enclosing session. */
  109. struct selinfo t_rsel; /* Tty read/oob select. */
  110. struct selinfo t_wsel; /* Tty write select. */
  111. struct termios t_termios; /* Termios state. */
  112. struct winsize t_winsize; /* Window size. */
  113. /* Start output. */
  114. void (*t_oproc)(struct tty *);
  115. /* Set hardware state. */
  116. int (*t_param)(struct tty *, struct termios *);
  117. /* Set hardware flow control. */
  118. int (*t_hwiflow)(struct tty *tp, int flag);
  119. void *t_sc; /* XXX: net/if_sl.c:sl_softc. */
  120. short t_column; /* Tty output column. */
  121. short t_rocount, t_rocol; /* Tty. */
  122. short t_hiwat; /* High water mark. */
  123. short t_lowat; /* Low water mark. */
  124. short t_gen; /* Generation number. */
  125. struct timeout t_rstrt_to; /* restart timeout */
  126. struct timeval t_tv; /* timestamp */
  127. };
  128. /*
  129. * Small version of struct tty exported via sysctl KERN_TTY_INFO
  130. */
  131. struct itty {
  132. dev_t t_dev;
  133. int t_rawq_c_cc;
  134. int t_canq_c_cc;
  135. int t_outq_c_cc;
  136. short t_hiwat;
  137. short t_lowat;
  138. short t_column;
  139. int t_state;
  140. struct session *t_session;
  141. pid_t t_pgrp_pg_id;
  142. u_char t_line;
  143. };
  144. #define t_cc t_termios.c_cc
  145. #define t_cflag t_termios.c_cflag
  146. #define t_iflag t_termios.c_iflag
  147. #define t_ispeed t_termios.c_ispeed
  148. #define t_lflag t_termios.c_lflag
  149. #define t_min t_termios.c_min
  150. #define t_oflag t_termios.c_oflag
  151. #define t_ospeed t_termios.c_ospeed
  152. #define t_time t_termios.c_time
  153. #define TTIPRI 25 /* Sleep priority for tty reads. */
  154. #define TTOPRI 26 /* Sleep priority for tty writes. */
  155. #define TTMASK 15
  156. #define OBUFSIZ 512
  157. #define TTYHOG(tp) (tp)->t_qlen
  158. #ifdef _KERNEL
  159. #define TTMAXLOWAT 256
  160. #define TTMINLOWAT 32
  161. #define TTMINHIWAT 100
  162. #define TTHIWATMINSPACE 200 /* Min space above hiwat */
  163. #endif
  164. /* These flags are kept in t_state. */
  165. #define TS_ASLEEP 0x00001 /* Process waiting for tty. */
  166. #define TS_ASYNC 0x00002 /* Tty in async I/O mode. */
  167. #define TS_BUSY 0x00004 /* Draining output. */
  168. #define TS_CARR_ON 0x00008 /* Carrier is present. */
  169. #define TS_FLUSH 0x00010 /* Outq has been flushed during DMA. */
  170. #define TS_ISOPEN 0x00020 /* Open has completed. */
  171. #define TS_TBLOCK 0x00040 /* Further input blocked. */
  172. #define TS_TIMEOUT 0x00080 /* Wait for output char processing. */
  173. #define TS_TTSTOP 0x00100 /* Output paused. */
  174. #define TS_WOPEN 0x00200 /* Open in progress. */
  175. #define TS_XCLUDE 0x00400 /* Tty requires exclusivity. */
  176. /* State for intra-line fancy editing work. */
  177. #define TS_BKSL 0x00800 /* State for lowercase \ work. */
  178. #define TS_CNTTB 0x01000 /* Counting tab width, ignore FLUSHO. */
  179. #define TS_ERASE 0x02000 /* Within a \.../ for PRTRUB. */
  180. #define TS_LNCH 0x04000 /* Next character is literal. */
  181. #define TS_TYPEN 0x08000 /* Retyping suspended input (PENDIN). */
  182. #define TS_LOCAL (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
  183. #define TS_TSTAMPDCDSET 0x10000 /* update timestamp on DCD set */
  184. #define TS_TSTAMPDCDCLR 0x20000 /* update timestamp on DCD clr */
  185. #define TS_TSTAMPCTSSET 0x40000 /* update timestamp on CTS set */
  186. #define TS_TSTAMPCTSCLR 0x80000 /* update timestamp on CTS clr */
  187. /* Character type information. */
  188. #define ORDINARY 0
  189. #define CONTROL 1
  190. #define BACKSPACE 2
  191. #define NEWLINE 3
  192. #define TAB 4
  193. #define VTAB 5
  194. #define RETURN 6
  195. struct speedtab {
  196. int sp_speed; /* Speed. */
  197. int sp_code; /* Code. */
  198. };
  199. /* Modem control commands (driver). */
  200. #define DMSET 0
  201. #define DMBIS 1
  202. #define DMBIC 2
  203. #define DMGET 3
  204. /* Flags on a character passed to ttyinput. */
  205. #define TTY_CHARMASK 0x000000ff /* Character mask */
  206. #define TTY_QUOTE 0x00000100 /* Character quoted */
  207. #define TTY_ERRORMASK 0xff000000 /* Error mask */
  208. #define TTY_FE 0x01000000 /* Framing error or BREAK condition */
  209. #define TTY_PE 0x02000000 /* Parity error */
  210. /* Is tp controlling terminal for pr? */
  211. #define isctty(pr, tp) \
  212. ((pr)->ps_session == (tp)->t_session && (pr)->ps_flags & PS_CONTROLT)
  213. /* Is pr in background of tp? */
  214. #define isbackground(pr, tp) \
  215. (isctty((pr), (tp)) && (pr)->ps_pgrp != (tp)->t_pgrp)
  216. /*
  217. * ttylist_head is defined here so that user-land has access to it.
  218. */
  219. TAILQ_HEAD(ttylist_head, tty); /* the ttylist is a TAILQ */
  220. #ifdef _KERNEL
  221. extern int tty_count; /* number of ttys in global ttylist */
  222. extern struct ttychars ttydefaults;
  223. /* Symbolic sleep message strings. */
  224. extern char ttyin[], ttyout[], ttopen[], ttclos[], ttybg[], ttybuf[];
  225. extern int64_t tk_cancc, tk_nin, tk_nout, tk_rawcc;
  226. int sysctl_tty(int *, u_int, void *, size_t *, void *, size_t);
  227. int sysctl_pty(int *, u_int, void *, size_t *, void *, size_t);
  228. int b_to_q(u_char *cp, int cc, struct clist *q);
  229. void catq(struct clist *from, struct clist *to);
  230. void clist_init(void);
  231. int getc(struct clist *q);
  232. void ndflush(struct clist *q, int cc);
  233. int ndqb(struct clist *q, int flag);
  234. u_char *nextc(struct clist *q, u_char *cp, int *c);
  235. int putc(int c, struct clist *q);
  236. int q_to_b(struct clist *q, u_char *cp, int cc);
  237. int unputc(struct clist *q);
  238. int nullmodem(struct tty *tp, int flag);
  239. int tputchar(int c, struct tty *tp);
  240. int ttioctl(struct tty *tp, u_long com, caddr_t data, int flag,
  241. struct proc *p);
  242. int ttread(struct tty *tp, struct uio *uio, int flag);
  243. void ttrstrt(void *tp);
  244. int ttpoll(dev_t device, int events, struct proc *p);
  245. int ttkqfilter(dev_t dev, struct knote *kn);
  246. void ttsetwater(struct tty *tp);
  247. int ttspeedtab(int speed, const struct speedtab *table);
  248. int ttstart(struct tty *tp);
  249. void ttwakeupwr(struct tty *tp);
  250. void ttwakeup(struct tty *tp);
  251. int ttwrite(struct tty *tp, struct uio *uio, int flag);
  252. void ttychars(struct tty *tp);
  253. int ttycheckoutq(struct tty *tp, int wait);
  254. int ttyclose(struct tty *tp);
  255. void ttyflush(struct tty *tp, int rw);
  256. void ttyinfo(struct tty *tp);
  257. int ttyinput(int c, struct tty *tp);
  258. int ttylclose(struct tty *tp, int flag, struct proc *p);
  259. int ttymodem(struct tty *tp, int flag);
  260. int ttyopen(dev_t device, struct tty *tp, struct proc *p);
  261. int ttyoutput(int c, struct tty *tp);
  262. void ttypend(struct tty *tp);
  263. void ttyretype(struct tty *tp);
  264. void ttyrub(int c, struct tty *tp);
  265. int ttysleep(struct tty *tp,
  266. void *chan, int pri, char *wmesg, int timeout);
  267. int ttywait(struct tty *tp);
  268. int ttywflush(struct tty *tp);
  269. void ttytstamp(struct tty *tp, int octs, int ncts, int odcd, int ndcd);
  270. void tty_init(void);
  271. struct tty *ttymalloc(int);
  272. void ttyfree(struct tty *);
  273. u_char *firstc(struct clist *clp, int *c);
  274. int cttyopen(dev_t, int, int, struct proc *);
  275. int cttyread(dev_t, struct uio *, int);
  276. int cttywrite(dev_t, struct uio *, int);
  277. int cttyioctl(dev_t, u_long, caddr_t, int, struct proc *);
  278. int cttypoll(dev_t, int, struct proc *);
  279. void clalloc(struct clist *, int, int);
  280. void clfree(struct clist *);
  281. #endif