Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | /* * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ /*- * Copyright (c) 1986, 1988, 1991, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95 */ /* HISTORY * 22-Sep-1997 Umesh Vaishampayan (umeshv@apple.com) * Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too. * * 17-July-97 Umesh Vaishampayan (umeshv@apple.com) * Eliminated multiple definition of constty which is defined * in bsd/dev/XXX/cons.c * * 26-MAR-1997 Umesh Vaishampayan (umeshv@NeXT.com * Fixed tharshing format in many functions. Cleanup. * * 17-Jun-1995 Mac Gillon (mgillon) at NeXT * Purged old history * New version based on 4.4 and NS3.3 */ #include <stdarg.h> #include <sys/conf.h> #include <sys/file_internal.h> #include <sys/ioctl.h> #include <sys/lock.h> #include <sys/malloc.h> #include <sys/msgbuf.h> #include <sys/param.h> #include <sys/proc_internal.h> #include <sys/reboot.h> #include <sys/subr_prf.h> #include <sys/syslog.h> #include <sys/systm.h> #include <sys/tprintf.h> #include <sys/tty.h> #include <console/serial_protos.h> #include <kern/task.h> /* for get_bsdthreadtask_info() */ #include <libkern/libkern.h> #include <os/log_private.h> struct snprintf_arg { char *str; size_t remain; }; struct putchar_args { int flags; struct tty *tty; bool last_char_was_cr; }; static void snprintf_func(int, void *); static void putchar(int c, void *arg); /* * In case console is off, debugger_panic_str contains argument to last call to * panic. */ extern const char *debugger_panic_str; extern struct tty cons; /* standard console tty */ extern struct tty *constty; /* pointer to console "window" tty */ extern int __doprnt(const char *, va_list, void (*)(int, void *), void *, int, int); extern void console_write_char(char); /* standard console putc */ extern void logwakeup(struct msgbuf *); extern bool bsd_log_lock(bool); extern void bsd_log_unlock(void); uint32_t vaddlog_msgcount = 0; uint32_t vaddlog_msgcount_dropped = 0; static void putchar_args_init(struct putchar_args *pca, struct session *sessp) { session_lock(sessp); pca->flags = TOTTY; pca->tty = sessp->s_ttyp; if (pca->tty != TTY_NULL) { ttyhold(pca->tty); } session_unlock(sessp); } static void putchar_args_destroy(struct putchar_args *pca) { if (pca->tty != TTY_NULL) { ttyfree(pca->tty); } } /* * Uprintf prints to the controlling terminal for the current process. * It may block if the tty queue is overfull. No message is printed if * the queue does not clear in a reasonable time. */ void uprintf(const char *fmt, ...) { struct proc *p = current_proc(); struct putchar_args pca; struct pgrp *pg; va_list ap; pg = proc_pgrp(p, NULL); if ((p->p_flag & P_CONTROLT) && pg) { putchar_args_init(&pca, pg->pg_session); if (pca.tty != NULL) { tty_lock(pca.tty); } va_start(ap, fmt); __doprnt(fmt, ap, putchar, &pca, 10, FALSE); va_end(ap); if (pca.tty != NULL) { tty_unlock(pca.tty); } putchar_args_destroy(&pca); } pgrp_rele(pg); } tpr_t tprintf_open(struct proc *p) { struct session *sessp; struct pgrp *pg; pg = proc_pgrp(p, &sessp); if ((p->p_flag & P_CONTROLT) && sessp->s_ttyvp) { return pg; } pgrp_rele(pg); return PGRP_NULL; } void tprintf_close(tpr_t pg) { pgrp_rele(pg); } static void tprintf_impl(tpr_t tpr, const char *fmt, va_list ap) { va_list ap2; struct putchar_args pca; if (tpr) { putchar_args_init(&pca, tpr->pg_session); if (pca.tty) { /* ttycheckoutq(), tputchar() require a locked tp */ tty_lock(pca.tty); if (ttycheckoutq(pca.tty, 0)) { /* going to the tty; leave locked */ va_copy(ap2, ap); __doprnt(fmt, ap2, putchar, &pca, 10, FALSE); va_end(ap2); } tty_unlock(pca.tty); } putchar_args_destroy(&pca); } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral" os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap, __builtin_return_address(0)); #pragma clang diagnostic pop } /* * tprintf prints on the controlling terminal associated * with the given session. * * NOTE: No one else should call this function!!! */ void tprintf(tpr_t tpr, const char *fmt, ...) { va_list ap; va_start(ap, fmt); tprintf_impl(tpr, fmt, ap); va_end(ap); } /* * tprintf_thd takes the session reference, calls tprintf * with user inputs, and then drops the reference. */ void tprintf_thd(thread_t thd, const char *fmt, ...) { struct proc * p = thd ? get_bsdthreadtask_info(thd) : NULL; tpr_t tpr = p ? tprintf_open(p) : NULL; va_list ap; va_start(ap, fmt); tprintf_impl(tpr, fmt, ap); va_end(ap); tprintf_close(tpr); } /* * 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. * * Locks: It is assumed that the tty_lock() is held over the call * to this function. Ensuring this is the responsibility * of the caller. */ void ttyprintf(struct tty *tp, const char *fmt, ...) { va_list ap; if (tp != NULL) { struct putchar_args pca; pca.flags = TOTTY; pca.tty = tp; va_start(ap, fmt); __doprnt(fmt, ap, putchar, &pca, 10, TRUE); va_end(ap); } } void logtime(time_t secs) { printf("Time 0x%lx Message ", secs); } static void putchar_asl(int c, void *arg) { struct putchar_args *pca = arg; if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) { log_putc_locked(aslbufp, (char)c); } putchar(c, arg); } /* * Vestigial support for kern_asl_msg() via /dev/klog */ int vaddlog(const char *fmt, va_list ap) { if (!bsd_log_lock(oslog_is_safe())) { os_atomic_inc(&vaddlog_msgcount_dropped, relaxed); return 1; } struct putchar_args pca = { .flags = TOLOGLOCKED, .tty = NULL, }; __doprnt(fmt, ap, putchar_asl, &pca, 10, TRUE); bsd_log_unlock(); logwakeup(NULL); os_atomic_inc(&vaddlog_msgcount, relaxed); return 0; } int prf(const char *fmt, va_list ap, int flags, struct tty *ttyp) { struct putchar_args pca; pca.flags = flags; pca.tty = ttyp; __doprnt(fmt, ap, putchar, &pca, 10, TRUE); return 0; } /* * Warn that a system table is full. */ void tablefull(const char *tab) { log(LOG_ERR, "%s: table is full\n", tab); } /* * Print a character on console or users terminal. * If destination is console then the last MSGBUFS characters * are saved in msgbuf for inspection later. * * Locks: If TOTTY is set, we assume that the tty lock is held * over the call to this function. */ /*ARGSUSED*/ void putchar(int c, void *arg) { struct putchar_args *pca = arg; char **sp = (char**) pca->tty; if (debugger_panic_str) { constty = 0; } if ((pca->flags & TOCONS) && pca->tty == NULL && constty) { pca->tty = constty; pca->flags |= TOTTY; } if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 && (pca->flags & TOCONS) && pca->tty == constty) { constty = 0; } if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177) { log_putc((char)c); } if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) { log_putc_locked(msgbufp, (char)c); } if ((pca->flags & TOCONS) && constty == 0 && c != '\0') { console_write_char((char)c); } if (pca->flags & TOSTR) { **sp = (char)c; (*sp)++; } pca->last_char_was_cr = ('\n' == c); } bool printf_log_locked(bool addcr, const char *fmt, ...) { bool retval; va_list args; va_start(args, fmt); retval = vprintf_log_locked(fmt, args, addcr); va_end(args); return retval; } bool vprintf_log_locked(const char *fmt, va_list ap, bool driverkit) { struct putchar_args pca; pca.flags = TOLOGLOCKED; if (driverkit && enable_dklog_serial_output) { pca.flags |= TOCONS; } pca.tty = NULL; pca.last_char_was_cr = false; __doprnt(fmt, ap, putchar, &pca, 10, TRUE); if (driverkit) { putchar('\n', &pca); } return pca.last_char_was_cr; } #if CONFIG_VSPRINTF /* * Scaled down version of vsprintf(3). * * Deprecation Warning: * vsprintf() is being deprecated. Please use vsnprintf() instead. */ int vsprintf(char *buf, const char *cfmt, va_list ap) { int retval; struct snprintf_arg info; info.str = buf; info.remain = 999999; retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE); if (info.remain >= 1) { *info.str++ = '\0'; } return 0; } #endif /* CONFIG_VSPRINTF */ /* * Scaled down version of snprintf(3). */ int snprintf(char *str, size_t size, const char *format, ...) { int retval; va_list ap; va_start(ap, format); retval = vsnprintf(str, size, format, ap); va_end(ap); return retval; } /* * Scaled down version of vsnprintf(3). */ int vsnprintf(char *str, size_t size, const char *format, va_list ap) { struct snprintf_arg info; int retval; info.str = str; info.remain = size; retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE); if (info.remain >= 1) { *info.str++ = '\0'; } return retval; } int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) { int i; i = vsnprintf(buf, size, fmt, args); /* Note: XNU's printf never returns negative values */ if ((uint32_t)i < size) { return i; } if (size == 0) { return 0; } if (size > INT_MAX) { return INT_MAX; } return (int)(size - 1); } int scnprintf(char *buf, size_t size, const char *fmt, ...) { va_list args; int i; va_start(args, fmt); i = vscnprintf(buf, size, fmt, args); va_end(args); return i; } static void snprintf_func(int ch, void *arg) { struct snprintf_arg *const info = arg; if (info->remain >= 2) { *info->str++ = (char)ch; info->remain--; } } |