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