| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <linux/module.h> |
| 8 |
|
| 9 |
#include <linux/sched.h> |
| 10 |
#include <linux/cpumask.h> |
| 11 |
#include <linux/time.h> |
| 12 |
#include <linux/smp_lock.h> |
| 13 |
#include <linux/types.h> |
| 14 |
#include <linux/errno.h> |
| 15 |
#include <linux/miscdevice.h> |
| 16 |
#include <linux/proc_fs.h> |
| 17 |
#include <linux/spinlock.h> |
| 18 |
#include <linux/uaccess.h> |
| 19 |
|
| 20 |
#include <asm/system.h> |
| 21 |
|
| 22 |
#define BACKFIRE_MINOR MISC_DYNAMIC_MINOR |
| 23 |
|
| 24 |
static spinlock_t backfire_state_lock = SPIN_LOCK_UNLOCKED; |
| 25 |
static int backfire_open_cnt; |
| 26 |
static int backfire_open_mode; |
| 27 |
static struct timeval sendtime; |
| 28 |
#define BACKFIRE_WRITE 1 |
| 29 |
#define BACKFIRE_EXCL 2 |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static ssize_t |
| 35 |
backfire_read(struct file *file, char *buf, size_t count, loff_t *ppos) |
| 36 |
{ |
| 37 |
return snprintf(buf, count, "%d,%d\n", (int) sendtime.tv_sec, |
| 38 |
(int) sendtime.tv_usec); |
| 39 |
} |
| 40 |
|
| 41 |
static ssize_t |
| 42 |
backfire_write(struct file *file, const char *buf, size_t count, loff_t *ppos) |
| 43 |
{ |
| 44 |
int signo; |
| 45 |
struct pid *pid; |
| 46 |
|
| 47 |
if (sscanf(buf, "%d", &signo) >= 1) { |
| 48 |
if (signo > 0 && signo < 32) { |
| 49 |
pid = get_pid(task_pid(current)); |
| 50 |
do_gettimeofday(&sendtime); |
| 51 |
kill_pid(pid, signo, 1); |
| 52 |
} else |
| 53 |
printk(KERN_ERR "Invalid signal no. %d\n", signo); |
| 54 |
} |
| 55 |
return strlen(buf); |
| 56 |
} |
| 57 |
|
| 58 |
static int |
| 59 |
backfire_open(struct inode *inode, struct file *file) |
| 60 |
{ |
| 61 |
spin_lock(&backfire_state_lock); |
| 62 |
|
| 63 |
if ((backfire_open_cnt && (file->f_flags & O_EXCL)) || |
| 64 |
(backfire_open_mode & BACKFIRE_EXCL)) { |
| 65 |
spin_unlock(&backfire_state_lock); |
| 66 |
return -EBUSY; |
| 67 |
} |
| 68 |
|
| 69 |
if (file->f_flags & O_EXCL) |
| 70 |
backfire_open_mode |= BACKFIRE_EXCL; |
| 71 |
if (file->f_mode & 2) |
| 72 |
backfire_open_mode |= BACKFIRE_WRITE; |
| 73 |
backfire_open_cnt++; |
| 74 |
|
| 75 |
spin_unlock(&backfire_state_lock); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int |
| 81 |
backfire_release(struct inode *inode, struct file *file) |
| 82 |
{ |
| 83 |
spin_lock(&backfire_state_lock); |
| 84 |
|
| 85 |
backfire_open_cnt--; |
| 86 |
|
| 87 |
if (backfire_open_cnt == 1 && backfire_open_mode & BACKFIRE_EXCL) |
| 88 |
backfire_open_mode &= ~BACKFIRE_EXCL; |
| 89 |
if (file->f_mode & 2) |
| 90 |
backfire_open_mode &= ~BACKFIRE_WRITE; |
| 91 |
|
| 92 |
spin_unlock(&backfire_state_lock); |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
static struct file_operations backfire_fops = { |
| 98 |
.owner = THIS_MODULE, |
| 99 |
.llseek = no_llseek, |
| 100 |
.open = backfire_open, |
| 101 |
.read = backfire_read, |
| 102 |
.write = backfire_write, |
| 103 |
.release = backfire_release, |
| 104 |
}; |
| 105 |
|
| 106 |
static struct miscdevice backfire_dev = { |
| 107 |
BACKFIRE_MINOR, |
| 108 |
"backfire", |
| 109 |
&backfire_fops |
| 110 |
}; |
| 111 |
|
| 112 |
static int __init backfire_init(void) |
| 113 |
{ |
| 114 |
int ret; |
| 115 |
|
| 116 |
ret = misc_register(&backfire_dev); |
| 117 |
if (ret) |
| 118 |
printk(KERN_ERR "backfire: can't register dynamic misc device\n"); |
| 119 |
else |
| 120 |
printk(KERN_INFO "backfire driver misc device %d\n", |
| 121 |
backfire_dev.minor); |
| 122 |
return ret; |
| 123 |
} |
| 124 |
|
| 125 |
static void __exit backfire_exit(void) |
| 126 |
{ |
| 127 |
misc_deregister(&backfire_dev); |
| 128 |
} |
| 129 |
|
| 130 |
module_init(backfire_init); |
| 131 |
module_exit(backfire_exit); |
| 132 |
|
| 133 |
MODULE_LICENSE("GPL"); |
| 134 |
MODULE_AUTHOR("Carsten Emde <[email protected]>"); |
| 135 |
MODULE_DESCRIPTION("Send signal back to caller"); |