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