xref: /openbsd-src/sys/ddb/db_usrreq.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: db_usrreq.c,v 1.20 2017/09/08 05:36:52 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Michael Shalayeff.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/proc.h>
30 #include <sys/tty.h>
31 #include <sys/sysctl.h>
32 #include <dev/cons.h>
33 
34 #include <ddb/db_var.h>
35 
36 int	db_log = 1;
37 int	db_profile;			/* Allow dynamic profiling */
38 
39 int
40 ddb_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
41     size_t newlen, struct proc *p)
42 {
43 	int error, ctlval;
44 
45 	/* All sysctl names at this level are terminal. */
46 	if (namelen != 1)
47 		return (ENOTDIR);
48 
49 	switch (name[0]) {
50 
51 	case DBCTL_RADIX:
52 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_radix);
53 	case DBCTL_MAXWIDTH:
54 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_max_width);
55 	case DBCTL_TABSTOP:
56 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_tab_stop_width);
57 	case DBCTL_MAXLINE:
58 		return sysctl_int(oldp, oldlenp, newp, newlen, &db_max_line);
59 	case DBCTL_PANIC:
60 		if (securelevel > 0)
61 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
62 			    &db_panic));
63 		else {
64 			ctlval = db_panic;
65 			if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
66 			    &ctlval)) || newp == NULL)
67 				return (error);
68 			if (ctlval != 1 && ctlval != 0)
69 				return (EINVAL);
70 			db_panic = ctlval;
71 			return (0);
72 		}
73 		break;
74 	case DBCTL_CONSOLE:
75 		if (securelevel > 0)
76 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
77 			    &db_console));
78 		else {
79 			ctlval = db_console;
80 			if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
81 			    &ctlval)) || newp == NULL)
82 				return (error);
83 			if (ctlval != 1 && ctlval != 0)
84 				return (EINVAL);
85 			db_console = ctlval;
86 			return (0);
87 		}
88 		break;
89 	case DBCTL_LOG:
90 		return (sysctl_int(oldp, oldlenp, newp, newlen, &db_log));
91 	case DBCTL_TRIGGER:
92 		if (newp && db_console) {
93 			struct process *pr = curproc->p_p;
94 
95 			if (securelevel < 1 ||
96 			    (pr->ps_flags & PS_CONTROLT && cn_tab &&
97 			    cn_tab->cn_dev == pr->ps_session->s_ttyp->t_dev)) {
98 				db_enter();
99 				newp = NULL;
100 			} else
101 				return (ENODEV);
102 		}
103 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
104 #if defined(DDBPROF)
105 	case DBCTL_PROFILE:
106 		if (securelevel > 0)
107 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
108 			    &db_profile));
109 		else {
110 			ctlval = db_profile;
111 			if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
112 			    &ctlval)) || newp == NULL)
113 				return (error);
114 			if (ctlval != 1 && ctlval != 0)
115 				return (EINVAL);
116 			db_profile = ctlval;
117 			return (0);
118 		}
119 		break;
120 #endif /* DDBPROF */
121 	default:
122 		return (EOPNOTSUPP);
123 	}
124 	/* NOTREACHED */
125 }
126