xref: /netbsd/sys/modules/panic/panic.c (revision 91309da9)
1*91309da9Schristos /* $NetBSD: panic.c,v 1.2 2021/06/21 03:08:07 christos Exp $ */
27b11d98dSjmcneill 
37b11d98dSjmcneill /*
47b11d98dSjmcneill  * Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca>
57b11d98dSjmcneill  * All rights reserved.
67b11d98dSjmcneill  *
77b11d98dSjmcneill  * Redistribution and use in source and binary forms, with or without
87b11d98dSjmcneill  * modification, are permitted provided that the following conditions
97b11d98dSjmcneill  * are met:
107b11d98dSjmcneill  * 1. Redistributions of source code must retain the above copyright
117b11d98dSjmcneill  *    notice, this list of conditions and the following disclaimer.
127b11d98dSjmcneill  * 2. The name of the author may not be used to endorse or promote products
137b11d98dSjmcneill  *    derived from this software without specific prior written permission.
147b11d98dSjmcneill  *
157b11d98dSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167b11d98dSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177b11d98dSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
187b11d98dSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
197b11d98dSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
207b11d98dSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
217b11d98dSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
227b11d98dSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
237b11d98dSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247b11d98dSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257b11d98dSjmcneill  * SUCH DAMAGE.
267b11d98dSjmcneill  */
277b11d98dSjmcneill 
287b11d98dSjmcneill #include <sys/cdefs.h>
29*91309da9Schristos __KERNEL_RCSID(0, "$NetBSD: panic.c,v 1.2 2021/06/21 03:08:07 christos Exp $");
307b11d98dSjmcneill 
317b11d98dSjmcneill #include <sys/module.h>
327b11d98dSjmcneill 
337b11d98dSjmcneill MODULE(MODULE_CLASS_MISC, panic, NULL);
347b11d98dSjmcneill 
357b11d98dSjmcneill static void
panic_dopanic(void)367b11d98dSjmcneill panic_dopanic(void)
377b11d98dSjmcneill {
387b11d98dSjmcneill 	/* just call panic */
397b11d98dSjmcneill 	panic("oops");
407b11d98dSjmcneill }
417b11d98dSjmcneill 
427b11d98dSjmcneill static void
panic_donullptr(void)437b11d98dSjmcneill panic_donullptr(void)
447b11d98dSjmcneill {
457b11d98dSjmcneill 	/* null ptr dereference */
467b11d98dSjmcneill 	*(int *)NULL = 1;
477b11d98dSjmcneill }
487b11d98dSjmcneill 
497b11d98dSjmcneill static const struct {
507b11d98dSjmcneill 	const char *name;
517b11d98dSjmcneill 	void (*func)(void);
527b11d98dSjmcneill } panic_howto[] = {
537b11d98dSjmcneill 	{ "panic",	panic_dopanic },
547b11d98dSjmcneill 	{ "nullptr",	panic_donullptr },
557b11d98dSjmcneill };
567b11d98dSjmcneill 
577b11d98dSjmcneill static int
panic_modcmd(modcmd_t cmd,void * opaque)587b11d98dSjmcneill panic_modcmd(modcmd_t cmd, void *opaque)
597b11d98dSjmcneill {
607b11d98dSjmcneill 	if (cmd == MODULE_CMD_INIT) {
617b11d98dSjmcneill 		prop_dictionary_t props = opaque;
627b11d98dSjmcneill 		const char *how = NULL;
637b11d98dSjmcneill 		unsigned int i;
647b11d98dSjmcneill 
657b11d98dSjmcneill 		if (props)
66*91309da9Schristos 			prop_dictionary_get_string(props, "how", &how);
677b11d98dSjmcneill 		if (how == NULL)
687b11d98dSjmcneill 			how = "panic";
697b11d98dSjmcneill 
707b11d98dSjmcneill 		for (i = 0; i < __arraycount(panic_howto); i++) {
717b11d98dSjmcneill 			if (strcmp(how, panic_howto[i].name) == 0) {
727b11d98dSjmcneill 				panic_howto[i].func();
737b11d98dSjmcneill 				break;
747b11d98dSjmcneill 			}
757b11d98dSjmcneill 		}
767b11d98dSjmcneill 		if (i == __arraycount(panic_howto))
777b11d98dSjmcneill 			printf("%s: no how '%s'\n", __func__, how);
787b11d98dSjmcneill 		else
797b11d98dSjmcneill 			printf("%s: how '%s' didn't panic?\n", __func__, how);
807b11d98dSjmcneill 
817b11d98dSjmcneill 		return EINVAL;
827b11d98dSjmcneill 	}
837b11d98dSjmcneill 
847b11d98dSjmcneill 	return ENOTTY;
857b11d98dSjmcneill }
86