tty.c revision 1.125.2.1 1 /* $NetBSD: tty.c,v 1.125.2.1 2001/03/05 22:49:46 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1990, 1991, 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)tty.c 8.13 (Berkeley) 1/9/95
41 */
42
43 #include "opt_uconsole.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/ioctl.h>
48 #include <sys/lwp.h>
49 #include <sys/proc.h>
50 #define TTYDEFCHARS
51 #include <sys/tty.h>
52 #undef TTYDEFCHARS
53 #include <sys/file.h>
54 #include <sys/conf.h>
55 #include <sys/dkstat.h>
56 #include <sys/uio.h>
57 #include <sys/kernel.h>
58 #include <sys/vnode.h>
59 #include <sys/syslog.h>
60 #include <sys/malloc.h>
61 #include <sys/pool.h>
62 #include <sys/signalvar.h>
63 #include <sys/resourcevar.h>
64 #include <sys/poll.h>
65
66 static int ttnread __P((struct tty *));
67 static void ttyblock __P((struct tty *));
68 static void ttyecho __P((int, struct tty *));
69 static void ttyrubo __P((struct tty *, int));
70 static int proc_compare __P((struct proc *, struct proc *));
71
72 /* Symbolic sleep message strings. */
73 const char ttclos[] = "ttycls";
74 const char ttopen[] = "ttyopn";
75 const char ttybg[] = "ttybg";
76 const char ttyin[] = "ttyin";
77 const char ttyout[] = "ttyout";
78
79 /*
80 * Used to determine whether we still have a connection. This is true in
81 * one of 3 cases:
82 * 1) We have carrier.
83 * 2) It's a locally attached terminal, and we are therefore ignoring carrier.
84 * 3) We're using a flow control mechanism that overloads the carrier signal.
85 */
86 #define CONNECTED(tp) (ISSET(tp->t_state, TS_CARR_ON) || \
87 ISSET(tp->t_cflag, CLOCAL | MDMBUF))
88
89 /*
90 * Table with character classes and parity. The 8th bit indicates parity,
91 * the 7th bit indicates the character is an alphameric or underscore (for
92 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
93 * are 0 then the character needs no special processing on output; classes
94 * other than 0 might be translated or (not currently) require delays.
95 */
96 #define E 0x00 /* Even parity. */
97 #define O 0x80 /* Odd parity. */
98 #define PARITY(c) (char_type[c] & O)
99
100 #define ALPHA 0x40 /* Alpha or underscore. */
101 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
102
103 #define CCLASSMASK 0x3f
104 #define CCLASS(c) (char_type[c] & CCLASSMASK)
105
106 #define BS BACKSPACE
107 #define CC CONTROL
108 #define CR RETURN
109 #define NA ORDINARY | ALPHA
110 #define NL NEWLINE
111 #define NO ORDINARY
112 #define TB TAB
113 #define VT VTAB
114
115 char const char_type[] = {
116 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
117 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
118 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
119 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
120 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
121 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
122 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
123 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
124 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
125 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
126 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
127 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
128 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
129 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
130 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
131 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
132 /*
133 * Meta chars; should be settable per character set;
134 * for now, treat them all as normal characters.
135 */
136 NA, NA, NA, NA, NA, NA, NA, NA,
137 NA, NA, NA, NA, NA, NA, NA, NA,
138 NA, NA, NA, NA, NA, NA, NA, NA,
139 NA, NA, NA, NA, NA, NA, NA, NA,
140 NA, NA, NA, NA, NA, NA, NA, NA,
141 NA, NA, NA, NA, NA, NA, NA, NA,
142 NA, NA, NA, NA, NA, NA, NA, NA,
143 NA, NA, NA, NA, NA, NA, NA, NA,
144 NA, NA, NA, NA, NA, NA, NA, NA,
145 NA, NA, NA, NA, NA, NA, NA, NA,
146 NA, NA, NA, NA, NA, NA, NA, NA,
147 NA, NA, NA, NA, NA, NA, NA, NA,
148 NA, NA, NA, NA, NA, NA, NA, NA,
149 NA, NA, NA, NA, NA, NA, NA, NA,
150 NA, NA, NA, NA, NA, NA, NA, NA,
151 NA, NA, NA, NA, NA, NA, NA, NA,
152 };
153 #undef BS
154 #undef CC
155 #undef CR
156 #undef NA
157 #undef NL
158 #undef NO
159 #undef TB
160 #undef VT
161
162 /* Macros to clear/set/test flags. */
163 #define SET(t, f) (t) |= (f)
164 #define CLR(t, f) (t) &= ~((unsigned)(f))
165 #define ISSET(t, f) ((t) & (f))
166
167 struct ttylist_head ttylist; /* TAILQ_HEAD */
168 int tty_count;
169
170 struct pool tty_pool;
171
172 int
173 ttyopen(tp, dialout, nonblock)
174 struct tty *tp;
175 int dialout, nonblock;
176 {
177 int s;
178 int error;
179
180 s = spltty();
181
182 if (dialout) {
183 /*
184 * If the device is already open for non-dialout, fail.
185 * Otherwise, set TS_DIALOUT to block any pending non-dialout
186 * opens.
187 */
188 if (ISSET(tp->t_state, TS_ISOPEN) &&
189 !ISSET(tp->t_state, TS_DIALOUT)) {
190 splx(s);
191 return (EBUSY);
192 }
193 SET(tp->t_state, TS_DIALOUT);
194 } else {
195 if (!nonblock) {
196 /*
197 * Wait for carrier. Also wait for any dialout
198 * processes to close the tty first.
199 */
200 while (ISSET(tp->t_state, TS_DIALOUT) ||
201 (!ISSET(tp->t_state, TS_CARR_ON) &&
202 !ISSET(tp->t_cflag, CLOCAL | MDMBUF))) {
203 tp->t_wopen++;
204 error = ttysleep(tp, &tp->t_rawq,
205 TTIPRI | PCATCH, ttopen, 0);
206 tp->t_wopen--;
207 if (error) {
208 splx(s);
209 return (error);
210 }
211 }
212 } else {
213 /*
214 * Don't allow a non-blocking non-dialout open if the
215 * device is already open for dialout.
216 */
217 if (ISSET(tp->t_state, TS_DIALOUT)) {
218 splx(s);
219 return (EBUSY);
220 }
221 }
222 }
223
224 splx(s);
225 return (0);
226 }
227
228 /*
229 * Initial open of tty, or (re)entry to standard tty line discipline.
230 */
231 int
232 ttylopen(device, tp)
233 dev_t device;
234 struct tty *tp;
235 {
236 int s;
237
238 s = spltty();
239 tp->t_dev = device;
240 if (!ISSET(tp->t_state, TS_ISOPEN)) {
241 SET(tp->t_state, TS_ISOPEN);
242 memset(&tp->t_winsize, 0, sizeof(tp->t_winsize));
243 #ifdef COMPAT_OLDTTY
244 tp->t_flags = 0;
245 #endif
246 }
247 splx(s);
248 return (0);
249 }
250
251 /*
252 * Handle close() on a tty line: flush and set to initial state,
253 * bumping generation number so that pending read/write calls
254 * can detect recycling of the tty.
255 */
256 int
257 ttyclose(tp)
258 struct tty *tp;
259 {
260 extern struct tty *constty; /* Temporary virtual console. */
261
262 if (constty == tp)
263 constty = NULL;
264
265 ttyflush(tp, FREAD | FWRITE);
266
267 tp->t_gen++;
268 tp->t_pgrp = NULL;
269 tp->t_session = NULL;
270 tp->t_state = 0;
271 return (0);
272 }
273
274 #define FLUSHQ(q) { \
275 if ((q)->c_cc) \
276 ndflush(q, (q)->c_cc); \
277 }
278
279 /*
280 * This macro is used in canonical mode input processing, where a read
281 * request shall not return unless a 'line delimiter' ('\n') or 'break'
282 * (EOF, EOL, EOL2) character (or a signal) has been received. As EOL2
283 * is an extension to the POSIX.1 defined set of special characters,
284 * recognize it only if IEXTEN is set in the set of local flags.
285 */
286 #define TTBREAKC(c, lflg) \
287 ((c) == '\n' || (((c) == cc[VEOF] || (c) == cc[VEOL] || \
288 ((c) == cc[VEOL2] && ISSET(lflg, IEXTEN))) && (c) != _POSIX_VDISABLE))
289
290
291 /*
292 * Process input of a single character received on a tty.
293 */
294 int
295 ttyinput(c, tp)
296 int c;
297 struct tty *tp;
298 {
299 int iflag, lflag;
300 u_char *cc;
301 int i, error;
302
303 /*
304 * Unless the receiver is enabled, drop incoming data.
305 */
306 if (!ISSET(tp->t_cflag, CREAD))
307 return (0);
308
309 /*
310 * If input is pending take it first.
311 */
312 lflag = tp->t_lflag;
313 if (ISSET(lflag, PENDIN))
314 ttypend(tp);
315 /*
316 * Gather stats.
317 */
318 if (ISSET(lflag, ICANON)) {
319 ++tk_cancc;
320 ++tp->t_cancc;
321 } else {
322 ++tk_rawcc;
323 ++tp->t_rawcc;
324 }
325 ++tk_nin;
326
327 cc = tp->t_cc;
328
329 /*
330 * Handle exceptional conditions (break, parity, framing).
331 */
332 iflag = tp->t_iflag;
333 if ((error = (ISSET(c, TTY_ERRORMASK))) != 0) {
334 CLR(c, TTY_ERRORMASK);
335 if (ISSET(error, TTY_FE) && c == 0) { /* Break. */
336 if (ISSET(iflag, IGNBRK))
337 return (0);
338 else if (ISSET(iflag, BRKINT)) {
339 ttyflush(tp, FREAD | FWRITE);
340 pgsignal(tp->t_pgrp, SIGINT, 1);
341 return (0);
342 }
343 else if (ISSET(iflag, PARMRK))
344 goto parmrk;
345 }
346 else if ((ISSET(error, TTY_PE) && ISSET(iflag, INPCK)) ||
347 ISSET(error, TTY_FE)) {
348 if (ISSET(iflag, IGNPAR))
349 return (0);
350 else if (ISSET(iflag, PARMRK)) {
351 parmrk: (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
352 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
353 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
354 return (0);
355 }
356 else
357 c = 0;
358 }
359 }
360 else if (c == 0377 &&
361 ISSET(iflag, ISTRIP|IGNPAR|INPCK|PARMRK) == (INPCK|PARMRK)) {
362 /* "Escape" a valid character of '\377'. */
363 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
364 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
365 goto endcase;
366 }
367
368 /*
369 * In tandem mode, check high water mark.
370 */
371 if (ISSET(iflag, IXOFF) || ISSET(tp->t_cflag, CHWFLOW))
372 ttyblock(tp);
373 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
374 CLR(c, 0x80);
375 if (!ISSET(lflag, EXTPROC)) {
376 /*
377 * Check for literal nexting very first
378 */
379 if (ISSET(tp->t_state, TS_LNCH)) {
380 SET(c, TTY_QUOTE);
381 CLR(tp->t_state, TS_LNCH);
382 }
383 /*
384 * Scan for special characters. This code
385 * is really just a big case statement with
386 * non-constant cases. The bottom of the
387 * case statement is labeled ``endcase'', so goto
388 * it after a case match, or similar.
389 */
390
391 /*
392 * Control chars which aren't controlled
393 * by ICANON, ISIG, or IXON.
394 */
395 if (ISSET(lflag, IEXTEN)) {
396 if (CCEQ(cc[VLNEXT], c)) {
397 if (ISSET(lflag, ECHO)) {
398 if (ISSET(lflag, ECHOE)) {
399 (void)ttyoutput('^', tp);
400 (void)ttyoutput('\b', tp);
401 } else
402 ttyecho(c, tp);
403 }
404 SET(tp->t_state, TS_LNCH);
405 goto endcase;
406 }
407 if (CCEQ(cc[VDISCARD], c)) {
408 if (ISSET(lflag, FLUSHO))
409 CLR(tp->t_lflag, FLUSHO);
410 else {
411 ttyflush(tp, FWRITE);
412 ttyecho(c, tp);
413 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
414 ttyretype(tp);
415 SET(tp->t_lflag, FLUSHO);
416 }
417 goto startoutput;
418 }
419 }
420 /*
421 * Signals.
422 */
423 if (ISSET(lflag, ISIG)) {
424 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
425 if (!ISSET(lflag, NOFLSH))
426 ttyflush(tp, FREAD | FWRITE);
427 ttyecho(c, tp);
428 pgsignal(tp->t_pgrp,
429 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
430 goto endcase;
431 }
432 if (CCEQ(cc[VSUSP], c)) {
433 if (!ISSET(lflag, NOFLSH))
434 ttyflush(tp, FREAD);
435 ttyecho(c, tp);
436 pgsignal(tp->t_pgrp, SIGTSTP, 1);
437 goto endcase;
438 }
439 }
440 /*
441 * Handle start/stop characters.
442 */
443 if (ISSET(iflag, IXON)) {
444 if (CCEQ(cc[VSTOP], c)) {
445 if (!ISSET(tp->t_state, TS_TTSTOP)) {
446 SET(tp->t_state, TS_TTSTOP);
447 (*cdevsw[major(tp->t_dev)].d_stop)(tp,
448 0);
449 return (0);
450 }
451 if (!CCEQ(cc[VSTART], c))
452 return (0);
453 /*
454 * if VSTART == VSTOP then toggle
455 */
456 goto endcase;
457 }
458 if (CCEQ(cc[VSTART], c))
459 goto restartoutput;
460 }
461 /*
462 * IGNCR, ICRNL, & INLCR
463 */
464 if (c == '\r') {
465 if (ISSET(iflag, IGNCR))
466 goto endcase;
467 else if (ISSET(iflag, ICRNL))
468 c = '\n';
469 } else if (c == '\n' && ISSET(iflag, INLCR))
470 c = '\r';
471 }
472 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
473 /*
474 * From here on down canonical mode character
475 * processing takes place.
476 */
477 /*
478 * erase (^H / ^?)
479 */
480 if (CCEQ(cc[VERASE], c)) {
481 if (tp->t_rawq.c_cc)
482 ttyrub(unputc(&tp->t_rawq), tp);
483 goto endcase;
484 }
485 /*
486 * kill (^U)
487 */
488 if (CCEQ(cc[VKILL], c)) {
489 if (ISSET(lflag, ECHOKE) &&
490 tp->t_rawq.c_cc == tp->t_rocount &&
491 !ISSET(lflag, ECHOPRT))
492 while (tp->t_rawq.c_cc)
493 ttyrub(unputc(&tp->t_rawq), tp);
494 else {
495 ttyecho(c, tp);
496 if (ISSET(lflag, ECHOK) ||
497 ISSET(lflag, ECHOKE))
498 ttyecho('\n', tp);
499 FLUSHQ(&tp->t_rawq);
500 tp->t_rocount = 0;
501 }
502 CLR(tp->t_state, TS_LOCAL);
503 goto endcase;
504 }
505 /*
506 * Extensions to the POSIX.1 GTI set of functions.
507 */
508 if (ISSET(lflag, IEXTEN)) {
509 /*
510 * word erase (^W)
511 */
512 if (CCEQ(cc[VWERASE], c)) {
513 int alt = ISSET(lflag, ALTWERASE);
514 int ctype;
515
516 /*
517 * erase whitespace
518 */
519 while ((c = unputc(&tp->t_rawq)) == ' ' ||
520 c == '\t')
521 ttyrub(c, tp);
522 if (c == -1)
523 goto endcase;
524 /*
525 * erase last char of word and remember the
526 * next chars type (for ALTWERASE)
527 */
528 ttyrub(c, tp);
529 c = unputc(&tp->t_rawq);
530 if (c == -1)
531 goto endcase;
532 if (c == ' ' || c == '\t') {
533 (void)putc(c, &tp->t_rawq);
534 goto endcase;
535 }
536 ctype = ISALPHA(c);
537 /*
538 * erase rest of word
539 */
540 do {
541 ttyrub(c, tp);
542 c = unputc(&tp->t_rawq);
543 if (c == -1)
544 goto endcase;
545 } while (c != ' ' && c != '\t' &&
546 (alt == 0 || ISALPHA(c) == ctype));
547 (void)putc(c, &tp->t_rawq);
548 goto endcase;
549 }
550 /*
551 * reprint line (^R)
552 */
553 if (CCEQ(cc[VREPRINT], c)) {
554 ttyretype(tp);
555 goto endcase;
556 }
557 /*
558 * ^T - kernel info and generate SIGINFO
559 */
560 if (CCEQ(cc[VSTATUS], c)) {
561 if (ISSET(lflag, ISIG))
562 pgsignal(tp->t_pgrp, SIGINFO, 1);
563 if (!ISSET(lflag, NOKERNINFO))
564 ttyinfo(tp);
565 goto endcase;
566 }
567 }
568 }
569 /*
570 * Check for input buffer overflow
571 */
572 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
573 if (ISSET(iflag, IMAXBEL)) {
574 if (tp->t_outq.c_cc < tp->t_hiwat)
575 (void)ttyoutput(CTRL('g'), tp);
576 } else
577 ttyflush(tp, FREAD | FWRITE);
578 goto endcase;
579 }
580 /*
581 * Put data char in q for user and
582 * wakeup on seeing a line delimiter.
583 */
584 if (putc(c, &tp->t_rawq) >= 0) {
585 if (!ISSET(lflag, ICANON)) {
586 ttwakeup(tp);
587 ttyecho(c, tp);
588 goto endcase;
589 }
590 if (TTBREAKC(c, lflag)) {
591 tp->t_rocount = 0;
592 catq(&tp->t_rawq, &tp->t_canq);
593 ttwakeup(tp);
594 } else if (tp->t_rocount++ == 0)
595 tp->t_rocol = tp->t_column;
596 if (ISSET(tp->t_state, TS_ERASE)) {
597 /*
598 * end of prterase \.../
599 */
600 CLR(tp->t_state, TS_ERASE);
601 (void)ttyoutput('/', tp);
602 }
603 i = tp->t_column;
604 ttyecho(c, tp);
605 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
606 /*
607 * Place the cursor over the '^' of the ^D.
608 */
609 i = min(2, tp->t_column - i);
610 while (i > 0) {
611 (void)ttyoutput('\b', tp);
612 i--;
613 }
614 }
615 }
616 endcase:
617 /*
618 * IXANY means allow any character to restart output.
619 */
620 if (ISSET(tp->t_state, TS_TTSTOP) &&
621 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
622 return (0);
623 restartoutput:
624 CLR(tp->t_lflag, FLUSHO);
625 CLR(tp->t_state, TS_TTSTOP);
626 startoutput:
627 return (ttstart(tp));
628 }
629
630 /*
631 * Output a single character on a tty, doing output processing
632 * as needed (expanding tabs, newline processing, etc.).
633 * Returns < 0 if succeeds, otherwise returns char to resend.
634 * Must be recursive.
635 */
636 int
637 ttyoutput(c, tp)
638 int c;
639 struct tty *tp;
640 {
641 long oflag;
642 int col, notout, s;
643
644 oflag = tp->t_oflag;
645 if (!ISSET(oflag, OPOST)) {
646 tk_nout++;
647 tp->t_outcc++;
648 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
649 return (c);
650 return (-1);
651 }
652 /*
653 * Do tab expansion if OXTABS is set. Special case if we do external
654 * processing, we don't do the tab expansion because we'll probably
655 * get it wrong. If tab expansion needs to be done, let it happen
656 * externally.
657 */
658 CLR(c, ~TTY_CHARMASK);
659 if (c == '\t' &&
660 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
661 c = 8 - (tp->t_column & 7);
662 if (ISSET(tp->t_lflag, FLUSHO)) {
663 notout = 0;
664 } else {
665 s = spltty(); /* Don't interrupt tabs. */
666 notout = b_to_q(" ", c, &tp->t_outq);
667 c -= notout;
668 tk_nout += c;
669 tp->t_outcc += c;
670 splx(s);
671 }
672 tp->t_column += c;
673 return (notout ? '\t' : -1);
674 }
675 if (c == CEOT && ISSET(oflag, ONOEOT))
676 return (-1);
677
678 /*
679 * Newline translation: if ONLCR is set,
680 * translate newline into "\r\n".
681 */
682 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
683 tk_nout++;
684 tp->t_outcc++;
685 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
686 return (c);
687 }
688 /* If OCRNL is set, translate "\r" into "\n". */
689 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
690 c = '\n';
691 /* If ONOCR is set, don't transmit CRs when on column 0. */
692 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
693 return (-1);
694
695 tk_nout++;
696 tp->t_outcc++;
697 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
698 return (c);
699
700 col = tp->t_column;
701 switch (CCLASS(c)) {
702 case BACKSPACE:
703 if (col > 0)
704 --col;
705 break;
706 case CONTROL:
707 break;
708 case NEWLINE:
709 if (ISSET(tp->t_oflag, ONLCR | ONLRET))
710 col = 0;
711 break;
712 case RETURN:
713 col = 0;
714 break;
715 case ORDINARY:
716 ++col;
717 break;
718 case TAB:
719 col = (col + 8) & ~7;
720 break;
721 }
722 tp->t_column = col;
723 return (-1);
724 }
725
726 /*
727 * Ioctls for all tty devices. Called after line-discipline specific ioctl
728 * has been called to do discipline-specific functions and/or reject any
729 * of these ioctl commands.
730 */
731 /* ARGSUSED */
732 int
733 ttioctl(tp, cmd, data, flag, p)
734 struct tty *tp;
735 u_long cmd;
736 caddr_t data;
737 int flag;
738 struct proc *p;
739 {
740 extern struct tty *constty; /* Temporary virtual console. */
741 extern int nlinesw;
742 struct linesw *lp;
743 int s, error;
744
745 /* If the ioctl involves modification, hang if in the background. */
746 switch (cmd) {
747 case TIOCFLUSH:
748 case TIOCDRAIN:
749 case TIOCSBRK:
750 case TIOCCBRK:
751 case TIOCSTART:
752 case TIOCSETA:
753 case TIOCSETD:
754 case TIOCSLINED:
755 case TIOCSETAF:
756 case TIOCSETAW:
757 #ifdef notdef
758 case TIOCSPGRP:
759 #endif
760 case TIOCSTAT:
761 case TIOCSTI:
762 case TIOCSWINSZ:
763 #ifdef COMPAT_OLDTTY
764 case TIOCLBIC:
765 case TIOCLBIS:
766 case TIOCLSET:
767 case TIOCSETC:
768 case OTIOCSETD:
769 case TIOCSETN:
770 case TIOCSETP:
771 case TIOCSLTC:
772 #endif
773 while (isbackground(curproc->l_proc, tp) &&
774 p->p_pgrp->pg_jobc && (p->p_flag & P_PPWAIT) == 0 &&
775 !sigismasked(p, SIGTTOU)) {
776 pgsignal(p->p_pgrp, SIGTTOU, 1);
777 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, ttybg, 0);
778 if (error)
779 return (error);
780 }
781 break;
782 }
783
784 switch (cmd) { /* Process the ioctl. */
785 case FIOASYNC: /* set/clear async i/o */
786 s = spltty();
787 if (*(int *)data)
788 SET(tp->t_state, TS_ASYNC);
789 else
790 CLR(tp->t_state, TS_ASYNC);
791 splx(s);
792 break;
793 case FIONBIO: /* set/clear non-blocking i/o */
794 break; /* XXX: delete. */
795 case FIONREAD: /* get # bytes to read */
796 *(int *)data = ttnread(tp);
797 break;
798 case TIOCEXCL: /* set exclusive use of tty */
799 s = spltty();
800 SET(tp->t_state, TS_XCLUDE);
801 splx(s);
802 break;
803 case TIOCFLUSH: { /* flush buffers */
804 int flags = *(int *)data;
805
806 if (flags == 0)
807 flags = FREAD | FWRITE;
808 else
809 flags &= FREAD | FWRITE;
810 ttyflush(tp, flags);
811 break;
812 }
813 case TIOCCONS: /* become virtual console */
814 if (*(int *)data) {
815 if (constty && constty != tp &&
816 ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) ==
817 (TS_CARR_ON | TS_ISOPEN))
818 return (EBUSY);
819 #ifndef UCONSOLE
820 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
821 return (error);
822 #endif
823 constty = tp;
824 } else if (tp == constty)
825 constty = NULL;
826 break;
827 case TIOCDRAIN: /* wait till output drained */
828 if ((error = ttywait(tp)) != 0)
829 return (error);
830 break;
831 case TIOCGETA: { /* get termios struct */
832 struct termios *t = (struct termios *)data;
833
834 memcpy(t, &tp->t_termios, sizeof(struct termios));
835 break;
836 }
837 case TIOCGETD: /* get line discipline */
838 *(int *)data = (tp->t_linesw) ? tp->t_linesw->l_no : 0;
839 break;
840 case TIOCGLINED:
841 if (!tp->t_linesw)
842 return (EIO);
843 strncpy((char *)data, tp->t_linesw->l_name,
844 TTLINEDNAMELEN);
845 break;
846 case TIOCGWINSZ: /* get window size */
847 *(struct winsize *)data = tp->t_winsize;
848 break;
849 case TIOCGPGRP: /* get pgrp of tty */
850 if (!isctty(p, tp))
851 return (ENOTTY);
852 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
853 break;
854 case TIOCGSID: /* get sid of tty */
855 if (!isctty(p, tp))
856 return (ENOTTY);
857 *(int *)data = tp->t_session->s_sid;
858 break;
859 #ifdef TIOCHPCL
860 case TIOCHPCL: /* hang up on last close */
861 s = spltty();
862 SET(tp->t_cflag, HUPCL);
863 splx(s);
864 break;
865 #endif
866 case TIOCNXCL: /* reset exclusive use of tty */
867 s = spltty();
868 CLR(tp->t_state, TS_XCLUDE);
869 splx(s);
870 break;
871 case TIOCOUTQ: /* output queue size */
872 *(int *)data = tp->t_outq.c_cc;
873 break;
874 case TIOCSETA: /* set termios struct */
875 case TIOCSETAW: /* drain output, set */
876 case TIOCSETAF: { /* drn out, fls in, set */
877 struct termios *t = (struct termios *)data;
878
879 s = spltty();
880 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
881 if ((error = ttywait(tp)) != 0) {
882 splx(s);
883 return (error);
884 }
885 if (cmd == TIOCSETAF)
886 ttyflush(tp, FREAD);
887 }
888 if (!ISSET(t->c_cflag, CIGNORE)) {
889 /*
890 * Set device hardware.
891 */
892 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
893 splx(s);
894 return (error);
895 } else {
896 tp->t_cflag = t->c_cflag;
897 tp->t_ispeed = t->c_ispeed;
898 tp->t_ospeed = t->c_ospeed;
899 if (t->c_ospeed == 0 && tp->t_session &&
900 tp->t_session->s_leader)
901 psignal(tp->t_session->s_leader,
902 SIGHUP);
903 }
904 ttsetwater(tp);
905 }
906 if (cmd != TIOCSETAF) {
907 if (ISSET(t->c_lflag, ICANON) !=
908 ISSET(tp->t_lflag, ICANON)) {
909 if (ISSET(t->c_lflag, ICANON)) {
910 SET(tp->t_lflag, PENDIN);
911 ttwakeup(tp);
912 } else {
913 struct clist tq;
914
915 catq(&tp->t_rawq, &tp->t_canq);
916 tq = tp->t_rawq;
917 tp->t_rawq = tp->t_canq;
918 tp->t_canq = tq;
919 CLR(tp->t_lflag, PENDIN);
920 }
921 }
922 }
923 tp->t_iflag = t->c_iflag;
924 tp->t_oflag = t->c_oflag;
925 /*
926 * Make the EXTPROC bit read only.
927 */
928 if (ISSET(tp->t_lflag, EXTPROC))
929 SET(t->c_lflag, EXTPROC);
930 else
931 CLR(t->c_lflag, EXTPROC);
932 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
933 memcpy(tp->t_cc, t->c_cc, sizeof(t->c_cc));
934 splx(s);
935 break;
936 }
937 case TIOCSETD: { /* set line discipline */
938 int t = *(int *)data;
939
940 if ((u_int)t >= nlinesw)
941 return (ENXIO);
942 lp = linesw[t];
943 goto setldisc;
944 }
945 case TIOCSLINED: { /* set line discipline */
946 char *name = (char *)data;
947 dev_t device;
948
949 /* Null terminate to prevent buffer overflow */
950 name[TTLINEDNAMELEN] = 0;
951 lp = ttyldisc_lookup(name);
952
953 setldisc:
954 if (lp == NULL)
955 return (ENXIO);
956
957 if (lp != tp->t_linesw) {
958 device = tp->t_dev;
959 s = spltty();
960 (*tp->t_linesw->l_close)(tp, flag);
961 error = (*lp->l_open)(device, tp);
962 if (error) {
963 (void)(*tp->t_linesw->l_open)(device, tp);
964 splx(s);
965 return (error);
966 }
967 tp->t_linesw = lp;
968 splx(s);
969 }
970 break;
971 }
972 case TIOCSTART: /* start output, like ^Q */
973 s = spltty();
974 if (ISSET(tp->t_state, TS_TTSTOP) ||
975 ISSET(tp->t_lflag, FLUSHO)) {
976 CLR(tp->t_lflag, FLUSHO);
977 CLR(tp->t_state, TS_TTSTOP);
978 ttstart(tp);
979 }
980 splx(s);
981 break;
982 case TIOCSTI: /* simulate terminal input */
983 if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
984 return (EPERM);
985 if (p->p_ucred->cr_uid && !isctty(p, tp))
986 return (EACCES);
987 (*tp->t_linesw->l_rint)(*(u_char *)data, tp);
988 break;
989 case TIOCSTOP: /* stop output, like ^S */
990 s = spltty();
991 if (!ISSET(tp->t_state, TS_TTSTOP)) {
992 SET(tp->t_state, TS_TTSTOP);
993 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
994 }
995 splx(s);
996 break;
997 case TIOCSCTTY: /* become controlling tty */
998 /* Session ctty vnode pointer set in vnode layer. */
999 if (!SESS_LEADER(p) ||
1000 ((p->p_session->s_ttyvp || tp->t_session) &&
1001 (tp->t_session != p->p_session)))
1002 return (EPERM);
1003 tp->t_session = p->p_session;
1004 tp->t_pgrp = p->p_pgrp;
1005 p->p_session->s_ttyp = tp;
1006 p->p_flag |= P_CONTROLT;
1007 break;
1008 case TIOCSPGRP: { /* set pgrp of tty */
1009 struct pgrp *pgrp = pgfind(*(int *)data);
1010
1011 if (!isctty(p, tp))
1012 return (ENOTTY);
1013 else if (pgrp == NULL)
1014 return (EINVAL);
1015 else if (pgrp->pg_session != p->p_session)
1016 return (EPERM);
1017 tp->t_pgrp = pgrp;
1018 break;
1019 }
1020 case TIOCSTAT: /* get load avg stats */
1021 ttyinfo(tp);
1022 break;
1023 case TIOCSWINSZ: /* set window size */
1024 if (memcmp((caddr_t)&tp->t_winsize, data,
1025 sizeof(struct winsize))) {
1026 tp->t_winsize = *(struct winsize *)data;
1027 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1028 }
1029 break;
1030 default:
1031 #ifdef COMPAT_OLDTTY
1032 return (ttcompat(tp, cmd, data, flag, p));
1033 #else
1034 return (-1);
1035 #endif
1036 }
1037 return (0);
1038 }
1039
1040 int
1041 ttpoll(dev, events, p)
1042 dev_t dev;
1043 int events;
1044 struct proc *p;
1045 {
1046 struct tty *tp = (*cdevsw[major(dev)].d_tty)(dev);
1047 int revents = 0;
1048 int s = spltty();
1049
1050 if (events & (POLLIN | POLLRDNORM))
1051 if (ttnread(tp) > 0)
1052 revents |= events & (POLLIN | POLLRDNORM);
1053
1054 if (events & (POLLOUT | POLLWRNORM))
1055 if (tp->t_outq.c_cc <= tp->t_lowat)
1056 revents |= events & (POLLOUT | POLLWRNORM);
1057
1058 if (events & POLLHUP)
1059 if (!CONNECTED(tp))
1060 revents |= POLLHUP;
1061
1062 if (revents == 0) {
1063 if (events & (POLLIN | POLLHUP | POLLRDNORM))
1064 selrecord(p, &tp->t_rsel);
1065
1066 if (events & (POLLOUT | POLLWRNORM))
1067 selrecord(p, &tp->t_wsel);
1068 }
1069
1070 splx(s);
1071 return (revents);
1072 }
1073
1074 static int
1075 ttnread(tp)
1076 struct tty *tp;
1077 {
1078 int nread;
1079
1080 if (ISSET(tp->t_lflag, PENDIN))
1081 ttypend(tp);
1082 nread = tp->t_canq.c_cc;
1083 if (!ISSET(tp->t_lflag, ICANON)) {
1084 nread += tp->t_rawq.c_cc;
1085 if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
1086 nread = 0;
1087 }
1088 return (nread);
1089 }
1090
1091 /*
1092 * Wait for output to drain.
1093 */
1094 int
1095 ttywait(tp)
1096 struct tty *tp;
1097 {
1098 int error, s;
1099
1100 error = 0;
1101 s = spltty();
1102 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1103 CONNECTED(tp) && tp->t_oproc) {
1104 (*tp->t_oproc)(tp);
1105 SET(tp->t_state, TS_ASLEEP);
1106 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1107 if (error)
1108 break;
1109 }
1110 splx(s);
1111 return (error);
1112 }
1113
1114 /*
1115 * Flush if successfully wait.
1116 */
1117 int
1118 ttywflush(tp)
1119 struct tty *tp;
1120 {
1121 int error;
1122
1123 if ((error = ttywait(tp)) == 0)
1124 ttyflush(tp, FREAD);
1125 return (error);
1126 }
1127
1128 /*
1129 * Flush tty read and/or write queues, notifying anyone waiting.
1130 */
1131 void
1132 ttyflush(tp, rw)
1133 struct tty *tp;
1134 int rw;
1135 {
1136 int s;
1137
1138 s = spltty();
1139 if (rw & FREAD) {
1140 FLUSHQ(&tp->t_canq);
1141 FLUSHQ(&tp->t_rawq);
1142 tp->t_rocount = 0;
1143 tp->t_rocol = 0;
1144 CLR(tp->t_state, TS_LOCAL);
1145 ttwakeup(tp);
1146 }
1147 if (rw & FWRITE) {
1148 CLR(tp->t_state, TS_TTSTOP);
1149 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
1150 FLUSHQ(&tp->t_outq);
1151 wakeup((caddr_t)&tp->t_outq);
1152 selwakeup(&tp->t_wsel);
1153 }
1154 splx(s);
1155 }
1156
1157 /*
1158 * Copy in the default termios characters.
1159 */
1160 void
1161 ttychars(tp)
1162 struct tty *tp;
1163 {
1164
1165 memcpy(tp->t_cc, ttydefchars, sizeof(ttydefchars));
1166 }
1167
1168 /*
1169 * Send stop character on input overflow.
1170 */
1171 static void
1172 ttyblock(tp)
1173 struct tty *tp;
1174 {
1175 int total;
1176
1177 total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
1178 if (tp->t_rawq.c_cc > TTYHOG) {
1179 ttyflush(tp, FREAD | FWRITE);
1180 CLR(tp->t_state, TS_TBLOCK);
1181 }
1182 /*
1183 * Block further input iff: current input > threshold
1184 * AND input is available to user program.
1185 */
1186 if (total >= TTYHOG / 2 &&
1187 !ISSET(tp->t_state, TS_TBLOCK) &&
1188 (!ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0)) {
1189 if (ISSET(tp->t_iflag, IXOFF) &&
1190 tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1191 putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
1192 SET(tp->t_state, TS_TBLOCK);
1193 ttstart(tp);
1194 }
1195 /* Try to block remote output via hardware flow control. */
1196 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1197 (*tp->t_hwiflow)(tp, 1) != 0)
1198 SET(tp->t_state, TS_TBLOCK);
1199 }
1200 }
1201
1202 void
1203 ttrstrt(tp_arg)
1204 void *tp_arg;
1205 {
1206 struct tty *tp;
1207 int s;
1208
1209 #ifdef DIAGNOSTIC
1210 if (tp_arg == NULL)
1211 panic("ttrstrt");
1212 #endif
1213 tp = tp_arg;
1214 s = spltty();
1215
1216 CLR(tp->t_state, TS_TIMEOUT);
1217 ttstart(tp);
1218
1219 splx(s);
1220 }
1221
1222 int
1223 ttstart(tp)
1224 struct tty *tp;
1225 {
1226
1227 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1228 (*tp->t_oproc)(tp);
1229 return (0);
1230 }
1231
1232 /*
1233 * "close" a line discipline
1234 */
1235 int
1236 ttylclose(tp, flag)
1237 struct tty *tp;
1238 int flag;
1239 {
1240
1241 if (flag & FNONBLOCK)
1242 ttyflush(tp, FREAD | FWRITE);
1243 else
1244 ttywflush(tp);
1245 return (0);
1246 }
1247
1248 /*
1249 * Handle modem control transition on a tty.
1250 * Flag indicates new state of carrier.
1251 * Returns 0 if the line should be turned off, otherwise 1.
1252 */
1253 int
1254 ttymodem(tp, flag)
1255 struct tty *tp;
1256 int flag;
1257 {
1258
1259 if (flag == 0) {
1260 if (ISSET(tp->t_state, TS_CARR_ON)) {
1261 /*
1262 * Lost carrier.
1263 */
1264 CLR(tp->t_state, TS_CARR_ON);
1265 if (ISSET(tp->t_state, TS_ISOPEN) && !CONNECTED(tp)) {
1266 if (tp->t_session && tp->t_session->s_leader)
1267 psignal(tp->t_session->s_leader, SIGHUP);
1268 ttyflush(tp, FREAD | FWRITE);
1269 return (0);
1270 }
1271 }
1272 } else {
1273 if (!ISSET(tp->t_state, TS_CARR_ON)) {
1274 /*
1275 * Carrier now on.
1276 */
1277 SET(tp->t_state, TS_CARR_ON);
1278 ttwakeup(tp);
1279 }
1280 }
1281 return (1);
1282 }
1283
1284 /*
1285 * Default modem control routine (for other line disciplines).
1286 * Return argument flag, to turn off device on carrier drop.
1287 */
1288 int
1289 nullmodem(tp, flag)
1290 struct tty *tp;
1291 int flag;
1292 {
1293
1294 if (flag)
1295 SET(tp->t_state, TS_CARR_ON);
1296 else {
1297 CLR(tp->t_state, TS_CARR_ON);
1298 if (!CONNECTED(tp)) {
1299 if (tp->t_session && tp->t_session->s_leader)
1300 psignal(tp->t_session->s_leader, SIGHUP);
1301 return (0);
1302 }
1303 }
1304 return (1);
1305 }
1306
1307 /*
1308 * Reinput pending characters after state switch
1309 * call at spltty().
1310 */
1311 void
1312 ttypend(tp)
1313 struct tty *tp;
1314 {
1315 struct clist tq;
1316 int c;
1317
1318 CLR(tp->t_lflag, PENDIN);
1319 SET(tp->t_state, TS_TYPEN);
1320 tq = tp->t_rawq;
1321 tp->t_rawq.c_cc = 0;
1322 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
1323 while ((c = getc(&tq)) >= 0)
1324 ttyinput(c, tp);
1325 CLR(tp->t_state, TS_TYPEN);
1326 }
1327
1328 /*
1329 * Process a read call on a tty device.
1330 */
1331 int
1332 ttread(tp, uio, flag)
1333 struct tty *tp;
1334 struct uio *uio;
1335 int flag;
1336 {
1337 struct clist *qp;
1338 int c;
1339 long lflag;
1340 u_char *cc = tp->t_cc;
1341 struct proc *p = curproc->l_proc;
1342 int s, first, error = 0;
1343 struct timeval stime;
1344 int has_stime = 0, last_cc = 0;
1345 long slp = 0;
1346
1347 loop: lflag = tp->t_lflag;
1348 s = spltty();
1349 /*
1350 * take pending input first
1351 */
1352 if (ISSET(lflag, PENDIN))
1353 ttypend(tp);
1354 splx(s);
1355
1356 /*
1357 * Hang process if it's in the background.
1358 */
1359 if (isbackground(p, tp)) {
1360 if (sigismember(&p->p_sigctx.ps_sigignore, SIGTTIN) ||
1361 sigismember(&p->p_sigctx.ps_sigmask, SIGTTIN) ||
1362 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0)
1363 return (EIO);
1364 pgsignal(p->p_pgrp, SIGTTIN, 1);
1365 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1366 if (error)
1367 return (error);
1368 goto loop;
1369 }
1370
1371 s = spltty();
1372 if (!ISSET(lflag, ICANON)) {
1373 int m = cc[VMIN];
1374 long t = cc[VTIME];
1375
1376 qp = &tp->t_rawq;
1377 /*
1378 * Check each of the four combinations.
1379 * (m > 0 && t == 0) is the normal read case.
1380 * It should be fairly efficient, so we check that and its
1381 * companion case (m == 0 && t == 0) first.
1382 * For the other two cases, we compute the target sleep time
1383 * into slp.
1384 */
1385 if (t == 0) {
1386 if (qp->c_cc < m)
1387 goto sleep;
1388 goto read;
1389 }
1390 t *= 100000; /* time in us */
1391 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1392 ((t1).tv_usec - (t2).tv_usec))
1393 if (m > 0) {
1394 if (qp->c_cc <= 0)
1395 goto sleep;
1396 if (qp->c_cc >= m)
1397 goto read;
1398 if (!has_stime) {
1399 /* first character, start timer */
1400 has_stime = 1;
1401 stime = time;
1402 slp = t;
1403 } else if (qp->c_cc > last_cc) {
1404 /* got a character, restart timer */
1405 stime = time;
1406 slp = t;
1407 } else {
1408 /* nothing, check expiration */
1409 slp = t - diff(time, stime);
1410 }
1411 } else { /* m == 0 */
1412 if (qp->c_cc > 0)
1413 goto read;
1414 if (!has_stime) {
1415 has_stime = 1;
1416 stime = time;
1417 slp = t;
1418 } else
1419 slp = t - diff(time, stime);
1420 }
1421 last_cc = qp->c_cc;
1422 #undef diff
1423 if (slp > 0) {
1424 /*
1425 * Rounding down may make us wake up just short
1426 * of the target, so we round up.
1427 * The formula is ceiling(slp * hz/1000000).
1428 * 32-bit arithmetic is enough for hz < 169.
1429 *
1430 * Also, use plain wakeup() not ttwakeup().
1431 */
1432 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1433 goto sleep;
1434 }
1435 } else if ((qp = &tp->t_canq)->c_cc <= 0) {
1436 int carrier;
1437
1438 sleep:
1439 /*
1440 * If there is no input, sleep on rawq
1441 * awaiting hardware receipt and notification.
1442 * If we have data, we don't need to check for carrier.
1443 */
1444 carrier = CONNECTED(tp);
1445 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1446 splx(s);
1447 return (0); /* EOF */
1448 }
1449 if (flag & IO_NDELAY) {
1450 splx(s);
1451 return (EWOULDBLOCK);
1452 }
1453 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
1454 carrier ? ttyin : ttopen, slp);
1455 splx(s);
1456 /* VMIN == 0: any quantity read satisfies */
1457 if (cc[VMIN] == 0 && error == EWOULDBLOCK)
1458 return (0);
1459 if (error && error != EWOULDBLOCK)
1460 return (error);
1461 goto loop;
1462 }
1463 read:
1464 splx(s);
1465
1466 /*
1467 * Input present, check for input mapping and processing.
1468 */
1469 first = 1;
1470 while ((c = getc(qp)) >= 0) {
1471 /*
1472 * delayed suspend (^Y)
1473 */
1474 if (CCEQ(cc[VDSUSP], c) &&
1475 ISSET(lflag, IEXTEN|ISIG) == (IEXTEN|ISIG)) {
1476 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1477 if (first) {
1478 error = ttysleep(tp, &lbolt,
1479 TTIPRI | PCATCH, ttybg, 0);
1480 if (error)
1481 break;
1482 goto loop;
1483 }
1484 break;
1485 }
1486 /*
1487 * Interpret EOF only in canonical mode.
1488 */
1489 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1490 break;
1491 /*
1492 * Give user character.
1493 */
1494 error = ureadc(c, uio);
1495 if (error)
1496 break;
1497 if (uio->uio_resid == 0)
1498 break;
1499 /*
1500 * In canonical mode check for a "break character"
1501 * marking the end of a "line of input".
1502 */
1503 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1504 break;
1505 first = 0;
1506 }
1507 /*
1508 * Look to unblock output now that (presumably)
1509 * the input queue has gone down.
1510 */
1511 s = spltty();
1512 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) {
1513 if (ISSET(tp->t_iflag, IXOFF) &&
1514 cc[VSTART] != _POSIX_VDISABLE &&
1515 putc(cc[VSTART], &tp->t_outq) == 0) {
1516 CLR(tp->t_state, TS_TBLOCK);
1517 ttstart(tp);
1518 }
1519 /* Try to unblock remote output via hardware flow control. */
1520 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1521 (*tp->t_hwiflow)(tp, 0) != 0)
1522 CLR(tp->t_state, TS_TBLOCK);
1523 }
1524 splx(s);
1525 return (error);
1526 }
1527
1528 /*
1529 * Check the output queue on tp for space for a kernel message (from uprintf
1530 * or tprintf). Allow some space over the normal hiwater mark so we don't
1531 * lose messages due to normal flow control, but don't let the tty run amok.
1532 * Sleeps here are not interruptible, but we return prematurely if new signals
1533 * arrive.
1534 */
1535 int
1536 ttycheckoutq(tp, wait)
1537 struct tty *tp;
1538 int wait;
1539 {
1540 int hiwat, s;
1541 int error;
1542
1543 hiwat = tp->t_hiwat;
1544 s = spltty();
1545 if (tp->t_outq.c_cc > hiwat + 200)
1546 while (tp->t_outq.c_cc > hiwat) {
1547 ttstart(tp);
1548 if (wait == 0) {
1549 splx(s);
1550 return (0);
1551 }
1552 callout_reset(&tp->t_outq_ch, hz,
1553 (void (*)__P((void *)))wakeup, &tp->t_outq);
1554 SET(tp->t_state, TS_ASLEEP);
1555 error = tsleep(&tp->t_outq, (PZERO - 1) | PCATCH,
1556 "ttckoutq", 0);
1557 if (error == EINTR)
1558 wait = 0;
1559 }
1560 splx(s);
1561 return (1);
1562 }
1563
1564 /*
1565 * Process a write call on a tty device.
1566 */
1567 int
1568 ttwrite(tp, uio, flag)
1569 struct tty *tp;
1570 struct uio *uio;
1571 int flag;
1572 {
1573 u_char *cp = NULL;
1574 int cc, ce;
1575 struct proc *p;
1576 int i, hiwat, cnt, error, s;
1577 u_char obuf[OBUFSIZ];
1578
1579 hiwat = tp->t_hiwat;
1580 cnt = uio->uio_resid;
1581 error = 0;
1582 cc = 0;
1583 loop:
1584 s = spltty();
1585 if (!CONNECTED(tp)) {
1586 if (ISSET(tp->t_state, TS_ISOPEN)) {
1587 splx(s);
1588 return (EIO);
1589 } else if (flag & IO_NDELAY) {
1590 splx(s);
1591 error = EWOULDBLOCK;
1592 goto out;
1593 } else {
1594 /* Sleep awaiting carrier. */
1595 error = ttysleep(tp,
1596 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
1597 splx(s);
1598 if (error)
1599 goto out;
1600 goto loop;
1601 }
1602 }
1603 splx(s);
1604 /*
1605 * Hang the process if it's in the background.
1606 */
1607 p = curproc->l_proc;
1608 if (isbackground(p, tp) &&
1609 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1610 !sigismember(&p->p_sigctx.ps_sigignore, SIGTTOU) &&
1611 !sigismember(&p->p_sigctx.ps_sigmask, SIGTTOU)) {
1612 if (p->p_pgrp->pg_jobc == 0) {
1613 error = EIO;
1614 goto out;
1615 }
1616 pgsignal(p->p_pgrp, SIGTTOU, 1);
1617 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1618 if (error)
1619 goto out;
1620 goto loop;
1621 }
1622 /*
1623 * Process the user's data in at most OBUFSIZ chunks. Perform any
1624 * output translation. Keep track of high water mark, sleep on
1625 * overflow awaiting device aid in acquiring new space.
1626 */
1627 while (uio->uio_resid > 0 || cc > 0) {
1628 if (ISSET(tp->t_lflag, FLUSHO)) {
1629 uio->uio_resid = 0;
1630 return (0);
1631 }
1632 if (tp->t_outq.c_cc > hiwat)
1633 goto ovhiwat;
1634 /*
1635 * Grab a hunk of data from the user, unless we have some
1636 * leftover from last time.
1637 */
1638 if (cc == 0) {
1639 cc = min(uio->uio_resid, OBUFSIZ);
1640 cp = obuf;
1641 error = uiomove(cp, cc, uio);
1642 if (error) {
1643 cc = 0;
1644 break;
1645 }
1646 }
1647 /*
1648 * If nothing fancy need be done, grab those characters we
1649 * can handle without any of ttyoutput's processing and
1650 * just transfer them to the output q. For those chars
1651 * which require special processing (as indicated by the
1652 * bits in char_type), call ttyoutput. After processing
1653 * a hunk of data, look for FLUSHO so ^O's will take effect
1654 * immediately.
1655 */
1656 while (cc > 0) {
1657 if (!ISSET(tp->t_oflag, OPOST))
1658 ce = cc;
1659 else {
1660 ce = cc - scanc((u_int)cc, cp, char_type,
1661 CCLASSMASK);
1662 /*
1663 * If ce is zero, then we're processing
1664 * a special character through ttyoutput.
1665 */
1666 if (ce == 0) {
1667 tp->t_rocount = 0;
1668 if (ttyoutput(*cp, tp) >= 0) {
1669 /* out of space */
1670 goto overfull;
1671 }
1672 cp++;
1673 cc--;
1674 if (ISSET(tp->t_lflag, FLUSHO) ||
1675 tp->t_outq.c_cc > hiwat)
1676 goto ovhiwat;
1677 continue;
1678 }
1679 }
1680 /*
1681 * A bunch of normal characters have been found.
1682 * Transfer them en masse to the output queue and
1683 * continue processing at the top of the loop.
1684 * If there are any further characters in this
1685 * <= OBUFSIZ chunk, the first should be a character
1686 * requiring special handling by ttyoutput.
1687 */
1688 tp->t_rocount = 0;
1689 i = b_to_q(cp, ce, &tp->t_outq);
1690 ce -= i;
1691 tp->t_column += ce;
1692 cp += ce, cc -= ce, tk_nout += ce;
1693 tp->t_outcc += ce;
1694 if (i > 0) {
1695 /* out of space */
1696 goto overfull;
1697 }
1698 if (ISSET(tp->t_lflag, FLUSHO) ||
1699 tp->t_outq.c_cc > hiwat)
1700 break;
1701 }
1702 ttstart(tp);
1703 }
1704 out:
1705 /*
1706 * If cc is nonzero, we leave the uio structure inconsistent, as the
1707 * offset and iov pointers have moved forward, but it doesn't matter
1708 * (the call will either return short or restart with a new uio).
1709 */
1710 uio->uio_resid += cc;
1711 return (error);
1712
1713 overfull:
1714 /*
1715 * Since we are using ring buffers, if we can't insert any more into
1716 * the output queue, we can assume the ring is full and that someone
1717 * forgot to set the high water mark correctly. We set it and then
1718 * proceed as normal.
1719 */
1720 hiwat = tp->t_outq.c_cc - 1;
1721
1722 ovhiwat:
1723 ttstart(tp);
1724 s = spltty();
1725 /*
1726 * This can only occur if FLUSHO is set in t_lflag,
1727 * or if ttstart/oproc is synchronous (or very fast).
1728 */
1729 if (tp->t_outq.c_cc <= hiwat) {
1730 splx(s);
1731 goto loop;
1732 }
1733 if (flag & IO_NDELAY) {
1734 splx(s);
1735 uio->uio_resid += cc;
1736 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1737 }
1738 SET(tp->t_state, TS_ASLEEP);
1739 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1740 splx(s);
1741 if (error)
1742 goto out;
1743 goto loop;
1744 }
1745
1746 /*
1747 * Rubout one character from the rawq of tp
1748 * as cleanly as possible.
1749 */
1750 void
1751 ttyrub(c, tp)
1752 int c;
1753 struct tty *tp;
1754 {
1755 u_char *cp;
1756 int savecol;
1757 int tabc, s;
1758
1759 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1760 return;
1761 CLR(tp->t_lflag, FLUSHO);
1762 if (ISSET(tp->t_lflag, ECHOE)) {
1763 if (tp->t_rocount == 0) {
1764 /*
1765 * Screwed by ttwrite; retype
1766 */
1767 ttyretype(tp);
1768 return;
1769 }
1770 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1771 ttyrubo(tp, 2);
1772 else {
1773 CLR(c, ~TTY_CHARMASK);
1774 switch (CCLASS(c)) {
1775 case ORDINARY:
1776 ttyrubo(tp, 1);
1777 break;
1778 case BACKSPACE:
1779 case CONTROL:
1780 case NEWLINE:
1781 case RETURN:
1782 case VTAB:
1783 if (ISSET(tp->t_lflag, ECHOCTL))
1784 ttyrubo(tp, 2);
1785 break;
1786 case TAB:
1787 if (tp->t_rocount < tp->t_rawq.c_cc) {
1788 ttyretype(tp);
1789 return;
1790 }
1791 s = spltty();
1792 savecol = tp->t_column;
1793 SET(tp->t_state, TS_CNTTB);
1794 SET(tp->t_lflag, FLUSHO);
1795 tp->t_column = tp->t_rocol;
1796 for (cp = firstc(&tp->t_rawq, &tabc); cp;
1797 cp = nextc(&tp->t_rawq, cp, &tabc))
1798 ttyecho(tabc, tp);
1799 CLR(tp->t_lflag, FLUSHO);
1800 CLR(tp->t_state, TS_CNTTB);
1801 splx(s);
1802
1803 /* savecol will now be length of the tab. */
1804 savecol -= tp->t_column;
1805 tp->t_column += savecol;
1806 if (savecol > 8)
1807 savecol = 8; /* overflow screw */
1808 while (--savecol >= 0)
1809 (void)ttyoutput('\b', tp);
1810 break;
1811 default: /* XXX */
1812 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
1813 (void)printf(PANICSTR, c, CCLASS(c));
1814 #ifdef notdef
1815 panic(PANICSTR, c, CCLASS(c));
1816 #endif
1817 }
1818 }
1819 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
1820 if (!ISSET(tp->t_state, TS_ERASE)) {
1821 SET(tp->t_state, TS_ERASE);
1822 (void)ttyoutput('\\', tp);
1823 }
1824 ttyecho(c, tp);
1825 } else
1826 ttyecho(tp->t_cc[VERASE], tp);
1827 --tp->t_rocount;
1828 }
1829
1830 /*
1831 * Back over cnt characters, erasing them.
1832 */
1833 static void
1834 ttyrubo(tp, cnt)
1835 struct tty *tp;
1836 int cnt;
1837 {
1838
1839 while (cnt-- > 0) {
1840 (void)ttyoutput('\b', tp);
1841 (void)ttyoutput(' ', tp);
1842 (void)ttyoutput('\b', tp);
1843 }
1844 }
1845
1846 /*
1847 * ttyretype --
1848 * Reprint the rawq line. Note, it is assumed that c_cc has already
1849 * been checked.
1850 */
1851 void
1852 ttyretype(tp)
1853 struct tty *tp;
1854 {
1855 u_char *cp;
1856 int s, c;
1857
1858 /* Echo the reprint character. */
1859 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1860 ttyecho(tp->t_cc[VREPRINT], tp);
1861
1862 (void)ttyoutput('\n', tp);
1863
1864 s = spltty();
1865 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
1866 ttyecho(c, tp);
1867 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
1868 ttyecho(c, tp);
1869 CLR(tp->t_state, TS_ERASE);
1870 splx(s);
1871
1872 tp->t_rocount = tp->t_rawq.c_cc;
1873 tp->t_rocol = 0;
1874 }
1875
1876 /*
1877 * Echo a typed character to the terminal.
1878 */
1879 static void
1880 ttyecho(c, tp)
1881 int c;
1882 struct tty *tp;
1883 {
1884
1885 if (!ISSET(tp->t_state, TS_CNTTB))
1886 CLR(tp->t_lflag, FLUSHO);
1887 if ((!ISSET(tp->t_lflag, ECHO) &&
1888 (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) ||
1889 ISSET(tp->t_lflag, EXTPROC))
1890 return;
1891 if (((ISSET(tp->t_lflag, ECHOCTL) &&
1892 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) ||
1893 ISSET(c, TTY_CHARMASK) == 0177)) {
1894 (void)ttyoutput('^', tp);
1895 CLR(c, ~TTY_CHARMASK);
1896 if (c == 0177)
1897 c = '?';
1898 else
1899 c += 'A' - 1;
1900 }
1901 (void)ttyoutput(c, tp);
1902 }
1903
1904 /*
1905 * Wake up any readers on a tty.
1906 */
1907 void
1908 ttwakeup(tp)
1909 struct tty *tp;
1910 {
1911
1912 selwakeup(&tp->t_rsel);
1913 if (ISSET(tp->t_state, TS_ASYNC))
1914 pgsignal(tp->t_pgrp, SIGIO, 1);
1915 wakeup((caddr_t)&tp->t_rawq);
1916 }
1917
1918 /*
1919 * Look up a code for a specified speed in a conversion table;
1920 * used by drivers to map software speed values to hardware parameters.
1921 */
1922 int
1923 ttspeedtab(speed, table)
1924 int speed;
1925 struct speedtab *table;
1926 {
1927
1928 for ( ; table->sp_speed != -1; table++)
1929 if (table->sp_speed == speed)
1930 return (table->sp_code);
1931 return (-1);
1932 }
1933
1934 /*
1935 * Set tty hi and low water marks.
1936 *
1937 * Try to arrange the dynamics so there's about one second
1938 * from hi to low water.
1939 */
1940 void
1941 ttsetwater(tp)
1942 struct tty *tp;
1943 {
1944 int cps, x;
1945
1946 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
1947
1948 cps = tp->t_ospeed / 10;
1949 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
1950 x += cps;
1951 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
1952 tp->t_hiwat = roundup(x, CBSIZE);
1953 #undef CLAMP
1954 }
1955
1956 /*
1957 * Report on state of foreground process group.
1958 */
1959 void
1960 ttyinfo(tp)
1961 struct tty *tp;
1962 {
1963 struct lwp *l;
1964 struct proc *p, *pick;
1965 struct timeval utime, stime;
1966 int tmp;
1967
1968 if (ttycheckoutq(tp,0) == 0)
1969 return;
1970
1971 /* Print load average. */
1972 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
1973 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1974
1975 if (tp->t_session == NULL)
1976 ttyprintf(tp, "not a controlling terminal\n");
1977 else if (tp->t_pgrp == NULL)
1978 ttyprintf(tp, "no foreground process group\n");
1979 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
1980 ttyprintf(tp, "empty foreground process group\n");
1981 else {
1982 /* Pick interesting process. */
1983 for (pick = NULL; p != NULL; p = p->p_pglist.le_next)
1984 if (proc_compare(pick, p))
1985 pick = p;
1986
1987 ttyprintf(tp, " cmd: %s %d [", pick->p_comm, pick->p_pid);
1988 LIST_FOREACH(l, &p->p_lwps, l_sibling)
1989 ttyprintf(tp, "%s%s",
1990 l->l_stat == LSONPROC ? "running" :
1991 l->l_stat == LSRUN ? "runnable" :
1992 l->l_wmesg ? l->l_wmesg : "iowait",
1993 (LIST_NEXT(l, l_sibling) != NULL) ? " " : "] ");
1994
1995 calcru(pick, &utime, &stime, NULL);
1996
1997 /* Round up and print user time. */
1998 utime.tv_usec += 5000;
1999 if (utime.tv_usec >= 1000000) {
2000 utime.tv_sec += 1;
2001 utime.tv_usec -= 1000000;
2002 }
2003 ttyprintf(tp, "%ld.%02ldu ", (long int)utime.tv_sec,
2004 (long int)utime.tv_usec / 10000);
2005
2006 /* Round up and print system time. */
2007 stime.tv_usec += 5000;
2008 if (stime.tv_usec >= 1000000) {
2009 stime.tv_sec += 1;
2010 stime.tv_usec -= 1000000;
2011 }
2012 ttyprintf(tp, "%ld.%02lds ", (long int)stime.tv_sec,
2013 (long int)stime.tv_usec / 10000);
2014
2015 #define pgtok(a) (((u_long) ((a) * PAGE_SIZE) / 1024))
2016 /* Print percentage cpu. */
2017 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
2018 ttyprintf(tp, "%d%% ", tmp / 100);
2019
2020 /* Print resident set size. */
2021 if (pick->p_stat == SIDL || P_ZOMBIE(pick))
2022 tmp = 0;
2023 else {
2024 struct vmspace *vm = pick->p_vmspace;
2025 tmp = pgtok(vm_resident_count(vm));
2026 }
2027 ttyprintf(tp, "%dk\n", tmp);
2028 }
2029 tp->t_rocount = 0; /* so pending input will be retyped if BS */
2030 }
2031
2032 /*
2033 * Returns 1 if p2 is "better" than p1
2034 *
2035 * The algorithm for picking the "interesting" process is thus:
2036 *
2037 * 1) Only foreground processes are eligible - implied.
2038 * 2) Runnable processes are favored over anything else. The runner
2039 * with the highest cpu utilization is picked (p_estcpu). Ties are
2040 * broken by picking the highest pid.
2041 * 3) The sleeper with the shortest sleep time is next. With ties,
2042 * we pick out just "short-term" sleepers (P_SINTR == 0).
2043 * 4) Further ties are broken by picking the highest pid.
2044 */
2045 #define ISRUN(p) ((p)->p_nrlwps > 0)
2046 #define TESTAB(a, b) ((a)<<1 | (b))
2047 #define ONLYA 2
2048 #define ONLYB 1
2049 #define BOTH 3
2050
2051 static int
2052 proc_compare(p1, p2)
2053 struct proc *p1, *p2;
2054 {
2055
2056 if (p1 == NULL)
2057 return (1);
2058 /*
2059 * see if at least one of them is runnable
2060 */
2061 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2062 case ONLYA:
2063 return (0);
2064 case ONLYB:
2065 return (1);
2066 case BOTH:
2067 /*
2068 * tie - favor one with highest recent cpu utilization
2069 */
2070 if (p2->p_estcpu > p1->p_estcpu)
2071 return (1);
2072 if (p1->p_estcpu > p2->p_estcpu)
2073 return (0);
2074 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2075 }
2076 /*
2077 * weed out zombies
2078 */
2079 switch (TESTAB(P_ZOMBIE(p1), P_ZOMBIE(p2))) {
2080 case ONLYA:
2081 return (1);
2082 case ONLYB:
2083 return (0);
2084 case BOTH:
2085 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2086 }
2087 #if 0 /* XXX NJWLWP */
2088 /*
2089 * pick the one with the smallest sleep time
2090 */
2091 if (p2->p_slptime > p1->p_slptime)
2092 return (0);
2093 if (p1->p_slptime > p2->p_slptime)
2094 return (1);
2095 /*
2096 * favor one sleeping in a non-interruptible sleep
2097 */
2098 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
2099 return (1);
2100 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
2101 return (0);
2102 #endif
2103 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2104 }
2105
2106 /*
2107 * Output char to tty; console putchar style.
2108 */
2109 int
2110 tputchar(c, tp)
2111 int c;
2112 struct tty *tp;
2113 {
2114 int s;
2115
2116 s = spltty();
2117 if (ISSET(tp->t_state,
2118 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) {
2119 splx(s);
2120 return (-1);
2121 }
2122 if (c == '\n')
2123 (void)ttyoutput('\r', tp);
2124 (void)ttyoutput(c, tp);
2125 ttstart(tp);
2126 splx(s);
2127 return (0);
2128 }
2129
2130 /*
2131 * Sleep on chan, returning ERESTART if tty changed while we napped and
2132 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
2133 * the tty is revoked, restarting a pending call will redo validation done
2134 * at the start of the call.
2135 */
2136 int
2137 ttysleep(tp, chan, pri, wmesg, timo)
2138 struct tty *tp;
2139 void *chan;
2140 int pri, timo;
2141 const char *wmesg;
2142 {
2143 int error;
2144 short gen;
2145
2146 gen = tp->t_gen;
2147 if ((error = tsleep(chan, pri, wmesg, timo)) != 0)
2148 return (error);
2149 return (tp->t_gen == gen ? 0 : ERESTART);
2150 }
2151
2152 /*
2153 * Initialise the global tty list.
2154 */
2155 void
2156 tty_init()
2157 {
2158 ttyldisc_init();
2159
2160 TAILQ_INIT(&ttylist);
2161 tty_count = 0;
2162
2163 pool_init(&tty_pool, sizeof(struct tty), 0, 0, 0, "ttypl",
2164 0, pool_page_alloc_nointr, pool_page_free_nointr, M_TTYS);
2165 }
2166
2167 /*
2168 * Attach a tty to the tty list.
2169 *
2170 * This should be called ONLY once per real tty (including pty's).
2171 * eg, on the sparc, the keyboard and mouse have struct tty's that are
2172 * distinctly NOT usable as tty's, and thus should not be attached to
2173 * the ttylist. This is why this call is not done from ttymalloc().
2174 *
2175 * Device drivers should attach tty's at a similar time that they are
2176 * ttymalloc()'ed, or, for the case of statically allocated struct tty's
2177 * either in the attach or (first) open routine.
2178 */
2179 void
2180 tty_attach(tp)
2181 struct tty *tp;
2182 {
2183
2184 TAILQ_INSERT_TAIL(&ttylist, tp, tty_link);
2185 ++tty_count;
2186 }
2187
2188 /*
2189 * Remove a tty from the tty list.
2190 */
2191 void
2192 tty_detach(tp)
2193 struct tty *tp;
2194 {
2195
2196 --tty_count;
2197 #ifdef DIAGNOSTIC
2198 if (tty_count < 0)
2199 panic("tty_detach: tty_count < 0");
2200 #endif
2201 TAILQ_REMOVE(&ttylist, tp, tty_link);
2202 }
2203
2204 /*
2205 * Allocate a tty structure and its associated buffers.
2206 */
2207 struct tty *
2208 ttymalloc()
2209 {
2210 struct tty *tp;
2211
2212 tp = pool_get(&tty_pool, PR_WAITOK);
2213 memset(tp, 0, sizeof(*tp));
2214 callout_init(&tp->t_outq_ch);
2215 callout_init(&tp->t_rstrt_ch);
2216 /* XXX: default to 1024 chars for now */
2217 clalloc(&tp->t_rawq, 1024, 1);
2218 clalloc(&tp->t_canq, 1024, 1);
2219 /* output queue doesn't need quoting */
2220 clalloc(&tp->t_outq, 1024, 0);
2221 /* Set default line discipline. */
2222 tp->t_linesw = linesw[0];
2223 return(tp);
2224 }
2225
2226 /*
2227 * Free a tty structure and its buffers.
2228 *
2229 * Be sure to call tty_detach() for any tty that has been
2230 * tty_attach()ed.
2231 */
2232 void
2233 ttyfree(tp)
2234 struct tty *tp;
2235 {
2236
2237 callout_stop(&tp->t_outq_ch);
2238 callout_stop(&tp->t_rstrt_ch);
2239 clfree(&tp->t_rawq);
2240 clfree(&tp->t_canq);
2241 clfree(&tp->t_outq);
2242 pool_put(&tty_pool, tp);
2243 }
2244