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