taskfs fix up, minor refactoring
[lunaix-os.git] / lunaix-os / kernel / process / task_attr.c
index f90ed35941fdb6878f3ab353c017d652c8cb7dd5..20d256f0b1e4de147bc45dbed398cecd8874738a 100644 (file)
 #include <lunaix/fs/taskfs.h>
+#include <lunaix/process.h>
+
+#define proc(map)   (twimap_data(map, struct proc_info*))
 
 void
-__read_pending_sig(struct twimap* map)
+__task_read_parent(struct twimap* map)
 {
-    struct proc_info* proc = twimap_data(map, struct proc_info*);
-    twimap_printf(map, "%bb", proc->sig_pending);
+    twimap_printf(map, "%d", proc(map)->parent->pid);
 }
 
 void
-__read_masked_sig(struct twimap* map)
+__task_read_created(struct twimap* map)
 {
-    struct proc_info* proc = twimap_data(map, struct proc_info*);
-    twimap_printf(map, "%bb", proc->sig_mask);
+    twimap_printf(map, "%d", proc(map)->created);
 }
 
 void
-__read_parent(struct twimap* map)
+__task_read_pgid(struct twimap* map)
 {
-    struct proc_info* proc = twimap_data(map, struct proc_info*);
-    twimap_printf(map, "%d", proc->parent->pid);
+    twimap_printf(map, "%d", proc(map)->pgid);
 }
 
 void
-__read_ctimestamp(struct twimap* map)
+__task_read_cmdline(struct twimap* map)
 {
-    struct proc_info* proc = twimap_data(map, struct proc_info*);
-    twimap_printf(map, "%d", proc->created);
+    twimap_printf(map, "%s", proc(map)->cmd);
 }
 
-void
-__read_pgid(struct twimap* map)
+static inline void
+__get_protection(struct mm_region* vmr, char* prot_buf)
+{
+    prot_buf[0] = (vmr->attr & REGION_READ) ? 'r' : '-';
+    prot_buf[1] = (vmr->attr & REGION_WRITE) ? 'w' : '-';
+    prot_buf[2] = (vmr->attr & REGION_EXEC) ? 'x' : '-';
+    prot_buf[3] = shared_writable_region(vmr) ? 's' : 'p';
+    prot_buf[4] = 0;
+}
+
+static inline void
+__get_vmr_name(struct mm_region* vmr, char* buf, unsigned int size)
 {
-    struct proc_info* proc = twimap_data(map, struct proc_info*);
-    twimap_printf(map, "%d", proc->pgid);
+    int region_type;
+
+    region_type = (vmr->attr >> 16) & 0xf;
+
+    if (region_type == REGION_TYPE_STACK) {
+        strcpy(buf, "[stack]");
+    }
+
+    else if (region_type == REGION_TYPE_HEAP) {
+        strcpy(buf, "[heap]");
+    }
+
+    else if (vmr->mfile) {
+        vfs_get_path(vmr->mfile->dnode, buf, size, 0);
+    }
+    
+    else {
+        buf[0] = 0;
+    }
 }
 
 void
-__read_children(struct twimap* map)
+__task_read_maps(struct twimap* map)
 {
-    struct proc_info* proc = twimap_index(map, struct proc_info*);
-    if (!proc)
-        return;
-    twimap_printf(map, "%d ", proc->pid);
+    struct llist_header* vmr_;
+    struct mm_region* vmr;
+    unsigned int size;
+
+    vmr_ = twimap_index(map, struct llist_header*);
+    vmr = container_of(vmr_, struct mm_region, head);
+    
+    assert(vmr_);
+
+    char prot[5], name[256];
+    
+    __get_protection(vmr, prot);
+    __get_vmr_name(vmr, name, 256);
+
+    size = vmr->end - vmr->start;
+
+    twimap_printf(map, "%012lx-%012lx %x %s    %s\n", 
+                 vmr->start, vmr->end, size, prot, name);
 }
 
 int
-__next_children(struct twimap* map)
+__task_gonext_maps(struct twimap* map)
 {
-    struct proc_info* proc = twimap_index(map, struct proc_info*);
-    if (!proc)
+    struct proc_mm* mm;
+    struct llist_header* vmr;
+
+    vmr = twimap_index(map, struct llist_header*);
+    mm = vmspace(proc(map));
+
+    if (!vmr)
         return 0;
-    map->index = container_of(proc->siblings.next, struct proc_info, siblings);
-    if (map->index == proc) {
+        
+    map->index = vmr->next;
+    if (map->index == &mm->regions) {
         return 0;
     }
+    
     return 1;
 }
 
 void
-__reset_children(struct twimap* map)
+__task_reset_maps(struct twimap* map)
 {
-    struct proc_info* proc = twimap_data(map, struct proc_info*);
-    if (llist_empty(&proc->children)) {
+    struct proc_mm* mm;
+
+    mm = vmspace(proc(map));
+    if (llist_empty(&mm->regions)) {
         map->index = 0;
         return;
     }
-    map->index = container_of(proc->children.next, struct proc_info, siblings);
+
+    map->index = mm->regions.next;
 }
 
 void
 export_task_attr()
 {
-    struct twimap* map;
-    map = twimap_create(NULL);
-    map->read = __read_pending_sig;
-    taskfs_export_attr("sig_pending", map);
-
-    map = twimap_create(NULL);
-    map->read = __read_masked_sig;
-    taskfs_export_attr("sig_masked", map);
-
-    map = twimap_create(NULL);
-    map->read = __read_parent;
-    taskfs_export_attr("parent", map);
-
-    map = twimap_create(NULL);
-    map->read = __read_ctimestamp;
-    taskfs_export_attr("created", map);
-
-    map = twimap_create(NULL);
-    map->read = __read_pgid;
-    taskfs_export_attr("pgid", map);
-
-    map = twimap_create(NULL);
-    map->read = __read_children;
-    map->go_next = __next_children;
-    map->reset = __reset_children;
-    taskfs_export_attr("children", map);
+    taskfs_export_attr(parent);
+    taskfs_export_attr(created);
+    taskfs_export_attr(pgid);
+    taskfs_export_attr(cmdline);
+    taskfs_export_list_attr(maps);
 }
\ No newline at end of file