From 6858c2cc8f2ac2b04f096efa38e202492921de47 Mon Sep 17 00:00:00 2001 From: Conrad Meyer Date: Sat, 20 Oct 2018 18:31:36 +0000 Subject: [PATCH] Replace ttyprintf with sbuf_printf and tty drain routine Add string variants of cnputc and tty_putchar, and use them from the tty sbuf drain routine. Suggested by: ed@ Sponsored by: Dell EMC Isilon --- sys/kern/kern_cons.c | 14 ++++++++---- sys/kern/subr_prf.c | 21 ------------------ sys/kern/tty_info.c | 48 +++++++++++++++++++++++++++++++++++------- sys/kern/tty_ttydisc.c | 14 ++++++++++-- sys/sys/cons.h | 1 + sys/sys/systm.h | 1 - sys/sys/tty.h | 8 +++++++ 7 files changed, 71 insertions(+), 36 deletions(-) diff --git a/sys/kern/kern_cons.c b/sys/kern/kern_cons.c index 4e2d4c6acbe..8c252af59bc 100644 --- a/sys/kern/kern_cons.c +++ b/sys/kern/kern_cons.c @@ -522,9 +522,9 @@ cnputc(int c) } void -cnputs(char *p) +cnputsn(const char *p, size_t n) { - int c; + size_t i; int unlock_reqd = 0; if (use_cnputs_mtx) { @@ -539,13 +539,19 @@ cnputs(char *p) unlock_reqd = 1; } - while ((c = *p++) != '\0') - cnputc(c); + for (i = 0; i < n; i++) + cnputc(p[i]); if (unlock_reqd) mtx_unlock_spin(&cnputs_mtx); } +void +cnputs(char *p) +{ + cnputsn(p, strlen(p)); +} + static int consmsgbuf_size = 8192; SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0, "Console tty buffer size"); diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index 8436cca30c6..ed5552fba1d 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -258,27 +258,6 @@ vtprintf(struct proc *p, int pri, const char *fmt, va_list ap) msgbuftrigger = 1; } -/* - * Ttyprintf displays a message on a tty; it should be used only by - * the tty driver, or anything that knows the underlying tty will not - * be revoke(2)'d away. Other callers should use tprintf. - */ -int -ttyprintf(struct tty *tp, const char *fmt, ...) -{ - va_list ap; - struct putchar_arg pca; - int retval; - - va_start(ap, fmt); - pca.tty = tp; - pca.flags = TOTTY; - pca.p_bufr = NULL; - retval = kvprintf(fmt, putchar, &pca, 10, ap); - va_end(ap); - return (retval); -} - static int _vprintf(int level, int flags, const char *fmt, va_list ap) { diff --git a/sys/kern/tty_info.c b/sys/kern/tty_info.c index ad3e1e77887..5233a19c585 100644 --- a/sys/kern/tty_info.c +++ b/sys/kern/tty_info.c @@ -46,10 +46,13 @@ __FBSDID("$FreeBSD$"); #include +#include +#include #include #include #include #include +#include #include #include #include @@ -209,6 +212,27 @@ proc_compare(struct proc *p1, struct proc *p2) return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ } +static int +sbuf_tty_drain(void *a, const char *d, int len) +{ + struct tty *tp; + int rc; + + tp = a; + + if (kdb_active) { + cnputsn(d, len); + return (len); + } + if (tp != NULL && panicstr == NULL) { + rc = tty_putstrn(tp, d, len); + if (rc != 0) + return (-ENXIO); + return (len); + } + return (-ENXIO); +} + /* * Report on state of foreground process group. */ @@ -219,6 +243,7 @@ tty_info(struct tty *tp) struct proc *p, *ppick; struct thread *td, *tdpick; const char *stateprefix, *state; + struct sbuf sb; long rss; int load, pctcpu; pid_t pid; @@ -230,24 +255,27 @@ tty_info(struct tty *tp) if (tty_checkoutq(tp) == 0) return; + (void)sbuf_new(&sb, tp->t_prbuf, sizeof(tp->t_prbuf), SBUF_FIXEDLEN); + sbuf_set_drain(&sb, sbuf_tty_drain, tp); + /* Print load average. */ load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; - ttyprintf(tp, "%sload: %d.%02d ", tp->t_column == 0 ? "" : "\n", + sbuf_printf(&sb, "%sload: %d.%02d ", tp->t_column == 0 ? "" : "\n", load / 100, load % 100); if (tp->t_session == NULL) { - ttyprintf(tp, "not a controlling terminal\n"); - return; + sbuf_printf(&sb, "not a controlling terminal\n"); + goto out; } if (tp->t_pgrp == NULL) { - ttyprintf(tp, "no foreground process group\n"); - return; + sbuf_printf(&sb, "no foreground process group\n"); + goto out; } PGRP_LOCK(tp->t_pgrp); if (LIST_EMPTY(&tp->t_pgrp->pg_members)) { PGRP_UNLOCK(tp->t_pgrp); - ttyprintf(tp, "empty foreground process group\n"); - return; + sbuf_printf(&sb, "empty foreground process group\n"); + goto out; } /* @@ -305,11 +333,15 @@ tty_info(struct tty *tp) PROC_UNLOCK(p); /* Print command, pid, state, rtime, utime, stime, %cpu, and rss. */ - ttyprintf(tp, + sbuf_printf(&sb, " cmd: %s %d [%s%s] %ld.%02ldr %ld.%02ldu %ld.%02lds %d%% %ldk\n", comm, pid, stateprefix, state, (long)rtime.tv_sec, rtime.tv_usec / 10000, (long)utime.tv_sec, utime.tv_usec / 10000, (long)stime.tv_sec, stime.tv_usec / 10000, pctcpu / 100, rss); + +out: + sbuf_finish(&sb); + sbuf_delete(&sb); } diff --git a/sys/kern/tty_ttydisc.c b/sys/kern/tty_ttydisc.c index a0798d0b54a..25cc380a866 100644 --- a/sys/kern/tty_ttydisc.c +++ b/sys/kern/tty_ttydisc.c @@ -1251,17 +1251,27 @@ ttydisc_getc_poll(struct tty *tp) */ int -tty_putchar(struct tty *tp, char c) +tty_putstrn(struct tty *tp, const char *p, size_t n) { + size_t i; + tty_lock_assert(tp, MA_OWNED); if (tty_gone(tp)) return (-1); - ttydisc_echo_force(tp, c, 0); + for (i = 0; i < n; i++) + ttydisc_echo_force(tp, p[i], 0); + tp->t_writepos = tp->t_column; ttyinq_reprintpos_set(&tp->t_inq); ttydevsw_outwakeup(tp); return (0); } + +int +tty_putchar(struct tty *tp, char c) +{ + return (tty_putstrn(tp, &c, 1)); +} diff --git a/sys/sys/cons.h b/sys/sys/cons.h index 047846463bf..432631d4999 100644 --- a/sys/sys/cons.h +++ b/sys/sys/cons.h @@ -137,6 +137,7 @@ int cngetc(void); void cngets(char *, size_t, int); void cnputc(int); void cnputs(char *); +void cnputsn(const char *, size_t); int cnunavailable(void); void constty_set(struct tty *tp); void constty_clear(void); diff --git a/sys/sys/systm.h b/sys/sys/systm.h index a008cc39d8d..4e0427927d7 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -290,7 +290,6 @@ int vasprintf(char **ret, struct malloc_type *mtp, const char *format, int vsnprintf(char *, size_t, const char *, __va_list) __printflike(3, 0); int vsnrprintf(char *, size_t, int, const char *, __va_list) __printflike(4, 0); int vsprintf(char *buf, const char *, __va_list) __printflike(2, 0); -int ttyprintf(struct tty *, const char *, ...) __printflike(2, 3); int sscanf(const char *, char const * _Nonnull, ...) __scanflike(2, 3); int vsscanf(const char * _Nonnull, char const * _Nonnull, __va_list) __scanflike(2, 0); long strtol(const char *, char **, int); diff --git a/sys/sys/tty.h b/sys/sys/tty.h index 92ad89a6b79..c85ca559361 100644 --- a/sys/sys/tty.h +++ b/sys/sys/tty.h @@ -132,6 +132,13 @@ struct tty { void *t_devswsoftc; /* (c) Soft config, for drivers. */ void *t_hooksoftc; /* (t) Soft config, for hooks. */ struct cdev *t_dev; /* (c) Primary character device. */ + +#ifndef PRINTF_BUFR_SIZE +#define TTY_PRINTF_SIZE 256 +#else +#define TTY_PRINTF_SIZE PRINTF_BUFR_SIZE +#endif + char t_prbuf[TTY_PRINTF_SIZE]; /* (t) */ }; /* @@ -194,6 +201,7 @@ void tty_wakeup(struct tty *tp, int flags); /* System messages. */ int tty_checkoutq(struct tty *tp); int tty_putchar(struct tty *tp, char c); +int tty_putstrn(struct tty *tp, const char *p, size_t n); int tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td);