This repository was archived by the owner on Aug 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhorok.c
108 lines (90 loc) · 2.43 KB
/
horok.c
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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/file.h>
#include <linux/string.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/netpoll.h>
#define M_MISC_MAJOR 0
static struct miscdevice misc;
char kbuf[PAGE_SIZE];
// horo_read displays info to user stored by horo_ioctl
static ssize_t horo_read(struct file *filp, char __user *buf,
size_t count, loff_t *offp)
{
return simple_read_from_buffer(buf, count, offp, kbuf, strlen(kbuf) + 1);
}
// call_horoproxy calls usermode helper: horoproxy to do real tasks
static int call_horoproxy(char *cmd) {
struct subprocess_info *sub_info;
char *argv[] = {"/usr/bin/horoproxy", cmd, NULL};
static char *envp[] = {
"HOME=/",
"TERM=linux",
NULL
};
schedule();
sub_info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC, NULL, NULL, NULL);
if (sub_info == NULL) return -ENOMEM;
return call_usermodehelper_exec(sub_info, UMH_WAIT_PROC);
}
// horo_write gets the command from user, and send it to horoproxy
static ssize_t horo_write(struct file *filp, const char __user *buf,
size_t count, loff_t *offp)
{
char *cmdbuf;
int ret;
cmdbuf = (char *)kmalloc(PAGE_SIZE, GFP_KERNEL);
memset(kbuf, 0, sizeof(kbuf));
ret = simple_write_to_buffer(kbuf, sizeof(kbuf), offp, buf, count);
if (ret < 0)
return ret;
pr_debug("Kbuf: %s", kbuf);
ret = call_horoproxy(kbuf);
kfree(cmdbuf);
if(ret == 0)
return count;
return ret;
}
// horo_ioctl is for store the result to buffer
// later will show the result when userspace call horo_read
static long horo_ioctl(struct file *filp, unsigned int op,
unsigned long paramp)
{
char __user *userdat = (char *)paramp;
int ret;
memset(kbuf, 0, sizeof(kbuf));
ret = copy_from_user(kbuf, userdat, PAGE_SIZE);
pr_debug("IOCTL Recv Message: %s\n", kbuf);
return ret;
}
const struct file_operations misc_fops = {
.read = horo_read,
.owner = THIS_MODULE,
.write = horo_write,
.unlocked_ioctl = horo_ioctl,
};
int horo_init(void)
{
int ret;
pr_debug("Start init misc device horo\n");
misc.minor = MISC_DYNAMIC_MINOR;
misc.mode = 0666;
misc.name = "horo";
misc.fops = &misc_fops;
ret = misc_register(&misc);
if (ret < 0) {
pr_err("Cannot register misc horo\n");
return ret;
}
return 0;
}
void horo_exit(void)
{
pr_debug("Start de-init misc device horo\n");
misc_deregister(&misc);
}
module_init(horo_init);
module_exit(horo_exit)
MODULE_LICENSE("GPL v3");
MODULE_AUTHOR("VOID001<telegram @VOID001>");