-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseconds.c
More file actions
64 lines (48 loc) · 1.3 KB
/
seconds.c
File metadata and controls
64 lines (48 loc) · 1.3 KB
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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/jiffies.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Expose elapsed seconds through /proc/seconds");
MODULE_AUTHOR("Brandon Tiong");
#define PROC_NAME "seconds"
static unsigned long loaded_jiffies;
static struct proc_dir_entry *seconds_entry;
static ssize_t proc_read(struct file *file, char __user *usr_buf,
size_t count, loff_t *pos)
{
int rv = 0;
char buffer[128];
static int completed = 0;
unsigned long elapsed_seconds;
if (completed) {
completed = 0;
return 0;
}
completed = 1;
elapsed_seconds = (jiffies - loaded_jiffies) / HZ;
rv = snprintf(buffer, sizeof(buffer), "Elapsed seconds = %lu\n", elapsed_seconds);
if(copy_to_user(usr_buf, buffer, rv)) {
rv = -EFAULT;
}
return rv;
}
static struct proc_ops my_fops = {
.proc_read = proc_read,
};
static int __init seconds_init(void)
{
loaded_jiffies = jiffies;
seconds_entry = proc_create(PROC_NAME, 0666, NULL, &my_fops);
if (seconds_entry == NULL) {
return -ENOMEM;
}
return 0;
}
static void __exit seconds_exit(void)
{
proc_remove(seconds_entry);
}
module_init(seconds_init);
module_exit(seconds_exit);