panic_string.c revision 1.1.2.2 1 1.1.2.2 pgoyette /* $NetBSD: panic_string.c,v 1.1.2.2 2018/06/25 07:26:05 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*-
4 1.1.2.2 pgoyette * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.1.2.2 pgoyette * are met:
10 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1.2.2 pgoyette *
16 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 pgoyette */
28 1.1.2.2 pgoyette
29 1.1.2.2 pgoyette
30 1.1.2.2 pgoyette #include <sys/cdefs.h>
31 1.1.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: panic_string.c,v 1.1.2.2 2018/06/25 07:26:05 pgoyette Exp $");
32 1.1.2.2 pgoyette
33 1.1.2.2 pgoyette #include <sys/param.h>
34 1.1.2.2 pgoyette #include <sys/types.h>
35 1.1.2.2 pgoyette #include <sys/conf.h>
36 1.1.2.2 pgoyette #include <sys/device.h>
37 1.1.2.2 pgoyette #include <sys/filedesc.h>
38 1.1.2.2 pgoyette #include <sys/kernel.h>
39 1.1.2.2 pgoyette #include <sys/kmem.h>
40 1.1.2.2 pgoyette #include <sys/lwp.h>
41 1.1.2.2 pgoyette #include <sys/module.h>
42 1.1.2.2 pgoyette #include <sys/vfs_syscalls.h>
43 1.1.2.2 pgoyette
44 1.1.2.2 pgoyette /*
45 1.1.2.2 pgoyette * Create a device /dev/panic from which you can read sequential
46 1.1.2.2 pgoyette * user input.
47 1.1.2.2 pgoyette *
48 1.1.2.2 pgoyette * To use this device you need to do:
49 1.1.2.2 pgoyette * mknod /dev/panic c 210 0
50 1.1.2.2 pgoyette *
51 1.1.2.2 pgoyette * To write to the device you might need:
52 1.1.2.2 pgoyette * chmod 666 /dev/panic
53 1.1.2.2 pgoyette *
54 1.1.2.2 pgoyette * Commentary:
55 1.1.2.2 pgoyette * This module manages the device /dev/panic,
56 1.1.2.2 pgoyette * tranfers a string from userspace to kernel space
57 1.1.2.2 pgoyette * and calls kernel panic with the passed string.
58 1.1.2.2 pgoyette *
59 1.1.2.2 pgoyette * echo 'string' > /dev/panic
60 1.1.2.2 pgoyette * will do the trick after loading the module.
61 1.1.2.2 pgoyette */
62 1.1.2.2 pgoyette
63 1.1.2.2 pgoyette dev_type_open(panic_string_open);
64 1.1.2.2 pgoyette dev_type_close(panic_string_close);
65 1.1.2.2 pgoyette dev_type_write(panic_string_write);
66 1.1.2.2 pgoyette
67 1.1.2.2 pgoyette static struct cdevsw panic_string_cdevsw = {
68 1.1.2.2 pgoyette .d_open = panic_string_open,
69 1.1.2.2 pgoyette .d_close = panic_string_close,
70 1.1.2.2 pgoyette .d_read = noread,
71 1.1.2.2 pgoyette .d_write = panic_string_write,
72 1.1.2.2 pgoyette .d_ioctl = noioctl,
73 1.1.2.2 pgoyette .d_stop = nostop,
74 1.1.2.2 pgoyette .d_tty = notty,
75 1.1.2.2 pgoyette .d_poll = nopoll,
76 1.1.2.2 pgoyette .d_mmap = nommap,
77 1.1.2.2 pgoyette .d_kqfilter = nokqfilter,
78 1.1.2.2 pgoyette .d_discard = nodiscard,
79 1.1.2.2 pgoyette .d_flag = D_OTHER
80 1.1.2.2 pgoyette };
81 1.1.2.2 pgoyette
82 1.1.2.2 pgoyette static struct panic_string_softc {
83 1.1.2.2 pgoyette int refcnt;
84 1.1.2.2 pgoyette } sc;
85 1.1.2.2 pgoyette
86 1.1.2.2 pgoyette /*
87 1.1.2.2 pgoyette * A function similar to strnlen + isprint
88 1.1.2.2 pgoyette *
89 1.1.2.2 pgoyette * Detect length of the printable and non-whitespace string in the buffer.
90 1.1.2.2 pgoyette * A string is accepted if it contains any non-space character.
91 1.1.2.2 pgoyette */
92 1.1.2.2 pgoyette
93 1.1.2.2 pgoyette static size_t
94 1.1.2.2 pgoyette printable_length(const char *str, size_t len)
95 1.1.2.2 pgoyette {
96 1.1.2.2 pgoyette size_t n;
97 1.1.2.2 pgoyette bool accepted;
98 1.1.2.2 pgoyette
99 1.1.2.2 pgoyette n = 0;
100 1.1.2.2 pgoyette accepted = false;
101 1.1.2.2 pgoyette
102 1.1.2.2 pgoyette while (len > n) {
103 1.1.2.2 pgoyette if (str[n] >= 0x20 && str[n] <= 0x7e) {
104 1.1.2.2 pgoyette if (str[n] != 0x20 /* space */)
105 1.1.2.2 pgoyette accepted = true;
106 1.1.2.2 pgoyette n++;
107 1.1.2.2 pgoyette } else
108 1.1.2.2 pgoyette break;
109 1.1.2.2 pgoyette }
110 1.1.2.2 pgoyette
111 1.1.2.2 pgoyette if (accepted)
112 1.1.2.2 pgoyette return n;
113 1.1.2.2 pgoyette else
114 1.1.2.2 pgoyette return 0;
115 1.1.2.2 pgoyette }
116 1.1.2.2 pgoyette
117 1.1.2.2 pgoyette int
118 1.1.2.2 pgoyette panic_string_open(dev_t self __unused, int flag __unused, int mod __unused, struct lwp *l)
119 1.1.2.2 pgoyette {
120 1.1.2.2 pgoyette
121 1.1.2.2 pgoyette /* Make sure the device is opened once at a time */
122 1.1.2.2 pgoyette if (sc.refcnt > 0)
123 1.1.2.2 pgoyette return EBUSY;
124 1.1.2.2 pgoyette
125 1.1.2.2 pgoyette ++sc.refcnt;
126 1.1.2.2 pgoyette
127 1.1.2.2 pgoyette return 0;
128 1.1.2.2 pgoyette }
129 1.1.2.2 pgoyette
130 1.1.2.2 pgoyette int
131 1.1.2.2 pgoyette panic_string_close(dev_t self __unused, int flag __unused, int mod __unused, struct lwp *l __unused)
132 1.1.2.2 pgoyette {
133 1.1.2.2 pgoyette
134 1.1.2.2 pgoyette --sc.refcnt;
135 1.1.2.2 pgoyette return 0;
136 1.1.2.2 pgoyette }
137 1.1.2.2 pgoyette
138 1.1.2.2 pgoyette int
139 1.1.2.2 pgoyette panic_string_write(dev_t self, struct uio *uio, int flags)
140 1.1.2.2 pgoyette {
141 1.1.2.2 pgoyette size_t len, printlen;
142 1.1.2.2 pgoyette char *buffer;
143 1.1.2.2 pgoyette
144 1.1.2.2 pgoyette /* Buffer length */
145 1.1.2.2 pgoyette len = uio->uio_iov->iov_len;
146 1.1.2.2 pgoyette
147 1.1.2.2 pgoyette /* Allocate a local buffer to store the string */
148 1.1.2.2 pgoyette buffer = (char *)kmem_alloc(len, KM_SLEEP);
149 1.1.2.2 pgoyette
150 1.1.2.2 pgoyette /* Move the string from user to kernel space and store it locally */
151 1.1.2.2 pgoyette uiomove(buffer, len, uio);
152 1.1.2.2 pgoyette
153 1.1.2.2 pgoyette printlen = printable_length(buffer, len);
154 1.1.2.2 pgoyette
155 1.1.2.2 pgoyette if (printlen > 0) {
156 1.1.2.2 pgoyette /* Flushing disk changes */
157 1.1.2.2 pgoyette do_sys_sync(curlwp);
158 1.1.2.2 pgoyette
159 1.1.2.2 pgoyette panic("panic string: %.*s\n", (int)printlen, buffer);
160 1.1.2.2 pgoyette // printf("panic string: %.*s\n", (int)printlen, buffer);
161 1.1.2.2 pgoyette
162 1.1.2.2 pgoyette /* NOTREACHED */
163 1.1.2.2 pgoyette }
164 1.1.2.2 pgoyette
165 1.1.2.2 pgoyette kmem_free(buffer, len);
166 1.1.2.2 pgoyette return 0;
167 1.1.2.2 pgoyette }
168 1.1.2.2 pgoyette
169 1.1.2.2 pgoyette MODULE(MODULE_CLASS_MISC, panic_string, NULL);
170 1.1.2.2 pgoyette
171 1.1.2.2 pgoyette static int
172 1.1.2.2 pgoyette panic_string_modcmd(modcmd_t cmd, void *arg __unused)
173 1.1.2.2 pgoyette {
174 1.1.2.2 pgoyette /* The major should be verified and changed if needed to avoid
175 1.1.2.2 pgoyette * conflicts with other devices. */
176 1.1.2.2 pgoyette int cmajor = 210, bmajor = -1;
177 1.1.2.2 pgoyette
178 1.1.2.2 pgoyette switch (cmd) {
179 1.1.2.2 pgoyette case MODULE_CMD_INIT:
180 1.1.2.2 pgoyette printf("Panic String module loaded.\n");
181 1.1.2.2 pgoyette if (devsw_attach("panic", NULL, &bmajor, &panic_string_cdevsw,
182 1.1.2.2 pgoyette &cmajor))
183 1.1.2.2 pgoyette return ENXIO;
184 1.1.2.2 pgoyette return 0;
185 1.1.2.2 pgoyette
186 1.1.2.2 pgoyette case MODULE_CMD_FINI:
187 1.1.2.2 pgoyette printf("Panic String module unloaded.\n");
188 1.1.2.2 pgoyette if (sc.refcnt > 0)
189 1.1.2.2 pgoyette return EBUSY;
190 1.1.2.2 pgoyette
191 1.1.2.2 pgoyette devsw_detach(NULL, &panic_string_cdevsw);
192 1.1.2.2 pgoyette return 0;
193 1.1.2.2 pgoyette default:
194 1.1.2.2 pgoyette return ENOTTY;
195 1.1.2.2 pgoyette }
196 1.1.2.2 pgoyette }
197