]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
3 | * | |
6601e61a | 4 | * @APPLE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
6601e61a A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
8f6c56a5 | 11 | * |
6601e61a A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
6601e61a A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
8f6c56a5 | 19 | * |
6601e61a | 20 | * @APPLE_LICENSE_HEADER_END@ |
1c79356b A |
21 | */ |
22 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
23 | /*- | |
24 | * Copyright (c) 1986, 1988, 1991, 1993 | |
25 | * The Regents of the University of California. All rights reserved. | |
26 | * (c) UNIX System Laboratories, Inc. | |
27 | * All or some portions of this file are derived from material licensed | |
28 | * to the University of California by American Telephone and Telegraph | |
29 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
30 | * the permission of UNIX System Laboratories, Inc. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95 | |
61 | */ | |
62 | /* HISTORY | |
63 | * 22-Sep-1997 Umesh Vaishampayan (umeshv@apple.com) | |
64 | * Cleaned up m68k crud. Fixed vlog() to do logpri() for ppc, too. | |
65 | * | |
66 | * 17-July-97 Umesh Vaishampayan (umeshv@apple.com) | |
67 | * Eliminated multiple definition of constty which is defined | |
68 | * in bsd/dev/XXX/cons.c | |
69 | * | |
70 | * 26-MAR-1997 Umesh Vaishampayan (umeshv@NeXT.com | |
71 | * Fixed tharshing format in many functions. Cleanup. | |
72 | * | |
73 | * 17-Jun-1995 Mac Gillon (mgillon) at NeXT | |
74 | * Purged old history | |
75 | * New version based on 4.4 and NS3.3 | |
76 | */ | |
77 | ||
78 | #include <sys/param.h> | |
79 | #include <sys/systm.h> | |
1c79356b A |
80 | #include <sys/conf.h> |
81 | #include <sys/reboot.h> | |
82 | #include <sys/msgbuf.h> | |
91447636 | 83 | #include <sys/proc_internal.h> |
1c79356b A |
84 | #include <sys/ioctl.h> |
85 | #include <sys/tty.h> | |
91447636 | 86 | #include <sys/file_internal.h> |
1c79356b A |
87 | #include <sys/tprintf.h> |
88 | #include <sys/syslog.h> | |
89 | #include <stdarg.h> | |
90 | #include <sys/malloc.h> | |
91 | #include <sys/lock.h> | |
1c79356b A |
92 | #include <sys/subr_prf.h> |
93 | ||
94 | #include <kern/cpu_number.h> /* for cpu_number() */ | |
95 | #include <machine/spl.h> | |
96 | #include <libkern/libkern.h> | |
97 | ||
98 | struct snprintf_arg { | |
99 | char *str; | |
100 | size_t remain; | |
101 | }; | |
102 | ||
103 | ||
104 | /* | |
105 | * In case console is off, | |
106 | * panicstr contains argument to last | |
107 | * call to panic. | |
108 | */ | |
109 | extern const char *panicstr; | |
110 | ||
111 | extern cnputc(); /* standard console putc */ | |
112 | int (*v_putc)() = cnputc; /* routine to putc on virtual console */ | |
113 | ||
114 | extern struct tty cons; /* standard console tty */ | |
115 | extern struct tty *constty; /* pointer to console "window" tty */ | |
9bccf70c A |
116 | extern int __doprnt(const char *fmt, |
117 | va_list *argp, | |
118 | void (*putc)(int, void *arg), | |
119 | void *arg, | |
120 | int radix); | |
1c79356b A |
121 | |
122 | /* | |
123 | * Record cpu that panic'd and lock around panic data | |
124 | */ | |
125 | ||
126 | static void puts(const char *s, int flags, struct tty *ttyp); | |
127 | static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size); | |
128 | ||
1c79356b A |
129 | #if NCPUS > 1 |
130 | boolean_t new_printf_cpu_number; /* do we need to output who we are */ | |
131 | #endif | |
132 | ||
133 | extern void logwakeup(); | |
134 | extern void halt_cpu(); | |
135 | extern boot(); | |
9bccf70c | 136 | |
1c79356b A |
137 | |
138 | static void | |
139 | snprintf_func(int ch, void *arg); | |
140 | ||
9bccf70c A |
141 | struct putchar_args { |
142 | int flags; | |
143 | struct tty *tty; | |
144 | }; | |
145 | static void putchar(int c, void *arg); | |
1c79356b A |
146 | |
147 | ||
148 | /* | |
149 | * Uprintf prints to the controlling terminal for the current process. | |
150 | * It may block if the tty queue is overfull. No message is printed if | |
151 | * the queue does not clear in a reasonable time. | |
152 | */ | |
153 | void | |
154 | uprintf(const char *fmt, ...) | |
155 | { | |
156 | register struct proc *p = current_proc(); | |
9bccf70c | 157 | struct putchar_args pca; |
1c79356b | 158 | va_list ap; |
9bccf70c A |
159 | |
160 | pca.flags = TOTTY; | |
161 | pca.tty = (struct tty *)p->p_session->s_ttyp; | |
1c79356b | 162 | |
1c79356b A |
163 | if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { |
164 | va_start(ap, fmt); | |
9bccf70c | 165 | __doprnt(fmt, &ap, putchar, &pca, 10); |
1c79356b A |
166 | va_end(ap); |
167 | } | |
1c79356b A |
168 | } |
169 | ||
170 | tpr_t | |
171 | tprintf_open(p) | |
172 | register struct proc *p; | |
173 | { | |
1c79356b A |
174 | if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { |
175 | SESSHOLD(p->p_session); | |
1c79356b A |
176 | return ((tpr_t) p->p_session); |
177 | } | |
1c79356b A |
178 | return ((tpr_t) NULL); |
179 | } | |
180 | ||
181 | void | |
182 | tprintf_close(sess) | |
183 | tpr_t sess; | |
184 | { | |
1c79356b A |
185 | if (sess) |
186 | SESSRELE((struct session *) sess); | |
1c79356b A |
187 | } |
188 | ||
189 | /* | |
190 | * tprintf prints on the controlling terminal associated | |
191 | * with the given session. | |
192 | */ | |
193 | void | |
194 | tprintf(tpr_t tpr, const char *fmt, ...) | |
195 | { | |
196 | register struct session *sess = (struct session *)tpr; | |
197 | struct tty *tp = NULL; | |
198 | int flags = TOLOG; | |
199 | va_list ap; | |
9bccf70c | 200 | struct putchar_args pca; |
1c79356b A |
201 | |
202 | logpri(LOG_INFO); | |
1c79356b A |
203 | if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { |
204 | flags |= TOTTY; | |
205 | tp = sess->s_ttyp; | |
206 | } | |
55e303ae A |
207 | |
208 | pca.flags = flags; | |
209 | pca.tty = tp; | |
210 | va_start(ap, fmt); | |
211 | __doprnt(fmt, &ap, putchar, &pca, 10); | |
212 | va_end(ap); | |
213 | ||
1c79356b A |
214 | logwakeup(); |
215 | } | |
216 | ||
217 | /* | |
218 | * Ttyprintf displays a message on a tty; it should be used only by | |
219 | * the tty driver, or anything that knows the underlying tty will not | |
220 | * be revoke(2)'d away. Other callers should use tprintf. | |
221 | */ | |
222 | void | |
223 | ttyprintf(struct tty *tp, const char *fmt, ...) | |
224 | { | |
225 | va_list ap; | |
226 | ||
227 | if (tp != NULL) { | |
9bccf70c A |
228 | struct putchar_args pca; |
229 | pca.flags = TOTTY; | |
230 | pca.tty = tp; | |
231 | ||
1c79356b | 232 | va_start(ap, fmt); |
9bccf70c | 233 | __doprnt(fmt, &ap, putchar, &pca, 10); |
1c79356b A |
234 | va_end(ap); |
235 | } | |
236 | } | |
237 | ||
238 | extern int log_open; | |
239 | ||
240 | ||
241 | void | |
242 | logpri(level) | |
243 | int level; | |
244 | { | |
9bccf70c A |
245 | struct putchar_args pca; |
246 | pca.flags = TOLOG; | |
247 | pca.tty = NULL; | |
248 | ||
249 | putchar('<', &pca); | |
1c79356b | 250 | printn((u_long)level, 10, TOLOG, (struct tty *)0, 0, 0); |
9bccf70c | 251 | putchar('>', &pca); |
1c79356b A |
252 | } |
253 | ||
254 | void | |
255 | addlog(const char *fmt, ...) | |
256 | { | |
257 | register s = splhigh(); | |
258 | va_list ap; | |
9bccf70c A |
259 | struct putchar_args pca; |
260 | ||
261 | pca.flags = TOLOG; | |
262 | pca.tty = NULL; | |
1c79356b A |
263 | |
264 | va_start(ap, fmt); | |
9bccf70c A |
265 | __doprnt(fmt, &ap, putchar, &pca, 10); |
266 | ||
1c79356b | 267 | splx(s); |
9bccf70c A |
268 | if (!log_open) { |
269 | pca.flags = TOCONS; | |
270 | __doprnt(fmt, &ap, putchar, &pca, 10); | |
271 | } | |
1c79356b A |
272 | va_end(ap); |
273 | logwakeup(); | |
274 | } | |
275 | void _printf(int flags, struct tty *ttyp, const char *format, ...) | |
276 | { | |
277 | va_list ap; | |
9bccf70c A |
278 | struct putchar_args pca; |
279 | ||
280 | pca.flags = flags; | |
281 | pca.tty = ttyp; | |
1c79356b A |
282 | |
283 | va_start(ap, format); | |
9bccf70c | 284 | __doprnt(format, &ap, putchar, &pca, 10); |
1c79356b A |
285 | va_end(ap); |
286 | } | |
287 | ||
288 | int prf(const char *fmt, va_list ap, int flags, struct tty *ttyp) | |
289 | { | |
9bccf70c | 290 | struct putchar_args pca; |
1c79356b | 291 | |
9bccf70c A |
292 | pca.flags = flags; |
293 | pca.tty = ttyp; | |
294 | ||
295 | #if NCPUS > 1 | |
296 | int cpun = cpu_number(); | |
297 | ||
298 | if(ttyp == 0) { | |
1c79356b A |
299 | } else |
300 | TTY_LOCK(ttyp); | |
9bccf70c | 301 | |
1c79356b | 302 | if (cpun != master_cpu) |
9bccf70c | 303 | new_printf_cpu_number = TRUE; |
1c79356b A |
304 | |
305 | if (new_printf_cpu_number) { | |
306 | putchar('{', flags, ttyp); | |
307 | printn((u_long)cpun, 10, flags, ttyp, 0, 0); | |
308 | putchar('}', flags, ttyp); | |
309 | } | |
9bccf70c A |
310 | #endif /* NCPUS > 1 */ |
311 | ||
312 | __doprnt(fmt, &ap, putchar, &pca, 10); | |
1c79356b | 313 | |
9bccf70c A |
314 | #if NCPUS > 1 |
315 | if(ttyp == 0) { | |
9bccf70c A |
316 | } else |
317 | TTY_UNLOCK(ttyp); | |
318 | #endif | |
319 | ||
320 | return 0; | |
1c79356b A |
321 | } |
322 | ||
323 | static void puts(const char *s, int flags, struct tty *ttyp) | |
324 | { | |
325 | register char c; | |
9bccf70c A |
326 | struct putchar_args pca; |
327 | ||
328 | pca.flags = flags; | |
329 | pca.tty = ttyp; | |
1c79356b A |
330 | |
331 | while ((c = *s++)) | |
9bccf70c | 332 | putchar(c, &pca); |
1c79356b A |
333 | } |
334 | ||
335 | /* | |
336 | * Printn prints a number n in base b. | |
337 | * We don't use recursion to avoid deep kernel stacks. | |
338 | */ | |
339 | static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size) | |
340 | { | |
341 | char prbuf[11]; | |
342 | register char *cp; | |
9bccf70c A |
343 | struct putchar_args pca; |
344 | ||
345 | pca.flags = flags; | |
346 | pca.tty = ttyp; | |
1c79356b A |
347 | |
348 | if (b == 10 && (int)n < 0) { | |
9bccf70c | 349 | putchar('-', &pca); |
1c79356b A |
350 | n = (unsigned)(-(int)n); |
351 | } | |
352 | cp = prbuf; | |
353 | do { | |
354 | *cp++ = "0123456789abcdef"[n%b]; | |
355 | n /= b; | |
356 | } while (n); | |
357 | if (fld_size) { | |
358 | for (fld_size -= cp - prbuf; fld_size > 0; fld_size--) | |
359 | if (zf) | |
9bccf70c | 360 | putchar('0', &pca); |
1c79356b | 361 | else |
9bccf70c | 362 | putchar(' ', &pca); |
1c79356b A |
363 | } |
364 | do | |
9bccf70c | 365 | putchar(*--cp, &pca); |
1c79356b A |
366 | while (cp > prbuf); |
367 | } | |
368 | ||
369 | ||
370 | ||
371 | /* | |
372 | * Warn that a system table is full. | |
373 | */ | |
374 | void tablefull(const char *tab) | |
375 | { | |
376 | log(LOG_ERR, "%s: table is full\n", tab); | |
377 | } | |
378 | ||
379 | /* | |
380 | * Print a character on console or users terminal. | |
381 | * If destination is console then the last MSGBUFS characters | |
382 | * are saved in msgbuf for inspection later. | |
383 | */ | |
384 | /*ARGSUSED*/ | |
9bccf70c A |
385 | void |
386 | putchar(int c, void *arg) | |
1c79356b | 387 | { |
9bccf70c | 388 | struct putchar_args *pca = arg; |
1c79356b | 389 | register struct msgbuf *mbp; |
9bccf70c | 390 | char **sp = (char**) pca->tty; |
1c79356b A |
391 | |
392 | if (panicstr) | |
393 | constty = 0; | |
9bccf70c A |
394 | if ((pca->flags & TOCONS) && pca->tty == NULL && constty) { |
395 | pca->tty = constty; | |
396 | pca->flags |= TOTTY; | |
1c79356b | 397 | } |
9bccf70c A |
398 | if ((pca->flags & TOTTY) && pca->tty && tputchar(c, pca->tty) < 0 && |
399 | (pca->flags & TOCONS) && pca->tty == constty) | |
1c79356b | 400 | constty = 0; |
9bccf70c | 401 | if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177) |
1c79356b | 402 | log_putc(c); |
9bccf70c | 403 | if ((pca->flags & TOCONS) && constty == 0 && c != '\0') |
1c79356b | 404 | (*v_putc)(c); |
9bccf70c | 405 | if (pca->flags & TOSTR) { |
1c79356b A |
406 | **sp = c; |
407 | (*sp)++; | |
408 | } | |
1c79356b A |
409 | } |
410 | ||
411 | ||
412 | ||
413 | /* | |
414 | * Scaled down version of vsprintf(3). | |
415 | */ | |
416 | int | |
417 | vsprintf(char *buf, const char *cfmt, va_list ap) | |
418 | { | |
419 | int retval; | |
9bccf70c | 420 | struct snprintf_arg info; |
1c79356b | 421 | |
9bccf70c A |
422 | info.str = buf; |
423 | info.remain = 999999; | |
424 | ||
425 | retval = __doprnt(cfmt, &ap, snprintf_func, &info, 10); | |
426 | if (info.remain >= 1) { | |
427 | *info.str++ = '\0'; | |
428 | } | |
429 | return 0; | |
1c79356b A |
430 | } |
431 | ||
432 | /* | |
433 | * Scaled down version of snprintf(3). | |
434 | */ | |
435 | int | |
436 | snprintf(char *str, size_t size, const char *format, ...) | |
437 | { | |
438 | int retval; | |
439 | va_list ap; | |
440 | ||
441 | va_start(ap, format); | |
442 | retval = vsnprintf(str, size, format, ap); | |
443 | va_end(ap); | |
444 | return(retval); | |
445 | } | |
446 | ||
447 | /* | |
448 | * Scaled down version of vsnprintf(3). | |
449 | */ | |
450 | int | |
451 | vsnprintf(char *str, size_t size, const char *format, va_list ap) | |
452 | { | |
453 | struct snprintf_arg info; | |
454 | int retval; | |
455 | ||
456 | info.str = str; | |
457 | info.remain = size; | |
9bccf70c | 458 | retval = __doprnt(format, &ap, snprintf_func, &info, 10); |
1c79356b A |
459 | if (info.remain >= 1) |
460 | *info.str++ = '\0'; | |
461 | return retval; | |
462 | } | |
463 | ||
464 | static void | |
465 | snprintf_func(int ch, void *arg) | |
466 | { | |
467 | struct snprintf_arg *const info = arg; | |
468 | ||
469 | if (info->remain >= 2) { | |
470 | *info->str++ = ch; | |
471 | info->remain--; | |
472 | } | |
473 | } | |
474 | ||
1c79356b A |
475 | int |
476 | kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) | |
477 | { | |
9bccf70c A |
478 | __doprnt(fmt, &ap, func, arg, radix); |
479 | return 0; | |
1c79356b | 480 | } |
9bccf70c | 481 |