xref: /xnu-8792.81.2/bsd/kern/subr_prf.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
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 <libkern/libkern.h>
103 #include <os/log_private.h>
104 
105 struct snprintf_arg {
106 	char *str;
107 	size_t remain;
108 };
109 
110 struct putchar_args {
111 	int flags;
112 	struct tty *tty;
113 	bool last_char_was_cr;
114 };
115 
116 static void snprintf_func(int, void *);
117 static void putchar(int c, void *arg);
118 
119 /*
120  * In case console is off, debugger_panic_str contains argument to last call to
121  * panic.
122  */
123 extern const char *debugger_panic_str;
124 
125 extern struct tty cons;     /* standard console tty */
126 extern struct tty *constty; /* pointer to console "window" tty */
127 
128 extern int __doprnt(const char *, va_list, void (*)(int, void *), void *, int, int);
129 extern void console_write_char(char);  /* standard console putc */
130 extern void logwakeup(struct msgbuf *);
131 extern bool bsd_log_lock(bool);
132 extern void bsd_log_unlock(void);
133 
134 uint32_t vaddlog_msgcount = 0;
135 uint32_t vaddlog_msgcount_dropped = 0;
136 
137 static void
putchar_args_init(struct putchar_args * pca,struct session * sessp)138 putchar_args_init(struct putchar_args *pca, struct session *sessp)
139 {
140 	session_lock(sessp);
141 	pca->flags = TOTTY;
142 	pca->tty   = sessp->s_ttyp;
143 	if (pca->tty != TTY_NULL) {
144 		ttyhold(pca->tty);
145 	}
146 	session_unlock(sessp);
147 }
148 
149 static void
putchar_args_destroy(struct putchar_args * pca)150 putchar_args_destroy(struct putchar_args *pca)
151 {
152 	if (pca->tty != TTY_NULL) {
153 		ttyfree(pca->tty);
154 	}
155 }
156 
157 /*
158  * Uprintf prints to the controlling terminal for the current process.
159  * It may block if the tty queue is overfull.  No message is printed if
160  * the queue does not clear in a reasonable time.
161  */
162 void
uprintf(const char * fmt,...)163 uprintf(const char *fmt, ...)
164 {
165 	struct proc *p = current_proc();
166 	struct putchar_args pca;
167 	struct pgrp *pg;
168 	va_list ap;
169 
170 	pg = proc_pgrp(p, NULL);
171 
172 	if ((p->p_flag & P_CONTROLT) && pg) {
173 		putchar_args_init(&pca, pg->pg_session);
174 
175 		if (pca.tty != NULL) {
176 			tty_lock(pca.tty);
177 		}
178 		va_start(ap, fmt);
179 		__doprnt(fmt, ap, putchar, &pca, 10, FALSE);
180 		va_end(ap);
181 		if (pca.tty != NULL) {
182 			tty_unlock(pca.tty);
183 		}
184 
185 		putchar_args_destroy(&pca);
186 	}
187 
188 	pgrp_rele(pg);
189 }
190 
191 tpr_t
tprintf_open(struct proc * p)192 tprintf_open(struct proc *p)
193 {
194 	struct session *sessp;
195 	struct pgrp *pg;
196 
197 	pg = proc_pgrp(p, &sessp);
198 
199 	if ((p->p_flag & P_CONTROLT) && sessp->s_ttyvp) {
200 		return pg;
201 	}
202 
203 	pgrp_rele(pg);
204 	return PGRP_NULL;
205 }
206 
207 void
tprintf_close(tpr_t pg)208 tprintf_close(tpr_t pg)
209 {
210 	pgrp_rele(pg);
211 }
212 
213 static void
tprintf_impl(tpr_t tpr,const char * fmt,va_list ap)214 tprintf_impl(tpr_t tpr, const char *fmt, va_list ap)
215 {
216 	va_list ap2;
217 	struct putchar_args pca;
218 
219 	if (tpr) {
220 		putchar_args_init(&pca, tpr->pg_session);
221 
222 		if (pca.tty) {
223 			/* ttycheckoutq(), tputchar() require a locked tp */
224 			tty_lock(pca.tty);
225 			if (ttycheckoutq(pca.tty, 0)) {
226 				/* going to the tty; leave locked */
227 				va_copy(ap2, ap);
228 				__doprnt(fmt, ap2, putchar, &pca, 10, FALSE);
229 				va_end(ap2);
230 			}
231 			tty_unlock(pca.tty);
232 		}
233 
234 		putchar_args_destroy(&pca);
235 	}
236 
237 #pragma clang diagnostic push
238 #pragma clang diagnostic ignored "-Wformat-nonliteral"
239 	os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap, __builtin_return_address(0));
240 #pragma clang diagnostic pop
241 }
242 
243 /*
244  * tprintf prints on the controlling terminal associated
245  * with the given session.
246  *
247  * NOTE:	No one else should call this function!!!
248  */
249 void
tprintf(tpr_t tpr,const char * fmt,...)250 tprintf(tpr_t tpr, const char *fmt, ...)
251 {
252 	va_list ap;
253 
254 	va_start(ap, fmt);
255 	tprintf_impl(tpr, fmt, ap);
256 	va_end(ap);
257 }
258 
259 /*
260  * tprintf_thd takes the session reference, calls tprintf
261  * with user inputs, and then drops the reference.
262  */
263 void
tprintf_thd(thread_t thd,const char * fmt,...)264 tprintf_thd(thread_t thd, const char *fmt, ...)
265 {
266 	struct proc * p = thd ? get_bsdthreadtask_info(thd) : NULL;
267 	tpr_t tpr = p ? tprintf_open(p) : NULL;
268 	va_list ap;
269 
270 	va_start(ap, fmt);
271 	tprintf_impl(tpr, fmt, ap);
272 	va_end(ap);
273 
274 	tprintf_close(tpr);
275 }
276 
277 /*
278  * Ttyprintf displays a message on a tty; it should be used only by
279  * the tty driver, or anything that knows the underlying tty will not
280  * be revoke(2)'d away.  Other callers should use tprintf.
281  *
282  * Locks:	It is assumed that the tty_lock() is held over the call
283  *		to this function.  Ensuring this is the responsibility
284  *		of the caller.
285  */
286 void
ttyprintf(struct tty * tp,const char * fmt,...)287 ttyprintf(struct tty *tp, const char *fmt, ...)
288 {
289 	va_list ap;
290 
291 	if (tp != NULL) {
292 		struct putchar_args pca;
293 		pca.flags = TOTTY;
294 		pca.tty   = tp;
295 
296 		va_start(ap, fmt);
297 		__doprnt(fmt, ap, putchar, &pca, 10, TRUE);
298 		va_end(ap);
299 	}
300 }
301 
302 void
logtime(time_t secs)303 logtime(time_t secs)
304 {
305 	printf("Time 0x%lx Message ", secs);
306 }
307 
308 static void
putchar_asl(int c,void * arg)309 putchar_asl(int c, void *arg)
310 {
311 	struct putchar_args *pca = arg;
312 
313 	if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) {
314 		log_putc_locked(aslbufp, (char)c);
315 	}
316 	putchar(c, arg);
317 }
318 
319 /*
320  * Vestigial support for kern_asl_msg() via /dev/klog
321  */
322 int
vaddlog(const char * fmt,va_list ap)323 vaddlog(const char *fmt, va_list ap)
324 {
325 	if (!bsd_log_lock(oslog_is_safe())) {
326 		os_atomic_inc(&vaddlog_msgcount_dropped, relaxed);
327 		return 1;
328 	}
329 
330 	struct putchar_args pca = {
331 		.flags = TOLOGLOCKED,
332 		.tty = NULL,
333 	};
334 
335 	__doprnt(fmt, ap, putchar_asl, &pca, 10, TRUE);
336 	bsd_log_unlock();
337 	logwakeup(NULL);
338 
339 	os_atomic_inc(&vaddlog_msgcount, relaxed);
340 	return 0;
341 }
342 
343 int
prf(const char * fmt,va_list ap,int flags,struct tty * ttyp)344 prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
345 {
346 	struct putchar_args pca;
347 
348 	pca.flags = flags;
349 	pca.tty   = ttyp;
350 
351 	__doprnt(fmt, ap, putchar, &pca, 10, TRUE);
352 
353 	return 0;
354 }
355 
356 /*
357  * Warn that a system table is full.
358  */
359 void
tablefull(const char * tab)360 tablefull(const char *tab)
361 {
362 	log(LOG_ERR, "%s: table is full\n", tab);
363 }
364 
365 /*
366  * Print a character on console or users terminal.
367  * If destination is console then the last MSGBUFS characters
368  * are saved in msgbuf for inspection later.
369  *
370  * Locks:	If TOTTY is set, we assume that the tty lock is held
371  *		over the call to this function.
372  */
373 /*ARGSUSED*/
374 void
putchar(int c,void * arg)375 putchar(int c, void *arg)
376 {
377 	struct putchar_args *pca = arg;
378 	char **sp = (char**) pca->tty;
379 
380 	if (debugger_panic_str) {
381 		constty = 0;
382 	}
383 	if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
384 		pca->tty = constty;
385 		pca->flags |= TOTTY;
386 	}
387 	if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
388 	    (pca->flags & TOCONS) && pca->tty == constty) {
389 		constty = 0;
390 	}
391 	if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177) {
392 		log_putc((char)c);
393 	}
394 	if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) {
395 		log_putc_locked(msgbufp, (char)c);
396 	}
397 	if ((pca->flags & TOCONS) && constty == 0 && c != '\0') {
398 		console_write_char((char)c);
399 	}
400 	if (pca->flags & TOSTR) {
401 		**sp = (char)c;
402 		(*sp)++;
403 	}
404 
405 	pca->last_char_was_cr = ('\n' == c);
406 }
407 
408 bool
printf_log_locked(bool addcr,const char * fmt,...)409 printf_log_locked(bool addcr, const char *fmt, ...)
410 {
411 	bool retval;
412 	va_list args;
413 
414 	va_start(args, fmt);
415 	retval = vprintf_log_locked(fmt, args, addcr);
416 	va_end(args);
417 
418 	return retval;
419 }
420 
421 bool
vprintf_log_locked(const char * fmt,va_list ap,bool driverkit)422 vprintf_log_locked(const char *fmt, va_list ap, bool driverkit)
423 {
424 	struct putchar_args pca;
425 
426 	pca.flags = TOLOGLOCKED;
427 	if (driverkit && enable_dklog_serial_output) {
428 		pca.flags |= TOCONS;
429 	}
430 	pca.tty   = NULL;
431 	pca.last_char_was_cr = false;
432 	__doprnt(fmt, ap, putchar, &pca, 10, TRUE);
433 	if (driverkit) {
434 		putchar('\n', &pca);
435 	}
436 	return pca.last_char_was_cr;
437 }
438 
439 #if CONFIG_VSPRINTF
440 /*
441  * Scaled down version of vsprintf(3).
442  *
443  * Deprecation Warning:
444  *	vsprintf() is being deprecated. Please use vsnprintf() instead.
445  */
446 int
vsprintf(char * buf,const char * cfmt,va_list ap)447 vsprintf(char *buf, const char *cfmt, va_list ap)
448 {
449 	int retval;
450 	struct snprintf_arg info;
451 
452 	info.str = buf;
453 	info.remain = 999999;
454 
455 	retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE);
456 	if (info.remain >= 1) {
457 		*info.str++ = '\0';
458 	}
459 	return 0;
460 }
461 #endif  /* CONFIG_VSPRINTF */
462 
463 /*
464  * Scaled down version of snprintf(3).
465  */
466 int
snprintf(char * str,size_t size,const char * format,...)467 snprintf(char *str, size_t size, const char *format, ...)
468 {
469 	int retval;
470 	va_list ap;
471 
472 	va_start(ap, format);
473 	retval = vsnprintf(str, size, format, ap);
474 	va_end(ap);
475 	return retval;
476 }
477 
478 /*
479  * Scaled down version of vsnprintf(3).
480  */
481 int
vsnprintf(char * str,size_t size,const char * format,va_list ap)482 vsnprintf(char *str, size_t size, const char *format, va_list ap)
483 {
484 	struct snprintf_arg info;
485 	int retval;
486 
487 	info.str = str;
488 	info.remain = size;
489 	retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE);
490 	if (info.remain >= 1) {
491 		*info.str++ = '\0';
492 	}
493 	return retval;
494 }
495 
496 int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)497 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
498 {
499 	int i;
500 
501 	i = vsnprintf(buf, size, fmt, args);
502 	/* Note: XNU's printf never returns negative values */
503 	if ((uint32_t)i < size) {
504 		return i;
505 	}
506 	if (size == 0) {
507 		return 0;
508 	}
509 	if (size > INT_MAX) {
510 		return INT_MAX;
511 	}
512 	return (int)(size - 1);
513 }
514 
515 int
scnprintf(char * buf,size_t size,const char * fmt,...)516 scnprintf(char *buf, size_t size, const char *fmt, ...)
517 {
518 	va_list args;
519 	int i;
520 
521 	va_start(args, fmt);
522 	i = vscnprintf(buf, size, fmt, args);
523 	va_end(args);
524 
525 	return i;
526 }
527 
528 static void
snprintf_func(int ch,void * arg)529 snprintf_func(int ch, void *arg)
530 {
531 	struct snprintf_arg *const info = arg;
532 
533 	if (info->remain >= 2) {
534 		*info->str++ = (char)ch;
535 		info->remain--;
536 	}
537 }
538