]>
Commit | Line | Data |
---|---|---|
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 */ |
105 | extern void bsd_log_lock(void); | |
106 | extern void bsd_log_unlock(void); | |
107 | ||
108 | /* Keep this around only because it's exported */ | |
109 | void _printf(int, struct tty *, const char *, ...); | |
110 | ||
1c79356b A |
111 | struct 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 | */ | |
122 | extern const char *panicstr; | |
123 | ||
2d21ac55 A |
124 | extern void cnputc(char); /* standard console putc */ |
125 | void (*v_putc)(char) = cnputc; /* routine to putc on virtual console */ | |
1c79356b A |
126 | |
127 | extern struct tty cons; /* standard console tty */ | |
128 | extern struct tty *constty; /* pointer to console "window" tty */ | |
9bccf70c A |
129 | extern int __doprnt(const char *fmt, |
130 | va_list *argp, | |
2d21ac55 | 131 | void (*)(int, void *), |
9bccf70c A |
132 | void *arg, |
133 | int radix); | |
1c79356b A |
134 | |
135 | /* | |
136 | * Record cpu that panic'd and lock around panic data | |
137 | */ | |
1c79356b A |
138 | static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size); |
139 | ||
1c79356b A |
140 | #if NCPUS > 1 |
141 | boolean_t new_printf_cpu_number; /* do we need to output who we are */ | |
142 | #endif | |
143 | ||
2d21ac55 A |
144 | extern void logwakeup(void); |
145 | extern void halt_cpu(void); | |
1c79356b A |
146 | |
147 | static void | |
148 | snprintf_func(int ch, void *arg); | |
149 | ||
9bccf70c A |
150 | struct putchar_args { |
151 | int flags; | |
152 | struct tty *tty; | |
153 | }; | |
154 | static void putchar(int c, void *arg); | |
1c79356b A |
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 | |
163 | uprintf(const char *fmt, ...) | |
164 | { | |
2d21ac55 | 165 | struct proc *p = current_proc(); |
9bccf70c | 166 | struct putchar_args pca; |
1c79356b | 167 | va_list ap; |
2d21ac55 A |
168 | struct session * sessp; |
169 | boolean_t fstate; | |
9bccf70c | 170 | |
2d21ac55 A |
171 | sessp = proc_session(p); |
172 | ||
173 | fstate = thread_funnel_set(kernel_flock, TRUE); | |
9bccf70c | 174 | pca.flags = TOTTY; |
2d21ac55 | 175 | pca.tty = (struct tty *)sessp->s_ttyp; |
1c79356b | 176 | |
2d21ac55 | 177 | if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) { |
1c79356b | 178 | va_start(ap, fmt); |
9bccf70c | 179 | __doprnt(fmt, &ap, putchar, &pca, 10); |
1c79356b A |
180 | va_end(ap); |
181 | } | |
2d21ac55 A |
182 | (void) thread_funnel_set(kernel_flock, fstate); |
183 | session_rele(sessp); | |
1c79356b A |
184 | } |
185 | ||
186 | tpr_t | |
2d21ac55 | 187 | tprintf_open(struct proc *p) |
1c79356b | 188 | { |
2d21ac55 A |
189 | struct session * sessp; |
190 | ||
191 | sessp = proc_session(p); | |
192 | ||
193 | if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) { | |
194 | return ((tpr_t)sessp); | |
1c79356b | 195 | } |
2d21ac55 A |
196 | if (sessp != SESSION_NULL) |
197 | session_rele(sessp); | |
198 | ||
1c79356b A |
199 | return ((tpr_t) NULL); |
200 | } | |
201 | ||
202 | void | |
2d21ac55 | 203 | tprintf_close(tpr_t sessp) |
1c79356b | 204 | { |
2d21ac55 A |
205 | if (sessp) |
206 | session_rele((struct session *) sessp); | |
1c79356b A |
207 | } |
208 | ||
209 | /* | |
210 | * tprintf prints on the controlling terminal associated | |
211 | * with the given session. | |
212 | */ | |
213 | void | |
214 | tprintf(tpr_t tpr, const char *fmt, ...) | |
215 | { | |
2d21ac55 | 216 | struct session *sess = (struct session *)tpr; |
1c79356b A |
217 | struct tty *tp = NULL; |
218 | int flags = TOLOG; | |
219 | va_list ap; | |
9bccf70c | 220 | struct putchar_args pca; |
2d21ac55 | 221 | boolean_t fstate; |
1c79356b A |
222 | |
223 | logpri(LOG_INFO); | |
2d21ac55 A |
224 | /* to protect tty */ |
225 | fstate = thread_funnel_set(kernel_flock, TRUE); | |
226 | ||
1c79356b A |
227 | if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { |
228 | flags |= TOTTY; | |
229 | tp = sess->s_ttyp; | |
230 | } | |
55e303ae A |
231 | |
232 | pca.flags = flags; | |
233 | pca.tty = tp; | |
234 | va_start(ap, fmt); | |
235 | __doprnt(fmt, &ap, putchar, &pca, 10); | |
236 | va_end(ap); | |
237 | ||
2d21ac55 A |
238 | (void) thread_funnel_set(kernel_flock, fstate); |
239 | ||
1c79356b A |
240 | logwakeup(); |
241 | } | |
242 | ||
243 | /* | |
244 | * Ttyprintf displays a message on a tty; it should be used only by | |
245 | * the tty driver, or anything that knows the underlying tty will not | |
246 | * be revoke(2)'d away. Other callers should use tprintf. | |
247 | */ | |
248 | void | |
249 | ttyprintf(struct tty *tp, const char *fmt, ...) | |
250 | { | |
251 | va_list ap; | |
2d21ac55 | 252 | boolean_t fstate; |
1c79356b A |
253 | |
254 | if (tp != NULL) { | |
2d21ac55 | 255 | fstate = thread_funnel_set(kernel_flock, TRUE); |
9bccf70c A |
256 | struct putchar_args pca; |
257 | pca.flags = TOTTY; | |
258 | pca.tty = tp; | |
259 | ||
1c79356b | 260 | va_start(ap, fmt); |
9bccf70c | 261 | __doprnt(fmt, &ap, putchar, &pca, 10); |
1c79356b | 262 | va_end(ap); |
2d21ac55 | 263 | (void) thread_funnel_set(kernel_flock, fstate); |
1c79356b A |
264 | } |
265 | } | |
266 | ||
267 | extern int log_open; | |
268 | ||
269 | ||
270 | void | |
2d21ac55 | 271 | logpri(int level) |
1c79356b | 272 | { |
9bccf70c A |
273 | struct putchar_args pca; |
274 | pca.flags = TOLOG; | |
275 | pca.tty = NULL; | |
276 | ||
277 | putchar('<', &pca); | |
1c79356b | 278 | printn((u_long)level, 10, TOLOG, (struct tty *)0, 0, 0); |
9bccf70c | 279 | putchar('>', &pca); |
1c79356b A |
280 | } |
281 | ||
2d21ac55 A |
282 | static void |
283 | _logtime(const char *fmt, ...) | |
1c79356b | 284 | { |
1c79356b | 285 | va_list ap; |
2d21ac55 A |
286 | va_start(ap, fmt); |
287 | vaddlog(fmt, ap); | |
288 | va_end(ap); | |
289 | } | |
290 | ||
291 | void | |
292 | logtime(time_t secs) | |
293 | { | |
294 | _logtime(" 0 [Time %ld] [Message ", secs); | |
295 | } | |
296 | ||
297 | int | |
298 | vaddlog(const char *fmt, va_list ap) | |
299 | { | |
9bccf70c A |
300 | struct putchar_args pca; |
301 | ||
2d21ac55 | 302 | pca.flags = TOLOGLOCKED; |
9bccf70c | 303 | pca.tty = NULL; |
1c79356b | 304 | |
9bccf70c | 305 | if (!log_open) { |
2d21ac55 | 306 | pca.flags |= TOCONS; |
9bccf70c | 307 | } |
2d21ac55 A |
308 | |
309 | bsd_log_lock(); | |
310 | __doprnt(fmt, &ap, putchar, &pca, 10); | |
311 | bsd_log_unlock(); | |
312 | ||
1c79356b | 313 | logwakeup(); |
2d21ac55 | 314 | return 0; |
1c79356b | 315 | } |
2d21ac55 A |
316 | |
317 | void | |
318 | _printf(int flags, struct tty *ttyp, const char *format, ...) | |
1c79356b A |
319 | { |
320 | va_list ap; | |
9bccf70c | 321 | struct putchar_args pca; |
2d21ac55 | 322 | boolean_t fstate; |
9bccf70c A |
323 | |
324 | pca.flags = flags; | |
325 | pca.tty = ttyp; | |
2d21ac55 | 326 | fstate = thread_funnel_set(kernel_flock, TRUE); |
1c79356b A |
327 | |
328 | va_start(ap, format); | |
9bccf70c | 329 | __doprnt(format, &ap, putchar, &pca, 10); |
1c79356b | 330 | va_end(ap); |
2d21ac55 | 331 | (void) thread_funnel_set(kernel_flock, fstate); |
1c79356b A |
332 | } |
333 | ||
334 | int prf(const char *fmt, va_list ap, int flags, struct tty *ttyp) | |
335 | { | |
9bccf70c | 336 | struct putchar_args pca; |
1c79356b | 337 | |
9bccf70c A |
338 | pca.flags = flags; |
339 | pca.tty = ttyp; | |
340 | ||
341 | #if NCPUS > 1 | |
342 | int cpun = cpu_number(); | |
343 | ||
344 | if(ttyp == 0) { | |
1c79356b A |
345 | } else |
346 | TTY_LOCK(ttyp); | |
9bccf70c | 347 | |
1c79356b | 348 | if (cpun != master_cpu) |
9bccf70c | 349 | new_printf_cpu_number = TRUE; |
1c79356b A |
350 | |
351 | if (new_printf_cpu_number) { | |
352 | putchar('{', flags, ttyp); | |
353 | printn((u_long)cpun, 10, flags, ttyp, 0, 0); | |
354 | putchar('}', flags, ttyp); | |
355 | } | |
9bccf70c A |
356 | #endif /* NCPUS > 1 */ |
357 | ||
358 | __doprnt(fmt, &ap, putchar, &pca, 10); | |
1c79356b | 359 | |
9bccf70c A |
360 | #if NCPUS > 1 |
361 | if(ttyp == 0) { | |
9bccf70c A |
362 | } else |
363 | TTY_UNLOCK(ttyp); | |
364 | #endif | |
365 | ||
366 | return 0; | |
1c79356b A |
367 | } |
368 | ||
1c79356b A |
369 | /* |
370 | * Printn prints a number n in base b. | |
371 | * We don't use recursion to avoid deep kernel stacks. | |
372 | */ | |
373 | static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size) | |
374 | { | |
375 | char prbuf[11]; | |
2d21ac55 | 376 | char *cp; |
9bccf70c A |
377 | struct putchar_args pca; |
378 | ||
379 | pca.flags = flags; | |
380 | pca.tty = ttyp; | |
1c79356b A |
381 | |
382 | if (b == 10 && (int)n < 0) { | |
9bccf70c | 383 | putchar('-', &pca); |
1c79356b A |
384 | n = (unsigned)(-(int)n); |
385 | } | |
386 | cp = prbuf; | |
387 | do { | |
388 | *cp++ = "0123456789abcdef"[n%b]; | |
389 | n /= b; | |
390 | } while (n); | |
391 | if (fld_size) { | |
392 | for (fld_size -= cp - prbuf; fld_size > 0; fld_size--) | |
393 | if (zf) | |
9bccf70c | 394 | putchar('0', &pca); |
1c79356b | 395 | else |
9bccf70c | 396 | putchar(' ', &pca); |
1c79356b A |
397 | } |
398 | do | |
9bccf70c | 399 | putchar(*--cp, &pca); |
1c79356b A |
400 | while (cp > prbuf); |
401 | } | |
402 | ||
403 | ||
404 | ||
405 | /* | |
406 | * Warn that a system table is full. | |
407 | */ | |
408 | void tablefull(const char *tab) | |
409 | { | |
410 | log(LOG_ERR, "%s: table is full\n", tab); | |
411 | } | |
412 | ||
413 | /* | |
414 | * Print a character on console or users terminal. | |
415 | * If destination is console then the last MSGBUFS characters | |
416 | * are saved in msgbuf for inspection later. | |
417 | */ | |
418 | /*ARGSUSED*/ | |
9bccf70c A |
419 | void |
420 | putchar(int c, void *arg) | |
1c79356b | 421 | { |
9bccf70c | 422 | struct putchar_args *pca = arg; |
9bccf70c | 423 | char **sp = (char**) pca->tty; |
1c79356b A |
424 | |
425 | if (panicstr) | |
426 | constty = 0; | |
9bccf70c A |
427 | if ((pca->flags & TOCONS) && pca->tty == NULL && constty) { |
428 | pca->tty = constty; | |
429 | pca->flags |= TOTTY; | |
1c79356b | 430 | } |
9bccf70c A |
431 | if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 && |
432 | (pca->flags & TOCONS) && pca->tty == constty) | |
1c79356b | 433 | constty = 0; |
9bccf70c | 434 | if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177) |
1c79356b | 435 | log_putc(c); |
2d21ac55 A |
436 | if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177) |
437 | log_putc_locked(c); | |
9bccf70c | 438 | if ((pca->flags & TOCONS) && constty == 0 && c != '\0') |
1c79356b | 439 | (*v_putc)(c); |
9bccf70c | 440 | if (pca->flags & TOSTR) { |
1c79356b A |
441 | **sp = c; |
442 | (*sp)++; | |
443 | } | |
1c79356b A |
444 | } |
445 | ||
2d21ac55 A |
446 | int |
447 | vprintf(const char *fmt, va_list ap) | |
448 | { | |
449 | struct putchar_args pca; | |
1c79356b | 450 | |
2d21ac55 A |
451 | pca.flags = TOLOG | TOCONS; |
452 | pca.tty = NULL; | |
453 | __doprnt(fmt, &ap, putchar, &pca, 10); | |
454 | return 0; | |
455 | } | |
1c79356b A |
456 | |
457 | /* | |
458 | * Scaled down version of vsprintf(3). | |
2d21ac55 A |
459 | * |
460 | * Deprecation Warning: | |
461 | * vsprintf() is being deprecated. Please use vsnprintf() instead. | |
1c79356b A |
462 | */ |
463 | int | |
464 | vsprintf(char *buf, const char *cfmt, va_list ap) | |
465 | { | |
466 | int retval; | |
9bccf70c | 467 | struct snprintf_arg info; |
1c79356b | 468 | |
9bccf70c A |
469 | info.str = buf; |
470 | info.remain = 999999; | |
471 | ||
472 | retval = __doprnt(cfmt, &ap, snprintf_func, &info, 10); | |
473 | if (info.remain >= 1) { | |
474 | *info.str++ = '\0'; | |
475 | } | |
476 | return 0; | |
1c79356b A |
477 | } |
478 | ||
479 | /* | |
480 | * Scaled down version of snprintf(3). | |
481 | */ | |
482 | int | |
483 | snprintf(char *str, size_t size, const char *format, ...) | |
484 | { | |
485 | int retval; | |
486 | va_list ap; | |
487 | ||
488 | va_start(ap, format); | |
489 | retval = vsnprintf(str, size, format, ap); | |
490 | va_end(ap); | |
491 | return(retval); | |
492 | } | |
493 | ||
494 | /* | |
495 | * Scaled down version of vsnprintf(3). | |
496 | */ | |
497 | int | |
498 | vsnprintf(char *str, size_t size, const char *format, va_list ap) | |
499 | { | |
500 | struct snprintf_arg info; | |
501 | int retval; | |
502 | ||
503 | info.str = str; | |
504 | info.remain = size; | |
9bccf70c | 505 | retval = __doprnt(format, &ap, snprintf_func, &info, 10); |
1c79356b A |
506 | if (info.remain >= 1) |
507 | *info.str++ = '\0'; | |
508 | return retval; | |
509 | } | |
510 | ||
511 | static void | |
512 | snprintf_func(int ch, void *arg) | |
513 | { | |
514 | struct snprintf_arg *const info = arg; | |
515 | ||
516 | if (info->remain >= 2) { | |
517 | *info->str++ = ch; | |
518 | info->remain--; | |
519 | } | |
520 | } | |
521 | ||
1c79356b A |
522 | int |
523 | kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) | |
524 | { | |
9bccf70c A |
525 | __doprnt(fmt, &ap, func, arg, radix); |
526 | return 0; | |
1c79356b | 527 | } |
9bccf70c | 528 |