-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocmonitor.c
64 lines (48 loc) · 1.44 KB
/
procmonitor.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
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/cred.h>
static struct timer_list procmonitor_timer;
static void procmonitor_check_proc_tree(unsigned long unused)
{
int ret;
struct task_struct *task;
/* Traversing all tasks */
for_each_process(task)
printk(KERN_INFO "process: %s, PID: %d\n", task->comm, task->pid);
/* Update the expiration time so that the callback got called again */
ret = mod_timer(&procmonitor_timer, jiffies + msecs_to_jiffies(2000));
if (ret)
printk(KERN_INFO "Error when setting timer\n");
}
static int __init procmonitor_init(void)
{
int ret;
printk(KERN_INFO "Starting module.\n");
/* Setting up our timer */
setup_timer(&procmonitor_timer, procmonitor_check_proc_tree, 0);
ret = mod_timer(&procmonitor_timer, jiffies + msecs_to_jiffies(200));
if (ret) {
printk(KERN_INFO "Error when setting timer\n");
return -1;
}
return 0;
}
static void __exit procmonitor_exit(void)
{
int ret;
/*
* This function only differs from del_timer on SMP: besides deactivating
* the timer it also makes sure the handler has finished executing on
* other CPUs.
*/
ret = del_timer_sync(&procmonitor_timer);
if (ret)
printk("Error when removing timer\n");
printk(KERN_INFO "Cleaning up module.\n");
}
module_init(procmonitor_init);
module_exit(procmonitor_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kernel BR team");
MODULE_DESCRIPTION("An example of Process Monitor");