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