-
Notifications
You must be signed in to change notification settings - Fork 0
/
lehmerCharDev.c
65 lines (48 loc) · 1.42 KB
/
lehmerCharDev.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
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/random.h>
#include "lehmer64.h"
#define NAME "lehmer_character_device"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Björn Stresing");
MODULE_DESCRIPTION("A character device that generates pseudo numbers using lehmer generator. This is very fast.");
static int major;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off) {
size_t written = 0;
while(written < len) {
size_t ret = 0;
char ring[2048];
size_t size = min(sizeof(ring) - sizeof(uint64_t) + 1, len - written);
while(ret < size) {
uint64_t rng = lehmer64();
memcpy(ring + ret, &rng, sizeof(rng));
ret += sizeof(rng);
}
ret = min(ret, len - written);
if (copy_to_user(buf + written, ring, ret)) {
written = -EFAULT;
break;
} else {
*off += ret;
}
written += ret;
}
return written;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.read = read,
};
static int myinit(void) {
lehmer64_seed(get_random_u64());
major = register_chrdev(0, NAME, &fops);
return 0;
}
static void myexit(void) {
unregister_chrdev(major, NAME);
}
module_init(myinit);
module_exit(myexit);