taskfs fix up, minor refactoring
[lunaix-os.git] / lunaix-os / kernel / process / task_attr.c
1 #include <lunaix/fs/taskfs.h>
2 #include <lunaix/process.h>
3
4 #define proc(map)   (twimap_data(map, struct proc_info*))
5
6 void
7 __task_read_parent(struct twimap* map)
8 {
9     twimap_printf(map, "%d", proc(map)->parent->pid);
10 }
11
12 void
13 __task_read_created(struct twimap* map)
14 {
15     twimap_printf(map, "%d", proc(map)->created);
16 }
17
18 void
19 __task_read_pgid(struct twimap* map)
20 {
21     twimap_printf(map, "%d", proc(map)->pgid);
22 }
23
24 void
25 __task_read_cmdline(struct twimap* map)
26 {
27     twimap_printf(map, "%s", proc(map)->cmd);
28 }
29
30 static inline void
31 __get_protection(struct mm_region* vmr, char* prot_buf)
32 {
33     prot_buf[0] = (vmr->attr & REGION_READ) ? 'r' : '-';
34     prot_buf[1] = (vmr->attr & REGION_WRITE) ? 'w' : '-';
35     prot_buf[2] = (vmr->attr & REGION_EXEC) ? 'x' : '-';
36     prot_buf[3] = shared_writable_region(vmr) ? 's' : 'p';
37     prot_buf[4] = 0;
38 }
39
40 static inline void
41 __get_vmr_name(struct mm_region* vmr, char* buf, unsigned int size)
42 {
43     int region_type;
44
45     region_type = (vmr->attr >> 16) & 0xf;
46
47     if (region_type == REGION_TYPE_STACK) {
48         strcpy(buf, "[stack]");
49     }
50
51     else if (region_type == REGION_TYPE_HEAP) {
52         strcpy(buf, "[heap]");
53     }
54
55     else if (vmr->mfile) {
56         vfs_get_path(vmr->mfile->dnode, buf, size, 0);
57     }
58     
59     else {
60         buf[0] = 0;
61     }
62 }
63
64 void
65 __task_read_maps(struct twimap* map)
66 {
67     struct llist_header* vmr_;
68     struct mm_region* vmr;
69     unsigned int size;
70
71     vmr_ = twimap_index(map, struct llist_header*);
72     vmr = container_of(vmr_, struct mm_region, head);
73     
74     assert(vmr_);
75
76     char prot[5], name[256];
77     
78     __get_protection(vmr, prot);
79     __get_vmr_name(vmr, name, 256);
80
81     size = vmr->end - vmr->start;
82
83     twimap_printf(map, "%012lx-%012lx %x %s    %s\n", 
84                  vmr->start, vmr->end, size, prot, name);
85 }
86
87 int
88 __task_gonext_maps(struct twimap* map)
89 {
90     struct proc_mm* mm;
91     struct llist_header* vmr;
92
93     vmr = twimap_index(map, struct llist_header*);
94     mm = vmspace(proc(map));
95
96     if (!vmr)
97         return 0;
98         
99     map->index = vmr->next;
100     if (map->index == &mm->regions) {
101         return 0;
102     }
103     
104     return 1;
105 }
106
107 void
108 __task_reset_maps(struct twimap* map)
109 {
110     struct proc_mm* mm;
111
112     mm = vmspace(proc(map));
113     if (llist_empty(&mm->regions)) {
114         map->index = 0;
115         return;
116     }
117
118     map->index = mm->regions.next;
119 }
120
121 void
122 export_task_attr()
123 {
124     taskfs_export_attr(parent);
125     taskfs_export_attr(created);
126     taskfs_export_attr(pgid);
127     taskfs_export_attr(cmdline);
128     taskfs_export_list_attr(maps);
129 }