5917 User-mode SMB server
[unleashed.git] / usr / src / lib / libfakekernel / common / printf.c
blob1bcef683d12746f3f8f5d6a2e7904b22901d3f5f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/varargs.h>
30 #include <sys/systm.h>
31 #include <sys/cmn_err.h>
32 #include <sys/log.h>
34 #include <fakekernel.h>
36 void abort(void) __NORETURN;
38 char *volatile panicstr;
39 va_list panicargs;
40 char panicbuf[512];
42 volatile int aok;
44 static const int
45 ce_flags[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
46 static const char
47 ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
48 static const char
49 ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
53 * This function is just a stub, exported NODIRECT so that
54 * comsumers like fksmbd can provide their own.
55 * (One that actually prints the messages.)
57 * It's used by fakekernel_cprintf() below.
58 * The flags are SL_... from strlog.h
60 /* ARGSUSED */
61 void
62 fakekernel_putlog(char *msg, size_t len, int flags)
67 * fakekernel_cprintf() corresponds to os/printf.c:cprintf()
68 * This formats the message and calls fakekernel_putlog().
69 * It's exported NODIRECT to allow replacment.
70 * The flags are SL_... from strlog.h
72 void
73 fakekernel_cprintf(const char *fmt, va_list adx, int flags,
74 const char *prefix, const char *suffix)
76 size_t bufsize = LOG_MSGSIZE;
77 char buf[LOG_MSGSIZE];
78 char *bufp = buf;
79 char *msgp, *bufend;
80 size_t len;
82 if (strchr("^!?", fmt[0]) != NULL) {
83 if (fmt[0] == '^')
84 flags |= SL_CONSONLY;
85 else if (fmt[0] == '!')
86 flags |= SL_LOGONLY;
87 fmt++;
90 bufend = bufp + bufsize;
91 msgp = bufp;
92 msgp += snprintf(msgp, bufend - msgp, "[fake_kernel] ");
93 msgp += snprintf(msgp, bufend - msgp, prefix);
94 msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
95 msgp += snprintf(msgp, bufend - msgp, suffix);
96 len = msgp - bufp;
98 fakekernel_putlog(bufp, len, flags);
102 * "User-level crash dump", if you will.
104 void
105 vpanic(const char *fmt, va_list adx)
107 va_list tmpargs;
109 panicstr = (char *)fmt;
110 va_copy(panicargs, adx);
112 va_copy(tmpargs, adx);
113 fakekernel_cprintf(fmt, tmpargs, SL_FATAL, "fatal: ", "\n");
115 /* Call libc`assfail() so that mdb ::status works */
116 (void) vsnprintf(panicbuf, sizeof (panicbuf), fmt, adx);
117 assfail(panicbuf, "(panic)", 0);
119 abort(); /* avoid "noreturn" warnings */
122 void
123 panic(const char *fmt, ...)
125 va_list adx;
127 va_start(adx, fmt);
128 vpanic(fmt, adx);
129 va_end(adx);
132 void
133 vcmn_err(int ce, const char *fmt, va_list adx)
136 if (ce == CE_PANIC)
137 vpanic(fmt, adx);
138 if (ce >= CE_IGNORE)
139 return;
141 fakekernel_cprintf(fmt, adx, ce_flags[ce] | SL_CONSOLE,
142 ce_prefix[ce], ce_suffix[ce]);
145 /*PRINTFLIKE2*/
146 void
147 cmn_err(int ce, const char *fmt, ...)
149 va_list adx;
151 va_start(adx, fmt);
152 vcmn_err(ce, fmt, adx);
153 va_end(adx);