summaryrefslogtreecommitdiff
path: root/sys/modules/examples/panic_string/panic_string.c
blob: f27cde06b6cd6044f7fa15c4d8bd0a1092c84410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*	$NetBSD: panic_string.c,v 1.1 2018/05/29 16:53:56 kamil Exp $	*/

/*-
 * Copyright (c) 2018 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: panic_string.c,v 1.1 2018/05/29 16:53:56 kamil Exp $");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/filedesc.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/lwp.h>
#include <sys/module.h>
#include <sys/vfs_syscalls.h>

/*
 * Create a device /dev/panic from which you can read sequential
 * user input.
 *
 * To use this device you need to do:
 *      mknod /dev/panic c 210 0
 *
 * To write to the device you might need:
 *      chmod 666 /dev/panic
 *
 * Commentary:
 * This module manages the device /dev/panic,
 * tranfers a string from userspace to kernel space
 * and calls kernel panic with the passed string.
 *
 *  echo 'string' > /dev/panic
 * will do the trick after loading the module.
 */

dev_type_open(panic_string_open);
dev_type_close(panic_string_close);
dev_type_write(panic_string_write);

static struct cdevsw panic_string_cdevsw = {
	.d_open = panic_string_open,
	.d_close = panic_string_close,
	.d_read = noread,
	.d_write = panic_string_write,
	.d_ioctl = noioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = nommap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = D_OTHER
};

static struct panic_string_softc {
	int refcnt;
} sc;

/*
 * A function similar to strnlen + isprint
 *
 * Detect length of the printable and non-whitespace string in the buffer.
 * A string is accepted if it contains any non-space character.
 */

static size_t
printable_length(const char *str, size_t len)
{
	size_t n;	
	bool accepted;

	n = 0;
	accepted = false;

	while (len > n) {
		if (str[n] >= 0x20 && str[n] <= 0x7e) {
			if (str[n] != 0x20 /* space */)
				accepted = true;
			n++;
		} else
			break;
	}

	if (accepted)
		return n;
	else
		return 0;
}

int
panic_string_open(dev_t self __unused, int flag __unused, int mod __unused, struct lwp *l)
{

	/* Make sure the device is opened once at a time */
	if (sc.refcnt > 0)
		return EBUSY;

	++sc.refcnt;

	return 0;
}

int
panic_string_close(dev_t self __unused, int flag __unused, int mod __unused, struct lwp *l __unused)
{

	--sc.refcnt;
	return 0;
}

int
panic_string_write(dev_t self, struct uio *uio, int flags)
{
	size_t len, printlen;
	char *buffer;

	/* Buffer length */
	len = uio->uio_iov->iov_len;

	/* Allocate a local buffer to store the string */
	buffer = (char *)kmem_alloc(len, KM_SLEEP);

	/* Move the string from user to kernel space and store it locally */
	uiomove(buffer, len, uio);

	printlen = printable_length(buffer, len);

	if (printlen > 0) {
		/* Flushing disk changes */
		do_sys_sync(curlwp);

		panic("panic string: %.*s\n", (int)printlen, buffer);
//		printf("panic string: %.*s\n", (int)printlen, buffer);

		/* NOTREACHED */
	}

	kmem_free(buffer, len);
	return 0;
}

MODULE(MODULE_CLASS_MISC, panic_string, NULL);

static int
panic_string_modcmd(modcmd_t cmd, void *arg __unused)
{
	/* The major should be verified and changed if needed to avoid
	 * conflicts with other devices. */
	int cmajor = 210, bmajor = -1;

	switch (cmd) {
	case MODULE_CMD_INIT:
		printf("Panic String module loaded.\n");
		if (devsw_attach("panic", NULL, &bmajor, &panic_string_cdevsw,
						 &cmajor))
			return ENXIO;
		return 0;

	case MODULE_CMD_FINI:
		printf("Panic String module unloaded.\n");
		if (sc.refcnt > 0)
			return EBUSY;

		devsw_detach(NULL, &panic_string_cdevsw);
		return 0;
	default:
		return ENOTTY;
	}
}