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