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