Home | History | Annotate | Line # | Download | only in panic
panic.c revision 1.1.2.2
      1  1.1.2.2  bouyer /* $NetBSD: panic.c,v 1.1.2.2 2011/03/05 15:10:46 bouyer Exp $ */
      2  1.1.2.2  bouyer 
      3  1.1.2.2  bouyer /*
      4  1.1.2.2  bouyer  * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1.2.2  bouyer  * All rights reserved.
      6  1.1.2.2  bouyer  *
      7  1.1.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.2  bouyer  * modification, are permitted provided that the following conditions
      9  1.1.2.2  bouyer  * are met:
     10  1.1.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.2  bouyer  * 2. The name of the author may not be used to endorse or promote products
     13  1.1.2.2  bouyer  *    derived from this software without specific prior written permission.
     14  1.1.2.2  bouyer  *
     15  1.1.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1.2.2  bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1.2.2  bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1.2.2  bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1.2.2  bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1.2.2  bouyer  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1.2.2  bouyer  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1.2.2  bouyer  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1.2.2  bouyer  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1.2.2  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1.2.2  bouyer  * SUCH DAMAGE.
     26  1.1.2.2  bouyer  */
     27  1.1.2.2  bouyer 
     28  1.1.2.2  bouyer #include <sys/cdefs.h>
     29  1.1.2.2  bouyer __KERNEL_RCSID(0, "$NetBSD: panic.c,v 1.1.2.2 2011/03/05 15:10:46 bouyer Exp $");
     30  1.1.2.2  bouyer 
     31  1.1.2.2  bouyer #include <sys/module.h>
     32  1.1.2.2  bouyer 
     33  1.1.2.2  bouyer MODULE(MODULE_CLASS_MISC, panic, NULL);
     34  1.1.2.2  bouyer 
     35  1.1.2.2  bouyer static void
     36  1.1.2.2  bouyer panic_dopanic(void)
     37  1.1.2.2  bouyer {
     38  1.1.2.2  bouyer 	/* just call panic */
     39  1.1.2.2  bouyer 	panic("oops");
     40  1.1.2.2  bouyer }
     41  1.1.2.2  bouyer 
     42  1.1.2.2  bouyer static void
     43  1.1.2.2  bouyer panic_donullptr(void)
     44  1.1.2.2  bouyer {
     45  1.1.2.2  bouyer 	/* null ptr dereference */
     46  1.1.2.2  bouyer 	*(int *)NULL = 1;
     47  1.1.2.2  bouyer }
     48  1.1.2.2  bouyer 
     49  1.1.2.2  bouyer static const struct {
     50  1.1.2.2  bouyer 	const char *name;
     51  1.1.2.2  bouyer 	void (*func)(void);
     52  1.1.2.2  bouyer } panic_howto[] = {
     53  1.1.2.2  bouyer 	{ "panic",	panic_dopanic },
     54  1.1.2.2  bouyer 	{ "nullptr",	panic_donullptr },
     55  1.1.2.2  bouyer };
     56  1.1.2.2  bouyer 
     57  1.1.2.2  bouyer static int
     58  1.1.2.2  bouyer panic_modcmd(modcmd_t cmd, void *opaque)
     59  1.1.2.2  bouyer {
     60  1.1.2.2  bouyer 	if (cmd == MODULE_CMD_INIT) {
     61  1.1.2.2  bouyer 		prop_dictionary_t props = opaque;
     62  1.1.2.2  bouyer 		const char *how = NULL;
     63  1.1.2.2  bouyer 		unsigned int i;
     64  1.1.2.2  bouyer 
     65  1.1.2.2  bouyer 		if (props)
     66  1.1.2.2  bouyer 			prop_dictionary_get_cstring_nocopy(props, "how", &how);
     67  1.1.2.2  bouyer 		if (how == NULL)
     68  1.1.2.2  bouyer 			how = "panic";
     69  1.1.2.2  bouyer 
     70  1.1.2.2  bouyer 		for (i = 0; i < __arraycount(panic_howto); i++) {
     71  1.1.2.2  bouyer 			if (strcmp(how, panic_howto[i].name) == 0) {
     72  1.1.2.2  bouyer 				panic_howto[i].func();
     73  1.1.2.2  bouyer 				break;
     74  1.1.2.2  bouyer 			}
     75  1.1.2.2  bouyer 		}
     76  1.1.2.2  bouyer 		if (i == __arraycount(panic_howto))
     77  1.1.2.2  bouyer 			printf("%s: no how '%s'\n", __func__, how);
     78  1.1.2.2  bouyer 		else
     79  1.1.2.2  bouyer 			printf("%s: how '%s' didn't panic?\n", __func__, how);
     80  1.1.2.2  bouyer 
     81  1.1.2.2  bouyer 		return EINVAL;
     82  1.1.2.2  bouyer 	}
     83  1.1.2.2  bouyer 
     84  1.1.2.2  bouyer 	return ENOTTY;
     85  1.1.2.2  bouyer }
     86