xref: /xnu-12377.1.9/bsd/kern/subr_prf.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
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 ([email protected])
70  *	Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too.
71  *
72  * 17-July-97  Umesh Vaishampayan ([email protected])
73  *	Eliminated multiple definition of constty which is defined
74  *	in bsd/dev/XXX/cons.c
75  *
76  * 26-MAR-1997 Umesh Vaishampayan ([email protected]
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 
106 struct snprintf_arg {
107 	char *str;
108 	size_t remain;
109 };
110 
111 struct putchar_args {
112 	int flags;
113 	struct tty *tty;
114 	bool last_char_was_cr;
115 };
116 
117 static void snprintf_func(int, void *);
118 static 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  */
124 extern const char *debugger_panic_str;
125 
126 extern struct tty cons;     /* standard console tty */
127 extern struct tty       *copy_constty(void);               /* current console device */
128 extern struct tty       *set_constty(struct tty *);
129 
130 extern int __doprnt(const char *, va_list, void (*)(int, void *), void *, int, int);
131 extern void console_write_char(char);  /* standard console putc */
132 
133 static void
putchar_args_init(struct putchar_args * pca,struct session * sessp)134 putchar_args_init(struct putchar_args *pca, struct session *sessp)
135 {
136 	session_lock(sessp);
137 	pca->flags = TOTTY;
138 	pca->tty   = sessp->s_ttyp;
139 	if (pca->tty != TTY_NULL) {
140 		ttyhold(pca->tty);
141 	}
142 	session_unlock(sessp);
143 }
144 
145 static void
putchar_args_destroy(struct putchar_args * pca)146 putchar_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  */
158 void
uprintf(const char * fmt,...)159 uprintf(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, pg->pg_session);
170 
171 		if (pca.tty != NULL) {
172 			tty_lock(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(pca.tty);
179 		}
180 
181 		putchar_args_destroy(&pca);
182 	}
183 
184 	pgrp_rele(pg);
185 }
186 
187 tpr_t
tprintf_open(struct proc * p)188 tprintf_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(pg);
200 	return PGRP_NULL;
201 }
202 
203 void
tprintf_close(tpr_t pg)204 tprintf_close(tpr_t pg)
205 {
206 	pgrp_rele(pg);
207 }
208 
209 static void
tprintf_impl(tpr_t tpr,const char * fmt,va_list ap)210 tprintf_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, tpr->pg_session);
217 
218 		if (pca.tty) {
219 			/* ttycheckoutq(), tputchar() require a locked tp */
220 			tty_lock(pca.tty);
221 			if (ttycheckoutq(pca.tty, 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(pca.tty);
228 		}
229 
230 		putchar_args_destroy(&pca);
231 	}
232 
233 #pragma clang diagnostic push
234 #pragma clang diagnostic ignored "-Wformat-nonliteral"
235 #pragma clang diagnostic ignored "-Wformat"
236 	os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap, __builtin_return_address(0));
237 #pragma clang diagnostic pop
238 }
239 
240 /*
241  * tprintf prints on the controlling terminal associated
242  * with the given session.
243  *
244  * NOTE:	No one else should call this function!!!
245  */
246 void
tprintf(tpr_t tpr,const char * fmt,...)247 tprintf(tpr_t tpr, const char *fmt, ...)
248 {
249 	va_list ap;
250 
251 	va_start(ap, fmt);
252 	tprintf_impl(tpr, fmt, ap);
253 	va_end(ap);
254 }
255 
256 /*
257  * tprintf_thd takes the session reference, calls tprintf
258  * with user inputs, and then drops the reference.
259  */
260 void
tprintf_thd(thread_t thd,const char * fmt,...)261 tprintf_thd(thread_t thd, const char *fmt, ...)
262 {
263 	struct proc * p = thd ? get_bsdthreadtask_info(thd) : NULL;
264 	tpr_t tpr = p ? tprintf_open(p) : NULL;
265 	va_list ap;
266 
267 	va_start(ap, fmt);
268 	tprintf_impl(tpr, fmt, ap);
269 	va_end(ap);
270 
271 	tprintf_close(tpr);
272 }
273 
274 /*
275  * Ttyprintf displays a message on a tty; it should be used only by
276  * the tty driver, or anything that knows the underlying tty will not
277  * be revoke(2)'d away.  Other callers should use tprintf.
278  *
279  * Locks:	It is assumed that the tty_lock() is held over the call
280  *		to this function.  Ensuring this is the responsibility
281  *		of the caller.
282  */
283 void
ttyprintf(struct tty * tp,const char * fmt,...)284 ttyprintf(struct tty *tp, const char *fmt, ...)
285 {
286 	va_list ap;
287 
288 	if (tp != NULL) {
289 		struct putchar_args pca;
290 		pca.flags = TOTTY;
291 		pca.tty   = tp;
292 
293 		va_start(ap, fmt);
294 		__doprnt(fmt, ap, putchar, &pca, 10, TRUE);
295 		va_end(ap);
296 	}
297 }
298 
299 void
logtime(time_t secs)300 logtime(time_t secs)
301 {
302 	printf("Time 0x%lx Message ", secs);
303 }
304 
305 int
prf(const char * fmt,va_list ap,int flags,struct tty * ttyp)306 prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
307 {
308 	struct putchar_args pca;
309 
310 	pca.flags = flags;
311 	pca.tty   = ttyp;
312 
313 	__doprnt(fmt, ap, putchar, &pca, 10, TRUE);
314 
315 	return 0;
316 }
317 
318 /*
319  * Warn that a system table is full.
320  */
321 void
tablefull(const char * tab)322 tablefull(const char *tab)
323 {
324 	log(LOG_ERR, "%s: table is full\n", tab);
325 }
326 
327 /*
328  * Print a character on console or users terminal.
329  * If destination is console then the last MSGBUFS characters
330  * are saved in msgbuf for inspection later.
331  *
332  * Locks:	If TOTTY is set, we assume that the tty lock is held
333  *		over the call to this function.
334  */
335 /*ARGSUSED*/
336 void
putchar(int c,void * arg)337 putchar(int c, void *arg)
338 {
339 	struct putchar_args *pca = arg;
340 	char **sp = (char**) pca->tty;
341 	struct tty *constty = NULL;
342 	struct tty *freetp = NULL;
343 	const bool allow_constty = preemption_enabled();
344 
345 	if (allow_constty) {
346 		constty = copy_constty();
347 	}
348 
349 	if (debugger_panic_str && allow_constty && constty != NULL) {
350 		if (tty_islocked(constty)) {
351 			ttyfree_locked(constty);
352 		} else {
353 			ttyfree(constty);
354 		}
355 		constty = NULL;
356 		freetp = set_constty(NULL);
357 		if (freetp != NULL) {
358 			if (tty_islocked(freetp)) {
359 				ttyfree_locked(freetp);
360 			} else {
361 				ttyfree(freetp);
362 			}
363 			freetp = NULL;
364 		}
365 	}
366 	if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
367 		pca->tty = constty;
368 		pca->flags |= TOTTY;
369 	}
370 	if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
371 	    (pca->flags & TOCONS) && pca->tty == constty && allow_constty) {
372 		if (tty_islocked(constty)) {
373 			ttyfree_locked(constty);
374 		} else {
375 			ttyfree(constty);
376 		}
377 		constty = NULL;
378 		freetp = set_constty(NULL);
379 		if (freetp) {
380 			if (tty_islocked(freetp)) {
381 				ttyfree_locked(freetp);
382 			} else {
383 				ttyfree(freetp);
384 			}
385 			freetp = NULL;
386 		}
387 	}
388 	if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177) {
389 		log_putc((char)c);
390 	}
391 	if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) {
392 		log_putc_locked(msgbufp, (char)c);
393 	}
394 	if ((pca->flags & TOCONS) && constty == NULL && c != '\0') {
395 		console_write_char((char)c);
396 	}
397 	if (pca->flags & TOSTR) {
398 		**sp = (char)c;
399 		(*sp)++;
400 	}
401 
402 	pca->last_char_was_cr = ('\n' == c);
403 	if (constty) {
404 		if (tty_islocked(constty)) {
405 			ttyfree_locked(constty);
406 		} else {
407 			ttyfree(constty);
408 		}
409 	}
410 }
411 
412 bool
printf_log_locked(bool addcr,const char * fmt,...)413 printf_log_locked(bool addcr, const char *fmt, ...)
414 {
415 	bool retval;
416 	va_list args;
417 
418 	va_start(args, fmt);
419 	retval = vprintf_log_locked(fmt, args, addcr);
420 	va_end(args);
421 
422 	return retval;
423 }
424 
425 extern bool IOSystemStateAOT(void);
426 
427 bool
vprintf_log_locked(const char * fmt,va_list ap,bool driverkit)428 vprintf_log_locked(const char *fmt, va_list ap, bool driverkit)
429 {
430 	struct putchar_args pca;
431 
432 	pca.flags = TOLOGLOCKED;
433 	if (driverkit && (enable_dklog_serial_output || IOSystemStateAOT())) {
434 		pca.flags |= TOCONS;
435 	}
436 	pca.tty   = NULL;
437 	pca.last_char_was_cr = false;
438 	__doprnt(fmt, ap, putchar, &pca, 10, TRUE);
439 	if (driverkit) {
440 		putchar('\n', &pca);
441 	}
442 	return pca.last_char_was_cr;
443 }
444 
445 #if CONFIG_VSPRINTF
446 /*
447  * Scaled down version of vsprintf(3).
448  *
449  * Deprecation Warning:
450  *	vsprintf() is being deprecated. Please use vsnprintf() instead.
451  */
452 int
vsprintf(char * buf,const char * cfmt,va_list ap)453 vsprintf(char *buf, const char *cfmt, va_list ap)
454 {
455 	int retval;
456 	struct snprintf_arg info;
457 
458 	info.str = buf;
459 	info.remain = 999999;
460 
461 	retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE);
462 	if (info.remain >= 1) {
463 		*info.str++ = '\0';
464 	}
465 	return 0;
466 }
467 #endif  /* CONFIG_VSPRINTF */
468 
469 /*
470  * Scaled down version of snprintf(3).
471  */
472 int
snprintf(char * str,size_t size,const char * format,...)473 snprintf(char *str, size_t size, const char *format, ...)
474 {
475 	int retval;
476 	va_list ap;
477 
478 	va_start(ap, format);
479 	retval = vsnprintf(str, size, format, ap);
480 	va_end(ap);
481 	return retval;
482 }
483 
484 const char *
tsnprintf(char * __counted_by (count)dst,size_t count,const char * fmt,...)485 tsnprintf(char *__counted_by(count)dst, size_t count, const char *fmt, ...)
486 {
487 	const char *result;
488 	va_list ap;
489 
490 	va_start(ap, fmt);
491 	result = vtsnprintf(dst, count, fmt, ap);
492 	va_end(ap);
493 	return result;
494 }
495 
496 /*
497  * Scaled down version of vsnprintf(3).
498  */
499 int
vsnprintf(char * str,size_t size,const char * format,va_list ap)500 vsnprintf(char *str, size_t size, const char *format, va_list ap)
501 {
502 	struct snprintf_arg info;
503 	int retval;
504 
505 	info.str = str;
506 	info.remain = size;
507 	retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE);
508 	if (info.remain >= 1) {
509 		*info.str++ = '\0';
510 	}
511 	return retval;
512 }
513 
514 const char *
vtsnprintf(char * __counted_by (count)dst,size_t count,const char * fmt,va_list ap)515 vtsnprintf(char *__counted_by(count)dst, size_t count, const char *fmt, va_list ap)
516 {
517 	if (count == 0) {
518 		return NULL;
519 	}
520 	(void) vsnprintf(dst, count, fmt, ap);
521 	return __unsafe_forge_null_terminated(const char *, dst);
522 }
523 
524 int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)525 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
526 {
527 	int i;
528 
529 	i = vsnprintf(buf, size, fmt, args);
530 	/* Note: XNU's printf never returns negative values */
531 	if ((uint32_t)i < size) {
532 		return i;
533 	}
534 	if (size == 0) {
535 		return 0;
536 	}
537 	if (size > INT_MAX) {
538 		return INT_MAX;
539 	}
540 	return (int)(size - 1);
541 }
542 
543 int
scnprintf(char * buf,size_t size,const char * fmt,...)544 scnprintf(char *buf, size_t size, const char *fmt, ...)
545 {
546 	va_list args;
547 	int i;
548 
549 	va_start(args, fmt);
550 	i = vscnprintf(buf, size, fmt, args);
551 	va_end(args);
552 
553 	return i;
554 }
555 
556 static void
snprintf_func(int ch,void * arg)557 snprintf_func(int ch, void *arg)
558 {
559 	struct snprintf_arg *const info = arg;
560 
561 	if (info->remain >= 2) {
562 		*info->str++ = (char)ch;
563 		info->remain--;
564 	}
565 }
566