xref: /openbsd-src/sys/ddb/db_usrreq.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: db_usrreq.c,v 1.21 2020/12/10 04:27:25 gnezdo 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 const struct sysctl_bounded_args ddb_vars[] = {
40 	{ DBCTL_RADIX, &db_radix, 8, 16 },
41 	{ DBCTL_MAXWIDTH, &db_max_width, 0, INT_MAX },
42 	{ DBCTL_TABSTOP, &db_tab_stop_width, 1, 16 },
43 	{ DBCTL_MAXLINE, &db_max_line, 0, INT_MAX },
44 	{ DBCTL_LOG, &db_log, 0, 1 },
45 };
46 
47 int
48 ddb_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
49     size_t newlen, struct proc *p)
50 {
51 	int error, ctlval;
52 
53 	/* All sysctl names at this level are terminal. */
54 	if (namelen != 1)
55 		return (ENOTDIR);
56 
57 	switch (name[0]) {
58 	case DBCTL_PANIC:
59 		if (securelevel > 0)
60 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
61 			    &db_panic));
62 		else {
63 			ctlval = db_panic;
64 			if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
65 			    &ctlval)) || newp == NULL)
66 				return (error);
67 			if (ctlval != 1 && ctlval != 0)
68 				return (EINVAL);
69 			db_panic = ctlval;
70 			return (0);
71 		}
72 		break;
73 	case DBCTL_CONSOLE:
74 		if (securelevel > 0)
75 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
76 			    &db_console));
77 		else {
78 			ctlval = db_console;
79 			if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
80 			    &ctlval)) || newp == NULL)
81 				return (error);
82 			if (ctlval != 1 && ctlval != 0)
83 				return (EINVAL);
84 			db_console = ctlval;
85 			return (0);
86 		}
87 		break;
88 	case DBCTL_TRIGGER:
89 		if (newp && db_console) {
90 			struct process *pr = curproc->p_p;
91 
92 			if (securelevel < 1 ||
93 			    (pr->ps_flags & PS_CONTROLT && cn_tab &&
94 			    cn_tab->cn_dev == pr->ps_session->s_ttyp->t_dev)) {
95 				db_enter();
96 				newp = NULL;
97 			} else
98 				return (ENODEV);
99 		}
100 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
101 #if defined(DDBPROF)
102 	case DBCTL_PROFILE:
103 		if (securelevel > 0)
104 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
105 			    &db_profile));
106 		else {
107 			ctlval = db_profile;
108 			if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
109 			    &ctlval)) || newp == NULL)
110 				return (error);
111 			if (ctlval != 1 && ctlval != 0)
112 				return (EINVAL);
113 			db_profile = ctlval;
114 			return (0);
115 		}
116 		break;
117 #endif /* DDBPROF */
118 	default:
119 		return (sysctl_bounded_arr(ddb_vars, nitems(ddb_vars), name,
120 		    namelen, oldp, oldlenp, newp, newlen));
121 	}
122 	/* NOTREACHED */
123 }
124