]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/subr_prf.c
xnu-3248.30.4.tar.gz
[apple/xnu.git] / bsd / kern / subr_prf.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 <sys/param.h>
85#include <sys/systm.h>
1c79356b
A
86#include <sys/conf.h>
87#include <sys/reboot.h>
88#include <sys/msgbuf.h>
91447636 89#include <sys/proc_internal.h>
1c79356b
A
90#include <sys/ioctl.h>
91#include <sys/tty.h>
91447636 92#include <sys/file_internal.h>
1c79356b
A
93#include <sys/tprintf.h>
94#include <sys/syslog.h>
95#include <stdarg.h>
96#include <sys/malloc.h>
97#include <sys/lock.h>
1c79356b
A
98#include <sys/subr_prf.h>
99
100#include <kern/cpu_number.h> /* for cpu_number() */
101#include <machine/spl.h>
102#include <libkern/libkern.h>
103
2d21ac55
A
104/* for vaddlog(): the following are implemented in osfmk/kern/printf.c */
105extern void bsd_log_lock(void);
106extern void bsd_log_unlock(void);
107
108/* Keep this around only because it's exported */
109void _printf(int, struct tty *, const char *, ...);
110
1c79356b
A
111struct snprintf_arg {
112 char *str;
113 size_t remain;
114};
115
116
117/*
118 * In case console is off,
119 * panicstr contains argument to last
120 * call to panic.
121 */
122extern const char *panicstr;
123
2d21ac55
A
124extern void cnputc(char); /* standard console putc */
125void (*v_putc)(char) = cnputc; /* routine to putc on virtual console */
1c79356b
A
126
127extern struct tty cons; /* standard console tty */
128extern struct tty *constty; /* pointer to console "window" tty */
9bccf70c 129extern int __doprnt(const char *fmt,
b0d623f7 130 va_list argp,
2d21ac55 131 void (*)(int, void *),
9bccf70c 132 void *arg,
3e170ce0
A
133 int radix,
134 int is_log);
1c79356b
A
135
136/*
137 * Record cpu that panic'd and lock around panic data
138 */
b0d623f7 139static void printn(uint32_t n, int b, int flags, struct tty *ttyp, int zf, int fld_size);
1c79356b 140
2d21ac55
A
141extern void logwakeup(void);
142extern void halt_cpu(void);
1c79356b
A
143
144static void
145snprintf_func(int ch, void *arg);
146
9bccf70c
A
147struct putchar_args {
148 int flags;
149 struct tty *tty;
150};
151static void putchar(int c, void *arg);
1c79356b
A
152
153
154/*
155 * Uprintf prints to the controlling terminal for the current process.
156 * It may block if the tty queue is overfull. No message is printed if
157 * the queue does not clear in a reasonable time.
158 */
159void
160uprintf(const char *fmt, ...)
161{
2d21ac55 162 struct proc *p = current_proc();
9bccf70c 163 struct putchar_args pca;
1c79356b 164 va_list ap;
b0d623f7 165 struct session *sessp;
9bccf70c 166
2d21ac55
A
167 sessp = proc_session(p);
168
b0d623f7
A
169 if (p->p_flag & P_CONTROLT && sessp != SESSION_NULL && sessp->s_ttyvp) {
170 pca.flags = TOTTY;
171 pca.tty = SESSION_TP(sessp);
172 if (pca.tty != NULL)
173 tty_lock(pca.tty);
1c79356b 174 va_start(ap, fmt);
3e170ce0 175 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
1c79356b 176 va_end(ap);
b0d623f7
A
177 if (pca.tty != NULL)
178 tty_unlock(pca.tty);
1c79356b 179 }
b0d623f7
A
180 if (sessp != SESSION_NULL)
181 session_rele(sessp);
1c79356b
A
182}
183
184tpr_t
2d21ac55 185tprintf_open(struct proc *p)
1c79356b 186{
2d21ac55
A
187 struct session * sessp;
188
189 sessp = proc_session(p);
190
191 if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) {
192 return ((tpr_t)sessp);
1c79356b 193 }
2d21ac55
A
194 if (sessp != SESSION_NULL)
195 session_rele(sessp);
196
1c79356b
A
197 return ((tpr_t) NULL);
198}
199
200void
2d21ac55 201tprintf_close(tpr_t sessp)
1c79356b 202{
2d21ac55
A
203 if (sessp)
204 session_rele((struct session *) sessp);
1c79356b
A
205}
206
207/*
208 * tprintf prints on the controlling terminal associated
209 * with the given session.
b0d623f7
A
210 *
211 * NOTE: No one else should call this function!!!
1c79356b
A
212 */
213void
214tprintf(tpr_t tpr, const char *fmt, ...)
215{
2d21ac55 216 struct session *sess = (struct session *)tpr;
b0d623f7 217 struct tty *tp = TTY_NULL;
1c79356b
A
218 int flags = TOLOG;
219 va_list ap;
9bccf70c 220 struct putchar_args pca;
1c79356b
A
221
222 logpri(LOG_INFO);
2d21ac55 223
b0d623f7
A
224 if (sess && (tp = SESSION_TP(sess)) != TTY_NULL) {
225 /* ttycheckoutq(), tputchar() require a locked tp */
226 tty_lock(tp);
227 if(ttycheckoutq(tp, 0)) {
228 flags |= TOTTY;
229 /* going to the tty; leave locked */
230 } else {
231 /* not going to the tty... */
232 tty_unlock(tp);
233 tp = TTY_NULL;
234 }
1c79356b 235 }
55e303ae
A
236
237 pca.flags = flags;
238 pca.tty = tp;
239 va_start(ap, fmt);
3e170ce0 240 __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
55e303ae
A
241 va_end(ap);
242
b0d623f7
A
243 if (tp != NULL)
244 tty_unlock(tp); /* lock/unlock is guarded by tp, above */
2d21ac55 245
1c79356b
A
246 logwakeup();
247}
248
249/*
250 * Ttyprintf displays a message on a tty; it should be used only by
251 * the tty driver, or anything that knows the underlying tty will not
252 * be revoke(2)'d away. Other callers should use tprintf.
b0d623f7
A
253 *
254 * Locks: It is assumed that the tty_lock() is held over the call
255 * to this function. Ensuring this is the responsibility
256 * of the caller.
1c79356b
A
257 */
258void
259ttyprintf(struct tty *tp, const char *fmt, ...)
260{
261 va_list ap;
262
263 if (tp != NULL) {
9bccf70c
A
264 struct putchar_args pca;
265 pca.flags = TOTTY;
266 pca.tty = tp;
267
1c79356b 268 va_start(ap, fmt);
3e170ce0 269 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
1c79356b
A
270 va_end(ap);
271 }
272}
273
b0d623f7 274
1c79356b
A
275extern int log_open;
276
277
278void
2d21ac55 279logpri(int level)
1c79356b 280{
9bccf70c
A
281 struct putchar_args pca;
282 pca.flags = TOLOG;
283 pca.tty = NULL;
284
285 putchar('<', &pca);
b0d623f7 286 printn((uint32_t)level, 10, TOLOG, (struct tty *)0, 0, 0);
9bccf70c 287 putchar('>', &pca);
1c79356b
A
288}
289
2d21ac55
A
290static void
291_logtime(const char *fmt, ...)
1c79356b 292{
1c79356b 293 va_list ap;
2d21ac55
A
294 va_start(ap, fmt);
295 vaddlog(fmt, ap);
296 va_end(ap);
297}
298
299void
300logtime(time_t secs)
301{
302 _logtime(" 0 [Time %ld] [Message ", secs);
303}
304
305int
306vaddlog(const char *fmt, va_list ap)
307{
9bccf70c
A
308 struct putchar_args pca;
309
2d21ac55 310 pca.flags = TOLOGLOCKED;
9bccf70c 311 pca.tty = NULL;
1c79356b 312
9bccf70c 313 if (!log_open) {
2d21ac55 314 pca.flags |= TOCONS;
9bccf70c 315 }
2d21ac55
A
316
317 bsd_log_lock();
3e170ce0 318 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
2d21ac55
A
319 bsd_log_unlock();
320
1c79356b 321 logwakeup();
2d21ac55 322 return 0;
1c79356b 323}
2d21ac55
A
324
325void
326_printf(int flags, struct tty *ttyp, const char *format, ...)
1c79356b
A
327{
328 va_list ap;
9bccf70c
A
329 struct putchar_args pca;
330
331 pca.flags = flags;
332 pca.tty = ttyp;
b0d623f7
A
333
334 if (ttyp != NULL) {
335 tty_lock(ttyp);
1c79356b 336
b0d623f7 337 va_start(ap, format);
3e170ce0 338 __doprnt(format, ap, putchar, &pca, 10, TRUE);
b0d623f7
A
339 va_end(ap);
340
341 tty_unlock(ttyp);
342 }
1c79356b
A
343}
344
b0d623f7
A
345int
346prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
1c79356b 347{
9bccf70c 348 struct putchar_args pca;
1c79356b 349
9bccf70c
A
350 pca.flags = flags;
351 pca.tty = ttyp;
352
3e170ce0 353 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
9bccf70c 354
9bccf70c 355 return 0;
1c79356b
A
356}
357
1c79356b
A
358/*
359 * Printn prints a number n in base b.
360 * We don't use recursion to avoid deep kernel stacks.
361 */
b0d623f7
A
362static void
363printn(uint32_t n, int b, int flags, struct tty *ttyp, int zf, int fld_size)
1c79356b
A
364{
365 char prbuf[11];
2d21ac55 366 char *cp;
9bccf70c
A
367 struct putchar_args pca;
368
369 pca.flags = flags;
370 pca.tty = ttyp;
1c79356b
A
371
372 if (b == 10 && (int)n < 0) {
9bccf70c 373 putchar('-', &pca);
1c79356b
A
374 n = (unsigned)(-(int)n);
375 }
376 cp = prbuf;
377 do {
378 *cp++ = "0123456789abcdef"[n%b];
379 n /= b;
380 } while (n);
381 if (fld_size) {
382 for (fld_size -= cp - prbuf; fld_size > 0; fld_size--)
383 if (zf)
9bccf70c 384 putchar('0', &pca);
1c79356b 385 else
9bccf70c 386 putchar(' ', &pca);
1c79356b
A
387 }
388 do
9bccf70c 389 putchar(*--cp, &pca);
1c79356b
A
390 while (cp > prbuf);
391}
392
393
394
395/*
396 * Warn that a system table is full.
397 */
398void tablefull(const char *tab)
399{
400 log(LOG_ERR, "%s: table is full\n", tab);
401}
402
403/*
404 * Print a character on console or users terminal.
405 * If destination is console then the last MSGBUFS characters
406 * are saved in msgbuf for inspection later.
b0d623f7
A
407 *
408 * Locks: If TOTTY is set, we assume that the tty lock is held
409 * over the call to this function.
1c79356b
A
410 */
411/*ARGSUSED*/
9bccf70c
A
412void
413putchar(int c, void *arg)
1c79356b 414{
9bccf70c 415 struct putchar_args *pca = arg;
9bccf70c 416 char **sp = (char**) pca->tty;
1c79356b
A
417
418 if (panicstr)
419 constty = 0;
9bccf70c
A
420 if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
421 pca->tty = constty;
422 pca->flags |= TOTTY;
1c79356b 423 }
9bccf70c
A
424 if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 &&
425 (pca->flags & TOCONS) && pca->tty == constty)
1c79356b 426 constty = 0;
9bccf70c 427 if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177)
1c79356b 428 log_putc(c);
2d21ac55
A
429 if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177)
430 log_putc_locked(c);
9bccf70c 431 if ((pca->flags & TOCONS) && constty == 0 && c != '\0')
1c79356b 432 (*v_putc)(c);
9bccf70c 433 if (pca->flags & TOSTR) {
1c79356b
A
434 **sp = c;
435 (*sp)++;
436 }
1c79356b
A
437}
438
2d21ac55
A
439int
440vprintf(const char *fmt, va_list ap)
441{
442 struct putchar_args pca;
1c79356b 443
2d21ac55
A
444 pca.flags = TOLOG | TOCONS;
445 pca.tty = NULL;
3e170ce0 446 __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
2d21ac55
A
447 return 0;
448}
1c79356b 449
316670eb 450
1c79356b
A
451/*
452 * Scaled down version of vsprintf(3).
2d21ac55
A
453 *
454 * Deprecation Warning:
455 * vsprintf() is being deprecated. Please use vsnprintf() instead.
1c79356b
A
456 */
457int
458vsprintf(char *buf, const char *cfmt, va_list ap)
459{
460 int retval;
9bccf70c 461 struct snprintf_arg info;
1c79356b 462
9bccf70c
A
463 info.str = buf;
464 info.remain = 999999;
465
3e170ce0 466 retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE);
9bccf70c
A
467 if (info.remain >= 1) {
468 *info.str++ = '\0';
469 }
470 return 0;
1c79356b
A
471}
472
473/*
474 * Scaled down version of snprintf(3).
475 */
476int
477snprintf(char *str, size_t size, const char *format, ...)
478{
479 int retval;
480 va_list ap;
481
482 va_start(ap, format);
483 retval = vsnprintf(str, size, format, ap);
484 va_end(ap);
485 return(retval);
486}
487
488/*
489 * Scaled down version of vsnprintf(3).
490 */
491int
492vsnprintf(char *str, size_t size, const char *format, va_list ap)
493{
494 struct snprintf_arg info;
495 int retval;
496
497 info.str = str;
498 info.remain = size;
3e170ce0 499 retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE);
1c79356b
A
500 if (info.remain >= 1)
501 *info.str++ = '\0';
502 return retval;
503}
504
505static void
506snprintf_func(int ch, void *arg)
507{
508 struct snprintf_arg *const info = arg;
509
510 if (info->remain >= 2) {
511 *info->str++ = ch;
512 info->remain--;
513 }
514}
515
1c79356b
A
516int
517kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
518{
3e170ce0 519 __doprnt(fmt, ap, func, arg, radix, TRUE);
9bccf70c 520 return 0;
1c79356b 521}
9bccf70c 522