forked from Tert-butyllithium/syscord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump-file.h
78 lines (66 loc) · 1.96 KB
/
dump-file.h
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
#ifndef _SYSCORD_DUMP_FILE_H
#define _SYSCORD_DUMP_FILE_H
#include <asm/io.h>
#include <linux/buffer_head.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#define WRITE_FILE_LOCK() spin_lock(&buf_lock)
#define WRITE_FILE_UNLOCK() spin_unlock(&buf_lock)
static struct file* __file_to_record = NULL;
// static struct file* __file_to_records[10];
spinlock_t buf_lock;
struct mutex file_write_mutex;
#define BUF_SIZE (1024 * 1024 * 32)
struct file* file_open(const char* path, int flags, int rights) {
struct file* filp = NULL;
mm_segment_t oldfs;
int err = 0;
// init mutex for write file
// mutex_init(&file_write_mutex);
oldfs = get_fs();
set_fs(KERNEL_DS);
filp = filp_open(path, flags, rights);
set_fs(oldfs);
if (IS_ERR(filp)) {
err = PTR_ERR(filp);
return NULL;
}
return filp;
}
void close_record_file(void) { filp_close(__file_to_record, NULL); }
static void open_record_file(const char* file_name) {
// __file_to_record = filp_open(file_name, O_RDWR | O_CREAT, S_IRWXU);
__file_to_record =
file_open(file_name, O_RDWR | O_CREAT | O_LARGEFILE, S_IRWXU);
}
int file_write(struct file* file, const char* data, unsigned int size) {
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
// set_fs(get_ds());
set_fs(KERNEL_DS);
ret = kernel_write(file, data, size, &file->f_pos);
set_fs(oldfs);
return ret;
}
static void save_to_file(const char* src, const u32 len) {
static unsigned long times = 0;
static long index = -1;
static const unsigned long LIMIT = (1UL << 31) / BUF_SIZE - 1;
// static const unsigned long LIMIT = 3;
times++;
printk("times: %ld, index: %ld", times, index);
if (times >= LIMIT) {
index++;
times = 0;
}
// if (index == -1) {
// printk("write to file: %p",__file_to_record);
file_write(__file_to_record, src, len);
// } else {
// // printk("write to file: %p",__file_to_records[index]);
// file_write(__file_to_records[index], src, len);
// }
}
#endif