| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /*- |
| 2 | * Copyright (c) 1986, 1988, 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 | * Redistribution and use in source and binary forms, with or without | |
| 11 | * modification, are permitted provided that the following conditions | |
| 12 | * are met: | |
| 13 | * 1. Redistributions of source code must retain the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer. | |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 16 | * notice, this list of conditions and the following disclaimer in the | |
| 17 | * documentation and/or other materials provided with the distribution. | |
| 18 | * 3. All advertising materials mentioning features or use of this software | |
| 19 | * must display the following acknowledgement: | |
| 20 | * This product includes software developed by the University of | |
| 21 | * California, Berkeley and its contributors. | |
| 22 | * 4. Neither the name of the University nor the names of its contributors | |
| 23 | * may be used to endorse or promote products derived from this software | |
| 24 | * without specific prior written permission. | |
| 25 | * | |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 36 | * SUCH DAMAGE. | |
| 37 | * | |
| 38 | * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 | |
| 39 | * $FreeBSD: src/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $ | |
| 448f8e02 | 40 | * $DragonFly: src/sys/kern/subr_prf.c,v 1.21 2008/07/17 23:56:23 dillon Exp $ |
| 984263bc MD |
41 | */ |
| 42 | ||
| a0a36cfd SW |
43 | #include "opt_ddb.h" |
| 44 | ||
| 984263bc MD |
45 | #include <sys/param.h> |
| 46 | #include <sys/systm.h> | |
| 47 | #include <sys/kernel.h> | |
| 48 | #include <sys/msgbuf.h> | |
| 49 | #include <sys/malloc.h> | |
| 50 | #include <sys/proc.h> | |
| 895c1f85 | 51 | #include <sys/priv.h> |
| 984263bc MD |
52 | #include <sys/tty.h> |
| 53 | #include <sys/tprintf.h> | |
| a0a36cfd | 54 | #include <sys/stdint.h> |
| 984263bc MD |
55 | #include <sys/syslog.h> |
| 56 | #include <sys/cons.h> | |
| 57 | #include <sys/uio.h> | |
| 58 | #include <sys/sysctl.h> | |
| 8a8d5d85 | 59 | #include <sys/lock.h> |
| a0a36cfd | 60 | #include <sys/ctype.h> |
| 28d7e704 MD |
61 | #include <sys/eventhandler.h> |
| 62 | #include <sys/kthread.h> | |
| a0a36cfd | 63 | |
| 5fddbda2 MD |
64 | #include <sys/thread2.h> |
| 65 | #include <sys/spinlock2.h> | |
| 66 | ||
| a0a36cfd SW |
67 | #ifdef DDB |
| 68 | #include <ddb/ddb.h> | |
| 69 | #endif | |
| 984263bc MD |
70 | |
| 71 | /* | |
| 72 | * Note that stdarg.h and the ANSI style va_start macro is used for both | |
| e2565a42 MD |
73 | * ANSI and traditional C compilers. We use the __ machine version to stay |
| 74 | * within the kernel header file set. | |
| 984263bc MD |
75 | */ |
| 76 | #include <machine/stdarg.h> | |
| 77 | ||
| 28d7e704 MD |
78 | #define TOCONS 0x01 |
| 79 | #define TOTTY 0x02 | |
| 80 | #define TOLOG 0x04 | |
| 81 | #define TOWAKEUP 0x08 | |
| 984263bc MD |
82 | |
| 83 | /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ | |
| a0a36cfd | 84 | #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) |
| 984263bc MD |
85 | |
| 86 | struct putchar_arg { | |
| 87 | int flags; | |
| 88 | int pri; | |
| 89 | struct tty *tty; | |
| 90 | }; | |
| 91 | ||
| 92 | struct snprintf_arg { | |
| 93 | char *str; | |
| 94 | size_t remain; | |
| 95 | }; | |
| 96 | ||
| 97 | extern int log_open; | |
| 98 | ||
| 99 | struct tty *constty; /* pointer to console "window" tty */ | |
| 100 | ||
| 984263bc MD |
101 | static void msglogchar(int c, int pri); |
| 102 | static void msgaddchar(int c, void *dummy); | |
| 0ced1954 | 103 | static void kputchar (int ch, void *arg); |
| a0a36cfd SW |
104 | static char *ksprintn (char *nbuf, uintmax_t num, int base, int *lenp, |
| 105 | int upper); | |
| 402ed7e1 | 106 | static void snprintf_func (int ch, void *arg); |
| 984263bc MD |
107 | |
| 108 | static int consintr = 1; /* Ok to handle console interrupts? */ | |
| 109 | static int msgbufmapped; /* Set when safe to use msgbuf */ | |
| 5fddbda2 | 110 | static struct spinlock cons_spin = SPINLOCK_INITIALIZER(cons_spin); |
| 28d7e704 | 111 | static thread_t constty_td = NULL; |
| 5fddbda2 | 112 | |
| 984263bc MD |
113 | int msgbuftrigger; |
| 114 | ||
| 115 | static int log_console_output = 1; | |
| 116 | TUNABLE_INT("kern.log_console_output", &log_console_output); | |
| 117 | SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW, | |
| 118 | &log_console_output, 0, ""); | |
| 119 | ||
| d41cbec3 | 120 | static int unprivileged_read_msgbuf = 1; |
| 43a0f7ae | 121 | SYSCTL_INT(_security, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW, |
| d41cbec3 MD |
122 | &unprivileged_read_msgbuf, 0, |
| 123 | "Unprivileged processes may read the kernel message buffer"); | |
| 124 | ||
| 984263bc MD |
125 | /* |
| 126 | * Warn that a system table is full. | |
| 127 | */ | |
| 128 | void | |
| 129 | tablefull(const char *tab) | |
| 130 | { | |
| 131 | ||
| 132 | log(LOG_ERR, "%s: table is full\n", tab); | |
| 133 | } | |
| 134 | ||
| 135 | /* | |
| 136 | * Uprintf prints to the controlling terminal for the current process. | |
| 984263bc MD |
137 | */ |
| 138 | int | |
| 139 | uprintf(const char *fmt, ...) | |
| 140 | { | |
| 141 | struct proc *p = curproc; | |
| e2565a42 | 142 | __va_list ap; |
| 984263bc MD |
143 | struct putchar_arg pca; |
| 144 | int retval = 0; | |
| 145 | ||
| 146 | if (p && p->p_flag & P_CONTROLT && | |
| 147 | p->p_session->s_ttyvp) { | |
| e2565a42 | 148 | __va_start(ap, fmt); |
| 984263bc MD |
149 | pca.tty = p->p_session->s_ttyp; |
| 150 | pca.flags = TOTTY; | |
| 8a8d5d85 | 151 | |
| 379210cb | 152 | retval = kvcprintf(fmt, kputchar, &pca, 10, ap); |
| e2565a42 | 153 | __va_end(ap); |
| 984263bc | 154 | } |
| a0a36cfd | 155 | return (retval); |
| 984263bc MD |
156 | } |
| 157 | ||
| 158 | tpr_t | |
| dadab5e9 | 159 | tprintf_open(struct proc *p) |
| 984263bc | 160 | { |
| dadab5e9 | 161 | if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) { |
| 8b90699b | 162 | sess_hold(p->p_session); |
| 984263bc MD |
163 | return ((tpr_t) p->p_session); |
| 164 | } | |
| 165 | return ((tpr_t) NULL); | |
| 166 | } | |
| 167 | ||
| 168 | void | |
| dadab5e9 | 169 | tprintf_close(tpr_t sess) |
| 984263bc | 170 | { |
| 984263bc | 171 | if (sess) |
| 8b90699b | 172 | sess_rele((struct session *) sess); |
| 984263bc MD |
173 | } |
| 174 | ||
| 175 | /* | |
| 176 | * tprintf prints on the controlling terminal associated | |
| 177 | * with the given session. | |
| 178 | */ | |
| 179 | int | |
| 180 | tprintf(tpr_t tpr, const char *fmt, ...) | |
| 181 | { | |
| 1fd87d54 | 182 | struct session *sess = (struct session *)tpr; |
| 984263bc MD |
183 | struct tty *tp = NULL; |
| 184 | int flags = TOLOG; | |
| e2565a42 | 185 | __va_list ap; |
| 984263bc MD |
186 | struct putchar_arg pca; |
| 187 | int retval; | |
| 188 | ||
| 189 | if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { | |
| 190 | flags |= TOTTY; | |
| 191 | tp = sess->s_ttyp; | |
| 192 | } | |
| e2565a42 | 193 | __va_start(ap, fmt); |
| 984263bc MD |
194 | pca.tty = tp; |
| 195 | pca.flags = flags; | |
| 196 | pca.pri = LOG_INFO; | |
| 379210cb | 197 | retval = kvcprintf(fmt, kputchar, &pca, 10, ap); |
| e2565a42 | 198 | __va_end(ap); |
| 984263bc | 199 | msgbuftrigger = 1; |
| a0a36cfd | 200 | return (retval); |
| 984263bc MD |
201 | } |
| 202 | ||
| 203 | /* | |
| 204 | * Ttyprintf displays a message on a tty; it should be used only by | |
| 205 | * the tty driver, or anything that knows the underlying tty will not | |
| 206 | * be revoke(2)'d away. Other callers should use tprintf. | |
| 207 | */ | |
| 208 | int | |
| 209 | ttyprintf(struct tty *tp, const char *fmt, ...) | |
| 210 | { | |
| e2565a42 | 211 | __va_list ap; |
| 984263bc MD |
212 | struct putchar_arg pca; |
| 213 | int retval; | |
| 214 | ||
| e2565a42 | 215 | __va_start(ap, fmt); |
| 984263bc MD |
216 | pca.tty = tp; |
| 217 | pca.flags = TOTTY; | |
| 379210cb | 218 | retval = kvcprintf(fmt, kputchar, &pca, 10, ap); |
| e2565a42 | 219 | __va_end(ap); |
| a0a36cfd | 220 | return (retval); |
| 984263bc MD |
221 | } |
| 222 | ||
| 223 | /* | |
| 224 | * Log writes to the log buffer, and guarantees not to sleep (so can be | |
| 225 | * called by interrupt routines). If there is no process reading the | |
| 226 | * log yet, it writes to the console also. | |
| 227 | */ | |
| 228 | int | |
| 229 | log(int level, const char *fmt, ...) | |
| 230 | { | |
| e2565a42 | 231 | __va_list ap; |
| 984263bc MD |
232 | int retval; |
| 233 | struct putchar_arg pca; | |
| 234 | ||
| 235 | pca.tty = NULL; | |
| 236 | pca.pri = level; | |
| 237 | pca.flags = log_open ? TOLOG : TOCONS; | |
| 238 | ||
| e2565a42 | 239 | __va_start(ap, fmt); |
| 379210cb | 240 | retval = kvcprintf(fmt, kputchar, &pca, 10, ap); |
| e2565a42 | 241 | __va_end(ap); |
| 984263bc MD |
242 | |
| 243 | msgbuftrigger = 1; | |
| 244 | return (retval); | |
| 245 | } | |
| 246 | ||
| 984263bc MD |
247 | #define CONSCHUNK 128 |
| 248 | ||
| 249 | void | |
| 250 | log_console(struct uio *uio) | |
| 251 | { | |
| 252 | int c, i, error, iovlen, nl; | |
| 253 | struct uio muio; | |
| 254 | struct iovec *miov = NULL; | |
| 255 | char *consbuffer; | |
| 256 | int pri; | |
| 257 | ||
| 258 | if (!log_console_output) | |
| 259 | return; | |
| 260 | ||
| 261 | pri = LOG_INFO | LOG_CONSOLE; | |
| 262 | muio = *uio; | |
| 263 | iovlen = uio->uio_iovcnt * sizeof (struct iovec); | |
| 264 | MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK); | |
| 265 | MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK); | |
| 266 | bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen); | |
| 267 | muio.uio_iov = miov; | |
| 268 | uio = &muio; | |
| 269 | ||
| 270 | nl = 0; | |
| 271 | while (uio->uio_resid > 0) { | |
| e54488bb MD |
272 | c = (int)szmin(uio->uio_resid, CONSCHUNK); |
| 273 | error = uiomove(consbuffer, (size_t)c, uio); | |
| 984263bc | 274 | if (error != 0) |
| a0a36cfd | 275 | break; |
| 984263bc MD |
276 | for (i = 0; i < c; i++) { |
| 277 | msglogchar(consbuffer[i], pri); | |
| 278 | if (consbuffer[i] == '\n') | |
| 279 | nl = 1; | |
| 280 | else | |
| 281 | nl = 0; | |
| 282 | } | |
| 283 | } | |
| 284 | if (!nl) | |
| 285 | msglogchar('\n', pri); | |
| 286 | msgbuftrigger = 1; | |
| 287 | FREE(miov, M_TEMP); | |
| 288 | FREE(consbuffer, M_TEMP); | |
| 289 | return; | |
| 290 | } | |
| 291 | ||
| e02941a7 MD |
292 | /* |
| 293 | * Output to the console. | |
| e02941a7 | 294 | */ |
| 984263bc | 295 | int |
| 169276ba MD |
296 | kprintf(const char *fmt, ...) |
| 297 | { | |
| 298 | __va_list ap; | |
| 299 | int savintr; | |
| 300 | struct putchar_arg pca; | |
| 301 | int retval; | |
| 302 | ||
| 303 | savintr = consintr; /* disable interrupts */ | |
| 304 | consintr = 0; | |
| 305 | __va_start(ap, fmt); | |
| 306 | pca.tty = NULL; | |
| 307 | pca.flags = TOCONS | TOLOG; | |
| 308 | pca.pri = -1; | |
| 169276ba | 309 | retval = kvcprintf(fmt, kputchar, &pca, 10, ap); |
| 169276ba MD |
310 | __va_end(ap); |
| 311 | if (!panicstr) | |
| 312 | msgbuftrigger = 1; | |
| 313 | consintr = savintr; /* reenable interrupts */ | |
| a0a36cfd | 314 | return (retval); |
| 169276ba MD |
315 | } |
| 316 | ||
| 984263bc | 317 | int |
| 379210cb | 318 | kvprintf(const char *fmt, __va_list ap) |
| 984263bc MD |
319 | { |
| 320 | int savintr; | |
| 321 | struct putchar_arg pca; | |
| 322 | int retval; | |
| 323 | ||
| 324 | savintr = consintr; /* disable interrupts */ | |
| 325 | consintr = 0; | |
| 326 | pca.tty = NULL; | |
| 327 | pca.flags = TOCONS | TOLOG; | |
| 328 | pca.pri = -1; | |
| 379210cb | 329 | retval = kvcprintf(fmt, kputchar, &pca, 10, ap); |
| 984263bc MD |
330 | if (!panicstr) |
| 331 | msgbuftrigger = 1; | |
| 332 | consintr = savintr; /* reenable interrupts */ | |
| a0a36cfd | 333 | return (retval); |
| 984263bc MD |
334 | } |
| 335 | ||
| 336 | /* | |
| c8dfd00d MD |
337 | * Limited rate kprintf. The passed rate structure must be initialized |
| 338 | * with the desired reporting frequency. A frequency of 0 will result in | |
| 339 | * no output. | |
| 448f8e02 MD |
340 | * |
| 341 | * count may be initialized to a negative number to allow an initial | |
| 342 | * burst. | |
| c8dfd00d MD |
343 | */ |
| 344 | void | |
| 345 | krateprintf(struct krate *rate, const char *fmt, ...) | |
| 346 | { | |
| 347 | __va_list ap; | |
| 348 | ||
| 1c9c514a MD |
349 | if (rate->ticks != (int)time_second) { |
| 350 | rate->ticks = (int)time_second; | |
| 448f8e02 MD |
351 | if (rate->count > 0) |
| 352 | rate->count = 0; | |
| c8dfd00d MD |
353 | } |
| 354 | if (rate->count < rate->freq) { | |
| 355 | ++rate->count; | |
| 356 | __va_start(ap, fmt); | |
| 357 | kvprintf(fmt, ap); | |
| 358 | __va_end(ap); | |
| 359 | } | |
| 360 | } | |
| 361 | ||
| 362 | /* | |
| 137b3005 MD |
363 | * Print a character to the dmesg log, the console, and/or the user's |
| 364 | * terminal. | |
| e02941a7 | 365 | * |
| 28d7e704 MD |
366 | * NOTE: TOTTY does not require nonblocking operation, but TOCONS |
| 367 | * and TOLOG do. When we have a constty we still output to | |
| 368 | * the real console but we have a monitoring thread which | |
| 369 | * we wakeup which tracks the log. | |
| 984263bc MD |
370 | */ |
| 371 | static void | |
| 0ced1954 | 372 | kputchar(int c, void *arg) |
| 984263bc MD |
373 | { |
| 374 | struct putchar_arg *ap = (struct putchar_arg*) arg; | |
| 375 | int flags = ap->flags; | |
| 376 | struct tty *tp = ap->tty; | |
| 137b3005 | 377 | |
| 984263bc MD |
378 | if (panicstr) |
| 379 | constty = NULL; | |
| 28d7e704 MD |
380 | if ((flags & TOCONS) && tp == NULL && constty) |
| 381 | flags |= TOLOG | TOWAKEUP; | |
| e5104a66 MD |
382 | if ((flags & TOTTY) && tputchar(c, tp) < 0) |
| 383 | ap->flags &= ~TOTTY; | |
| 984263bc MD |
384 | if ((flags & TOLOG)) |
| 385 | msglogchar(c, ap->pri); | |
| 28d7e704 | 386 | if ((flags & TOCONS) && c) |
| 5fddbda2 | 387 | cnputc(c); |
| 28d7e704 MD |
388 | if (flags & TOWAKEUP) |
| 389 | wakeup(constty_td); | |
| 984263bc MD |
390 | } |
| 391 | ||
| 392 | /* | |
| 393 | * Scaled down version of sprintf(3). | |
| 394 | */ | |
| 395 | int | |
| 169276ba MD |
396 | ksprintf(char *buf, const char *cfmt, ...) |
| 397 | { | |
| 398 | int retval; | |
| 399 | __va_list ap; | |
| 400 | ||
| 401 | __va_start(ap, cfmt); | |
| 402 | retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap); | |
| 403 | buf[retval] = '\0'; | |
| 404 | __va_end(ap); | |
| a0a36cfd | 405 | return (retval); |
| 169276ba MD |
406 | } |
| 407 | ||
| 408 | /* | |
| 984263bc MD |
409 | * Scaled down version of vsprintf(3). |
| 410 | */ | |
| 411 | int | |
| 379210cb | 412 | kvsprintf(char *buf, const char *cfmt, __va_list ap) |
| 984263bc MD |
413 | { |
| 414 | int retval; | |
| 415 | ||
| 379210cb | 416 | retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap); |
| 984263bc | 417 | buf[retval] = '\0'; |
| a0a36cfd | 418 | return (retval); |
| 984263bc MD |
419 | } |
| 420 | ||
| 421 | /* | |
| 422 | * Scaled down version of snprintf(3). | |
| 423 | */ | |
| 424 | int | |
| 169276ba MD |
425 | ksnprintf(char *str, size_t size, const char *format, ...) |
| 426 | { | |
| 427 | int retval; | |
| 428 | __va_list ap; | |
| 984263bc | 429 | |
| e2565a42 | 430 | __va_start(ap, format); |
| 379210cb | 431 | retval = kvsnprintf(str, size, format, ap); |
| e2565a42 | 432 | __va_end(ap); |
| 984263bc MD |
433 | return(retval); |
| 434 | } | |
| 435 | ||
| 436 | /* | |
| 437 | * Scaled down version of vsnprintf(3). | |
| 438 | */ | |
| 439 | int | |
| 379210cb | 440 | kvsnprintf(char *str, size_t size, const char *format, __va_list ap) |
| 984263bc MD |
441 | { |
| 442 | struct snprintf_arg info; | |
| 443 | int retval; | |
| 444 | ||
| 445 | info.str = str; | |
| 446 | info.remain = size; | |
| 379210cb | 447 | retval = kvcprintf(format, snprintf_func, &info, 10, ap); |
| 984263bc MD |
448 | if (info.remain >= 1) |
| 449 | *info.str++ = '\0'; | |
| a0a36cfd | 450 | return (retval); |
| 984263bc MD |
451 | } |
| 452 | ||
| 93c46bf9 MD |
453 | int |
| 454 | ksnrprintf(char *str, size_t size, int radix, const char *format, ...) | |
| 455 | { | |
| 456 | int retval; | |
| 457 | __va_list ap; | |
| 458 | ||
| 459 | __va_start(ap, format); | |
| 460 | retval = kvsnrprintf(str, size, radix, format, ap); | |
| 461 | __va_end(ap); | |
| 462 | return(retval); | |
| 463 | } | |
| 464 | ||
| 465 | int | |
| 466 | kvsnrprintf(char *str, size_t size, int radix, const char *format, __va_list ap) | |
| 467 | { | |
| 468 | struct snprintf_arg info; | |
| 469 | int retval; | |
| 470 | ||
| 471 | info.str = str; | |
| 472 | info.remain = size; | |
| 473 | retval = kvcprintf(format, snprintf_func, &info, radix, ap); | |
| 474 | if (info.remain >= 1) | |
| 475 | *info.str++ = '\0'; | |
| 476 | return (retval); | |
| 477 | } | |
| 478 | ||
| 479 | int | |
| 480 | kvasnrprintf(char **strp, size_t size, int radix, | |
| 481 | const char *format, __va_list ap) | |
| 482 | { | |
| 483 | struct snprintf_arg info; | |
| 484 | int retval; | |
| 485 | ||
| 486 | *strp = kmalloc(size, M_TEMP, M_WAITOK); | |
| 487 | info.str = *strp; | |
| 488 | info.remain = size; | |
| 489 | retval = kvcprintf(format, snprintf_func, &info, radix, ap); | |
| 490 | if (info.remain >= 1) | |
| 491 | *info.str++ = '\0'; | |
| 492 | return (retval); | |
| 493 | } | |
| 494 | ||
| 495 | void | |
| 496 | kvasfree(char **strp) | |
| 497 | { | |
| 498 | if (*strp) { | |
| 499 | kfree(*strp, M_TEMP); | |
| 500 | *strp = NULL; | |
| 501 | } | |
| 502 | } | |
| 503 | ||
| 984263bc MD |
504 | static void |
| 505 | snprintf_func(int ch, void *arg) | |
| 506 | { | |
| 507 | struct snprintf_arg *const info = arg; | |
| 508 | ||
| 509 | if (info->remain >= 2) { | |
| 510 | *info->str++ = ch; | |
| 511 | info->remain--; | |
| 512 | } | |
| 513 | } | |
| 514 | ||
| 515 | /* | |
| 516 | * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse | |
| 517 | * order; return an optional length and a pointer to the last character | |
| 518 | * written in the buffer (i.e., the first character of the string). | |
| 519 | * The buffer pointed to by `nbuf' must have length >= MAXNBUF. | |
| 520 | */ | |
| 521 | static char * | |
| a0a36cfd | 522 | ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) |
| 984263bc | 523 | { |
| a0a36cfd | 524 | char *p, c; |
| 984263bc MD |
525 | |
| 526 | p = nbuf; | |
| 527 | *p = '\0'; | |
| 528 | do { | |
| a0a36cfd SW |
529 | c = hex2ascii(num % base); |
| 530 | *++p = upper ? toupper(c) : c; | |
| 531 | } while (num /= base); | |
| 984263bc MD |
532 | if (lenp) |
| 533 | *lenp = p - nbuf; | |
| 534 | return (p); | |
| 535 | } | |
| 536 | ||
| 537 | /* | |
| 538 | * Scaled down version of printf(3). | |
| 539 | * | |
| 540 | * Two additional formats: | |
| 541 | * | |
| 542 | * The format %b is supported to decode error registers. | |
| 543 | * Its usage is: | |
| 544 | * | |
| 71681fd7 | 545 | * kprintf("reg=%b\n", regval, "<base><arg>*"); |
| 984263bc MD |
546 | * |
| 547 | * where <base> is the output base expressed as a control character, e.g. | |
| 548 | * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, | |
| 549 | * the first of which gives the bit number to be inspected (origin 1), and | |
| 550 | * the next characters (up to a control character, i.e. a character <= 32), | |
| 551 | * give the name of the register. Thus: | |
| 552 | * | |
| 379210cb | 553 | * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); |
| 984263bc MD |
554 | * |
| 555 | * would produce output: | |
| 556 | * | |
| 557 | * reg=3<BITTWO,BITONE> | |
| 558 | * | |
| 559 | * XXX: %D -- Hexdump, takes pointer and separator string: | |
| 560 | * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX | |
| 561 | * ("%*D", len, ptr, " " -> XX XX XX XX ... | |
| 562 | */ | |
| 5fddbda2 MD |
563 | |
| 564 | #define PCHAR(c) {int cc=(c); if(func) (*func)(cc,arg); else *d++=cc; retval++;} | |
| 565 | ||
| 984263bc | 566 | int |
| 5fddbda2 MD |
567 | kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, |
| 568 | int radix, __va_list ap) | |
| 984263bc | 569 | { |
| 984263bc | 570 | char nbuf[MAXNBUF]; |
| a0a36cfd SW |
571 | char *d; |
| 572 | const char *p, *percent, *q; | |
| 984263bc MD |
573 | u_char *up; |
| 574 | int ch, n; | |
| a0a36cfd SW |
575 | uintmax_t num; |
| 576 | int base, tmp, width, ladjust, sharpflag, neg, sign, dot; | |
| f481fa5d | 577 | int cflag, hflag, jflag, lflag, qflag, tflag, zflag; |
| a0a36cfd | 578 | int dwidth, upper; |
| 984263bc | 579 | char padc; |
| a0a36cfd | 580 | int retval = 0, stop = 0; |
| 438acbcc MD |
581 | int usespin; |
| 582 | ||
| 583 | /* | |
| 584 | * Make a supreme effort to avoid reentrant panics or deadlocks. | |
| 3dc9b698 MD |
585 | * |
| 586 | * NOTE! Do nothing that would access mycpu/gd/fs unless the | |
| 587 | * function is the normal kputchar(), which allows us to | |
| 588 | * use this function for very early debugging with a special | |
| 589 | * function. | |
| 438acbcc MD |
590 | */ |
| 591 | if (func == kputchar) { | |
| 592 | if (mycpu->gd_flags & GDF_KPRINTF) | |
| 593 | return(0); | |
| 594 | atomic_set_long(&mycpu->gd_flags, GDF_KPRINTF); | |
| 595 | } | |
| 984263bc | 596 | |
| a0a36cfd | 597 | num = 0; |
| 984263bc MD |
598 | if (!func) |
| 599 | d = (char *) arg; | |
| 600 | else | |
| 601 | d = NULL; | |
| 602 | ||
| 603 | if (fmt == NULL) | |
| 604 | fmt = "(fmt null)\n"; | |
| 605 | ||
| 606 | if (radix < 2 || radix > 36) | |
| 607 | radix = 10; | |
| 608 | ||
| 3dc9b698 MD |
609 | usespin = (func == kputchar && |
| 610 | panic_cpu_gd != mycpu && | |
| 438acbcc MD |
611 | (((struct putchar_arg *)arg)->flags & TOTTY) == 0); |
| 612 | if (usespin) { | |
| 5fddbda2 | 613 | crit_enter_hard(); |
| 287a8577 | 614 | spin_lock(&cons_spin); |
| 5fddbda2 MD |
615 | } |
| 616 | ||
| 984263bc MD |
617 | for (;;) { |
| 618 | padc = ' '; | |
| 619 | width = 0; | |
| a0a36cfd SW |
620 | while ((ch = (u_char)*fmt++) != '%' || stop) { |
| 621 | if (ch == '\0') | |
| 5fddbda2 | 622 | goto done; |
| 984263bc MD |
623 | PCHAR(ch); |
| 624 | } | |
| a0a36cfd SW |
625 | percent = fmt - 1; |
| 626 | dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0; | |
| f481fa5d | 627 | cflag = hflag = jflag = lflag = qflag = tflag = zflag = 0; |
| a0a36cfd SW |
628 | |
| 629 | reswitch: | |
| 630 | switch (ch = (u_char)*fmt++) { | |
| 984263bc MD |
631 | case '.': |
| 632 | dot = 1; | |
| 633 | goto reswitch; | |
| 634 | case '#': | |
| 635 | sharpflag = 1; | |
| 636 | goto reswitch; | |
| 637 | case '+': | |
| 638 | sign = 1; | |
| 639 | goto reswitch; | |
| 640 | case '-': | |
| 641 | ladjust = 1; | |
| 642 | goto reswitch; | |
| 643 | case '%': | |
| 644 | PCHAR(ch); | |
| 645 | break; | |
| 646 | case '*': | |
| 647 | if (!dot) { | |
| e2565a42 | 648 | width = __va_arg(ap, int); |
| 984263bc MD |
649 | if (width < 0) { |
| 650 | ladjust = !ladjust; | |
| 651 | width = -width; | |
| 652 | } | |
| 653 | } else { | |
| e2565a42 | 654 | dwidth = __va_arg(ap, int); |
| 984263bc MD |
655 | } |
| 656 | goto reswitch; | |
| 657 | case '0': | |
| 658 | if (!dot) { | |
| 659 | padc = '0'; | |
| 660 | goto reswitch; | |
| 661 | } | |
| 662 | case '1': case '2': case '3': case '4': | |
| 663 | case '5': case '6': case '7': case '8': case '9': | |
| 664 | for (n = 0;; ++fmt) { | |
| 665 | n = n * 10 + ch - '0'; | |
| 666 | ch = *fmt; | |
| 667 | if (ch < '0' || ch > '9') | |
| 668 | break; | |
| 669 | } | |
| 670 | if (dot) | |
| 671 | dwidth = n; | |
| 672 | else | |
| 673 | width = n; | |
| 674 | goto reswitch; | |
| 675 | case 'b': | |
| a0a36cfd | 676 | num = (u_int)__va_arg(ap, int); |
| e2565a42 | 677 | p = __va_arg(ap, char *); |
| a0a36cfd | 678 | for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) |
| 984263bc MD |
679 | PCHAR(*q--); |
| 680 | ||
| a0a36cfd | 681 | if (num == 0) |
| 984263bc MD |
682 | break; |
| 683 | ||
| 684 | for (tmp = 0; *p;) { | |
| 685 | n = *p++; | |
| a0a36cfd | 686 | if (num & (1 << (n - 1))) { |
| 984263bc MD |
687 | PCHAR(tmp ? ',' : '<'); |
| 688 | for (; (n = *p) > ' '; ++p) | |
| 689 | PCHAR(n); | |
| 690 | tmp = 1; | |
| 691 | } else | |
| 692 | for (; *p > ' '; ++p) | |
| 693 | continue; | |
| 694 | } | |
| 695 | if (tmp) | |
| 696 | PCHAR('>'); | |
| 697 | break; | |
| 698 | case 'c': | |
| e2565a42 | 699 | PCHAR(__va_arg(ap, int)); |
| 984263bc MD |
700 | break; |
| 701 | case 'D': | |
| e2565a42 MD |
702 | up = __va_arg(ap, u_char *); |
| 703 | p = __va_arg(ap, char *); | |
| 984263bc MD |
704 | if (!width) |
| 705 | width = 16; | |
| 706 | while(width--) { | |
| 707 | PCHAR(hex2ascii(*up >> 4)); | |
| 708 | PCHAR(hex2ascii(*up & 0x0f)); | |
| 709 | up++; | |
| 710 | if (width) | |
| 711 | for (q=p;*q;q++) | |
| 712 | PCHAR(*q); | |
| 713 | } | |
| 714 | break; | |
| 715 | case 'd': | |
| a0a36cfd | 716 | case 'i': |
| 984263bc | 717 | base = 10; |
| a0a36cfd SW |
718 | sign = 1; |
| 719 | goto handle_sign; | |
| f481fa5d SW |
720 | case 'h': |
| 721 | if (hflag) { | |
| 722 | hflag = 0; | |
| 723 | cflag = 1; | |
| 724 | } else | |
| 725 | hflag = 1; | |
| 726 | goto reswitch; | |
| a0a36cfd SW |
727 | case 'j': |
| 728 | jflag = 1; | |
| 729 | goto reswitch; | |
| 984263bc MD |
730 | case 'l': |
| 731 | if (lflag) { | |
| 732 | lflag = 0; | |
| 733 | qflag = 1; | |
| 734 | } else | |
| 735 | lflag = 1; | |
| 736 | goto reswitch; | |
| a0a36cfd | 737 | case 'n': |
| f481fa5d SW |
738 | if (cflag) |
| 739 | *(__va_arg(ap, char *)) = retval; | |
| 740 | else if (hflag) | |
| 741 | *(__va_arg(ap, short *)) = retval; | |
| 742 | else if (jflag) | |
| a0a36cfd | 743 | *(__va_arg(ap, intmax_t *)) = retval; |
| 984263bc | 744 | else if (lflag) |
| a0a36cfd SW |
745 | *(__va_arg(ap, long *)) = retval; |
| 746 | else if (qflag) | |
| 747 | *(__va_arg(ap, quad_t *)) = retval; | |
| 984263bc | 748 | else |
| a0a36cfd SW |
749 | *(__va_arg(ap, int *)) = retval; |
| 750 | break; | |
| 751 | case 'o': | |
| 984263bc | 752 | base = 8; |
| a0a36cfd | 753 | goto handle_nosign; |
| 984263bc | 754 | case 'p': |
| 984263bc MD |
755 | base = 16; |
| 756 | sharpflag = (width == 0); | |
| a0a36cfd SW |
757 | sign = 0; |
| 758 | num = (uintptr_t)__va_arg(ap, void *); | |
| 759 | goto number; | |
| 984263bc MD |
760 | case 'q': |
| 761 | qflag = 1; | |
| 762 | goto reswitch; | |
| 984263bc | 763 | case 'r': |
| 984263bc | 764 | base = radix; |
| a0a36cfd SW |
765 | if (sign) |
| 766 | goto handle_sign; | |
| 767 | goto handle_nosign; | |
| 984263bc | 768 | case 's': |
| e2565a42 | 769 | p = __va_arg(ap, char *); |
| 984263bc MD |
770 | if (p == NULL) |
| 771 | p = "(null)"; | |
| 772 | if (!dot) | |
| 773 | n = strlen (p); | |
| 774 | else | |
| 775 | for (n = 0; n < dwidth && p[n]; n++) | |
| 776 | continue; | |
| 777 | ||
| 778 | width -= n; | |
| 779 | ||
| 780 | if (!ladjust && width > 0) | |
| 781 | while (width--) | |
| 782 | PCHAR(padc); | |
| 783 | while (n--) | |
| 784 | PCHAR(*p++); | |
| 785 | if (ladjust && width > 0) | |
| 786 | while (width--) | |
| 787 | PCHAR(padc); | |
| 788 | break; | |
| a0a36cfd SW |
789 | case 't': |
| 790 | tflag = 1; | |
| 791 | goto reswitch; | |
| 984263bc | 792 | case 'u': |
| 984263bc | 793 | base = 10; |
| a0a36cfd | 794 | goto handle_nosign; |
| 984263bc | 795 | case 'X': |
| a0a36cfd SW |
796 | upper = 1; |
| 797 | /* FALLTHROUGH */ | |
| 798 | case 'x': | |
| 984263bc | 799 | base = 16; |
| a0a36cfd | 800 | goto handle_nosign; |
| 984263bc | 801 | case 'z': |
| 9c7517ab MD |
802 | zflag = 1; |
| 803 | goto reswitch; | |
| a0a36cfd SW |
804 | handle_nosign: |
| 805 | sign = 0; | |
| f481fa5d SW |
806 | if (cflag) |
| 807 | num = (u_char)__va_arg(ap, int); | |
| 808 | else if (hflag) | |
| 809 | num = (u_short)__va_arg(ap, int); | |
| 810 | else if (jflag) | |
| a0a36cfd | 811 | num = __va_arg(ap, uintmax_t); |
| 984263bc | 812 | else if (lflag) |
| a0a36cfd SW |
813 | num = __va_arg(ap, u_long); |
| 814 | else if (qflag) | |
| 815 | num = __va_arg(ap, u_quad_t); | |
| 816 | else if (tflag) | |
| 817 | num = __va_arg(ap, ptrdiff_t); | |
| 9c7517ab MD |
818 | else if (zflag) |
| 819 | num = __va_arg(ap, size_t); | |
| 984263bc | 820 | else |
| a0a36cfd | 821 | num = __va_arg(ap, u_int); |
| 984263bc | 822 | goto number; |
| a0a36cfd | 823 | handle_sign: |
| f481fa5d SW |
824 | if (cflag) |
| 825 | num = (char)__va_arg(ap, int); | |
| 826 | else if (hflag) | |
| 827 | num = (short)__va_arg(ap, int); | |
| 828 | else if (jflag) | |
| a0a36cfd SW |
829 | num = __va_arg(ap, intmax_t); |
| 830 | else if (lflag) | |
| 831 | num = __va_arg(ap, long); | |
| 832 | else if (qflag) | |
| 833 | num = __va_arg(ap, quad_t); | |
| 834 | else if (tflag) | |
| 835 | num = __va_arg(ap, ptrdiff_t); | |
| 9c7517ab MD |
836 | else if (zflag) |
| 837 | num = __va_arg(ap, ssize_t); | |
| a0a36cfd SW |
838 | else |
| 839 | num = __va_arg(ap, int); | |
| 840 | number: | |
| 841 | if (sign && (intmax_t)num < 0) { | |
| 842 | neg = 1; | |
| 843 | num = -(intmax_t)num; | |
| 984263bc | 844 | } |
| a0a36cfd SW |
845 | p = ksprintn(nbuf, num, base, &tmp, upper); |
| 846 | if (sharpflag && num != 0) { | |
| 984263bc MD |
847 | if (base == 8) |
| 848 | tmp++; | |
| 849 | else if (base == 16) | |
| 850 | tmp += 2; | |
| 851 | } | |
| 852 | if (neg) | |
| 853 | tmp++; | |
| 854 | ||
| a0a36cfd SW |
855 | if (!ladjust && padc != '0' && width && |
| 856 | (width -= tmp) > 0) { | |
| 984263bc MD |
857 | while (width--) |
| 858 | PCHAR(padc); | |
| a0a36cfd | 859 | } |
| 984263bc MD |
860 | if (neg) |
| 861 | PCHAR('-'); | |
| a0a36cfd | 862 | if (sharpflag && num != 0) { |
| 984263bc MD |
863 | if (base == 8) { |
| 864 | PCHAR('0'); | |
| 865 | } else if (base == 16) { | |
| 866 | PCHAR('0'); | |
| 867 | PCHAR('x'); | |
| 868 | } | |
| 869 | } | |
| a0a36cfd SW |
870 | if (!ladjust && width && (width -= tmp) > 0) |
| 871 | while (width--) | |
| 872 | PCHAR(padc); | |
| 984263bc MD |
873 | |
| 874 | while (*p) | |
| 875 | PCHAR(*p--); | |
| 876 | ||
| 877 | if (ladjust && width && (width -= tmp) > 0) | |
| 878 | while (width--) | |
| 879 | PCHAR(padc); | |
| 880 | ||
| 881 | break; | |
| 882 | default: | |
| a0a36cfd SW |
883 | while (percent < fmt) |
| 884 | PCHAR(*percent++); | |
| 885 | /* | |
| 886 | * Since we ignore an formatting argument it is no | |
| 887 | * longer safe to obey the remaining formatting | |
| 888 | * arguments as the arguments will no longer match | |
| 889 | * the format specs. | |
| 890 | */ | |
| 891 | stop = 1; | |
| 984263bc MD |
892 | break; |
| 893 | } | |
| 894 | } | |
| 5fddbda2 | 895 | done: |
| 438acbcc MD |
896 | /* |
| 897 | * Cleanup reentrancy issues. | |
| 898 | */ | |
| 899 | if (func == kputchar) | |
| 900 | atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF); | |
| 901 | if (usespin) { | |
| 287a8577 | 902 | spin_unlock(&cons_spin); |
| 5fddbda2 MD |
903 | crit_exit_hard(); |
| 904 | } | |
| 905 | return (retval); | |
| 906 | } | |
| 907 | ||
| 984263bc | 908 | #undef PCHAR |
| 5fddbda2 MD |
909 | |
| 910 | /* | |
| 438acbcc MD |
911 | * Called from the panic code to try to get the console working |
| 912 | * again in case we paniced inside a kprintf(). | |
| 5fddbda2 MD |
913 | */ |
| 914 | void | |
| 915 | kvcreinitspin(void) | |
| 916 | { | |
| 917 | spin_init(&cons_spin); | |
| 438acbcc | 918 | atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF); |
| 984263bc MD |
919 | } |
| 920 | ||
| 28d7e704 MD |
921 | /* |
| 922 | * Console support thread for constty intercepts. This is needed because | |
| 923 | * console tty intercepts can block. Instead of having kputchar() attempt | |
| 924 | * to directly write to the console intercept we just force it to log | |
| 925 | * and wakeup this baby to track and dump the log to constty. | |
| 926 | */ | |
| 927 | static void | |
| 928 | constty_daemon(void) | |
| 929 | { | |
| 930 | int rindex = -1; | |
| 931 | int windex = -1; | |
| 932 | struct msgbuf *mbp; | |
| 933 | struct tty *tp; | |
| 934 | ||
| 935 | EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, | |
| 936 | constty_td, SHUTDOWN_PRI_FIRST); | |
| 937 | constty_td->td_flags |= TDF_SYSTHREAD; | |
| 938 | ||
| 939 | for (;;) { | |
| 940 | kproc_suspend_loop(); | |
| 941 | ||
| 942 | crit_enter(); | |
| 943 | mbp = msgbufp; | |
| 944 | if (mbp == NULL || msgbufmapped == 0 || | |
| 945 | windex == mbp->msg_bufx) { | |
| 946 | tsleep(constty_td, 0, "waiting", hz*60); | |
| 947 | crit_exit(); | |
| 948 | continue; | |
| 949 | } | |
| 950 | windex = mbp->msg_bufx; | |
| 951 | crit_exit(); | |
| 952 | ||
| 953 | /* | |
| 954 | * Get message buf FIFO indices. rindex is tracking. | |
| 955 | */ | |
| 956 | if ((tp = constty) == NULL) { | |
| 957 | rindex = mbp->msg_bufx; | |
| 958 | continue; | |
| 959 | } | |
| 960 | ||
| 961 | /* | |
| 962 | * Don't blow up if the message buffer is broken | |
| 963 | */ | |
| 964 | if (windex < 0 || windex >= mbp->msg_size) | |
| 965 | continue; | |
| 966 | if (rindex < 0 || rindex >= mbp->msg_size) | |
| 967 | rindex = windex; | |
| 968 | ||
| 969 | /* | |
| 970 | * And dump it. If constty gets stuck will give up. | |
| 971 | */ | |
| 972 | while (rindex != windex) { | |
| 973 | if (tputchar((uint8_t)mbp->msg_ptr[rindex], tp) < 0) { | |
| 974 | constty = NULL; | |
| 975 | rindex = mbp->msg_bufx; | |
| 976 | break; | |
| 977 | } | |
| 978 | if (++rindex >= mbp->msg_size) | |
| 979 | rindex = 0; | |
| 980 | if (tp->t_outq.c_cc >= tp->t_ohiwat) { | |
| 981 | tsleep(constty_daemon, 0, "blocked", hz / 10); | |
| 982 | if (tp->t_outq.c_cc >= tp->t_ohiwat) { | |
| 983 | rindex = windex; | |
| 984 | break; | |
| 985 | } | |
| 986 | } | |
| 987 | } | |
| 988 | } | |
| 989 | } | |
| 990 | ||
| 991 | static struct kproc_desc constty_kp = { | |
| 992 | "consttyd", | |
| 993 | constty_daemon, | |
| 994 | &constty_td | |
| 995 | }; | |
| 996 | SYSINIT(bufdaemon, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, | |
| 997 | kproc_start, &constty_kp) | |
| 5fddbda2 | 998 | |
| 984263bc MD |
999 | /* |
| 1000 | * Put character in log buffer with a particular priority. | |
| e02941a7 MD |
1001 | * |
| 1002 | * MPSAFE | |
| 984263bc MD |
1003 | */ |
| 1004 | static void | |
| 1005 | msglogchar(int c, int pri) | |
| 1006 | { | |
| 1007 | static int lastpri = -1; | |
| 1008 | static int dangling; | |
| 1009 | char nbuf[MAXNBUF]; | |
| 1010 | char *p; | |
| 1011 | ||
| 1012 | if (!msgbufmapped) | |
| 1013 | return; | |
| 1014 | if (c == '\0' || c == '\r') | |
| 1015 | return; | |
| 1016 | if (pri != -1 && pri != lastpri) { | |
| 1017 | if (dangling) { | |
| 1018 | msgaddchar('\n', NULL); | |
| 1019 | dangling = 0; | |
| 1020 | } | |
| 1021 | msgaddchar('<', NULL); | |
| a0a36cfd | 1022 | for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;) |
| 984263bc MD |
1023 | msgaddchar(*p--, NULL); |
| 1024 | msgaddchar('>', NULL); | |
| 1025 | lastpri = pri; | |
| 1026 | } | |
| 1027 | msgaddchar(c, NULL); | |
| 1028 | if (c == '\n') { | |
| 1029 | dangling = 0; | |
| 1030 | lastpri = -1; | |
| 1031 | } else { | |
| 1032 | dangling = 1; | |
| 1033 | } | |
| 1034 | } | |
| 1035 | ||
| 1036 | /* | |
| e02941a7 MD |
1037 | * Put char in log buffer. Make sure nothing blows up beyond repair if |
| 1038 | * we have an MP race. | |
| 1039 | * | |
| 1040 | * MPSAFE. | |
| 984263bc MD |
1041 | */ |
| 1042 | static void | |
| 1043 | msgaddchar(int c, void *dummy) | |
| 1044 | { | |
| 1045 | struct msgbuf *mbp; | |
| e02941a7 MD |
1046 | int rindex; |
| 1047 | int windex; | |
| 984263bc MD |
1048 | |
| 1049 | if (!msgbufmapped) | |
| 1050 | return; | |
| 1051 | mbp = msgbufp; | |
| e02941a7 MD |
1052 | windex = mbp->msg_bufx; |
| 1053 | mbp->msg_ptr[windex] = c; | |
| 1054 | if (++windex >= mbp->msg_size) | |
| 1055 | windex = 0; | |
| 1056 | rindex = mbp->msg_bufr; | |
| 1057 | if (windex == rindex) { | |
| 1058 | rindex += 32; | |
| 1059 | if (rindex >= mbp->msg_size) | |
| 1060 | rindex -= mbp->msg_size; | |
| 1061 | mbp->msg_bufr = rindex; | |
| 984263bc | 1062 | } |
| e02941a7 | 1063 | mbp->msg_bufx = windex; |
| 984263bc MD |
1064 | } |
| 1065 | ||
| 1066 | static void | |
| 1067 | msgbufcopy(struct msgbuf *oldp) | |
| 1068 | { | |
| 1069 | int pos; | |
| 1070 | ||
| 1071 | pos = oldp->msg_bufr; | |
| 1072 | while (pos != oldp->msg_bufx) { | |
| 1073 | msglogchar(oldp->msg_ptr[pos], -1); | |
| 1074 | if (++pos >= oldp->msg_size) | |
| 1075 | pos = 0; | |
| 1076 | } | |
| 1077 | } | |
| 1078 | ||
| 1079 | void | |
| 1080 | msgbufinit(void *ptr, size_t size) | |
| 1081 | { | |
| 1082 | char *cp; | |
| 1083 | static struct msgbuf *oldp = NULL; | |
| 1084 | ||
| 1085 | size -= sizeof(*msgbufp); | |
| 1086 | cp = (char *)ptr; | |
| 1087 | msgbufp = (struct msgbuf *) (cp + size); | |
| 1088 | if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size || | |
| 1089 | msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) { | |
| 1090 | bzero(cp, size); | |
| 1091 | bzero(msgbufp, sizeof(*msgbufp)); | |
| 1092 | msgbufp->msg_magic = MSG_MAGIC; | |
| 1093 | msgbufp->msg_size = (char *)msgbufp - cp; | |
| 1094 | } | |
| 1095 | msgbufp->msg_ptr = cp; | |
| 1096 | if (msgbufmapped && oldp != msgbufp) | |
| 1097 | msgbufcopy(oldp); | |
| 1098 | msgbufmapped = 1; | |
| 1099 | oldp = msgbufp; | |
| 1100 | } | |
| 1101 | ||
| 1102 | /* Sysctls for accessing/clearing the msgbuf */ | |
| d41cbec3 | 1103 | |
| 984263bc MD |
1104 | static int |
| 1105 | sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) | |
| 1106 | { | |
| d41cbec3 | 1107 | struct ucred *cred; |
| 984263bc MD |
1108 | int error; |
| 1109 | ||
| 1110 | /* | |
| d41cbec3 MD |
1111 | * Only wheel or root can access the message log. |
| 1112 | */ | |
| 1113 | if (unprivileged_read_msgbuf == 0) { | |
| 1114 | KKASSERT(req->td->td_proc); | |
| 1115 | cred = req->td->td_proc->p_ucred; | |
| 1116 | ||
| 1117 | if ((cred->cr_prison || groupmember(0, cred) == 0) && | |
| 895c1f85 | 1118 | priv_check(req->td, PRIV_ROOT) != 0 |
| d41cbec3 MD |
1119 | ) { |
| 1120 | return (EPERM); | |
| 1121 | } | |
| 1122 | } | |
| 1123 | ||
| 1124 | /* | |
| 984263bc MD |
1125 | * Unwind the buffer, so that it's linear (possibly starting with |
| 1126 | * some initial nulls). | |
| 1127 | */ | |
| 1128 | error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx, | |
| 1129 | msgbufp->msg_size - msgbufp->msg_bufx, req); | |
| 1130 | if (error) | |
| 1131 | return (error); | |
| 1132 | if (msgbufp->msg_bufx > 0) { | |
| 1133 | error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr, | |
| 1134 | msgbufp->msg_bufx, req); | |
| 1135 | } | |
| 1136 | return (error); | |
| 1137 | } | |
| 1138 | ||
| 1139 | SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD, | |
| 1140 | 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); | |
| 1141 | ||
| 1142 | static int msgbuf_clear; | |
| 1143 | ||
| 1144 | static int | |
| 1145 | sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS) | |
| 1146 | { | |
| 1147 | int error; | |
| 1148 | error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); | |
| 1149 | if (!error && req->newptr) { | |
| 1150 | /* Clear the buffer and reset write pointer */ | |
| 1151 | bzero(msgbufp->msg_ptr, msgbufp->msg_size); | |
| 1152 | msgbufp->msg_bufr = msgbufp->msg_bufx = 0; | |
| 1153 | msgbuf_clear = 0; | |
| 1154 | } | |
| 1155 | return (error); | |
| 1156 | } | |
| 1157 | ||
| 1158 | SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear, | |
| a0a36cfd | 1159 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0, |
| 984263bc MD |
1160 | sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer"); |
| 1161 | ||
| 984263bc | 1162 | #ifdef DDB |
| 984263bc MD |
1163 | |
| 1164 | DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) | |
| 1165 | { | |
| 1166 | int i, j; | |
| 1167 | ||
| 1168 | if (!msgbufmapped) { | |
| 1169 | db_printf("msgbuf not mapped yet\n"); | |
| 1170 | return; | |
| 1171 | } | |
| 1172 | db_printf("msgbufp = %p\n", msgbufp); | |
| 1173 | db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n", | |
| 1174 | msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr, | |
| 1175 | msgbufp->msg_bufx, msgbufp->msg_ptr); | |
| 1176 | for (i = 0; i < msgbufp->msg_size; i++) { | |
| 1177 | j = (i + msgbufp->msg_bufr) % msgbufp->msg_size; | |
| 1178 | db_printf("%c", msgbufp->msg_ptr[j]); | |
| 1179 | } | |
| 1180 | db_printf("\n"); | |
| 1181 | } | |
| 1182 | ||
| 1183 | #endif /* DDB */ |