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.
25f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
26*a923e7f4SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
27b819cea2SGordon Ross  */
28b819cea2SGordon Ross 
29b819cea2SGordon Ross #include <sys/param.h>
30b819cea2SGordon Ross #include <sys/types.h>
31b819cea2SGordon Ross #include <sys/varargs.h>
32b819cea2SGordon Ross #include <sys/systm.h>
33b819cea2SGordon Ross #include <sys/cmn_err.h>
34b819cea2SGordon Ross #include <sys/log.h>
35b819cea2SGordon Ross 
36b819cea2SGordon Ross #include <fakekernel.h>
37b819cea2SGordon Ross 
38b819cea2SGordon Ross void	abort(void) __NORETURN;
39b819cea2SGordon Ross 
40b819cea2SGordon Ross char *volatile panicstr;
41b819cea2SGordon Ross va_list  panicargs;
42b819cea2SGordon Ross char panicbuf[512];
43b819cea2SGordon Ross 
44b819cea2SGordon Ross volatile int aok;
45b819cea2SGordon Ross 
46b819cea2SGordon Ross static const int
47b819cea2SGordon Ross ce_flags[CE_IGNORE] = { SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL };
48b819cea2SGordon Ross static const char
49b819cea2SGordon Ross ce_prefix[CE_IGNORE][10] = { "", "NOTICE: ", "WARNING: ", "" };
50b819cea2SGordon Ross static const char
51b819cea2SGordon Ross ce_suffix[CE_IGNORE][2] = { "", "\n", "\n", "" };
52b819cea2SGordon Ross 
53b819cea2SGordon Ross 
54b819cea2SGordon Ross /*
55b819cea2SGordon Ross  * This function is just a stub, exported NODIRECT so that
56b819cea2SGordon Ross  * comsumers like fksmbd can provide their own.
57b819cea2SGordon Ross  * (One that actually prints the messages.)
58b819cea2SGordon Ross  *
59b819cea2SGordon Ross  * It's used by fakekernel_cprintf() below.
60b819cea2SGordon Ross  * The flags are SL_... from strlog.h
61b819cea2SGordon Ross  */
62b819cea2SGordon Ross /* ARGSUSED */
63b819cea2SGordon Ross void
64b819cea2SGordon Ross fakekernel_putlog(char *msg, size_t len, int flags)
65b819cea2SGordon Ross {
66b819cea2SGordon Ross }
67b819cea2SGordon Ross 
68b819cea2SGordon Ross /*
69b819cea2SGordon Ross  * fakekernel_cprintf() corresponds to os/printf.c:cprintf()
70b819cea2SGordon Ross  * This formats the message and calls fakekernel_putlog().
71b819cea2SGordon Ross  * It's exported NODIRECT to allow replacment.
72b819cea2SGordon Ross  * The flags are SL_... from strlog.h
73b819cea2SGordon Ross  */
74b819cea2SGordon Ross void
75b819cea2SGordon Ross fakekernel_cprintf(const char *fmt, va_list adx, int flags,
76f06dce2cSAndrew Stormont     const char *prefix, const char *suffix)
77b819cea2SGordon Ross {
78b819cea2SGordon Ross 	size_t bufsize = LOG_MSGSIZE;
79b819cea2SGordon Ross 	char buf[LOG_MSGSIZE];
80b819cea2SGordon Ross 	char *bufp = buf;
81b819cea2SGordon Ross 	char *msgp, *bufend;
82b819cea2SGordon Ross 	size_t len;
83b819cea2SGordon Ross 
84b819cea2SGordon Ross 	if (strchr("^!?", fmt[0]) != NULL) {
85b819cea2SGordon Ross 		if (fmt[0] == '^')
86b819cea2SGordon Ross 			flags |= SL_CONSONLY;
87b819cea2SGordon Ross 		else if (fmt[0] == '!')
88b819cea2SGordon Ross 			flags |= SL_LOGONLY;
89b819cea2SGordon Ross 		fmt++;
90b819cea2SGordon Ross 	}
91b819cea2SGordon Ross 
92b819cea2SGordon Ross 	bufend = bufp + bufsize;
93b819cea2SGordon Ross 	msgp = bufp;
94b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, "[fake_kernel] ");
95b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, prefix);
96b819cea2SGordon Ross 	msgp += vsnprintf(msgp, bufend - msgp, fmt, adx);
97b819cea2SGordon Ross 	msgp += snprintf(msgp, bufend - msgp, suffix);
98b819cea2SGordon Ross 	len = msgp - bufp;
99b819cea2SGordon Ross 
100b819cea2SGordon Ross 	fakekernel_putlog(bufp, len, flags);
101b819cea2SGordon Ross }
102b819cea2SGordon Ross 
103b819cea2SGordon Ross /*
104b819cea2SGordon Ross  * "User-level crash dump", if you will.
105b819cea2SGordon Ross  */
106b819cea2SGordon Ross void
107b819cea2SGordon Ross vpanic(const char *fmt, va_list adx)
108b819cea2SGordon Ross {
109b819cea2SGordon Ross 	va_list tmpargs;
110b819cea2SGordon Ross 
111b819cea2SGordon Ross 	panicstr = (char *)fmt;
112b819cea2SGordon Ross 	va_copy(panicargs, adx);
113b819cea2SGordon Ross 
114b819cea2SGordon Ross 	va_copy(tmpargs, adx);
115b819cea2SGordon Ross 	fakekernel_cprintf(fmt, tmpargs, SL_FATAL, "fatal: ", "\n");
116b819cea2SGordon Ross 
117b819cea2SGordon Ross 	/* Call libc`assfail() so that mdb ::status works */
118b819cea2SGordon Ross 	(void) vsnprintf(panicbuf, sizeof (panicbuf), fmt, adx);
119*a923e7f4SJohn Levon 	(void) assfail(panicbuf, "(panic)", 0);
120b819cea2SGordon Ross 
121b819cea2SGordon Ross 	abort();	/* avoid "noreturn" warnings */
122b819cea2SGordon Ross }
123b819cea2SGordon Ross 
124b819cea2SGordon Ross void
125b819cea2SGordon Ross panic(const char *fmt, ...)
126b819cea2SGordon Ross {
127b819cea2SGordon Ross 	va_list adx;
128b819cea2SGordon Ross 
129b819cea2SGordon Ross 	va_start(adx, fmt);
130b819cea2SGordon Ross 	vpanic(fmt, adx);
131b819cea2SGordon Ross 	va_end(adx);
132b819cea2SGordon Ross }
133b819cea2SGordon Ross 
134f06dce2cSAndrew Stormont void
135f06dce2cSAndrew Stormont fm_panic(const char *fmt, ...)
136f06dce2cSAndrew Stormont {
137f06dce2cSAndrew Stormont 	va_list adx;
138f06dce2cSAndrew Stormont 
139f06dce2cSAndrew Stormont 	va_start(adx, fmt);
140f06dce2cSAndrew Stormont 	vpanic(fmt, adx);
141f06dce2cSAndrew Stormont 	va_end(adx);
142f06dce2cSAndrew Stormont }
143f06dce2cSAndrew Stormont 
144b819cea2SGordon Ross void
145b819cea2SGordon Ross vcmn_err(int ce, const char *fmt, va_list adx)
146b819cea2SGordon Ross {
147b819cea2SGordon Ross 
148b819cea2SGordon Ross 	if (ce == CE_PANIC)
149b819cea2SGordon Ross 		vpanic(fmt, adx);
150b819cea2SGordon Ross 	if (ce >= CE_IGNORE)
151b819cea2SGordon Ross 		return;
152b819cea2SGordon Ross 
153b819cea2SGordon Ross 	fakekernel_cprintf(fmt, adx, ce_flags[ce] | SL_CONSOLE,
154b819cea2SGordon Ross 	    ce_prefix[ce], ce_suffix[ce]);
155b819cea2SGordon Ross }
156b819cea2SGordon Ross 
157b819cea2SGordon Ross /*PRINTFLIKE2*/
158b819cea2SGordon Ross void
159b819cea2SGordon Ross cmn_err(int ce, const char *fmt, ...)
160b819cea2SGordon Ross {
161b819cea2SGordon Ross 	va_list adx;
162b819cea2SGordon Ross 
163b819cea2SGordon Ross 	va_start(adx, fmt);
164b819cea2SGordon Ross 	vcmn_err(ce, fmt, adx);
165b819cea2SGordon Ross 	va_end(adx);
166b819cea2SGordon Ross }
167