feat: taskfs for export process to filesystem
[lunaix-os.git] / lunaix-os / kernel / process / task_attr.c
1 #include <lunaix/fs/taskfs.h>
2
3 void
4 __read_pending_sig(struct twimap* map)
5 {
6     struct proc_info* proc = twimap_data(map, struct proc_info*);
7     twimap_printf(map, "%bb", proc->sig_pending);
8 }
9
10 void
11 __read_masked_sig(struct twimap* map)
12 {
13     struct proc_info* proc = twimap_data(map, struct proc_info*);
14     twimap_printf(map, "%bb", proc->sig_mask);
15 }
16
17 void
18 __read_parent(struct twimap* map)
19 {
20     struct proc_info* proc = twimap_data(map, struct proc_info*);
21     twimap_printf(map, "%d", proc->parent->pid);
22 }
23
24 void
25 __read_ctimestamp(struct twimap* map)
26 {
27     struct proc_info* proc = twimap_data(map, struct proc_info*);
28     twimap_printf(map, "%d", proc->created);
29 }
30
31 void
32 __read_pgid(struct twimap* map)
33 {
34     struct proc_info* proc = twimap_data(map, struct proc_info*);
35     twimap_printf(map, "%d", proc->pgid);
36 }
37
38 void
39 __read_children(struct twimap* map)
40 {
41     struct proc_info* proc = twimap_index(map, struct proc_info*);
42     if (!proc)
43         return;
44     twimap_printf(map, "%d ", proc->pid);
45 }
46
47 int
48 __next_children(struct twimap* map)
49 {
50     struct proc_info* proc = twimap_index(map, struct proc_info*);
51     if (!proc)
52         return 0;
53     map->index = container_of(proc->siblings.next, struct proc_info, siblings);
54     if (map->index == proc) {
55         return 0;
56     }
57     return 1;
58 }
59
60 void
61 __reset_children(struct twimap* map)
62 {
63     struct proc_info* proc = twimap_data(map, struct proc_info*);
64     if (llist_empty(&proc->children)) {
65         map->index = 0;
66         return;
67     }
68     map->index = container_of(proc->children.next, struct proc_info, siblings);
69 }
70
71 void
72 export_task_attr()
73 {
74     struct twimap* map;
75     map = twimap_create(NULL);
76     map->read = __read_pending_sig;
77     taskfs_export_attr("sig_pending", map);
78
79     map = twimap_create(NULL);
80     map->read = __read_masked_sig;
81     taskfs_export_attr("sig_masked", map);
82
83     map = twimap_create(NULL);
84     map->read = __read_parent;
85     taskfs_export_attr("parent", map);
86
87     map = twimap_create(NULL);
88     map->read = __read_ctimestamp;
89     taskfs_export_attr("created", map);
90
91     map = twimap_create(NULL);
92     map->read = __read_pgid;
93     taskfs_export_attr("pgid", map);
94
95     map = twimap_create(NULL);
96     map->read = __read_children;
97     map->go_next = __next_children;
98     map->reset = __reset_children;
99     taskfs_export_attr("children", map);
100 }