-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproclog.c
154 lines (121 loc) · 3.37 KB
/
proclog.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//Logger that creates a proc file
//idea from tldp.org/LDP/lkmpg/2.6/html/index.html
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#ifndef __KERNEL__
#define __KERNEL__
#endif
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kernel module to log process times");
//size of buffer ~32Kb
#define PROCFS_MAX_SIZE 32768
//buffer to hold information from log
static char procfs_buffer[PROCFS_MAX_SIZE];
//size of buffer
static unsigned long procfs_buffer_size = 0;
//pointer for buffer location in read
static char *buff_ptr;
//struct to hold info about proc file
struct proc_dir_entry *log_file;
static int endflag;
static void *proc_seq_start(struct seq_file *s, loff_t *pos){
printk("Start of sequence read!\n");
(*pos) = endflag;
buff_ptr = procfs_buffer + ((*pos) * sizeof(char));
//if pos is greater than or equal to buffer size then leave sequence read
if((*pos) >= procfs_buffer_size-1 || *buff_ptr == '\0'){
printk("End sequence read\n");
return NULL;
}
printk("Place in buffer is: %Ld\n", (*pos));
return buff_ptr;
}
static void *proc_seq_next(struct seq_file *s, void *v, loff_t *pos){
printk("Sequence Next!");
char *temp = (char*)v;
while((*temp) != '\n'){
(*pos)++;
printk("position increased");
if((*pos) >= procfs_buffer_size){
return NULL;
}
temp++;
printk("temp increased");
}
temp++;
endflag = (*pos);
printk("position is %Ld\n", (*pos));
return temp;
}
static void proc_seq_stop(struct seq_file *s, void *v){
printk("Sequence stop!");
buff_ptr = NULL;
printk("Sequence stop 2: electric bugaloo");
}
static int proc_seq_show(struct seq_file *s, void *v){
printk("Showing value");
char *temp = (char*)v;
do{
seq_putc(s,*temp);
temp++;
}while(*temp != '\n');
seq_putc(s,'\n');
return 0;
}
static struct seq_operations proc_seq_ops = {
.start = proc_seq_start,
.next = proc_seq_next,
.stop = proc_seq_stop,
.show = proc_seq_show
};
static int procfile_open(struct inode *inode, struct file *file){
printk("open procfile");
return seq_open(file, &proc_seq_ops);
}
//function to write to proc file
static ssize_t procfile_write(struct file *file, const char *buffer, size_t count, loff_t * off){
//set buffer size
procfs_buffer_size += count;
if(procfs_buffer_size > PROCFS_MAX_SIZE){
procfs_buffer_size = PROCFS_MAX_SIZE;
printk("Proc file buffer overflow");
}
else{
printk("Buffer size updated to: %lu", procfs_buffer_size);
}
//write data to buffer
if(copy_from_user(procfs_buffer+(procfs_buffer_size - count), buffer, count)){
return -EFAULT;
}
return count;
}
//struct that holds what functions run for different aspects of log file
static const struct file_operations log_file_fops = {
.owner = THIS_MODULE,
.open = procfile_open,
.read = seq_read,
.write = procfile_write,
.llseek = seq_lseek,
.release = seq_release
};
int __init init_MyKernelModule(void){
//adapted from stackoverflow.com/questions/8516021/proc-create-example-for-kernel-module
log_file = proc_create("timing_log", 0, NULL, &log_file_fops);
if(log_file == NULL){
return -ENOMEM;
}
printk("kernel module initialized");
endflag = 0;
return 0;
}
void __exit exit_MyKernelModule(void){
remove_proc_entry("timing_log",NULL);
printk("exiting kernel module");
return;
}
module_init(init_MyKernelModule);
module_exit(exit_MyKernelModule);