1997-06-13 15:05:55 +05:30
|
|
|
/*
|
2007-05-30 20:57:13 +05:30
|
|
|
oops.c - Dummy loadable module for testing klogd.
|
|
|
|
Copyright (c) 2007 Martin Schulze <joey@infodrom.org>
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
This file is part of the sysklogd package, a kernel and system log daemon.
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
Helpful documentation: http://www.tldp.org/LDP/lkmpg/2.6/html/
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
|
|
|
|
echo TEXT > /proc/oops Emits TEXT via printk at log level
|
|
|
|
[<address+delta>] triggers klogd address decoding
|
|
|
|
echo level: info > /proc/oops Sets the log level to 'info'
|
|
|
|
echo oops > /proc/oops Creates a real oops, kills executing shell
|
|
|
|
cat /proc/oops Display current log level and last oops time
|
|
|
|
*/
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
#include <linux/init.h>
|
2018-08-05 22:24:27 +05:30
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2007-05-30 20:57:13 +05:30
|
|
|
#include <linux/poll.h>
|
2018-08-05 22:24:27 +05:30
|
|
|
#include <linux/proc_fs.h>
|
2007-05-30 20:57:13 +05:30
|
|
|
#include <linux/time.h>
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
#define MODNAME "oops"
|
|
|
|
#define PROCNAME "oops"
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
MODULE_AUTHOR("Martin Schulze <joey@infodrom.org>");
|
|
|
|
MODULE_DESCRIPTION("Oops module from klogd");
|
|
|
|
MODULE_LICENSE("GPL");
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
static DEFINE_MUTEX(oops_lock);
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
struct oops_t {
|
2018-08-05 22:24:27 +05:30
|
|
|
unsigned long lastoops;
|
|
|
|
int loglevel;
|
2007-05-30 20:57:13 +05:30
|
|
|
};
|
|
|
|
static struct oops_t oops_data;
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
static int procflag = 0;
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
struct code {
|
2018-08-05 22:24:27 +05:30
|
|
|
char *name;
|
|
|
|
int level;
|
1997-06-13 15:05:55 +05:30
|
|
|
};
|
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
struct code priorities[] = {
|
2018-08-05 22:24:27 +05:30
|
|
|
{ "emerg", 0 },
|
|
|
|
{ "panic", 0 }, /* DEPRECATED */
|
|
|
|
{ "alert", 1 },
|
|
|
|
{ "crit", 2 },
|
|
|
|
{ "err", 3 },
|
|
|
|
{ "error", 3 }, /* DEPRECATED */
|
|
|
|
{ "warning", 4 },
|
|
|
|
{ "warn", 4 }, /* DEPRECATED */
|
|
|
|
{ "notice", 5 },
|
|
|
|
{ "info", 6 },
|
|
|
|
{ "debug", 7 },
|
|
|
|
{ NULL, -1 }
|
1997-06-13 15:05:55 +05:30
|
|
|
};
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
void oops_decode_level(char *line)
|
1997-06-13 15:05:55 +05:30
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
struct code *prio;
|
|
|
|
char *p;
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (strncmp(line, "level:", 6))
|
|
|
|
return;
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
for (p = (char *)(line) + 6; *p == ' ' || *p == '\t'; p++)
|
|
|
|
;
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
for (prio = priorities; prio->name; prio++)
|
|
|
|
if (!strcmp(p, prio->name)) {
|
|
|
|
oops_data.loglevel = prio->level;
|
|
|
|
return;
|
|
|
|
}
|
2007-05-30 20:57:13 +05:30
|
|
|
}
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
/*
|
|
|
|
* This routine will create a real and ugly oops
|
|
|
|
*/
|
|
|
|
static void oops(void)
|
1997-06-13 15:05:55 +05:30
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
unsigned long *p = (unsigned long *)828282828;
|
1997-06-13 15:05:55 +05:30
|
|
|
*p = 5;
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
static int oops_proc_open(struct inode *inode, struct file *file)
|
1997-06-13 15:05:55 +05:30
|
|
|
{
|
2007-05-30 20:57:13 +05:30
|
|
|
#ifdef DEBUG
|
2018-08-05 22:24:27 +05:30
|
|
|
printk(KERN_DEBUG "oops_proc_open().\n");
|
2007-05-30 20:57:13 +05:30
|
|
|
#endif
|
2018-08-05 22:24:27 +05:30
|
|
|
return 0;
|
1997-06-13 15:05:55 +05:30
|
|
|
}
|
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
static ssize_t
|
2018-08-05 22:24:27 +05:30
|
|
|
oops_proc_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
|
2007-05-30 20:57:13 +05:30
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
struct code *prio;
|
|
|
|
char *level = NULL;
|
|
|
|
char s[70];
|
|
|
|
int size;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
|
|
|
#ifdef DEBUG
|
2018-08-05 22:24:27 +05:30
|
|
|
printk(KERN_DEBUG "oops_proc_read(%d).\n", nbytes);
|
2007-05-30 20:57:13 +05:30
|
|
|
#endif
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (procflag) {
|
|
|
|
procflag = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
for (prio = priorities;
|
|
|
|
prio->name && prio->level != oops_data.loglevel;
|
|
|
|
prio++)
|
|
|
|
;
|
|
|
|
level = prio->name;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (oops_data.lastoops == 0)
|
|
|
|
size = sprintf(s, "Log level: %s\nLast oops: none\n", level);
|
|
|
|
else {
|
|
|
|
unsigned long now = get_seconds();
|
|
|
|
unsigned long delta = now - oops_data.lastoops;
|
|
|
|
size = sprintf(s, "Log level: %s\nLast oops: %lu (%lu second%s ago)\n",
|
|
|
|
level, oops_data.lastoops,
|
|
|
|
delta, delta == 1 ? "" : "s");
|
|
|
|
}
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (size < nbytes)
|
|
|
|
nbytes = size;
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (copy_to_user(buf, s, nbytes))
|
|
|
|
return -EFAULT;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
*ppos += nbytes;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
procflag++;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
return nbytes;
|
2007-05-30 20:57:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
oops_proc_release(struct inode *inode, struct file *filp)
|
1997-06-13 15:05:55 +05:30
|
|
|
{
|
2007-05-30 20:57:13 +05:30
|
|
|
#ifdef DEBUG
|
2018-08-05 22:24:27 +05:30
|
|
|
printk(KERN_DEBUG "oops_proc_release().\n");
|
2007-05-30 20:57:13 +05:30
|
|
|
#endif
|
2018-08-05 22:24:27 +05:30
|
|
|
return 0;
|
2007-05-30 20:57:13 +05:30
|
|
|
}
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2007-05-30 20:57:13 +05:30
|
|
|
static ssize_t
|
|
|
|
oops_proc_write(struct file *file, const char __user *buf,
|
|
|
|
size_t nbytes, loff_t *ppos)
|
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
char input[100];
|
|
|
|
int len;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
|
|
|
#ifdef DEBUG
|
2018-08-05 22:24:27 +05:30
|
|
|
printk(KERN_DEBUG "oops_proc_write(%d).\n", nbytes);
|
2007-05-30 20:57:13 +05:30
|
|
|
#endif
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
len = nbytes >= sizeof(input) ? sizeof(input) - 1 : nbytes;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (copy_from_user(input, buf, len))
|
|
|
|
return -EFAULT;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
input[len] = '\0';
|
|
|
|
if (input[len - 1] == '\n')
|
|
|
|
input[len - 1] = '\0';
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (!strncmp(input, "level:", 6))
|
|
|
|
oops_decode_level(input);
|
|
|
|
else if (!strcmp(input, "oops")) {
|
|
|
|
oops_data.lastoops = get_seconds();
|
|
|
|
oops();
|
|
|
|
} else
|
|
|
|
printk("<%d>%s\n", oops_data.loglevel, input);
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
return nbytes;
|
2007-05-30 20:57:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations oops_proc_operations = {
|
2018-08-05 22:24:27 +05:30
|
|
|
.read = oops_proc_read,
|
|
|
|
.release = oops_proc_release,
|
|
|
|
.write = oops_proc_write,
|
|
|
|
.open = oops_proc_open,
|
2007-05-30 20:57:13 +05:30
|
|
|
};
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
void oops_proc_add(void)
|
2007-05-30 20:57:13 +05:30
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
struct proc_dir_entry *entry;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
mutex_lock(&oops_lock);
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
entry = create_proc_entry(PROCNAME, 0, NULL);
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
if (entry) {
|
|
|
|
entry->proc_fops = &oops_proc_operations;
|
|
|
|
}
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
mutex_unlock(&oops_lock);
|
2007-05-30 20:57:13 +05:30
|
|
|
}
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
void oops_proc_remove(void)
|
2007-05-30 20:57:13 +05:30
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
mutex_lock(&oops_lock);
|
1997-06-13 15:05:55 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
remove_proc_entry(PROCNAME, NULL);
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
mutex_unlock(&oops_lock);
|
1997-06-13 15:05:55 +05:30
|
|
|
}
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
int oops_init(void)
|
2007-05-30 20:57:13 +05:30
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
printk(KERN_INFO "Loading module " MODNAME ".\n");
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
oops_data.lastoops = 0;
|
|
|
|
oops_data.loglevel = 5;
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
oops_proc_add();
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
return 0;
|
2007-05-30 20:57:13 +05:30
|
|
|
}
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
void oops_cleanup(void)
|
2007-05-30 20:57:13 +05:30
|
|
|
{
|
2018-08-05 22:24:27 +05:30
|
|
|
oops_proc_remove();
|
2007-05-30 20:57:13 +05:30
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
printk(KERN_INFO "Removing module " MODNAME ".\n");
|
2007-05-30 20:57:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
module_init(oops_init);
|
|
|
|
module_exit(oops_cleanup);
|
|
|
|
|
2018-08-05 22:24:27 +05:30
|
|
|
/**
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* c-file-style: "linux"
|
|
|
|
* End:
|
|
|
|
*/
|