-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_process.c
84 lines (74 loc) · 2.23 KB
/
list_process.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
#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/sched/task.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Preethi");
MODULE_DESCRIPTION("Linux Kernel Module for listing all processes");
char* get_state_string(long state)
{
switch (state)
{
case TASK_RUNNING:
return "Running";
case TASK_INTERRUPTIBLE:
return "Sleeping (Interruptible)";
case TASK_UNINTERRUPTIBLE:
return "Sleeping (Uninterruptible)";
case __TASK_STOPPED:
return "Stopped";
case __TASK_TRACED:
return "Tracing";
case TASK_DEAD:
return "Dead(Zombie)";
case TASK_WAKEKILL:
return "Wakekill";
case TASK_WAKING:
return "Waking";
case TASK_PARKED:
return "Parked";
case TASK_NOLOAD:
return "Noload";
case TASK_NEW:
return "New";
case 1026://1024 OR 2
return "New (Interruptible)";
case 1028://1024 OR 4
return "New (Uninterruptible)";
default:
return "Unknown";
}
}
void fix_display(int i,int count) //implementing a for loop as recursion cause for not available in kernel modules
{
if(i!=count)
{
printk(KERN_CONT "| ");
fix_display(i+1,count);
}
}
void traverse_process(struct task_struct *process,int count)
{
struct task_struct *next_process;
struct list_head *list;
fix_display(0,count);
printk(KERN_CONT "|_%s(PID:%d, PPID:%d) ---> %s\n",process->comm,process->pid,(process->parent)->pid,get_state_string(process->__state));
list_for_each(list, &process->children)
{
next_process=list_entry(list,struct task_struct,sibling);
traverse_process(next_process,count+1);
}
}
static int __init InitModule(void)
{
printk("Loading Kernel Module\n");
printk("Process name, PID, PPID, State\n");
traverse_process(&init_task,0);
return 0;
}
static void __exit ExitModule(void)
{
printk("Removing Kernel Module\n");
}
module_init(InitModule);
module_exit(ExitModule);