FreeBSD/Linux Kernel Cross Reference
sys/kern/tty.c
1 /*-
2 * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Copyright (c) 2002 Networks Associates Technologies, Inc.
11 * All rights reserved.
12 *
13 * Portions of this software were developed for the FreeBSD Project by
14 * ThinkSec AS and NAI Labs, the Security Research Division of Network
15 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
16 * ("CBOSS"), as part of the DARPA CHATS research program.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)tty.c 8.8 (Berkeley) 1/21/94
43 */
44
45 /*-
46 * TODO:
47 * o Fix races for sending the start char in ttyflush().
48 * o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
49 * With luck, there will be MIN chars before select() returns().
50 * o Handle CLOCAL consistently for ptys. Perhaps disallow setting it.
51 * o Don't allow input in TS_ZOMBIE case. It would be visible through
52 * FIONREAD.
53 * o Do the new sio locking stuff here and use it to avoid special
54 * case for EXTPROC?
55 * o Lock PENDIN too?
56 * o Move EXTPROC and/or PENDIN to t_state?
57 * o Wrap most of ttioctl in spltty/splx.
58 * o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
59 * o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
60 * o Don't allow certain termios flags to affect disciplines other
61 * than TTYDISC. Cancel their effects before switch disciplines
62 * and ignore them if they are set while we are in another
63 * discipline.
64 * o Now that historical speed conversions are handled here, don't
65 * do them in drivers.
66 * o Check for TS_CARR_ON being set while everything is closed and not
67 * waiting for carrier. TS_CARR_ON isn't cleared if nothing is open,
68 * so it would live until the next open even if carrier drops.
69 * o Restore TS_WOPEN since it is useful in pstat. It must be cleared
70 * only when _all_ openers leave open().
71 */
72
73 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD: releng/7.4/sys/kern/tty.c 202814 2010-01-22 17:02:07Z jhb $");
75
76 #include "opt_compat.h"
77 #include "opt_tty.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/filio.h>
82 #include <sys/lock.h>
83 #include <sys/mutex.h>
84 #include <sys/namei.h>
85 #include <sys/sx.h>
86 #if defined(COMPAT_43TTY)
87 #include <sys/ioctl_compat.h>
88 #endif
89 #include <sys/priv.h>
90 #include <sys/proc.h>
91 #define TTYDEFCHARS
92 #include <sys/tty.h>
93 #undef TTYDEFCHARS
94 #include <sys/fcntl.h>
95 #include <sys/conf.h>
96 #include <sys/poll.h>
97 #include <sys/kernel.h>
98 #include <sys/vnode.h>
99 #include <sys/serial.h>
100 #include <sys/signalvar.h>
101 #include <sys/resourcevar.h>
102 #include <sys/malloc.h>
103 #include <sys/filedesc.h>
104 #include <sys/sched.h>
105 #include <sys/sysctl.h>
106 #include <sys/timepps.h>
107
108 #include <machine/stdarg.h>
109
110 #include <vm/vm.h>
111 #include <vm/pmap.h>
112 #include <vm/vm_map.h>
113
114 MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures");
115
116 long tk_cancc;
117 long tk_nin;
118 long tk_nout;
119 long tk_rawcc;
120
121 static d_open_t ttysopen;
122 static d_close_t ttysclose;
123 static d_read_t ttysrdwr;
124 static d_ioctl_t ttysioctl;
125 static d_purge_t ttypurge;
126
127 /* Default cdevsw for common tty devices */
128 static struct cdevsw tty_cdevsw = {
129 .d_version = D_VERSION,
130 .d_open = ttyopen,
131 .d_close = ttyclose,
132 .d_ioctl = ttyioctl,
133 .d_purge = ttypurge,
134 .d_name = "ttydrv",
135 .d_flags = D_TTY | D_NEEDGIANT,
136 };
137
138 /* Cdevsw for slave tty devices */
139 static struct cdevsw ttys_cdevsw = {
140 .d_version = D_VERSION,
141 .d_open = ttysopen,
142 .d_close = ttysclose,
143 .d_read = ttysrdwr,
144 .d_write = ttysrdwr,
145 .d_ioctl = ttysioctl,
146 .d_name = "TTYS",
147 .d_flags = D_TTY | D_NEEDGIANT,
148 };
149
150 static int proc_sum(struct proc *, int *);
151 static int proc_compare(struct proc *, struct proc *);
152 static int thread_compare(struct thread *, struct thread *);
153 static int ttnread(struct tty *tp);
154 static void ttyecho(int c, struct tty *tp);
155 static int ttyoutput(int c, struct tty *tp);
156 static void ttypend(struct tty *tp);
157 static void ttyretype(struct tty *tp);
158 static void ttyrub(int c, struct tty *tp);
159 static void ttyrubo(struct tty *tp, int cnt);
160 static void ttyunblock(struct tty *tp);
161 static int ttywflush(struct tty *tp);
162 static int filt_ttyread(struct knote *kn, long hint);
163 static void filt_ttyrdetach(struct knote *kn);
164 static int filt_ttywrite(struct knote *kn, long hint);
165 static void filt_ttywdetach(struct knote *kn);
166
167 /*
168 * Table with character classes and parity. The 8th bit indicates parity,
169 * the 7th bit indicates the character is an alphameric or underscore (for
170 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
171 * are 0 then the character needs no special processing on output; classes
172 * other than 0 might be translated or (not currently) require delays.
173 */
174 #define E 0x00 /* Even parity. */
175 #define O 0x80 /* Odd parity. */
176 #define PARITY(c) (char_type[c] & O)
177
178 #define ALPHA 0x40 /* Alpha or underscore. */
179 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
180
181 #define CCLASSMASK 0x3f
182 #define CCLASS(c) (char_type[c] & CCLASSMASK)
183
184 #define BS BACKSPACE
185 #define CC CONTROL
186 #define CR RETURN
187 #define NA ORDINARY | ALPHA
188 #define NL NEWLINE
189 #define NO ORDINARY
190 #define TB TAB
191 #define VT VTAB
192
193 static u_char const char_type[] = {
194 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
195 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
196 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
197 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
198 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
199 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
200 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
201 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
202 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
203 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
204 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
205 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
206 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
207 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
208 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
209 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
210 /*
211 * Meta chars; should be settable per character set;
212 * for now, treat them all as normal characters.
213 */
214 NA, NA, NA, NA, NA, NA, NA, NA,
215 NA, NA, NA, NA, NA, NA, NA, NA,
216 NA, NA, NA, NA, NA, NA, NA, NA,
217 NA, NA, NA, NA, NA, NA, NA, NA,
218 NA, NA, NA, NA, NA, NA, NA, NA,
219 NA, NA, NA, NA, NA, NA, NA, NA,
220 NA, NA, NA, NA, NA, NA, NA, NA,
221 NA, NA, NA, NA, NA, NA, NA, NA,
222 NA, NA, NA, NA, NA, NA, NA, NA,
223 NA, NA, NA, NA, NA, NA, NA, NA,
224 NA, NA, NA, NA, NA, NA, NA, NA,
225 NA, NA, NA, NA, NA, NA, NA, NA,
226 NA, NA, NA, NA, NA, NA, NA, NA,
227 NA, NA, NA, NA, NA, NA, NA, NA,
228 NA, NA, NA, NA, NA, NA, NA, NA,
229 NA, NA, NA, NA, NA, NA, NA, NA,
230 };
231 #undef BS
232 #undef CC
233 #undef CR
234 #undef NA
235 #undef NL
236 #undef NO
237 #undef TB
238 #undef VT
239
240 /* Macros to clear/set/test flags. */
241 #define SET(t, f) (t) |= (f)
242 #define CLR(t, f) (t) &= ~(f)
243 #define ISSET(t, f) ((t) & (f))
244
245 #undef MAX_INPUT /* XXX wrong in <sys/syslimits.h> */
246 #define MAX_INPUT TTYHOG /* XXX limit is usually larger for !ICANON */
247
248 /*
249 * list of struct tty where pstat(8) can pick it up with sysctl
250 *
251 * The lock order is to grab the list mutex before the tty mutex.
252 * Together with additions going on the tail of the list, this allows
253 * the sysctl to avoid doing retries.
254 */
255 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
256 static struct mtx tty_list_mutex;
257 MTX_SYSINIT(tty_list, &tty_list_mutex, "ttylist", MTX_DEF);
258
259 static struct unrhdr *tty_unit;
260
261 static int drainwait = 5*60;
262 SYSCTL_INT(_kern, OID_AUTO, drainwait, CTLFLAG_RW, &drainwait,
263 0, "Output drain timeout in seconds");
264
265 static struct tty *
266 tty_gettp(struct cdev *dev)
267 {
268 struct tty *tp;
269 struct cdevsw *csw;
270
271 csw = dev_refthread(dev);
272 if (csw == NULL)
273 return (NULL);
274 KASSERT(csw->d_flags & D_TTY,
275 ("non D_TTY (%s) in tty code", devtoname(dev)));
276 tp = dev->si_tty;
277 dev_relthread(dev);
278 KASSERT(tp != NULL,
279 ("no tty pointer on (%s) in tty code", devtoname(dev)));
280 return (tp);
281 }
282
283 /*
284 * Initial open of tty, or (re)entry to standard tty line discipline.
285 */
286 int
287 tty_open(struct cdev *device, struct tty *tp)
288 {
289 int s;
290
291 s = spltty();
292 tp->t_dev = device;
293 tp->t_hotchar = 0;
294 if (!ISSET(tp->t_state, TS_ISOPEN)) {
295 ttyref(tp);
296 SET(tp->t_state, TS_ISOPEN);
297 if (ISSET(tp->t_cflag, CLOCAL))
298 SET(tp->t_state, TS_CONNECTED);
299 bzero(&tp->t_winsize, sizeof(tp->t_winsize));
300 }
301 /* XXX don't hang forever on output */
302 if (tp->t_timeout < 0)
303 tp->t_timeout = drainwait*hz;
304 ttsetwater(tp);
305 splx(s);
306 return (0);
307 }
308
309 /*
310 * Handle close() on a tty line: flush and set to initial state,
311 * bumping generation number so that pending read/write calls
312 * can detect recycling of the tty.
313 * XXX our caller should have done `spltty(); l_close(); tty_close();'
314 * and l_close() should have flushed, but we repeat the spltty() and
315 * the flush in case there are buggy callers.
316 */
317 int
318 tty_close(struct tty *tp)
319 {
320 int ostate, s;
321
322 funsetown(&tp->t_sigio);
323 s = spltty();
324 if (constty == tp)
325 constty_clear();
326
327 ttyflush(tp, FREAD | FWRITE);
328 clist_free_cblocks(&tp->t_canq);
329 clist_free_cblocks(&tp->t_outq);
330 clist_free_cblocks(&tp->t_rawq);
331
332 tp->t_gen++;
333 tp->t_line = TTYDISC;
334 tp->t_hotchar = 0;
335 tp->t_pgrp = NULL;
336 tp->t_session = NULL;
337 ostate = tp->t_state;
338 tp->t_state = 0;
339 knlist_clear(&tp->t_rsel.si_note, 0);
340 knlist_clear(&tp->t_wsel.si_note, 0);
341 /*
342 * Both final close and revocation close might end up calling
343 * this method. Only the thread clearing TS_ISOPEN should
344 * release the reference to the tty.
345 */
346 if (ISSET(ostate, TS_ISOPEN))
347 ttyrel(tp);
348 splx(s);
349 return (0);
350 }
351
352 #define FLUSHQ(q) { \
353 if ((q)->c_cc) \
354 ndflush(q, (q)->c_cc); \
355 }
356
357 /* Is 'c' a line delimiter ("break" character)? */
358 #define TTBREAKC(c, lflag) \
359 ((c) == '\n' || (((c) == cc[VEOF] || \
360 (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) && \
361 (c) != _POSIX_VDISABLE))
362
363 /*
364 * Process input of a single character received on a tty.
365 */
366 int
367 ttyinput(int c, struct tty *tp)
368 {
369 tcflag_t iflag, lflag;
370 cc_t *cc;
371 int i, err;
372
373 /*
374 * If input is pending take it first.
375 */
376 lflag = tp->t_lflag;
377 if (ISSET(lflag, PENDIN))
378 ttypend(tp);
379 /*
380 * Gather stats.
381 */
382 if (ISSET(lflag, ICANON)) {
383 ++tk_cancc;
384 ++tp->t_cancc;
385 } else {
386 ++tk_rawcc;
387 ++tp->t_rawcc;
388 }
389 ++tk_nin;
390
391 /*
392 * Block further input iff:
393 * current input > threshold AND input is available to user program
394 * AND input flow control is enabled and not yet invoked.
395 * The 3 is slop for PARMRK.
396 */
397 iflag = tp->t_iflag;
398 if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 &&
399 (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) &&
400 (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) &&
401 !ISSET(tp->t_state, TS_TBLOCK))
402 ttyblock(tp);
403
404 /* Handle exceptional conditions (break, parity, framing). */
405 cc = tp->t_cc;
406 err = (ISSET(c, TTY_ERRORMASK));
407 if (err) {
408 CLR(c, TTY_ERRORMASK);
409 if (ISSET(err, TTY_BI)) {
410 if (ISSET(iflag, IGNBRK))
411 return (0);
412 if (ISSET(iflag, BRKINT)) {
413 ttyflush(tp, FREAD | FWRITE);
414 if (tp->t_pgrp != NULL) {
415 PGRP_LOCK(tp->t_pgrp);
416 pgsignal(tp->t_pgrp, SIGINT, 1);
417 PGRP_UNLOCK(tp->t_pgrp);
418 }
419 goto endcase;
420 }
421 if (ISSET(iflag, PARMRK))
422 goto parmrk;
423 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK))
424 || ISSET(err, TTY_FE)) {
425 if (ISSET(iflag, IGNPAR))
426 return (0);
427 else if (ISSET(iflag, PARMRK)) {
428 parmrk:
429 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >
430 MAX_INPUT - 3)
431 goto input_overflow;
432 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
433 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
434 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
435 goto endcase;
436 } else
437 c = 0;
438 }
439 }
440
441 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
442 CLR(c, 0x80);
443 if (!ISSET(lflag, EXTPROC)) {
444 /*
445 * Check for literal nexting very first
446 */
447 if (ISSET(tp->t_state, TS_LNCH)) {
448 SET(c, TTY_QUOTE);
449 CLR(tp->t_state, TS_LNCH);
450 }
451 /*
452 * Scan for special characters. This code
453 * is really just a big case statement with
454 * non-constant cases. The bottom of the
455 * case statement is labeled ``endcase'', so goto
456 * it after a case match, or similar.
457 */
458
459 /*
460 * Control chars which aren't controlled
461 * by ICANON, ISIG, or IXON.
462 */
463 if (ISSET(lflag, IEXTEN)) {
464 if (CCEQ(cc[VLNEXT], c)) {
465 if (ISSET(lflag, ECHO)) {
466 if (ISSET(lflag, ECHOE)) {
467 (void)ttyoutput('^', tp);
468 (void)ttyoutput('\b', tp);
469 } else
470 ttyecho(c, tp);
471 }
472 SET(tp->t_state, TS_LNCH);
473 goto endcase;
474 }
475 if (CCEQ(cc[VDISCARD], c)) {
476 if (ISSET(lflag, FLUSHO))
477 CLR(tp->t_lflag, FLUSHO);
478 else {
479 ttyflush(tp, FWRITE);
480 ttyecho(c, tp);
481 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
482 ttyretype(tp);
483 SET(tp->t_lflag, FLUSHO);
484 }
485 goto startoutput;
486 }
487 }
488 /*
489 * Signals.
490 */
491 if (ISSET(lflag, ISIG)) {
492 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
493 if (!ISSET(lflag, NOFLSH))
494 ttyflush(tp, FREAD | FWRITE);
495 ttyecho(c, tp);
496 if (tp->t_pgrp != NULL) {
497 PGRP_LOCK(tp->t_pgrp);
498 pgsignal(tp->t_pgrp,
499 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
500 PGRP_UNLOCK(tp->t_pgrp);
501 }
502 goto endcase;
503 }
504 if (CCEQ(cc[VSUSP], c)) {
505 if (!ISSET(lflag, NOFLSH))
506 ttyflush(tp, FREAD);
507 ttyecho(c, tp);
508 if (tp->t_pgrp != NULL) {
509 PGRP_LOCK(tp->t_pgrp);
510 pgsignal(tp->t_pgrp, SIGTSTP, 1);
511 PGRP_UNLOCK(tp->t_pgrp);
512 }
513 goto endcase;
514 }
515 }
516 /*
517 * Handle start/stop characters.
518 */
519 if (ISSET(iflag, IXON)) {
520 if (CCEQ(cc[VSTOP], c)) {
521 if (!ISSET(tp->t_state, TS_TTSTOP)) {
522 SET(tp->t_state, TS_TTSTOP);
523 tt_stop(tp, 0);
524 return (0);
525 }
526 if (!CCEQ(cc[VSTART], c))
527 return (0);
528 /*
529 * if VSTART == VSTOP then toggle
530 */
531 goto endcase;
532 }
533 if (CCEQ(cc[VSTART], c))
534 goto restartoutput;
535 }
536 /*
537 * IGNCR, ICRNL, & INLCR
538 */
539 if (c == '\r') {
540 if (ISSET(iflag, IGNCR))
541 return (0);
542 else if (ISSET(iflag, ICRNL))
543 c = '\n';
544 } else if (c == '\n' && ISSET(iflag, INLCR))
545 c = '\r';
546 }
547 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
548 /*
549 * From here on down canonical mode character
550 * processing takes place.
551 */
552 /*
553 * erase or erase2 (^H / ^?)
554 */
555 if (CCEQ(cc[VERASE], c) || CCEQ(cc[VERASE2], c) ) {
556 if (tp->t_rawq.c_cc)
557 ttyrub(unputc(&tp->t_rawq), tp);
558 goto endcase;
559 }
560 /*
561 * kill (^U)
562 */
563 if (CCEQ(cc[VKILL], c)) {
564 if (ISSET(lflag, ECHOKE) &&
565 tp->t_rawq.c_cc == tp->t_rocount &&
566 !ISSET(lflag, ECHOPRT))
567 while (tp->t_rawq.c_cc)
568 ttyrub(unputc(&tp->t_rawq), tp);
569 else {
570 ttyecho(c, tp);
571 if (ISSET(lflag, ECHOK) ||
572 ISSET(lflag, ECHOKE))
573 ttyecho('\n', tp);
574 FLUSHQ(&tp->t_rawq);
575 tp->t_rocount = 0;
576 }
577 CLR(tp->t_state, TS_LOCAL);
578 goto endcase;
579 }
580 /*
581 * word erase (^W)
582 */
583 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) {
584 int ctype;
585
586 /*
587 * erase whitespace
588 */
589 while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
590 ttyrub(c, tp);
591 if (c == -1)
592 goto endcase;
593 /*
594 * erase last char of word and remember the
595 * next chars type (for ALTWERASE)
596 */
597 ttyrub(c, tp);
598 c = unputc(&tp->t_rawq);
599 if (c == -1)
600 goto endcase;
601 if (c == ' ' || c == '\t') {
602 (void)putc(c, &tp->t_rawq);
603 goto endcase;
604 }
605 ctype = ISALPHA(c);
606 /*
607 * erase rest of word
608 */
609 do {
610 ttyrub(c, tp);
611 c = unputc(&tp->t_rawq);
612 if (c == -1)
613 goto endcase;
614 } while (c != ' ' && c != '\t' &&
615 (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype));
616 (void)putc(c, &tp->t_rawq);
617 goto endcase;
618 }
619 /*
620 * reprint line (^R)
621 */
622 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) {
623 ttyretype(tp);
624 goto endcase;
625 }
626 /*
627 * ^T - kernel info and generate SIGINFO
628 */
629 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) {
630 if (ISSET(lflag, ISIG) && tp->t_pgrp != NULL) {
631 PGRP_LOCK(tp->t_pgrp);
632 pgsignal(tp->t_pgrp, SIGINFO, 1);
633 PGRP_UNLOCK(tp->t_pgrp);
634 }
635 if (!ISSET(lflag, NOKERNINFO))
636 ttyinfo(tp);
637 goto endcase;
638 }
639 }
640 /*
641 * Check for input buffer overflow
642 */
643 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) {
644 input_overflow:
645 if (ISSET(iflag, IMAXBEL)) {
646 if (tp->t_outq.c_cc < tp->t_ohiwat)
647 (void)ttyoutput(CTRL('g'), tp);
648 }
649 goto endcase;
650 }
651
652 if ( c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP)
653 && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR))
654 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
655
656 /*
657 * Put data char in q for user and
658 * wakeup on seeing a line delimiter.
659 */
660 if (putc(c, &tp->t_rawq) >= 0) {
661 if (!ISSET(lflag, ICANON)) {
662 ttwakeup(tp);
663 ttyecho(c, tp);
664 goto endcase;
665 }
666 if (TTBREAKC(c, lflag)) {
667 tp->t_rocount = 0;
668 catq(&tp->t_rawq, &tp->t_canq);
669 ttwakeup(tp);
670 } else if (tp->t_rocount++ == 0)
671 tp->t_rocol = tp->t_column;
672 if (ISSET(tp->t_state, TS_ERASE)) {
673 /*
674 * end of prterase \.../
675 */
676 CLR(tp->t_state, TS_ERASE);
677 (void)ttyoutput('/', tp);
678 }
679 i = tp->t_column;
680 ttyecho(c, tp);
681 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
682 /*
683 * Place the cursor over the '^' of the ^D.
684 */
685 i = imin(2, tp->t_column - i);
686 while (i > 0) {
687 (void)ttyoutput('\b', tp);
688 i--;
689 }
690 }
691 }
692 endcase:
693 /*
694 * IXANY means allow any character to restart output.
695 */
696 if (ISSET(tp->t_state, TS_TTSTOP) &&
697 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
698 return (0);
699 restartoutput:
700 CLR(tp->t_lflag, FLUSHO);
701 CLR(tp->t_state, TS_TTSTOP);
702 startoutput:
703 return (ttstart(tp));
704 }
705
706 /*
707 * Output a single character on a tty, doing output processing
708 * as needed (expanding tabs, newline processing, etc.).
709 * Returns < 0 if succeeds, otherwise returns char to resend.
710 * Must be recursive.
711 */
712 static int
713 ttyoutput(int c, struct tty *tp)
714 {
715 tcflag_t oflag;
716 int col, s;
717
718 oflag = tp->t_oflag;
719 if (!ISSET(oflag, OPOST)) {
720 if (ISSET(tp->t_lflag, FLUSHO))
721 return (-1);
722 if (putc(c, &tp->t_outq))
723 return (c);
724 tk_nout++;
725 tp->t_outcc++;
726 return (-1);
727 }
728 /*
729 * Do tab expansion if OXTABS is set. Special case if we external
730 * processing, we don't do the tab expansion because we'll probably
731 * get it wrong. If tab expansion needs to be done, let it happen
732 * externally.
733 */
734 CLR(c, ~TTY_CHARMASK);
735 if (c == '\t' &&
736 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
737 c = 8 - (tp->t_column & 7);
738 if (!ISSET(tp->t_lflag, FLUSHO)) {
739 s = spltty(); /* Don't interrupt tabs. */
740 c -= b_to_q(" ", c, &tp->t_outq);
741 tk_nout += c;
742 tp->t_outcc += c;
743 splx(s);
744 }
745 tp->t_column += c;
746 return (c ? -1 : '\t');
747 }
748 if (c == CEOT && ISSET(oflag, ONOEOT))
749 return (-1);
750
751 /*
752 * Newline translation: if ONLCR is set,
753 * translate newline into "\r\n".
754 */
755 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
756 tk_nout++;
757 tp->t_outcc++;
758 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
759 return (c);
760 }
761 /* If OCRNL is set, translate "\r" into "\n". */
762 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
763 c = '\n';
764 /* If ONOCR is set, don't transmit CRs when on column 0. */
765 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
766 return (-1);
767
768 tk_nout++;
769 tp->t_outcc++;
770 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
771 return (c);
772
773 col = tp->t_column;
774 switch (CCLASS(c)) {
775 case BACKSPACE:
776 if (col > 0)
777 --col;
778 break;
779 case CONTROL:
780 break;
781 case NEWLINE:
782 if (ISSET(tp->t_oflag, ONLCR | ONLRET))
783 col = 0;
784 break;
785 case RETURN:
786 col = 0;
787 break;
788 case ORDINARY:
789 ++col;
790 break;
791 case TAB:
792 col = (col + 8) & ~7;
793 break;
794 }
795 tp->t_column = col;
796 return (-1);
797 }
798
799 /*
800 * Ioctls for all tty devices. Called after line-discipline specific ioctl
801 * has been called to do discipline-specific functions and/or reject any
802 * of these ioctl commands.
803 */
804 /* ARGSUSED */
805 int
806 ttioctl(struct tty *tp, u_long cmd, void *data, int flag)
807 {
808 struct proc *p;
809 struct thread *td;
810 struct pgrp *pgrp;
811 int s, error, bits, sig, sig2;
812
813 td = curthread; /* XXX */
814 p = td->td_proc;
815
816 /* If the ioctl involves modification, hang if in the background. */
817 switch (cmd) {
818 case TIOCCBRK:
819 case TIOCCONS:
820 case TIOCDRAIN:
821 case TIOCEXCL:
822 case TIOCFLUSH:
823 #ifdef TIOCHPCL
824 case TIOCHPCL:
825 #endif
826 case TIOCNXCL:
827 case TIOCSBRK:
828 case TIOCSCTTY:
829 case TIOCSDRAINWAIT:
830 case TIOCSETA:
831 case TIOCSETAF:
832 case TIOCSETAW:
833 case TIOCSETD:
834 case TIOCSPGRP:
835 case TIOCSTART:
836 case TIOCSTAT:
837 case TIOCSTI:
838 case TIOCSTOP:
839 case TIOCSWINSZ:
840 #if defined(COMPAT_43TTY)
841 case TIOCLBIC:
842 case TIOCLBIS:
843 case TIOCLSET:
844 case TIOCSETC:
845 case OTIOCSETD:
846 case TIOCSETN:
847 case TIOCSETP:
848 case TIOCSLTC:
849 #endif
850 sx_slock(&proctree_lock);
851 PROC_LOCK(p);
852 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) &&
853 !SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTOU) &&
854 !SIGISMEMBER(td->td_sigmask, SIGTTOU)) {
855 pgrp = p->p_pgrp;
856 PROC_UNLOCK(p);
857 if (pgrp->pg_jobc == 0) {
858 sx_sunlock(&proctree_lock);
859 return (EIO);
860 }
861 PGRP_LOCK(pgrp);
862 sx_sunlock(&proctree_lock);
863 pgsignal(pgrp, SIGTTOU, 1);
864 PGRP_UNLOCK(pgrp);
865 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, "ttybg1",
866 0);
867 if (error)
868 return (error);
869 sx_slock(&proctree_lock);
870 PROC_LOCK(p);
871 }
872 PROC_UNLOCK(p);
873 sx_sunlock(&proctree_lock);
874 break;
875 }
876
877
878 if (tp->t_modem != NULL) {
879 switch (cmd) {
880 case TIOCSDTR:
881 tt_modem(tp, SER_DTR, 0);
882 return (0);
883 case TIOCCDTR:
884 tt_modem(tp, 0, SER_DTR);
885 return (0);
886 case TIOCMSET:
887 bits = *(int *)data;
888 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1;
889 sig2 = ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1;
890 tt_modem(tp, sig, sig2);
891 return (0);
892 case TIOCMBIS:
893 bits = *(int *)data;
894 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1;
895 tt_modem(tp, sig, 0);
896 return (0);
897 case TIOCMBIC:
898 bits = *(int *)data;
899 sig = (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1;
900 tt_modem(tp, 0, sig);
901 return (0);
902 case TIOCMGET:
903 sig = tt_modem(tp, 0, 0);
904 /* See <sys/serial.h. for the "<< 1" stuff */
905 bits = TIOCM_LE + (sig << 1);
906 *(int *)data = bits;
907 return (0);
908 default:
909 break;
910 }
911 }
912
913 if (tp->t_pps != NULL) {
914 error = pps_ioctl(cmd, data, tp->t_pps);
915 if (error != ENOIOCTL)
916 return (error);
917 }
918
919 switch (cmd) { /* Process the ioctl. */
920 case FIOASYNC: /* set/clear async i/o */
921 s = spltty();
922 if (*(int *)data)
923 SET(tp->t_state, TS_ASYNC);
924 else
925 CLR(tp->t_state, TS_ASYNC);
926 splx(s);
927 break;
928 case FIONBIO: /* set/clear non-blocking i/o */
929 break; /* XXX: delete. */
930 case FIONREAD: /* get # bytes to read */
931 s = spltty();
932 *(int *)data = ttnread(tp);
933 splx(s);
934 break;
935
936 case FIOSETOWN:
937 /*
938 * Policy -- Don't allow FIOSETOWN on someone else's
939 * controlling tty
940 */
941 if (tp->t_session != NULL && !isctty(p, tp))
942 return (ENOTTY);
943
944 error = fsetown(*(int *)data, &tp->t_sigio);
945 if (error)
946 return (error);
947 break;
948 case FIOGETOWN:
949 if (tp->t_session != NULL && !isctty(p, tp))
950 return (ENOTTY);
951 *(int *)data = fgetown(&tp->t_sigio);
952 break;
953
954 case TIOCEXCL: /* set exclusive use of tty */
955 s = spltty();
956 SET(tp->t_state, TS_XCLUDE);
957 splx(s);
958 break;
959 case TIOCFLUSH: { /* flush buffers */
960 int flags = *(int *)data;
961
962 if (flags == 0)
963 flags = FREAD | FWRITE;
964 else
965 flags &= FREAD | FWRITE;
966 ttyflush(tp, flags);
967 break;
968 }
969 case TIOCCONS: /* become virtual console */
970 if (*(int *)data) {
971 struct nameidata nid;
972
973 if (constty && constty != tp &&
974 ISSET(constty->t_state, TS_CONNECTED))
975 return (EBUSY);
976
977 /* Ensure user can open the real console. */
978 NDINIT(&nid, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE,
979 "/dev/console", td);
980 if ((error = namei(&nid)) != 0)
981 return (error);
982 NDFREE(&nid, NDF_ONLY_PNBUF);
983 error = VOP_ACCESS(nid.ni_vp, VREAD, td->td_ucred, td);
984 vput(nid.ni_vp);
985 if (error)
986 return (error);
987
988 constty_set(tp);
989 } else if (tp == constty)
990 constty_clear();
991 break;
992 case TIOCDRAIN: /* wait till output drained */
993 error = ttywait(tp);
994 if (error)
995 return (error);
996 break;
997 case TIOCGETA: { /* get termios struct */
998 struct termios *t = (struct termios *)data;
999
1000 bcopy(&tp->t_termios, t, sizeof(struct termios));
1001 break;
1002 }
1003 case TIOCGETD: /* get line discipline */
1004 *(int *)data = tp->t_line;
1005 break;
1006 case TIOCGWINSZ: /* get window size */
1007 *(struct winsize *)data = tp->t_winsize;
1008 break;
1009 case TIOCGPGRP: /* get pgrp of tty */
1010 if (!isctty(p, tp))
1011 return (ENOTTY);
1012 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1013 break;
1014 #ifdef TIOCHPCL
1015 case TIOCHPCL: /* hang up on last close */
1016 s = spltty();
1017 SET(tp->t_cflag, HUPCL);
1018 splx(s);
1019 break;
1020 #endif
1021 case TIOCMGDTRWAIT:
1022 *(int *)data = tp->t_dtr_wait * 100 / hz;
1023 break;
1024 case TIOCMSDTRWAIT:
1025 /* must be root since the wait applies to following logins */
1026 error = priv_check(td, PRIV_TTY_DTRWAIT);
1027 if (error)
1028 return (error);
1029 tp->t_dtr_wait = *(int *)data * hz / 100;
1030 break;
1031 case TIOCNXCL: /* reset exclusive use of tty */
1032 s = spltty();
1033 CLR(tp->t_state, TS_XCLUDE);
1034 splx(s);
1035 break;
1036 case TIOCOUTQ: /* output queue size */
1037 *(int *)data = tp->t_outq.c_cc;
1038 break;
1039 case TIOCSETA: /* set termios struct */
1040 case TIOCSETAW: /* drain output, set */
1041 case TIOCSETAF: { /* drn out, fls in, set */
1042 struct termios *t = (struct termios *)data;
1043
1044 if (t->c_ispeed == 0)
1045 t->c_ispeed = t->c_ospeed;
1046 if (t->c_ispeed == 0)
1047 t->c_ispeed = tp->t_ospeed;
1048 if (t->c_ispeed == 0)
1049 return (EINVAL);
1050 s = spltty();
1051 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1052 error = ttywait(tp);
1053 if (error) {
1054 splx(s);
1055 return (error);
1056 }
1057 if (cmd == TIOCSETAF)
1058 ttyflush(tp, FREAD);
1059 }
1060 if (!ISSET(t->c_cflag, CIGNORE)) {
1061 /*
1062 * Set device hardware.
1063 */
1064 error = tt_param(tp, t);
1065 if (error) {
1066 splx(s);
1067 return (error);
1068 }
1069 if (ISSET(t->c_cflag, CLOCAL) &&
1070 !ISSET(tp->t_cflag, CLOCAL)) {
1071 /*
1072 * XXX disconnections would be too hard to
1073 * get rid of without this kludge. The only
1074 * way to get rid of controlling terminals
1075 * is to exit from the session leader.
1076 */
1077 CLR(tp->t_state, TS_ZOMBIE);
1078
1079 wakeup(TSA_CARR_ON(tp));
1080 ttwakeup(tp);
1081 ttwwakeup(tp);
1082 }
1083 if ((ISSET(tp->t_state, TS_CARR_ON) ||
1084 ISSET(t->c_cflag, CLOCAL)) &&
1085 !ISSET(tp->t_state, TS_ZOMBIE))
1086 SET(tp->t_state, TS_CONNECTED);
1087 else
1088 CLR(tp->t_state, TS_CONNECTED);
1089 tp->t_cflag = t->c_cflag;
1090 tp->t_ispeed = t->c_ispeed;
1091 if (t->c_ospeed != 0)
1092 tp->t_ospeed = t->c_ospeed;
1093 ttsetwater(tp);
1094 }
1095 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) &&
1096 cmd != TIOCSETAF) {
1097 if (ISSET(t->c_lflag, ICANON))
1098 SET(tp->t_lflag, PENDIN);
1099 else {
1100 /*
1101 * XXX we really shouldn't allow toggling
1102 * ICANON while we're in a non-termios line
1103 * discipline. Now we have to worry about
1104 * panicing for a null queue.
1105 */
1106 if (tp->t_canq.c_cbreserved > 0 &&
1107 tp->t_rawq.c_cbreserved > 0) {
1108 catq(&tp->t_rawq, &tp->t_canq);
1109 /*
1110 * XXX the queue limits may be
1111 * different, so the old queue
1112 * swapping method no longer works.
1113 */
1114 catq(&tp->t_canq, &tp->t_rawq);
1115 }
1116 CLR(tp->t_lflag, PENDIN);
1117 }
1118 ttwakeup(tp);
1119 }
1120 tp->t_iflag = t->c_iflag;
1121 tp->t_oflag = t->c_oflag;
1122 /*
1123 * Make the EXTPROC bit read only.
1124 */
1125 if (ISSET(tp->t_lflag, EXTPROC))
1126 SET(t->c_lflag, EXTPROC);
1127 else
1128 CLR(t->c_lflag, EXTPROC);
1129 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
1130 if (t->c_cc[VMIN] != tp->t_cc[VMIN] ||
1131 t->c_cc[VTIME] != tp->t_cc[VTIME])
1132 ttwakeup(tp);
1133 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
1134 splx(s);
1135 break;
1136 }
1137 case TIOCSETD: { /* set line discipline */
1138 int t = *(int *)data;
1139
1140 if ((u_int)t >= nlinesw)
1141 return (ENXIO);
1142 if (t == tp->t_line)
1143 return (0);
1144 s = spltty();
1145 ttyld_close(tp, flag);
1146 tp->t_line = t;
1147 /* XXX: we should use the correct cdev here */
1148 error = ttyld_open(tp, tp->t_dev);
1149 if (error) {
1150 /*
1151 * If we fail to switch line discipline we cannot
1152 * fall back to the previous, because we can not
1153 * trust that ldisc to open successfully either.
1154 * Fall back to the default ldisc which we know
1155 * will allways succeed.
1156 */
1157 tp->t_line = TTYDISC;
1158 (void)ttyld_open(tp, tp->t_dev);
1159 }
1160 splx(s);
1161 return (error);
1162 break;
1163 }
1164 case TIOCSTART: /* start output, like ^Q */
1165 s = spltty();
1166 if (ISSET(tp->t_state, TS_TTSTOP) ||
1167 ISSET(tp->t_lflag, FLUSHO)) {
1168 CLR(tp->t_lflag, FLUSHO);
1169 CLR(tp->t_state, TS_TTSTOP);
1170 ttstart(tp);
1171 }
1172 splx(s);
1173 break;
1174 case TIOCSTI: /* simulate terminal input */
1175 if ((flag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1176 return (EPERM);
1177 if (!isctty(p, tp) && priv_check(td, PRIV_TTY_STI))
1178 return (EACCES);
1179 s = spltty();
1180 ttyld_rint(tp, *(u_char *)data);
1181 splx(s);
1182 break;
1183 case TIOCSTOP: /* stop output, like ^S */
1184 s = spltty();
1185 if (!ISSET(tp->t_state, TS_TTSTOP)) {
1186 SET(tp->t_state, TS_TTSTOP);
1187 tt_stop(tp, 0);
1188 }
1189 splx(s);
1190 break;
1191 case TIOCSCTTY: /* become controlling tty */
1192 /* Session ctty vnode pointer set in vnode layer. */
1193 sx_slock(&proctree_lock);
1194 if (!SESS_LEADER(p) ||
1195 ((p->p_session->s_ttyvp || tp->t_session) &&
1196 (tp->t_session != p->p_session))) {
1197 sx_sunlock(&proctree_lock);
1198 return (EPERM);
1199 }
1200 tp->t_session = p->p_session;
1201 tp->t_pgrp = p->p_pgrp;
1202 SESS_LOCK(p->p_session);
1203 ttyref(tp); /* ttyrel(): kern_proc.c:pgdelete() */
1204 p->p_session->s_ttyp = tp;
1205 SESS_UNLOCK(p->p_session);
1206 PROC_LOCK(p);
1207 p->p_flag |= P_CONTROLT;
1208 PROC_UNLOCK(p);
1209 sx_sunlock(&proctree_lock);
1210 break;
1211 case TIOCSPGRP: { /* set pgrp of tty */
1212 sx_slock(&proctree_lock);
1213 pgrp = pgfind(*(int *)data);
1214 if (!isctty(p, tp)) {
1215 if (pgrp != NULL)
1216 PGRP_UNLOCK(pgrp);
1217 sx_sunlock(&proctree_lock);
1218 return (ENOTTY);
1219 }
1220 if (pgrp == NULL) {
1221 sx_sunlock(&proctree_lock);
1222 return (EPERM);
1223 }
1224 PGRP_UNLOCK(pgrp);
1225 if (pgrp->pg_session != p->p_session) {
1226 sx_sunlock(&proctree_lock);
1227 return (EPERM);
1228 }
1229 sx_sunlock(&proctree_lock);
1230 tp->t_pgrp = pgrp;
1231 break;
1232 }
1233 case TIOCSTAT: /* simulate control-T */
1234 s = spltty();
1235 ttyinfo(tp);
1236 splx(s);
1237 break;
1238 case TIOCSWINSZ: /* set window size */
1239 if (bcmp((caddr_t)&tp->t_winsize, data,
1240 sizeof (struct winsize))) {
1241 tp->t_winsize = *(struct winsize *)data;
1242 if (tp->t_pgrp != NULL) {
1243 PGRP_LOCK(tp->t_pgrp);
1244 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1245 PGRP_UNLOCK(tp->t_pgrp);
1246 }
1247 }
1248 break;
1249 case TIOCSDRAINWAIT:
1250 error = priv_check(td, PRIV_TTY_DRAINWAIT);
1251 if (error)
1252 return (error);
1253 tp->t_timeout = *(int *)data * hz;
1254 wakeup(TSA_OCOMPLETE(tp));
1255 wakeup(TSA_OLOWAT(tp));
1256 break;
1257 case TIOCGDRAINWAIT:
1258 *(int *)data = tp->t_timeout / hz;
1259 break;
1260 case TIOCSBRK:
1261 return (tt_break(tp, 1));
1262 case TIOCCBRK:
1263 return (tt_break(tp, 0));
1264 default:
1265 #if defined(COMPAT_43TTY)
1266 return (ttcompat(tp, cmd, data, flag));
1267 #else
1268 return (ENOIOCTL);
1269 #endif
1270 }
1271 return (0);
1272 }
1273
1274 int
1275 ttypoll(struct cdev *dev, int events, struct thread *td)
1276 {
1277 int s;
1278 int revents = 0;
1279 struct tty *tp;
1280
1281 tp = tty_gettp(dev);
1282
1283 if (tp == NULL) /* XXX used to return ENXIO, but that means true! */
1284 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM))
1285 | POLLHUP);
1286
1287 s = spltty();
1288 if (events & (POLLIN | POLLRDNORM)) {
1289 if (ISSET(tp->t_state, TS_ZOMBIE))
1290 revents |= (events & (POLLIN | POLLRDNORM)) |
1291 POLLHUP;
1292 else if (ttnread(tp) > 0)
1293 revents |= events & (POLLIN | POLLRDNORM);
1294 else
1295 selrecord(td, &tp->t_rsel);
1296 }
1297 if (events & POLLOUT) {
1298 if (ISSET(tp->t_state, TS_ZOMBIE))
1299 revents |= POLLHUP;
1300 else if (tp->t_outq.c_cc <= tp->t_olowat &&
1301 ISSET(tp->t_state, TS_CONNECTED))
1302 revents |= events & POLLOUT;
1303 else
1304 selrecord(td, &tp->t_wsel);
1305 }
1306 splx(s);
1307 return (revents);
1308 }
1309
1310 static struct filterops ttyread_filtops =
1311 { 1, NULL, filt_ttyrdetach, filt_ttyread };
1312 static struct filterops ttywrite_filtops =
1313 { 1, NULL, filt_ttywdetach, filt_ttywrite };
1314
1315 int
1316 ttykqfilter(struct cdev *dev, struct knote *kn)
1317 {
1318 struct tty *tp;
1319 struct knlist *klist;
1320 int s;
1321
1322 tp = tty_gettp(dev);
1323 if (tp == NULL || (tp->t_state & TS_GONE))
1324 return (ENODEV);
1325
1326 switch (kn->kn_filter) {
1327 case EVFILT_READ:
1328 klist = &tp->t_rsel.si_note;
1329 kn->kn_fop = &ttyread_filtops;
1330 break;
1331 case EVFILT_WRITE:
1332 klist = &tp->t_wsel.si_note;
1333 kn->kn_fop = &ttywrite_filtops;
1334 break;
1335 default:
1336 return (EINVAL);
1337 }
1338
1339 kn->kn_hook = (caddr_t)tp;
1340
1341 s = spltty();
1342 knlist_add(klist, kn, 0);
1343 splx(s);
1344
1345 return (0);
1346 }
1347
1348 static void
1349 filt_ttyrdetach(struct knote *kn)
1350 {
1351 struct tty *tp = (struct tty *)kn->kn_hook;
1352 int s = spltty();
1353
1354 knlist_remove(&tp->t_rsel.si_note, kn, 0);
1355 splx(s);
1356 }
1357
1358 static int
1359 filt_ttyread(struct knote *kn, long hint)
1360 {
1361 struct tty *tp = (struct tty *)kn->kn_hook;
1362
1363 kn->kn_data = ttnread(tp);
1364 if ((tp->t_state & TS_GONE) || ISSET(tp->t_state, TS_ZOMBIE)) {
1365 kn->kn_flags |= EV_EOF;
1366 return (1);
1367 }
1368 return (kn->kn_data > 0);
1369 }
1370
1371 static void
1372 filt_ttywdetach(struct knote *kn)
1373 {
1374 struct tty *tp = (struct tty *)kn->kn_hook;
1375 int s = spltty();
1376
1377 knlist_remove(&tp->t_wsel.si_note, kn, 0);
1378 splx(s);
1379 }
1380
1381 static int
1382 filt_ttywrite(struct knote *kn, long hint)
1383 {
1384 struct tty *tp = (struct tty *)kn->kn_hook;
1385
1386 kn->kn_data = tp->t_outq.c_cc;
1387 if ((tp->t_state & TS_GONE) || ISSET(tp->t_state, TS_ZOMBIE))
1388 return (1);
1389 return (kn->kn_data <= tp->t_olowat &&
1390 ISSET(tp->t_state, TS_CONNECTED));
1391 }
1392
1393 /*
1394 * Must be called at spltty().
1395 */
1396 static int
1397 ttnread(struct tty *tp)
1398 {
1399 int nread;
1400
1401 if (ISSET(tp->t_lflag, PENDIN))
1402 ttypend(tp);
1403 nread = tp->t_canq.c_cc;
1404 if (!ISSET(tp->t_lflag, ICANON)) {
1405 nread += tp->t_rawq.c_cc;
1406 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0)
1407 nread = 0;
1408 }
1409 return (nread);
1410 }
1411
1412 /*
1413 * Wait for output to drain.
1414 */
1415 int
1416 ttywait(struct tty *tp)
1417 {
1418 int error, s;
1419
1420 error = 0;
1421 s = spltty();
1422 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1423 ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) {
1424 tt_oproc(tp);
1425 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1426 ISSET(tp->t_state, TS_CONNECTED)) {
1427 SET(tp->t_state, TS_SO_OCOMPLETE);
1428 error = ttysleep(tp, TSA_OCOMPLETE(tp),
1429 TTOPRI | PCATCH, "ttywai",
1430 tp->t_timeout);
1431 if (error) {
1432 if (error == EWOULDBLOCK)
1433 error = EIO;
1434 break;
1435 }
1436 } else
1437 break;
1438 }
1439 if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)))
1440 error = EIO;
1441 splx(s);
1442 return (error);
1443 }
1444
1445 /*
1446 * Flush if successfully wait.
1447 */
1448 static int
1449 ttywflush(struct tty *tp)
1450 {
1451 int error;
1452
1453 if ((error = ttywait(tp)) == 0)
1454 ttyflush(tp, FREAD);
1455 return (error);
1456 }
1457
1458 /*
1459 * Flush tty read and/or write queues, notifying anyone waiting.
1460 */
1461 void
1462 ttyflush(struct tty *tp, int rw)
1463 {
1464 int s;
1465
1466 s = spltty();
1467 #if 0
1468 again:
1469 #endif
1470 if (rw & FWRITE) {
1471 FLUSHQ(&tp->t_outq);
1472 CLR(tp->t_state, TS_TTSTOP);
1473 }
1474 tt_stop(tp, rw);
1475 if (rw & FREAD) {
1476 FLUSHQ(&tp->t_canq);
1477 FLUSHQ(&tp->t_rawq);
1478 CLR(tp->t_lflag, PENDIN);
1479 tp->t_rocount = 0;
1480 tp->t_rocol = 0;
1481 CLR(tp->t_state, TS_LOCAL);
1482 ttwakeup(tp);
1483 if (ISSET(tp->t_state, TS_TBLOCK)) {
1484 if (rw & FWRITE)
1485 FLUSHQ(&tp->t_outq);
1486 ttyunblock(tp);
1487
1488 /*
1489 * Don't let leave any state that might clobber the
1490 * next line discipline (although we should do more
1491 * to send the START char). Not clearing the state
1492 * may have caused the "putc to a clist with no
1493 * reserved cblocks" panic/printf.
1494 */
1495 CLR(tp->t_state, TS_TBLOCK);
1496
1497 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1498 if (ISSET(tp->t_iflag, IXOFF)) {
1499 /*
1500 * XXX wait a bit in the hope that the stop
1501 * character (if any) will go out. Waiting
1502 * isn't good since it allows races. This
1503 * will be fixed when the stop character is
1504 * put in a special queue. Don't bother with
1505 * the checks in ttywait() since the timeout
1506 * will save us.
1507 */
1508 SET(tp->t_state, TS_SO_OCOMPLETE);
1509 ttysleep(tp, TSA_OCOMPLETE(tp), TTOPRI,
1510 "ttyfls", hz / 10);
1511 /*
1512 * Don't try sending the stop character again.
1513 */
1514 CLR(tp->t_state, TS_TBLOCK);
1515 goto again;
1516 }
1517 #endif
1518 }
1519 }
1520 if (rw & FWRITE) {
1521 FLUSHQ(&tp->t_outq);
1522 ttwwakeup(tp);
1523 }
1524 splx(s);
1525 }
1526
1527 /*
1528 * Copy in the default termios characters.
1529 */
1530 void
1531 termioschars(struct termios *t)
1532 {
1533
1534 bcopy(ttydefchars, t->c_cc, sizeof t->c_cc);
1535 }
1536
1537 /*
1538 * Old interface.
1539 */
1540 void
1541 ttychars(struct tty *tp)
1542 {
1543
1544 termioschars(&tp->t_termios);
1545 }
1546
1547 /*
1548 * Handle input high water. Send stop character for the IXOFF case. Turn
1549 * on our input flow control bit and propagate the changes to the driver.
1550 * XXX the stop character should be put in a special high priority queue.
1551 */
1552 void
1553 ttyblock(struct tty *tp)
1554 {
1555
1556 SET(tp->t_state, TS_TBLOCK);
1557 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1558 putc(tp->t_cc[VSTOP], &tp->t_outq) != 0)
1559 CLR(tp->t_state, TS_TBLOCK); /* try again later */
1560 ttstart(tp);
1561 }
1562
1563 /*
1564 * Handle input low water. Send start character for the IXOFF case. Turn
1565 * off our input flow control bit and propagate the changes to the driver.
1566 * XXX the start character should be put in a special high priority queue.
1567 */
1568 static void
1569 ttyunblock(struct tty *tp)
1570 {
1571
1572 CLR(tp->t_state, TS_TBLOCK);
1573 if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE &&
1574 putc(tp->t_cc[VSTART], &tp->t_outq) != 0)
1575 SET(tp->t_state, TS_TBLOCK); /* try again later */
1576 ttstart(tp);
1577 }
1578
1579 #ifdef notyet
1580 /* Not used by any current (i386) drivers. */
1581 /*
1582 * Restart after an inter-char delay.
1583 */
1584 void
1585 ttrstrt(void *tp_arg)
1586 {
1587 struct tty *tp;
1588 int s;
1589
1590 KASSERT(tp_arg != NULL, ("ttrstrt"));
1591
1592 tp = tp_arg;
1593 s = spltty();
1594
1595 CLR(tp->t_state, TS_TIMEOUT);
1596 ttstart(tp);
1597
1598 splx(s);
1599 }
1600 #endif
1601
1602 int
1603 ttstart(struct tty *tp)
1604 {
1605
1606 tt_oproc(tp);
1607 return (0);
1608 }
1609
1610 /*
1611 * "close" a line discipline
1612 */
1613 int
1614 ttylclose(struct tty *tp, int flag)
1615 {
1616
1617 if (flag & FNONBLOCK || ttywflush(tp))
1618 ttyflush(tp, FREAD | FWRITE);
1619 return (0);
1620 }
1621
1622 /*
1623 * Handle modem control transition on a tty.
1624 * Flag indicates new state of carrier.
1625 * Returns 0 if the line should be turned off, otherwise 1.
1626 */
1627 int
1628 ttymodem(struct tty *tp, int flag)
1629 {
1630
1631 if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) {
1632 /*
1633 * MDMBUF: do flow control according to carrier flag
1634 * XXX TS_CAR_OFLOW doesn't do anything yet. TS_TTSTOP
1635 * works if IXON and IXANY are clear.
1636 */
1637 if (flag) {
1638 CLR(tp->t_state, TS_CAR_OFLOW);
1639 CLR(tp->t_state, TS_TTSTOP);
1640 ttstart(tp);
1641 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) {
1642 SET(tp->t_state, TS_CAR_OFLOW);
1643 SET(tp->t_state, TS_TTSTOP);
1644 tt_stop(tp, 0);
1645 }
1646 } else if (flag == 0) {
1647 /*
1648 * Lost carrier.
1649 */
1650 CLR(tp->t_state, TS_CARR_ON);
1651 if (ISSET(tp->t_state, TS_ISOPEN) &&
1652 !ISSET(tp->t_cflag, CLOCAL)) {
1653 SET(tp->t_state, TS_ZOMBIE);
1654 CLR(tp->t_state, TS_CONNECTED);
1655 if (tp->t_session) {
1656 sx_slock(&proctree_lock);
1657 if (tp->t_session && tp->t_session->s_leader) {
1658 struct proc *p;
1659
1660 p = tp->t_session->s_leader;
1661 PROC_LOCK(p);
1662 psignal(p, SIGHUP);
1663 PROC_UNLOCK(p);
1664 }
1665 sx_sunlock(&proctree_lock);
1666 }
1667 ttyflush(tp, FREAD | FWRITE);
1668 return (0);
1669 }
1670 } else {
1671 /*
1672 * Carrier now on.
1673 */
1674 SET(tp->t_state, TS_CARR_ON);
1675 if (!ISSET(tp->t_state, TS_ZOMBIE))
1676 SET(tp->t_state, TS_CONNECTED);
1677 wakeup(TSA_CARR_ON(tp));
1678 ttwakeup(tp);
1679 ttwwakeup(tp);
1680 }
1681 return (1);
1682 }
1683
1684 /*
1685 * Reinput pending characters after state switch
1686 * call at spltty().
1687 */
1688 static void
1689 ttypend(struct tty *tp)
1690 {
1691 struct clist tq;
1692 int c;
1693
1694 CLR(tp->t_lflag, PENDIN);
1695 SET(tp->t_state, TS_TYPEN);
1696 /*
1697 * XXX this assumes too much about clist internals. It may even
1698 * fail if the cblock slush pool is empty. We can't allocate more
1699 * cblocks here because we are called from an interrupt handler
1700 * and clist_alloc_cblocks() can wait.
1701 */
1702 tq = tp->t_rawq;
1703 bzero(&tp->t_rawq, sizeof tp->t_rawq);
1704 tp->t_rawq.c_cbmax = tq.c_cbmax;
1705 tp->t_rawq.c_cbreserved = tq.c_cbreserved;
1706 while ((c = getc(&tq)) >= 0)
1707 ttyinput(c, tp);
1708 CLR(tp->t_state, TS_TYPEN);
1709 }
1710
1711 /*
1712 * Process a read call on a tty device.
1713 */
1714 int
1715 ttread(struct tty *tp, struct uio *uio, int flag)
1716 {
1717 struct clist *qp;
1718 int c;
1719 tcflag_t lflag;
1720 cc_t *cc = tp->t_cc;
1721 struct thread *td;
1722 struct proc *p;
1723 int s, first, error = 0;
1724 int has_stime = 0, last_cc = 0;
1725 long slp = 0; /* XXX this should be renamed `timo'. */
1726 struct timeval stime = { 0, 0 };
1727 struct pgrp *pg;
1728
1729 td = curthread;
1730 p = td->td_proc;
1731 loop:
1732 s = spltty();
1733 lflag = tp->t_lflag;
1734 /*
1735 * take pending input first
1736 */
1737 if (ISSET(lflag, PENDIN)) {
1738 ttypend(tp);
1739 splx(s); /* reduce latency */
1740 s = spltty();
1741 lflag = tp->t_lflag; /* XXX ttypend() clobbers it */
1742 }
1743
1744 /*
1745 * Hang process if it's in the background.
1746 */
1747 if (isbackground(p, tp)) {
1748 splx(s);
1749 sx_slock(&proctree_lock);
1750 PROC_LOCK(p);
1751 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTIN) ||
1752 SIGISMEMBER(td->td_sigmask, SIGTTIN) ||
1753 (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0) {
1754 PROC_UNLOCK(p);
1755 sx_sunlock(&proctree_lock);
1756 return (EIO);
1757 }
1758 pg = p->p_pgrp;
1759 PROC_UNLOCK(p);
1760 PGRP_LOCK(pg);
1761 sx_sunlock(&proctree_lock);
1762 pgsignal(pg, SIGTTIN, 1);
1763 PGRP_UNLOCK(pg);
1764 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg2", 0);
1765 if (error)
1766 return (error);
1767 goto loop;
1768 }
1769
1770 if (ISSET(tp->t_state, TS_ZOMBIE)) {
1771 splx(s);
1772 return (0); /* EOF */
1773 }
1774
1775 /*
1776 * If canonical, use the canonical queue,
1777 * else use the raw queue.
1778 *
1779 * (should get rid of clists...)
1780 */
1781 qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq;
1782
1783 if (flag & IO_NDELAY) {
1784 if (qp->c_cc > 0)
1785 goto read;
1786 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) {
1787 splx(s);
1788 return (0);
1789 }
1790 splx(s);
1791 return (EWOULDBLOCK);
1792 }
1793 if (!ISSET(lflag, ICANON)) {
1794 int m = cc[VMIN];
1795 long t = cc[VTIME];
1796 struct timeval timecopy;
1797
1798 /*
1799 * Check each of the four combinations.
1800 * (m > 0 && t == 0) is the normal read case.
1801 * It should be fairly efficient, so we check that and its
1802 * companion case (m == 0 && t == 0) first.
1803 * For the other two cases, we compute the target sleep time
1804 * into slp.
1805 */
1806 if (t == 0) {
1807 if (qp->c_cc < m)
1808 goto sleep;
1809 if (qp->c_cc > 0)
1810 goto read;
1811
1812 /* m, t and qp->c_cc are all 0. 0 is enough input. */
1813 splx(s);
1814 return (0);
1815 }
1816 t *= 100000; /* time in us */
1817 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1818 ((t1).tv_usec - (t2).tv_usec))
1819 if (m > 0) {
1820 if (qp->c_cc <= 0)
1821 goto sleep;
1822 if (qp->c_cc >= m)
1823 goto read;
1824 getmicrotime(&timecopy);
1825 if (!has_stime) {
1826 /* first character, start timer */
1827 has_stime = 1;
1828 stime = timecopy;
1829 slp = t;
1830 } else if (qp->c_cc > last_cc) {
1831 /* got a character, restart timer */
1832 stime = timecopy;
1833 slp = t;
1834 } else {
1835 /* nothing, check expiration */
1836 slp = t - diff(timecopy, stime);
1837 if (slp <= 0)
1838 goto read;
1839 }
1840 last_cc = qp->c_cc;
1841 } else { /* m == 0 */
1842 if (qp->c_cc > 0)
1843 goto read;
1844 getmicrotime(&timecopy);
1845 if (!has_stime) {
1846 has_stime = 1;
1847 stime = timecopy;
1848 slp = t;
1849 } else {
1850 slp = t - diff(timecopy, stime);
1851 if (slp <= 0) {
1852 /* Timed out, but 0 is enough input. */
1853 splx(s);
1854 return (0);
1855 }
1856 }
1857 }
1858 #undef diff
1859 if (slp != 0) {
1860 struct timeval tv; /* XXX style bug. */
1861
1862 tv.tv_sec = slp / 1000000;
1863 tv.tv_usec = slp % 1000000;
1864 slp = tvtohz(&tv);
1865 /*
1866 * XXX bad variable names. slp was the timeout in
1867 * usec. Now it is the timeout in ticks.
1868 */
1869 }
1870 goto sleep;
1871 }
1872 if (qp->c_cc <= 0) {
1873 sleep:
1874 /*
1875 * There is no input, or not enough input and we can block.
1876 */
1877 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), TTIPRI | PCATCH,
1878 ISSET(tp->t_state, TS_CONNECTED) ?
1879 "ttyin" : "ttyhup", (int)slp);
1880 splx(s);
1881 if (error == EWOULDBLOCK)
1882 error = 0;
1883 else if (error)
1884 return (error);
1885 /*
1886 * XXX what happens if another process eats some input
1887 * while we are asleep (not just here)? It would be
1888 * safest to detect changes and reset our state variables
1889 * (has_stime and last_cc).
1890 */
1891 slp = 0;
1892 goto loop;
1893 }
1894 read:
1895 splx(s);
1896 /*
1897 * Input present, check for input mapping and processing.
1898 */
1899 first = 1;
1900 if (ISSET(lflag, ICANON | ISIG))
1901 goto slowcase;
1902 for (;;) {
1903 char ibuf[IBUFSIZ];
1904 int icc;
1905
1906 icc = imin(uio->uio_resid, IBUFSIZ);
1907 icc = q_to_b(qp, ibuf, icc);
1908 if (icc <= 0) {
1909 if (first)
1910 goto loop;
1911 break;
1912 }
1913 error = uiomove(ibuf, icc, uio);
1914 /*
1915 * XXX if there was an error then we should ungetc() the
1916 * unmoved chars and reduce icc here.
1917 */
1918 if (error)
1919 break;
1920 if (uio->uio_resid == 0)
1921 break;
1922 first = 0;
1923 }
1924 goto out;
1925 slowcase:
1926 for (;;) {
1927 c = getc(qp);
1928 if (c < 0) {
1929 if (first)
1930 goto loop;
1931 break;
1932 }
1933 /*
1934 * delayed suspend (^Y)
1935 */
1936 if (CCEQ(cc[VDSUSP], c) &&
1937 ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) {
1938 if (tp->t_pgrp != NULL) {
1939 PGRP_LOCK(tp->t_pgrp);
1940 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1941 PGRP_UNLOCK(tp->t_pgrp);
1942 }
1943 if (first) {
1944 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH,
1945 "ttybg3", 0);
1946 if (error)
1947 break;
1948 goto loop;
1949 }
1950 break;
1951 }
1952 /*
1953 * Interpret EOF only in canonical mode.
1954 */
1955 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1956 break;
1957 /*
1958 * Give user character.
1959 */
1960 error = ureadc(c, uio);
1961 if (error)
1962 /* XXX should ungetc(c, qp). */
1963 break;
1964 if (uio->uio_resid == 0)
1965 break;
1966 /*
1967 * In canonical mode check for a "break character"
1968 * marking the end of a "line of input".
1969 */
1970 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1971 break;
1972 first = 0;
1973 }
1974
1975 out:
1976 /*
1977 * Look to unblock input now that (presumably)
1978 * the input queue has gone down.
1979 */
1980 s = spltty();
1981 if (ISSET(tp->t_state, TS_TBLOCK) &&
1982 tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat)
1983 ttyunblock(tp);
1984 splx(s);
1985
1986 return (error);
1987 }
1988
1989 /*
1990 * Check the output queue on tp for space for a kernel message (from uprintf
1991 * or tprintf). Allow some space over the normal hiwater mark so we don't
1992 * lose messages due to normal flow control, but don't let the tty run amok.
1993 * Sleeps here are not interruptible, but we return prematurely if new signals
1994 * arrive.
1995 */
1996 int
1997 ttycheckoutq(struct tty *tp, int wait)
1998 {
1999 int hiwat, s;
2000 sigset_t oldmask;
2001 struct thread *td;
2002 struct proc *p;
2003
2004 td = curthread;
2005 p = td->td_proc;
2006 hiwat = tp->t_ohiwat;
2007 SIGEMPTYSET(oldmask);
2008 s = spltty();
2009 if (wait) {
2010 PROC_LOCK(p);
2011 oldmask = td->td_siglist;
2012 PROC_UNLOCK(p);
2013 }
2014 if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100)
2015 while (tp->t_outq.c_cc > hiwat) {
2016 ttstart(tp);
2017 if (tp->t_outq.c_cc <= hiwat)
2018 break;
2019 if (!wait) {
2020 splx(s);
2021 return (0);
2022 }
2023 PROC_LOCK(p);
2024 if (!SIGSETEQ(td->td_siglist, oldmask)) {
2025 PROC_UNLOCK(p);
2026 splx(s);
2027 return (0);
2028 }
2029 PROC_UNLOCK(p);
2030 SET(tp->t_state, TS_SO_OLOWAT);
2031 tsleep(TSA_OLOWAT(tp), PZERO - 1, "ttoutq", hz);
2032 }
2033 splx(s);
2034 return (1);
2035 }
2036
2037 /*
2038 * Process a write call on a tty device.
2039 */
2040 int
2041 ttwrite(struct tty *tp, struct uio *uio, int flag)
2042 {
2043 char *cp = NULL;
2044 int cc, ce;
2045 struct thread *td;
2046 struct proc *p;
2047 int i, hiwat, cnt, error, s;
2048 char obuf[OBUFSIZ];
2049
2050 hiwat = tp->t_ohiwat;
2051 cnt = uio->uio_resid;
2052 error = 0;
2053 cc = 0;
2054 td = curthread;
2055 p = td->td_proc;
2056 loop:
2057 s = spltty();
2058 if (ISSET(tp->t_state, TS_ZOMBIE)) {
2059 splx(s);
2060 if (uio->uio_resid == cnt)
2061 error = EIO;
2062 goto out;
2063 }
2064 if (!ISSET(tp->t_state, TS_CONNECTED)) {
2065 if (flag & IO_NDELAY) {
2066 splx(s);
2067 error = EWOULDBLOCK;
2068 goto out;
2069 }
2070 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
2071 "ttywdcd", 0);
2072 splx(s);
2073 if (error)
2074 goto out;
2075 goto loop;
2076 }
2077 splx(s);
2078 /*
2079 * Hang the process if it's in the background.
2080 */
2081 sx_slock(&proctree_lock);
2082 PROC_LOCK(p);
2083 if (isbackground(p, tp) &&
2084 ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) &&
2085 !SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTOU) &&
2086 !SIGISMEMBER(td->td_sigmask, SIGTTOU)) {
2087 if (p->p_pgrp->pg_jobc == 0) {
2088 PROC_UNLOCK(p);
2089 sx_sunlock(&proctree_lock);
2090 error = EIO;
2091 goto out;
2092 }
2093 PROC_UNLOCK(p);
2094 PGRP_LOCK(p->p_pgrp);
2095 sx_sunlock(&proctree_lock);
2096 pgsignal(p->p_pgrp, SIGTTOU, 1);
2097 PGRP_UNLOCK(p->p_pgrp);
2098 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ttybg4", 0);
2099 if (error)
2100 goto out;
2101 goto loop;
2102 } else {
2103 PROC_UNLOCK(p);
2104 sx_sunlock(&proctree_lock);
2105 }
2106 /*
2107 * Process the user's data in at most OBUFSIZ chunks. Perform any
2108 * output translation. Keep track of high water mark, sleep on
2109 * overflow awaiting device aid in acquiring new space.
2110 */
2111 while (uio->uio_resid > 0 || cc > 0) {
2112 if (ISSET(tp->t_lflag, FLUSHO)) {
2113 uio->uio_resid = 0;
2114 return (0);
2115 }
2116 if (tp->t_outq.c_cc > hiwat)
2117 goto ovhiwat;
2118 /*
2119 * Grab a hunk of data from the user, unless we have some
2120 * leftover from last time.
2121 */
2122 if (cc == 0) {
2123 cc = imin(uio->uio_resid, OBUFSIZ);
2124 cp = obuf;
2125 error = uiomove(cp, cc, uio);
2126 if (error) {
2127 cc = 0;
2128 break;
2129 }
2130 }
2131 /*
2132 * If nothing fancy need be done, grab those characters we
2133 * can handle without any of ttyoutput's processing and
2134 * just transfer them to the output q. For those chars
2135 * which require special processing (as indicated by the
2136 * bits in char_type), call ttyoutput. After processing
2137 * a hunk of data, look for FLUSHO so ^O's will take effect
2138 * immediately.
2139 */
2140 while (cc > 0) {
2141 if (!ISSET(tp->t_oflag, OPOST))
2142 ce = cc;
2143 else {
2144 ce = cc - scanc((u_int)cc, (u_char *)cp,
2145 char_type, CCLASSMASK);
2146 /*
2147 * If ce is zero, then we're processing
2148 * a special character through ttyoutput.
2149 */
2150 if (ce == 0) {
2151 tp->t_rocount = 0;
2152 if (ttyoutput(*cp, tp) >= 0) {
2153 /* No Clists, wait a bit. */
2154 ttstart(tp);
2155 if (flag & IO_NDELAY) {
2156 error = EWOULDBLOCK;
2157 goto out;
2158 }
2159 error = ttysleep(tp, &lbolt,
2160 TTOPRI|PCATCH,
2161 "ttybf1", 0);
2162 if (error)
2163 goto out;
2164 goto loop;
2165 }
2166 cp++;
2167 cc--;
2168 if (ISSET(tp->t_lflag, FLUSHO) ||
2169 tp->t_outq.c_cc > hiwat)
2170 goto ovhiwat;
2171 continue;
2172 }
2173 }
2174 /*
2175 * A bunch of normal characters have been found.
2176 * Transfer them en masse to the output queue and
2177 * continue processing at the top of the loop.
2178 * If there are any further characters in this
2179 * <= OBUFSIZ chunk, the first should be a character
2180 * requiring special handling by ttyoutput.
2181 */
2182 tp->t_rocount = 0;
2183 i = b_to_q(cp, ce, &tp->t_outq);
2184 ce -= i;
2185 tp->t_column += ce;
2186 cp += ce, cc -= ce, tk_nout += ce;
2187 tp->t_outcc += ce;
2188 if (i > 0) {
2189 /* No Clists, wait a bit. */
2190 ttstart(tp);
2191 if (flag & IO_NDELAY) {
2192 error = EWOULDBLOCK;
2193 goto out;
2194 }
2195 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH,
2196 "ttybf2", 0);
2197 if (error)
2198 goto out;
2199 goto loop;
2200 }
2201 if (ISSET(tp->t_lflag, FLUSHO) ||
2202 tp->t_outq.c_cc > hiwat)
2203 break;
2204 }
2205 ttstart(tp);
2206 }
2207 out:
2208 /*
2209 * If cc is nonzero, we leave the uio structure inconsistent, as the
2210 * offset and iov pointers have moved forward, but it doesn't matter
2211 * (the call will either return short or restart with a new uio).
2212 */
2213 uio->uio_resid += cc;
2214 return (error);
2215
2216 ovhiwat:
2217 ttstart(tp);
2218 s = spltty();
2219 /*
2220 * This can only occur if FLUSHO is set in t_lflag,
2221 * or if ttstart/oproc is synchronous (or very fast).
2222 */
2223 if (tp->t_outq.c_cc <= hiwat) {
2224 splx(s);
2225 goto loop;
2226 }
2227 if (flag & IO_NDELAY) {
2228 splx(s);
2229 uio->uio_resid += cc;
2230 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
2231 }
2232 SET(tp->t_state, TS_SO_OLOWAT);
2233 error = ttysleep(tp, TSA_OLOWAT(tp), TTOPRI | PCATCH, "ttywri",
2234 tp->t_timeout);
2235 splx(s);
2236 if (error == EWOULDBLOCK)
2237 error = EIO;
2238 if (error)
2239 goto out;
2240 goto loop;
2241 }
2242
2243 /*
2244 * Rubout one character from the rawq of tp
2245 * as cleanly as possible.
2246 */
2247 static void
2248 ttyrub(int c, struct tty *tp)
2249 {
2250 char *cp;
2251 int savecol;
2252 int tabc, s;
2253
2254 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
2255 return;
2256 CLR(tp->t_lflag, FLUSHO);
2257 if (ISSET(tp->t_lflag, ECHOE)) {
2258 if (tp->t_rocount == 0) {
2259 /*
2260 * Screwed by ttwrite; retype
2261 */
2262 ttyretype(tp);
2263 return;
2264 }
2265 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
2266 ttyrubo(tp, 2);
2267 else {
2268 CLR(c, ~TTY_CHARMASK);
2269 switch (CCLASS(c)) {
2270 case ORDINARY:
2271 ttyrubo(tp, 1);
2272 break;
2273 case BACKSPACE:
2274 case CONTROL:
2275 case NEWLINE:
2276 case RETURN:
2277 case VTAB:
2278 if (ISSET(tp->t_lflag, ECHOCTL))
2279 ttyrubo(tp, 2);
2280 break;
2281 case TAB:
2282 if (tp->t_rocount < tp->t_rawq.c_cc) {
2283 ttyretype(tp);
2284 return;
2285 }
2286 s = spltty();
2287 savecol = tp->t_column;
2288 SET(tp->t_state, TS_CNTTB);
2289 SET(tp->t_lflag, FLUSHO);
2290 tp->t_column = tp->t_rocol;
2291 cp = tp->t_rawq.c_cf;
2292 if (cp)
2293 tabc = *cp; /* XXX FIX NEXTC */
2294 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc))
2295 ttyecho(tabc, tp);
2296 CLR(tp->t_lflag, FLUSHO);
2297 CLR(tp->t_state, TS_CNTTB);
2298 splx(s);
2299
2300 /* savecol will now be length of the tab. */
2301 savecol -= tp->t_column;
2302 tp->t_column += savecol;
2303 if (savecol > 8)
2304 savecol = 8; /* overflow screw */
2305 while (--savecol >= 0)
2306 (void)ttyoutput('\b', tp);
2307 break;
2308 default: /* XXX */
2309 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
2310 (void)printf(PANICSTR, c, CCLASS(c));
2311 #ifdef notdef
2312 panic(PANICSTR, c, CCLASS(c));
2313 #endif
2314 }
2315 }
2316 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
2317 if (!ISSET(tp->t_state, TS_ERASE)) {
2318 SET(tp->t_state, TS_ERASE);
2319 (void)ttyoutput('\\', tp);
2320 }
2321 ttyecho(c, tp);
2322 } else {
2323 ttyecho(tp->t_cc[VERASE], tp);
2324 /*
2325 * This code may be executed not only when an ERASE key
2326 * is pressed, but also when ^U (KILL) or ^W (WERASE) are.
2327 * So, I didn't think it was worthwhile to pass the extra
2328 * information (which would need an extra parameter,
2329 * changing every call) needed to distinguish the ERASE2
2330 * case from the ERASE.
2331 */
2332 }
2333 --tp->t_rocount;
2334 }
2335
2336 /*
2337 * Back over cnt characters, erasing them.
2338 */
2339 static void
2340 ttyrubo(struct tty *tp, int cnt)
2341 {
2342
2343 while (cnt-- > 0) {
2344 (void)ttyoutput('\b', tp);
2345 (void)ttyoutput(' ', tp);
2346 (void)ttyoutput('\b', tp);
2347 }
2348 }
2349
2350 /*
2351 * ttyretype --
2352 * Reprint the rawq line. Note, it is assumed that c_cc has already
2353 * been checked.
2354 */
2355 static void
2356 ttyretype(struct tty *tp)
2357 {
2358 char *cp;
2359 int s, c;
2360
2361 /* Echo the reprint character. */
2362 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
2363 ttyecho(tp->t_cc[VREPRINT], tp);
2364
2365 (void)ttyoutput('\n', tp);
2366
2367 /*
2368 * XXX
2369 * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2370 * BIT OF FIRST CHAR.
2371 */
2372 s = spltty();
2373 for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0);
2374 cp != NULL; cp = nextc(&tp->t_canq, cp, &c))
2375 ttyecho(c, tp);
2376 for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0);
2377 cp != NULL; cp = nextc(&tp->t_rawq, cp, &c))
2378 ttyecho(c, tp);
2379 CLR(tp->t_state, TS_ERASE);
2380 splx(s);
2381
2382 tp->t_rocount = tp->t_rawq.c_cc;
2383 tp->t_rocol = 0;
2384 }
2385
2386 /*
2387 * Echo a typed character to the terminal.
2388 */
2389 static void
2390 ttyecho(int c, struct tty *tp)
2391 {
2392
2393 if (!ISSET(tp->t_state, TS_CNTTB))
2394 CLR(tp->t_lflag, FLUSHO);
2395 if ((!ISSET(tp->t_lflag, ECHO) &&
2396 (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) ||
2397 ISSET(tp->t_lflag, EXTPROC))
2398 return;
2399 if (ISSET(tp->t_lflag, ECHOCTL) &&
2400 ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') ||
2401 ISSET(c, TTY_CHARMASK) == 0177)) {
2402 (void)ttyoutput('^', tp);
2403 CLR(c, ~TTY_CHARMASK);
2404 if (c == 0177)
2405 c = '?';
2406 else
2407 c += 'A' - 1;
2408 }
2409 (void)ttyoutput(c, tp);
2410 }
2411
2412 /*
2413 * Wake up any readers on a tty.
2414 */
2415 void
2416 ttwakeup(struct tty *tp)
2417 {
2418
2419 if (SEL_WAITING(&tp->t_rsel))
2420 selwakeuppri(&tp->t_rsel, TTIPRI);
2421 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2422 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
2423 wakeup(TSA_HUP_OR_INPUT(tp));
2424 KNOTE_UNLOCKED(&tp->t_rsel.si_note, 0);
2425 }
2426
2427 /*
2428 * Wake up any writers on a tty.
2429 */
2430 void
2431 ttwwakeup(struct tty *tp)
2432 {
2433
2434 if (SEL_WAITING(&tp->t_wsel) && tp->t_outq.c_cc <= tp->t_olowat)
2435 selwakeuppri(&tp->t_wsel, TTOPRI);
2436 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2437 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
2438 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
2439 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
2440 CLR(tp->t_state, TS_SO_OCOMPLETE);
2441 wakeup(TSA_OCOMPLETE(tp));
2442 }
2443 if (ISSET(tp->t_state, TS_SO_OLOWAT) &&
2444 tp->t_outq.c_cc <= tp->t_olowat) {
2445 CLR(tp->t_state, TS_SO_OLOWAT);
2446 wakeup(TSA_OLOWAT(tp));
2447 }
2448 KNOTE_UNLOCKED(&tp->t_wsel.si_note, 0);
2449 }
2450
2451 /*
2452 * Look up a code for a specified speed in a conversion table;
2453 * used by drivers to map software speed values to hardware parameters.
2454 */
2455 int
2456 ttspeedtab(int speed, struct speedtab *table)
2457 {
2458
2459 for ( ; table->sp_speed != -1; table++)
2460 if (table->sp_speed == speed)
2461 return (table->sp_code);
2462 return (-1);
2463 }
2464
2465 /*
2466 * Set input and output watermarks and buffer sizes. For input, the
2467 * high watermark is about one second's worth of input above empty, the
2468 * low watermark is slightly below high water, and the buffer size is a
2469 * driver-dependent amount above high water. For output, the watermarks
2470 * are near the ends of the buffer, with about 1 second's worth of input
2471 * between them. All this only applies to the standard line discipline.
2472 */
2473 void
2474 ttsetwater(struct tty *tp)
2475 {
2476 int cps, ttmaxhiwat, x;
2477
2478 /* Input. */
2479 clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512);
2480 switch (tp->t_ispeedwat) {
2481 case (speed_t)-1:
2482 cps = tp->t_ispeed / 10;
2483 break;
2484 case 0:
2485 /*
2486 * This case is for old drivers that don't know about
2487 * t_ispeedwat. Arrange for them to get the old buffer
2488 * sizes and watermarks.
2489 */
2490 cps = TTYHOG - 2 * 256;
2491 tp->t_ififosize = 2 * 256;
2492 break;
2493 default:
2494 cps = tp->t_ispeedwat / 10;
2495 break;
2496 }
2497 tp->t_ihiwat = cps;
2498 tp->t_ilowat = 7 * cps / 8;
2499 x = cps + tp->t_ififosize;
2500 clist_alloc_cblocks(&tp->t_rawq, x, x);
2501
2502 /* Output. */
2503 switch (tp->t_ospeedwat) {
2504 case (speed_t)-1:
2505 cps = tp->t_ospeed / 10;
2506 ttmaxhiwat = 2 * TTMAXHIWAT;
2507 break;
2508 case 0:
2509 cps = tp->t_ospeed / 10;
2510 ttmaxhiwat = TTMAXHIWAT;
2511 break;
2512 default:
2513 cps = tp->t_ospeedwat / 10;
2514 ttmaxhiwat = 8 * TTMAXHIWAT;
2515 break;
2516 }
2517 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
2518 tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
2519 x += cps;
2520 x = CLAMP(x, ttmaxhiwat, TTMINHIWAT); /* XXX clamps are too magic */
2521 tp->t_ohiwat = roundup(x, CBSIZE); /* XXX for compat */
2522 x = imax(tp->t_ohiwat, TTMAXHIWAT); /* XXX for compat/safety */
2523 x += OBUFSIZ + 100;
2524 clist_alloc_cblocks(&tp->t_outq, x, x);
2525 #undef CLAMP
2526 }
2527
2528 /*
2529 * Report on state of foreground process group.
2530 */
2531 void
2532 ttyinfo(struct tty *tp)
2533 {
2534 struct timeval utime, stime;
2535 struct proc *p, *pick;
2536 struct thread *td, *picktd;
2537 const char *stateprefix, *state;
2538 long rss;
2539 int load, pctcpu;
2540 pid_t pid;
2541 char comm[MAXCOMLEN + 1];
2542 struct rusage ru;
2543
2544 if (ttycheckoutq(tp,0) == 0)
2545 return;
2546
2547 /* Print load average. */
2548 load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
2549 ttyprintf(tp, "load: %d.%02d ", load / 100, load % 100);
2550
2551 /*
2552 * On return following a ttyprintf(), we set tp->t_rocount to 0 so
2553 * that pending input will be retyped on BS.
2554 */
2555 if (tp->t_session == NULL) {
2556 ttyprintf(tp, "not a controlling terminal\n");
2557 tp->t_rocount = 0;
2558 return;
2559 }
2560 if (tp->t_pgrp == NULL) {
2561 ttyprintf(tp, "no foreground process group\n");
2562 tp->t_rocount = 0;
2563 return;
2564 }
2565 PGRP_LOCK(tp->t_pgrp);
2566 if (LIST_EMPTY(&tp->t_pgrp->pg_members)) {
2567 PGRP_UNLOCK(tp->t_pgrp);
2568 ttyprintf(tp, "empty foreground process group\n");
2569 tp->t_rocount = 0;
2570 return;
2571 }
2572
2573 /*
2574 * Pick the most interesting process and copy some of its
2575 * state for printing later. This operation could rely on stale
2576 * data as we can't hold the proc slock or thread locks over the
2577 * whole list. However, we're guaranteed not to reference an exited
2578 * thread or proc since we hold the tty locked.
2579 */
2580 pick = NULL;
2581 LIST_FOREACH(p, &tp->t_pgrp->pg_members, p_pglist)
2582 if (proc_compare(pick, p))
2583 pick = p;
2584
2585 PROC_SLOCK(pick);
2586 picktd = NULL;
2587 td = FIRST_THREAD_IN_PROC(pick);
2588 FOREACH_THREAD_IN_PROC(pick, td)
2589 if (thread_compare(picktd, td))
2590 picktd = td;
2591 td = picktd;
2592 stateprefix = "";
2593 thread_lock(td);
2594 if (TD_IS_RUNNING(td))
2595 state = "running";
2596 else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
2597 state = "runnable";
2598 else if (TD_IS_SLEEPING(td)) {
2599 /* XXX: If we're sleeping, are we ever not in a queue? */
2600 if (TD_ON_SLEEPQ(td))
2601 state = td->td_wmesg;
2602 else
2603 state = "sleeping without queue";
2604 } else if (TD_ON_LOCK(td)) {
2605 state = td->td_lockname;
2606 stateprefix = "*";
2607 } else if (TD_IS_SUSPENDED(td))
2608 state = "suspended";
2609 else if (TD_AWAITING_INTR(td))
2610 state = "intrwait";
2611 else if (pick->p_state == PRS_ZOMBIE)
2612 state = "zombie";
2613 else
2614 state = "unknown";
2615 pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
2616 thread_unlock(td);
2617 if (pick->p_state == PRS_NEW || pick->p_state == PRS_ZOMBIE)
2618 rss = 0;
2619 else
2620 rss = pgtok(vmspace_resident_count(pick->p_vmspace));
2621 PROC_SUNLOCK(pick);
2622 PROC_LOCK(pick);
2623 PGRP_UNLOCK(tp->t_pgrp);
2624 rufetchcalc(pick, &ru, &utime, &stime);
2625 pid = pick->p_pid;
2626 bcopy(pick->p_comm, comm, sizeof(comm));
2627 PROC_UNLOCK(pick);
2628
2629 /* Print command, pid, state, utime, stime, %cpu, and rss. */
2630 ttyprintf(tp,
2631 " cmd: %s %d [%s%s] %ld.%02ldu %ld.%02lds %d%% %ldk\n",
2632 comm, pid, stateprefix, state,
2633 (long)utime.tv_sec, utime.tv_usec / 10000,
2634 (long)stime.tv_sec, stime.tv_usec / 10000,
2635 pctcpu / 100, rss);
2636 tp->t_rocount = 0;
2637 }
2638
2639 /*
2640 * Returns 1 if p2 is "better" than p1
2641 *
2642 * The algorithm for picking the "interesting" process is thus:
2643 *
2644 * 1) Only foreground processes are eligible - implied.
2645 * 2) Runnable processes are favored over anything else. The runner
2646 * with the highest cpu utilization is picked (p_estcpu). Ties are
2647 * broken by picking the highest pid.
2648 * 3) The sleeper with the shortest sleep time is next. With ties,
2649 * we pick out just "short-term" sleepers (P_SINTR == 0).
2650 * 4) Further ties are broken by picking the highest pid.
2651 */
2652
2653 #define TESTAB(a, b) ((a)<<1 | (b))
2654 #define ONLYA 2
2655 #define ONLYB 1
2656 #define BOTH 3
2657
2658 static int
2659 proc_sum(struct proc *p, int *estcpup)
2660 {
2661 struct thread *td;
2662 int estcpu;
2663 int val;
2664
2665 val = 0;
2666 estcpu = 0;
2667 FOREACH_THREAD_IN_PROC(p, td) {
2668 thread_lock(td);
2669 if (TD_ON_RUNQ(td) ||
2670 TD_IS_RUNNING(td))
2671 val = 1;
2672 estcpu += sched_pctcpu(td);
2673 thread_unlock(td);
2674 }
2675 *estcpup = estcpu;
2676
2677 return (val);
2678 }
2679
2680 static int
2681 thread_compare(struct thread *td, struct thread *td2)
2682 {
2683 int runa, runb;
2684 int slpa, slpb;
2685 fixpt_t esta, estb;
2686
2687 if (td == NULL)
2688 return (1);
2689
2690 /*
2691 * Fetch running stats, pctcpu usage, and interruptable flag.
2692 */
2693 thread_lock(td);
2694 runa = TD_IS_RUNNING(td) | TD_ON_RUNQ(td);
2695 slpa = td->td_flags & TDF_SINTR;
2696 esta = sched_pctcpu(td);
2697 thread_unlock(td);
2698 thread_lock(td2);
2699 runb = TD_IS_RUNNING(td2) | TD_ON_RUNQ(td2);
2700 estb = sched_pctcpu(td2);
2701 slpb = td2->td_flags & TDF_SINTR;
2702 thread_unlock(td2);
2703 /*
2704 * see if at least one of them is runnable
2705 */
2706 switch (TESTAB(runa, runb)) {
2707 case ONLYA:
2708 return (0);
2709 case ONLYB:
2710 return (1);
2711 case BOTH:
2712 break;
2713 }
2714 /*
2715 * favor one with highest recent cpu utilization
2716 */
2717 if (estb > esta)
2718 return (1);
2719 if (esta > estb)
2720 return (0);
2721 /*
2722 * favor one sleeping in a non-interruptible sleep
2723 */
2724 switch (TESTAB(slpa, slpb)) {
2725 case ONLYA:
2726 return (0);
2727 case ONLYB:
2728 return (1);
2729 case BOTH:
2730 break;
2731 }
2732
2733 return (td < td2);
2734 }
2735
2736 static int
2737 proc_compare(struct proc *p1, struct proc *p2)
2738 {
2739
2740 int runa, runb;
2741 fixpt_t esta, estb;
2742
2743 if (p1 == NULL)
2744 return (1);
2745
2746 /*
2747 * Fetch various stats about these processes. After we drop the
2748 * lock the information could be stale but the race is unimportant.
2749 */
2750 PROC_SLOCK(p1);
2751 runa = proc_sum(p1, &esta);
2752 PROC_SUNLOCK(p1);
2753 PROC_SLOCK(p2);
2754 runb = proc_sum(p2, &estb);
2755 PROC_SUNLOCK(p2);
2756
2757 /*
2758 * see if at least one of them is runnable
2759 */
2760 switch (TESTAB(runa, runb)) {
2761 case ONLYA:
2762 return (0);
2763 case ONLYB:
2764 return (1);
2765 case BOTH:
2766 break;
2767 }
2768 /*
2769 * favor one with highest recent cpu utilization
2770 */
2771 if (estb > esta)
2772 return (1);
2773 if (esta > estb)
2774 return (0);
2775 /*
2776 * weed out zombies
2777 */
2778 switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) {
2779 case ONLYA:
2780 return (1);
2781 case ONLYB:
2782 return (0);
2783 case BOTH:
2784 break;
2785 }
2786
2787 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2788 }
2789
2790 /*
2791 * Output char to tty; console putchar style.
2792 */
2793 int
2794 tputchar(int c, struct tty *tp)
2795 {
2796 int s;
2797
2798 s = spltty();
2799 if (!ISSET(tp->t_state, TS_CONNECTED)) {
2800 splx(s);
2801 return (-1);
2802 }
2803 if (c == '\n')
2804 (void)ttyoutput('\r', tp);
2805 (void)ttyoutput(c, tp);
2806 ttstart(tp);
2807 splx(s);
2808 return (0);
2809 }
2810
2811 /*
2812 * Sleep on chan, returning ERESTART if tty changed while we napped and
2813 * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep. If
2814 * the tty is revoked, restarting a pending call will redo validation done
2815 * at the start of the call.
2816 */
2817 int
2818 ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo)
2819 {
2820 int error;
2821 int gen;
2822
2823 gen = tp->t_gen;
2824 error = tsleep(chan, pri, wmesg, timo);
2825 if (tp->t_state & TS_GONE)
2826 return (ENXIO);
2827 if (error)
2828 return (error);
2829 return (tp->t_gen == gen ? 0 : ERESTART);
2830 }
2831
2832 /*
2833 * Gain a reference to a TTY
2834 */
2835 int
2836 ttyref(struct tty *tp)
2837 {
2838 int i;
2839
2840 mtx_lock(&tp->t_mtx);
2841 KASSERT(tp->t_refcnt > 0,
2842 ("ttyref(): tty refcnt is %d (%s)",
2843 tp->t_refcnt, tp->t_dev != NULL ? devtoname(tp->t_dev) : "??"));
2844 i = ++tp->t_refcnt;
2845 mtx_unlock(&tp->t_mtx);
2846 return (i);
2847 }
2848
2849 /*
2850 * Drop a reference to a TTY.
2851 * When reference count drops to zero, we free it.
2852 */
2853 int
2854 ttyrel(struct tty *tp)
2855 {
2856 int i;
2857
2858 mtx_lock(&tty_list_mutex);
2859 mtx_lock(&tp->t_mtx);
2860 KASSERT(tp->t_refcnt > 0,
2861 ("ttyrel(): tty refcnt is %d (%s)",
2862 tp->t_refcnt, tp->t_dev != NULL ? devtoname(tp->t_dev) : "??"));
2863 i = --tp->t_refcnt;
2864 if (i != 0) {
2865 mtx_unlock(&tp->t_mtx);
2866 mtx_unlock(&tty_list_mutex);
2867 return (i);
2868 }
2869 TAILQ_REMOVE(&tty_list, tp, t_list);
2870 mtx_unlock(&tp->t_mtx);
2871 mtx_unlock(&tty_list_mutex);
2872 knlist_destroy(&tp->t_rsel.si_note);
2873 knlist_destroy(&tp->t_wsel.si_note);
2874 mtx_destroy(&tp->t_mtx);
2875 free(tp, M_TTYS);
2876 return (i);
2877 }
2878
2879 /*
2880 * Allocate a tty struct. Clists in the struct will be allocated by
2881 * tty_open().
2882 */
2883 struct tty *
2884 ttyalloc()
2885 {
2886 struct tty *tp;
2887
2888 tp = malloc(sizeof *tp, M_TTYS, M_WAITOK | M_ZERO);
2889 mtx_init(&tp->t_mtx, "tty", NULL, MTX_DEF);
2890
2891 /*
2892 * Set up the initial state
2893 */
2894 tp->t_refcnt = 1;
2895 tp->t_timeout = -1;
2896 tp->t_dtr_wait = 3 * hz;
2897
2898 ttyinitmode(tp, 0, 0);
2899 bcopy(ttydefchars, tp->t_init_in.c_cc, sizeof tp->t_init_in.c_cc);
2900
2901 /* Make callout the same as callin */
2902 tp->t_init_out = tp->t_init_in;
2903
2904 mtx_lock(&tty_list_mutex);
2905 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
2906 mtx_unlock(&tty_list_mutex);
2907 knlist_init_mtx(&tp->t_rsel.si_note, &tp->t_mtx);
2908 knlist_init_mtx(&tp->t_wsel.si_note, &tp->t_mtx);
2909 return (tp);
2910 }
2911
2912 static void
2913 ttypurge(struct cdev *dev)
2914 {
2915
2916 if (dev->si_tty == NULL)
2917 return;
2918 ttygone(dev->si_tty);
2919 }
2920
2921 /*
2922 * ttycreate()
2923 *
2924 * Create the device entries for this tty thereby opening it for business.
2925 *
2926 * The flags argument controls if "cua" units are created.
2927 *
2928 * The t_sc filed is copied to si_drv1 in the created cdevs. This
2929 * is particularly important for ->t_cioctl() users.
2930 *
2931 * XXX: implement the init and lock devices by cloning.
2932 */
2933
2934 int
2935 ttycreate(struct tty *tp, int flags, const char *fmt, ...)
2936 {
2937 char namebuf[SPECNAMELEN - 3]; /* XXX space for "tty" */
2938 struct cdevsw *csw = NULL;
2939 int unit = 0;
2940 va_list ap;
2941 struct cdev *cp;
2942 int i, minor, sminor, sunit;
2943
2944 mtx_assert(&Giant, MA_OWNED);
2945
2946 if (tty_unit == NULL)
2947 tty_unit = new_unrhdr(0, 0xffff, NULL);
2948
2949 sunit = alloc_unr(tty_unit);
2950 tp->t_devunit = sunit;
2951
2952 if (csw == NULL) {
2953 csw = &tty_cdevsw;
2954 unit = sunit;
2955 }
2956 KASSERT(csw->d_purge == NULL || csw->d_purge == ttypurge,
2957 ("tty should not have d_purge"));
2958
2959 csw->d_purge = ttypurge;
2960
2961 minor = unit2minor(unit);
2962 sminor = unit2minor(sunit);
2963 va_start(ap, fmt);
2964 i = vsnrprintf(namebuf, sizeof namebuf, 32, fmt, ap);
2965 va_end(ap);
2966 KASSERT(i < sizeof namebuf, ("Too long tty name (%s)", namebuf));
2967
2968 cp = make_dev(csw, minor,
2969 UID_ROOT, GID_WHEEL, 0600, "tty%s", namebuf);
2970 tp->t_dev = cp;
2971 tp->t_mdev = cp;
2972 cp->si_tty = tp;
2973 cp->si_drv1 = tp->t_sc;
2974
2975 cp = make_dev(&ttys_cdevsw, sminor | MINOR_INIT,
2976 UID_ROOT, GID_WHEEL, 0600, "tty%s.init", namebuf);
2977 dev_depends(tp->t_dev, cp);
2978 cp->si_drv1 = tp->t_sc;
2979 cp->si_drv2 = &tp->t_init_in;
2980 cp->si_tty = tp;
2981
2982 cp = make_dev(&ttys_cdevsw, sminor | MINOR_LOCK,
2983 UID_ROOT, GID_WHEEL, 0600, "tty%s.lock", namebuf);
2984 dev_depends(tp->t_dev, cp);
2985 cp->si_drv1 = tp->t_sc;
2986 cp->si_drv2 = &tp->t_lock_in;
2987 cp->si_tty = tp;
2988
2989 if (flags & TS_CALLOUT) {
2990 cp = make_dev(csw, minor | MINOR_CALLOUT,
2991 UID_UUCP, GID_DIALER, 0660, "cua%s", namebuf);
2992 dev_depends(tp->t_dev, cp);
2993 cp->si_drv1 = tp->t_sc;
2994 cp->si_tty = tp;
2995
2996 cp = make_dev(&ttys_cdevsw, sminor | MINOR_CALLOUT | MINOR_INIT,
2997 UID_UUCP, GID_DIALER, 0660, "cua%s.init", namebuf);
2998 dev_depends(tp->t_dev, cp);
2999 cp->si_drv1 = tp->t_sc;
3000 cp->si_drv2 = &tp->t_init_out;
3001 cp->si_tty = tp;
3002
3003 cp = make_dev(&ttys_cdevsw, sminor | MINOR_CALLOUT | MINOR_LOCK,
3004 UID_UUCP, GID_DIALER, 0660, "cua%s.lock", namebuf);
3005 dev_depends(tp->t_dev, cp);
3006 cp->si_drv1 = tp->t_sc;
3007 cp->si_drv2 = &tp->t_lock_out;
3008 cp->si_tty = tp;
3009 }
3010
3011 return (0);
3012 }
3013
3014 /*
3015 * This function is called when the hardware disappears. We set a flag
3016 * and wake up stuff so all sleeping threads will notice.
3017 */
3018 void
3019 ttygone(struct tty *tp)
3020 {
3021
3022 tp->t_state |= TS_GONE;
3023 if (SEL_WAITING(&tp->t_rsel))
3024 selwakeuppri(&tp->t_rsel, TTIPRI);
3025 if (SEL_WAITING(&tp->t_wsel))
3026 selwakeuppri(&tp->t_wsel, TTOPRI);
3027 if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
3028 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
3029 wakeup(&tp->t_dtr_wait);
3030 wakeup(TSA_CARR_ON(tp));
3031 wakeup(TSA_HUP_OR_INPUT(tp));
3032 wakeup(TSA_OCOMPLETE(tp));
3033 wakeup(TSA_OLOWAT(tp));
3034 KNOTE_UNLOCKED(&tp->t_rsel.si_note, 0);
3035 KNOTE_UNLOCKED(&tp->t_wsel.si_note, 0);
3036 tt_purge(tp);
3037 }
3038
3039 /*
3040 * ttyfree()
3041 *
3042 * Called when the driver is ready to free the tty structure.
3043 *
3044 * XXX: This shall sleep until all threads have left the driver.
3045 */
3046 void
3047 ttyfree(struct tty *tp)
3048 {
3049 struct cdev *dev;
3050 u_int unit;
3051
3052 mtx_assert(&Giant, MA_OWNED);
3053 ttygone(tp);
3054 unit = tp->t_devunit;
3055 dev = tp->t_mdev;
3056 dev->si_tty = NULL;
3057 tp->t_dev = NULL;
3058 destroy_dev(dev);
3059 ttyrel(tp);
3060 free_unr(tty_unit, unit);
3061 }
3062
3063 static int
3064 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
3065 {
3066 struct tty *tp, *tp2;
3067 struct xtty xt;
3068 int error;
3069
3070 error = 0;
3071 mtx_lock(&tty_list_mutex);
3072 tp = TAILQ_FIRST(&tty_list);
3073 if (tp != NULL)
3074 ttyref(tp);
3075 while (tp != NULL) {
3076 if (tp->t_state & TS_GONE)
3077 goto nexttp;
3078 bzero(&xt, sizeof xt);
3079 xt.xt_size = sizeof xt;
3080 #define XT_COPY(field) xt.xt_##field = tp->t_##field
3081 xt.xt_rawcc = tp->t_rawq.c_cc;
3082 xt.xt_cancc = tp->t_canq.c_cc;
3083 xt.xt_outcc = tp->t_outq.c_cc;
3084 XT_COPY(line);
3085
3086 /*
3087 * XXX: We hold the tty list lock while doing this to
3088 * work around a race with pty/pts tty destruction.
3089 * They set t_dev to NULL and then call ttyrel() to
3090 * free the structure which will block on the list
3091 * lock before they call destroy_dev() on the cdev
3092 * backing t_dev.
3093 *
3094 * XXX: ttyfree() now does the same since it has been
3095 * fixed to not leak ttys.
3096 */
3097 if (tp->t_dev != NULL)
3098 xt.xt_dev = dev2udev(tp->t_dev);
3099 XT_COPY(state);
3100 XT_COPY(flags);
3101 XT_COPY(timeout);
3102 if (tp->t_pgrp != NULL)
3103 xt.xt_pgid = tp->t_pgrp->pg_id;
3104 if (tp->t_session != NULL)
3105 xt.xt_sid = tp->t_session->s_sid;
3106 XT_COPY(termios);
3107 XT_COPY(winsize);
3108 XT_COPY(column);
3109 XT_COPY(rocount);
3110 XT_COPY(rocol);
3111 XT_COPY(ififosize);
3112 XT_COPY(ihiwat);
3113 XT_COPY(ilowat);
3114 XT_COPY(ispeedwat);
3115 XT_COPY(ohiwat);
3116 XT_COPY(olowat);
3117 XT_COPY(ospeedwat);
3118 #undef XT_COPY
3119 mtx_unlock(&tty_list_mutex);
3120 error = SYSCTL_OUT(req, &xt, sizeof xt);
3121 if (error != 0) {
3122 ttyrel(tp);
3123 return (error);
3124 }
3125 mtx_lock(&tty_list_mutex);
3126 nexttp: tp2 = TAILQ_NEXT(tp, t_list);
3127 if (tp2 != NULL)
3128 ttyref(tp2);
3129 mtx_unlock(&tty_list_mutex);
3130 ttyrel(tp);
3131 tp = tp2;
3132 mtx_lock(&tty_list_mutex);
3133 }
3134 mtx_unlock(&tty_list_mutex);
3135 return (0);
3136 }
3137
3138 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD,
3139 0, 0, sysctl_kern_ttys, "S,xtty", "All ttys");
3140 SYSCTL_LONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
3141 &tk_nin, 0, "Total TTY in characters");
3142 SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
3143 &tk_nout, 0, "Total TTY out characters");
3144
3145 void
3146 nottystop(struct tty *tp, int rw)
3147 {
3148
3149 return;
3150 }
3151
3152 int
3153 ttyopen(struct cdev *dev, int flag, int mode, struct thread *td)
3154 {
3155 int error;
3156 int s;
3157 struct tty *tp;
3158
3159 tp = dev->si_tty;
3160
3161 s = spltty();
3162 /*
3163 * We jump to this label after all non-interrupted sleeps to pick
3164 * up any changes of the device state.
3165 */
3166 open_top:
3167 if (tp->t_state & TS_GONE)
3168 return (ENXIO);
3169 error = ttydtrwaitsleep(tp);
3170 if (error)
3171 goto out;
3172 if (tp->t_state & TS_ISOPEN) {
3173 /*
3174 * The device is open, so everything has been initialized.
3175 * Handle conflicts.
3176 */
3177 if (ISCALLOUT(dev) && !tp->t_actout)
3178 return (EBUSY);
3179 if (tp->t_actout && !ISCALLOUT(dev)) {
3180 if (flag & O_NONBLOCK)
3181 return (EBUSY);
3182 error = tsleep(&tp->t_actout,
3183 TTIPRI | PCATCH, "ttybi", 0);
3184 if (error != 0 || (tp->t_state & TS_GONE))
3185 goto out;
3186 goto open_top;
3187 }
3188 if (tp->t_state & TS_XCLUDE && priv_check(td,
3189 PRIV_TTY_EXCLUSIVE))
3190 return (EBUSY);
3191 } else {
3192 /*
3193 * The device isn't open, so there are no conflicts.
3194 * Initialize it. Initialization is done twice in many
3195 * cases: to preempt sleeping callin opens if we are
3196 * callout, and to complete a callin open after DCD rises.
3197 */
3198 tp->t_termios = ISCALLOUT(dev) ? tp->t_init_out : tp->t_init_in;
3199 tp->t_cflag = tp->t_termios.c_cflag;
3200 if (tp->t_modem != NULL)
3201 tt_modem(tp, SER_DTR | SER_RTS, 0);
3202 ++tp->t_wopeners;
3203 error = tt_param(tp, &tp->t_termios);
3204 --tp->t_wopeners;
3205 if (error == 0)
3206 error = tt_open(tp, dev);
3207 if (error != 0)
3208 goto out;
3209 if (ISCALLOUT(dev) || (tt_modem(tp, 0, 0) & SER_DCD))
3210 ttyld_modem(tp, 1);
3211 }
3212 /*
3213 * Wait for DCD if necessary.
3214 */
3215 if (!(tp->t_state & TS_CARR_ON) && !ISCALLOUT(dev)
3216 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
3217 ++tp->t_wopeners;
3218 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "ttydcd", 0);
3219 --tp->t_wopeners;
3220 if (error != 0 || (tp->t_state & TS_GONE))
3221 goto out;
3222 goto open_top;
3223 }
3224 error = ttyld_open(tp, dev);
3225 ttyldoptim(tp);
3226 if (tp->t_state & TS_ISOPEN && ISCALLOUT(dev))
3227 tp->t_actout = TRUE;
3228 out:
3229 splx(s);
3230 if (!(tp->t_state & TS_ISOPEN) && tp->t_wopeners == 0)
3231 tt_close(tp);
3232 return (error);
3233 }
3234
3235 int
3236 ttyclose(struct cdev *dev, int flag, int mode, struct thread *td)
3237 {
3238 struct tty *tp;
3239
3240 tp = dev->si_tty;
3241 ttyld_close(tp, flag);
3242 ttyldoptim(tp);
3243 tt_close(tp);
3244 tp->t_do_timestamp = 0;
3245 if (tp->t_pps != NULL)
3246 tp->t_pps->ppsparam.mode = 0;
3247 tty_close(tp);
3248 return (0);
3249 }
3250
3251 int
3252 ttyread(struct cdev *dev, struct uio *uio, int flag)
3253 {
3254 struct tty *tp;
3255
3256 tp = tty_gettp(dev);
3257
3258 if (tp == NULL || (tp->t_state & TS_GONE))
3259 return (ENODEV);
3260 return (ttyld_read(tp, uio, flag));
3261 }
3262
3263 int
3264 ttywrite(struct cdev *dev, struct uio *uio, int flag)
3265 {
3266 struct tty *tp;
3267
3268 tp = tty_gettp(dev);
3269
3270 if (tp == NULL || (tp->t_state & TS_GONE))
3271 return (ENODEV);
3272 return (ttyld_write(tp, uio, flag));
3273 }
3274
3275 int
3276 ttyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
3277 {
3278 struct tty *tp;
3279 int error;
3280
3281 tp = dev->si_tty;
3282
3283 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
3284 int cc;
3285 struct termios *dt = (struct termios *)data;
3286 struct termios *lt =
3287 ISCALLOUT(dev) ? &tp->t_lock_out : &tp->t_lock_in;
3288
3289 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
3290 | (dt->c_iflag & ~lt->c_iflag);
3291 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
3292 | (dt->c_oflag & ~lt->c_oflag);
3293 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
3294 | (dt->c_cflag & ~lt->c_cflag);
3295 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
3296 | (dt->c_lflag & ~lt->c_lflag);
3297 for (cc = 0; cc < NCCS; ++cc)
3298 if (lt->c_cc[cc] != 0)
3299 dt->c_cc[cc] = tp->t_cc[cc];
3300 if (lt->c_ispeed != 0)
3301 dt->c_ispeed = tp->t_ispeed;
3302 if (lt->c_ospeed != 0)
3303 dt->c_ospeed = tp->t_ospeed;
3304 }
3305
3306 error = ttyld_ioctl(tp, cmd, data, flag, td);
3307 if (error == ENOIOCTL)
3308 error = ttioctl(tp, cmd, data, flag);
3309 ttyldoptim(tp);
3310 if (error != ENOIOCTL)
3311 return (error);
3312 return (ENOTTY);
3313 }
3314
3315 void
3316 ttyldoptim(struct tty *tp)
3317 {
3318 struct termios *t;
3319
3320 t = &tp->t_termios;
3321 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
3322 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
3323 && (!(t->c_iflag & PARMRK)
3324 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
3325 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
3326 && linesw[tp->t_line]->l_rint == ttyinput)
3327 tp->t_state |= TS_CAN_BYPASS_L_RINT;
3328 else
3329 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
3330 }
3331
3332 static void
3333 ttydtrwaitwakeup(void *arg)
3334 {
3335 struct tty *tp;
3336
3337 tp = arg;
3338 tp->t_state &= ~TS_DTR_WAIT;
3339 wakeup(&tp->t_dtr_wait);
3340 }
3341
3342
3343 void
3344 ttydtrwaitstart(struct tty *tp)
3345 {
3346
3347 if (tp->t_dtr_wait == 0)
3348 return;
3349 if (tp->t_state & TS_DTR_WAIT)
3350 return;
3351 timeout(ttydtrwaitwakeup, tp, tp->t_dtr_wait);
3352 tp->t_state |= TS_DTR_WAIT;
3353 }
3354
3355 int
3356 ttydtrwaitsleep(struct tty *tp)
3357 {
3358 int error;
3359
3360 error = 0;
3361 while (error == 0) {
3362 if (tp->t_state & TS_GONE)
3363 error = ENXIO;
3364 else if (!(tp->t_state & TS_DTR_WAIT))
3365 break;
3366 else
3367 error = tsleep(&tp->t_dtr_wait, TTIPRI | PCATCH,
3368 "dtrwait", 0);
3369 }
3370 return (error);
3371 }
3372
3373 static int
3374 ttysopen(struct cdev *dev, int flag, int mode, struct thread *td)
3375 {
3376 struct tty *tp;
3377
3378 tp = dev->si_tty;
3379 KASSERT(tp != NULL,
3380 ("ttysopen(): no tty pointer on device (%s)", devtoname(dev)));
3381 if (tp->t_state & TS_GONE)
3382 return (ENODEV);
3383 return (0);
3384 }
3385
3386 static int
3387 ttysclose(struct cdev *dev, int flag, int mode, struct thread *td)
3388 {
3389
3390 return (0);
3391 }
3392
3393 static int
3394 ttysrdwr(struct cdev *dev, struct uio *uio, int flag)
3395 {
3396
3397 return (ENODEV);
3398 }
3399
3400 static int
3401 ttysioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
3402 {
3403 struct tty *tp;
3404 int error;
3405 struct termios *ct;
3406
3407 tp = dev->si_tty;
3408 KASSERT(tp != NULL,
3409 ("ttysopen(): no tty pointer on device (%s)", devtoname(dev)));
3410 if (tp->t_state & TS_GONE)
3411 return (ENODEV);
3412 ct = dev->si_drv2;
3413 switch (cmd) {
3414 case TIOCSETA:
3415 error = priv_check(td, PRIV_TTY_SETA);
3416 if (error != 0)
3417 return (error);
3418 *ct = *(struct termios *)data;
3419 return (0);
3420 case TIOCGETA:
3421 *(struct termios *)data = *ct;
3422 return (0);
3423 case TIOCGETD:
3424 *(int *)data = TTYDISC;
3425 return (0);
3426 case TIOCGWINSZ:
3427 bzero(data, sizeof(struct winsize));
3428 return (0);
3429 default:
3430 if (tp->t_cioctl != NULL)
3431 return(tp->t_cioctl(dev, cmd, data, flag, td));
3432 return (ENOTTY);
3433 }
3434 }
3435
3436 /*
3437 * Initialize a tty to sane modes.
3438 */
3439 void
3440 ttyinitmode(struct tty *tp, int echo, int speed)
3441 {
3442
3443 if (speed == 0)
3444 speed = TTYDEF_SPEED;
3445 tp->t_init_in.c_iflag = TTYDEF_IFLAG;
3446 tp->t_init_in.c_oflag = TTYDEF_OFLAG;
3447 tp->t_init_in.c_cflag = TTYDEF_CFLAG;
3448 if (echo)
3449 tp->t_init_in.c_lflag = TTYDEF_LFLAG_ECHO;
3450 else
3451 tp->t_init_in.c_lflag = TTYDEF_LFLAG_NOECHO;
3452
3453 tp->t_init_in.c_ispeed = tp->t_init_in.c_ospeed = speed;
3454 termioschars(&tp->t_init_in);
3455 tp->t_init_out = tp->t_init_in;
3456 tp->t_termios = tp->t_init_in;
3457 }
3458
3459 /*
3460 * Use more "normal" termios paramters for consoles.
3461 */
3462 void
3463 ttyconsolemode(struct tty *tp, int speed)
3464 {
3465
3466 if (speed == 0)
3467 speed = TTYDEF_SPEED;
3468 ttyinitmode(tp, 1, speed);
3469 tp->t_init_in.c_cflag |= CLOCAL;
3470 tp->t_lock_out.c_cflag = tp->t_lock_in.c_cflag = CLOCAL;
3471 tp->t_lock_out.c_ispeed = tp->t_lock_out.c_ospeed =
3472 tp->t_lock_in.c_ispeed = tp->t_lock_in.c_ospeed = speed;
3473 tp->t_init_out = tp->t_init_in;
3474 tp->t_termios = tp->t_init_in;
3475 ttsetwater(tp);
3476 }
3477
3478 /*
3479 * Record the relationship between the serial ports notion of modem control
3480 * signals and the one used in certain ioctls in a way the compiler can enforce
3481 * XXX: We should define TIOCM_* in terms of SER_ if we can limit the
3482 * XXX: consequences of the #include work that would take.
3483 */
3484 CTASSERT(SER_DTR == TIOCM_DTR / 2);
3485 CTASSERT(SER_RTS == TIOCM_RTS / 2);
3486 CTASSERT(SER_STX == TIOCM_ST / 2);
3487 CTASSERT(SER_SRX == TIOCM_SR / 2);
3488 CTASSERT(SER_CTS == TIOCM_CTS / 2);
3489 CTASSERT(SER_DCD == TIOCM_DCD / 2);
3490 CTASSERT(SER_RI == TIOCM_RI / 2);
3491 CTASSERT(SER_DSR == TIOCM_DSR / 2);
3492
Cache object: 515e6139d74bcb5a02f2a2292a9b4557
|