1/*	$OpenBSD: db_usrreq.c,v 1.22 2021/01/09 20:58:12 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
36int	db_log = 1;
37int	db_profile;			/* Allow dynamic profiling */
38
39const 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
47int
48ddb_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
49    size_t newlen, struct proc *p)
50{
51	/* All sysctl names at this level are terminal. */
52	if (namelen != 1)
53		return (ENOTDIR);
54
55	switch (name[0]) {
56	case DBCTL_PANIC:
57		if (securelevel > 0)
58			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
59			    &db_panic));
60		else {
61			return (sysctl_int_bounded(oldp, oldlenp, newp, newlen,
62			    &db_panic, 0, 1));
63		}
64		break;
65	case DBCTL_CONSOLE:
66		if (securelevel > 0)
67			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
68			    &db_console));
69		else {
70			return (sysctl_int_bounded(oldp, oldlenp, newp, newlen,
71			    &db_console, 0, 1));
72		}
73		break;
74	case DBCTL_TRIGGER:
75		if (newp && db_console) {
76			struct process *pr = curproc->p_p;
77
78			if (securelevel < 1 ||
79			    (pr->ps_flags & PS_CONTROLT && cn_tab &&
80			    cn_tab->cn_dev == pr->ps_session->s_ttyp->t_dev)) {
81				db_enter();
82				newp = NULL;
83			} else
84				return (ENODEV);
85		}
86		return (sysctl_rdint(oldp, oldlenp, newp, 0));
87#if defined(DDBPROF)
88	case DBCTL_PROFILE:
89		if (securelevel > 0)
90			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
91			    &db_profile));
92		else {
93			return (sysctl_int_bounded(oldp, oldlenp, newp, newlen,
94			    &db_profile, 0, 1));
95		}
96		break;
97#endif /* DDBPROF */
98	default:
99		return (sysctl_bounded_arr(ddb_vars, nitems(ddb_vars), name,
100		    namelen, oldp, oldlenp, newp, newlen));
101	}
102	/* NOTREACHED */
103}
104