-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessMonitor.c
More file actions
61 lines (49 loc) · 1.35 KB
/
processMonitor.c
File metadata and controls
61 lines (49 loc) · 1.35 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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched/signal.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("RYAN WELZEL");
MODULE_DESCRIPTION("Process monitor module");
MODULE_VERSION("1.0");
#define PROC_NAME "processMonitor"
// Writes process info to /proc/ProcessMonitor
// seq_file writes to /proc
static int procmon_show(struct seq_file *m, void *v){
// Process struct
struct task_struct *task;
// Header row for column names
seq_printf(m, "PID\tPPID\tSTATE\tNAME\n");
// Disp pid, parent pid, state, name
for_each_process(task){
seq_printf(m, "%d\t%d\t%u\t%s\n",
task->pid,
task_ppid_nr(task),
READ_ONCE(task->__state),
task->comm);
}
return 0;
}
// Set up seq_file context
static int procmon_open(struct inode *inode, struct file *file){
return single_open(file, procmon_show, NULL);
}
// On cat /proc/ProcessMonitor call procmon_open
// Other params default
static const struct proc_ops proc_file_ops = {
.proc_open = procmon_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
};
static int __init procmon_init(void){
proc_create(PROC_NAME, 0, NULL, &proc_file_ops);
return 0;
}
static void __exit procmon_exit(void){
remove_proc_entry(PROC_NAME, NULL);
}
module_init(procmon_init);
module_exit(procmon_exit);