xref: /netbsd-src/sys/modules/examples/panic_string/panic_string.c (revision 6f8dc1509fa98d2f403ca646d8d47b18bead7044)
1*6f8dc150Sandvar /*	$NetBSD: panic_string.c,v 1.3 2021/10/21 13:21:55 andvar Exp $	*/
2926e9b7fSkamil 
3926e9b7fSkamil /*-
4926e9b7fSkamil  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5926e9b7fSkamil  * All rights reserved.
6926e9b7fSkamil  *
7926e9b7fSkamil  * Redistribution and use in source and binary forms, with or without
8926e9b7fSkamil  * modification, are permitted provided that the following conditions
9926e9b7fSkamil  * are met:
10926e9b7fSkamil  * 1. Redistributions of source code must retain the above copyright
11926e9b7fSkamil  *    notice, this list of conditions and the following disclaimer.
12926e9b7fSkamil  * 2. Redistributions in binary form must reproduce the above copyright
13926e9b7fSkamil  *    notice, this list of conditions and the following disclaimer in the
14926e9b7fSkamil  *    documentation and/or other materials provided with the distribution.
15926e9b7fSkamil  *
16926e9b7fSkamil  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17926e9b7fSkamil  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18926e9b7fSkamil  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19926e9b7fSkamil  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20926e9b7fSkamil  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21926e9b7fSkamil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22926e9b7fSkamil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23926e9b7fSkamil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24926e9b7fSkamil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25926e9b7fSkamil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26926e9b7fSkamil  * POSSIBILITY OF SUCH DAMAGE.
27926e9b7fSkamil  */
28926e9b7fSkamil 
29926e9b7fSkamil 
30926e9b7fSkamil #include <sys/cdefs.h>
31*6f8dc150Sandvar __KERNEL_RCSID(0, "$NetBSD: panic_string.c,v 1.3 2021/10/21 13:21:55 andvar Exp $");
32926e9b7fSkamil 
33926e9b7fSkamil #include <sys/param.h>
34926e9b7fSkamil #include <sys/types.h>
35926e9b7fSkamil #include <sys/conf.h>
36926e9b7fSkamil #include <sys/device.h>
37926e9b7fSkamil #include <sys/filedesc.h>
38926e9b7fSkamil #include <sys/kernel.h>
39926e9b7fSkamil #include <sys/kmem.h>
40926e9b7fSkamil #include <sys/lwp.h>
41926e9b7fSkamil #include <sys/module.h>
42926e9b7fSkamil #include <sys/vfs_syscalls.h>
43926e9b7fSkamil 
44926e9b7fSkamil /*
45926e9b7fSkamil  * Create a device /dev/panic from which you can read sequential
46926e9b7fSkamil  * user input.
47926e9b7fSkamil  *
48926e9b7fSkamil  * To use this device you need to do:
4967a548a8Skamil  *      mknod /dev/panic c 351 0
50926e9b7fSkamil  *
51926e9b7fSkamil  * To write to the device you might need:
52926e9b7fSkamil  *      chmod 666 /dev/panic
53926e9b7fSkamil  *
54926e9b7fSkamil  * Commentary:
55926e9b7fSkamil  * This module manages the device /dev/panic,
56*6f8dc150Sandvar  * transfers a string from userspace to kernel space
57926e9b7fSkamil  * and calls kernel panic with the passed string.
58926e9b7fSkamil  *
59926e9b7fSkamil  *  echo 'string' > /dev/panic
60926e9b7fSkamil  * will do the trick after loading the module.
61926e9b7fSkamil  */
62926e9b7fSkamil 
63926e9b7fSkamil dev_type_open(panic_string_open);
64926e9b7fSkamil dev_type_close(panic_string_close);
65926e9b7fSkamil dev_type_write(panic_string_write);
66926e9b7fSkamil 
67926e9b7fSkamil static struct cdevsw panic_string_cdevsw = {
68926e9b7fSkamil 	.d_open = panic_string_open,
69926e9b7fSkamil 	.d_close = panic_string_close,
70926e9b7fSkamil 	.d_read = noread,
71926e9b7fSkamil 	.d_write = panic_string_write,
72926e9b7fSkamil 	.d_ioctl = noioctl,
73926e9b7fSkamil 	.d_stop = nostop,
74926e9b7fSkamil 	.d_tty = notty,
75926e9b7fSkamil 	.d_poll = nopoll,
76926e9b7fSkamil 	.d_mmap = nommap,
77926e9b7fSkamil 	.d_kqfilter = nokqfilter,
78926e9b7fSkamil 	.d_discard = nodiscard,
79926e9b7fSkamil 	.d_flag = D_OTHER
80926e9b7fSkamil };
81926e9b7fSkamil 
82926e9b7fSkamil static struct panic_string_softc {
83926e9b7fSkamil 	int refcnt;
84926e9b7fSkamil } sc;
85926e9b7fSkamil 
86926e9b7fSkamil /*
87926e9b7fSkamil  * A function similar to strnlen + isprint
88926e9b7fSkamil  *
89926e9b7fSkamil  * Detect length of the printable and non-whitespace string in the buffer.
90926e9b7fSkamil  * A string is accepted if it contains any non-space character.
91926e9b7fSkamil  */
92926e9b7fSkamil 
93926e9b7fSkamil static size_t
printable_length(const char * str,size_t len)94926e9b7fSkamil printable_length(const char *str, size_t len)
95926e9b7fSkamil {
96926e9b7fSkamil 	size_t n;
97926e9b7fSkamil 	bool accepted;
98926e9b7fSkamil 
99926e9b7fSkamil 	n = 0;
100926e9b7fSkamil 	accepted = false;
101926e9b7fSkamil 
102926e9b7fSkamil 	while (len > n) {
103926e9b7fSkamil 		if (str[n] >= 0x20 && str[n] <= 0x7e) {
104926e9b7fSkamil 			if (str[n] != 0x20 /* space */)
105926e9b7fSkamil 				accepted = true;
106926e9b7fSkamil 			n++;
107926e9b7fSkamil 		} else
108926e9b7fSkamil 			break;
109926e9b7fSkamil 	}
110926e9b7fSkamil 
111926e9b7fSkamil 	if (accepted)
112926e9b7fSkamil 		return n;
113926e9b7fSkamil 	else
114926e9b7fSkamil 		return 0;
115926e9b7fSkamil }
116926e9b7fSkamil 
117926e9b7fSkamil int
panic_string_open(dev_t self __unused,int flag __unused,int mod __unused,struct lwp * l)118926e9b7fSkamil panic_string_open(dev_t self __unused, int flag __unused, int mod __unused, struct lwp *l)
119926e9b7fSkamil {
120926e9b7fSkamil 
121926e9b7fSkamil 	/* Make sure the device is opened once at a time */
122926e9b7fSkamil 	if (sc.refcnt > 0)
123926e9b7fSkamil 		return EBUSY;
124926e9b7fSkamil 
125926e9b7fSkamil 	++sc.refcnt;
126926e9b7fSkamil 
127926e9b7fSkamil 	return 0;
128926e9b7fSkamil }
129926e9b7fSkamil 
130926e9b7fSkamil int
panic_string_close(dev_t self __unused,int flag __unused,int mod __unused,struct lwp * l __unused)131926e9b7fSkamil panic_string_close(dev_t self __unused, int flag __unused, int mod __unused, struct lwp *l __unused)
132926e9b7fSkamil {
133926e9b7fSkamil 
134926e9b7fSkamil 	--sc.refcnt;
135926e9b7fSkamil 	return 0;
136926e9b7fSkamil }
137926e9b7fSkamil 
138926e9b7fSkamil int
panic_string_write(dev_t self,struct uio * uio,int flags)139926e9b7fSkamil panic_string_write(dev_t self, struct uio *uio, int flags)
140926e9b7fSkamil {
141926e9b7fSkamil 	size_t len, printlen;
142926e9b7fSkamil 	char *buffer;
143926e9b7fSkamil 
144926e9b7fSkamil 	/* Buffer length */
145926e9b7fSkamil 	len = uio->uio_iov->iov_len;
146926e9b7fSkamil 
147926e9b7fSkamil 	/* Allocate a local buffer to store the string */
148926e9b7fSkamil 	buffer = (char *)kmem_alloc(len, KM_SLEEP);
149926e9b7fSkamil 
150926e9b7fSkamil 	/* Move the string from user to kernel space and store it locally */
151926e9b7fSkamil 	uiomove(buffer, len, uio);
152926e9b7fSkamil 
153926e9b7fSkamil 	printlen = printable_length(buffer, len);
154926e9b7fSkamil 
155926e9b7fSkamil 	if (printlen > 0) {
156926e9b7fSkamil 		/* Flushing disk changes */
157926e9b7fSkamil 		do_sys_sync(curlwp);
158926e9b7fSkamil 
159926e9b7fSkamil 		panic("panic string: %.*s\n", (int)printlen, buffer);
160926e9b7fSkamil //		printf("panic string: %.*s\n", (int)printlen, buffer);
161926e9b7fSkamil 
162926e9b7fSkamil 		/* NOTREACHED */
163926e9b7fSkamil 	}
164926e9b7fSkamil 
165926e9b7fSkamil 	kmem_free(buffer, len);
166926e9b7fSkamil 	return 0;
167926e9b7fSkamil }
168926e9b7fSkamil 
169926e9b7fSkamil MODULE(MODULE_CLASS_MISC, panic_string, NULL);
170926e9b7fSkamil 
171926e9b7fSkamil static int
panic_string_modcmd(modcmd_t cmd,void * arg __unused)172926e9b7fSkamil panic_string_modcmd(modcmd_t cmd, void *arg __unused)
173926e9b7fSkamil {
174926e9b7fSkamil 	/* The major should be verified and changed if needed to avoid
175926e9b7fSkamil 	 * conflicts with other devices. */
17667a548a8Skamil 	int cmajor = 351, bmajor = -1;
177926e9b7fSkamil 
178926e9b7fSkamil 	switch (cmd) {
179926e9b7fSkamil 	case MODULE_CMD_INIT:
180926e9b7fSkamil 		printf("Panic String module loaded.\n");
181926e9b7fSkamil 		if (devsw_attach("panic", NULL, &bmajor, &panic_string_cdevsw,
182926e9b7fSkamil 						 &cmajor))
183926e9b7fSkamil 			return ENXIO;
184926e9b7fSkamil 		return 0;
185926e9b7fSkamil 
186926e9b7fSkamil 	case MODULE_CMD_FINI:
187926e9b7fSkamil 		printf("Panic String module unloaded.\n");
188926e9b7fSkamil 		if (sc.refcnt > 0)
189926e9b7fSkamil 			return EBUSY;
190926e9b7fSkamil 
191926e9b7fSkamil 		devsw_detach(NULL, &panic_string_cdevsw);
192926e9b7fSkamil 		return 0;
193926e9b7fSkamil 	default:
194926e9b7fSkamil 		return ENOTTY;
195926e9b7fSkamil 	}
196926e9b7fSkamil }
197