1 /**	$MirOS: src/sys/ddb/db_usrreq.c,v 1.4 2013/10/31 20:06:50 tg Exp $ */
2 /*	$OpenBSD: db_usrreq.c,v 1.9 2004/02/06 22:19:21 tedu Exp $	*/
3 
4 /*
5  * Copyright © 2006, 2013
6  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
7  * Copyright (c) 1996 Michael Shalayeff.  All rights reserved.
8  * Copyright (c) 1986, 1988, 1991, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  * (c) UNIX System Laboratories, Inc.
11  * All or some portions of this file are derived from material licensed
12  * to the University of California by American Telephone and Telegraph
13  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14  * the permission of UNIX System Laboratories, Inc.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <uvm/uvm_extern.h>
45 #include <sys/sysctl.h>
46 
47 #include <ddb/db_output.h>
48 #include <ddb/db_var.h>
49 
50 static void paniK(const char *, ...)
51     __attribute__((__format__(__kprintf__, 1, 2)));
52 
53 int	db_log = 1;
54 int	db_allowcrash = 0;
55 
56 int
ddb_sysctl(name,namelen,oldp,oldlenp,newp,newlen,p)57 ddb_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
58 	int	*name;
59 	u_int	namelen;
60 	void	*oldp;
61 	size_t	*oldlenp;
62 	void	*newp;
63 	size_t	newlen;
64 	struct proc *p;
65 {
66 	int error, ctlval;
67 
68 	/* All sysctl names at this level are terminal. */
69 	if (namelen != 1)
70 		return (ENOTDIR);
71 
72 	switch (name[0]) {
73 
74 	case DBCTL_RADIX:
75 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_radix);
76 	case DBCTL_MAXWIDTH:
77 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_max_width);
78 	case DBCTL_TABSTOP:
79 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_tab_stop_width);
80 	case DBCTL_MAXLINE:
81 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line);
82 	case DBCTL_PANIC:
83 		ctlval = db_panic;
84 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &ctlval)) ||
85 		    newp == NULL)
86 			return (error);
87 		if (ctlval != 1 && ctlval != 0)
88 			return (EINVAL);
89 		if (ctlval > db_panic && securelevel > 1)
90 			return (EPERM);
91 		db_panic = ctlval;
92 		return (0);
93 	case DBCTL_CONSOLE:
94 		ctlval = db_console;
95 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &ctlval)) ||
96 		    newp == NULL)
97 			return (error);
98 		if (ctlval != 1 && ctlval != 0)
99 			return (EINVAL);
100 		if (ctlval > db_console && securelevel > 1)
101 			return (EPERM);
102 		db_console = ctlval;
103 		return (0);
104 	case DBCTL_LOG:
105 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_log));
106 	case DBCTL_CRASH:
107 		ctlval = db_allowcrash;
108 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &ctlval)) ||
109 		    newp == NULL)
110 			return (error);
111 		switch (ctlval) {
112 		case 0:
113 			db_allowcrash = 0;
114 			break;
115 		case 1:
116 			if (securelevel > 0)
117 				return (EPERM);
118 			db_allowcrash = 1;
119 			break;
120 		default:
121 			if (!db_allowcrash)
122 				return (EPERM);
123 			paniK("ddb user requested panic: sysctl ddb.crash=%d",
124 			    ctlval);
125 			printf("db_usrreq.c: recovering sysop after panik\n");
126 		}
127 		return (0);
128 	default:
129 		return (EOPNOTSUPP);
130 	}
131 	/* NOTREACHED */
132 }
133 
134 /* this comes from panic() in kern/subr_prf.c - keep it in sync */
135 static void
paniK(const char * fmt,...)136 paniK(const char *fmt, ...)
137 {
138 	va_list ap;
139 	void (*tmp_panic_hook)(void) = panic_hook;
140 
141 	if (panic_hook) {
142 		panic_hook = NULL;
143 		tmp_panic_hook();
144 	}
145 
146 	printf("panic: ");
147 	va_start(ap, fmt);
148 	vprintf(fmt, ap);
149 	printf("\n");
150 	va_end(ap);
151 
152 #ifdef KGDB
153 	kgdb_panic();
154 #endif
155 #ifdef KADB
156 	if (boothowto & RB_KDB)
157 		kdbpanic();
158 #endif
159 #ifdef DDB
160 	if (db_panic)
161 		Debugger();
162 	else
163 		db_stack_dump();
164 #endif
165 
166 	panic_hook = tmp_panic_hook;
167 }
168