1 /* ProcFS - main.c - main functions of the process file system */
2
3 #include "inc.h"
4
5 static void init_hook(void);
6
7 /* The hook functions that will be called by VTreeFS. */
8 static struct fs_hooks hooks = {
9 .init_hook = init_hook,
10 .lookup_hook = lookup_hook,
11 .getdents_hook = getdents_hook,
12 .read_hook = read_hook,
13 .rdlink_hook = rdlink_hook,
14 };
15
16 /*
17 * Construct a tree of static files from a null-terminated array of file
18 * structures, recursively creating directories which have their associated
19 * data point to child file structures.
20 */
21 static void
construct_tree(struct inode * dir,struct file * files)22 construct_tree(struct inode * dir, struct file * files)
23 {
24 struct file *file;
25 struct inode *node;
26 struct inode_stat stat;
27
28 stat.uid = SUPER_USER;
29 stat.gid = SUPER_USER;
30 stat.size = 0;
31 stat.dev = NO_DEV;
32
33 for (file = files; file->name != NULL; file++) {
34 stat.mode = file->mode;
35
36 node = add_inode(dir, file->name, NO_INDEX, &stat, (index_t)0,
37 (cbdata_t)file->data);
38
39 assert(node != NULL);
40
41 if (S_ISDIR(file->mode))
42 construct_tree(node, (struct file *)file->data);
43 }
44 }
45
46 /*
47 * Initialization hook. Generate the static part of the tree.
48 */
49 static void
init_hook(void)50 init_hook(void)
51 {
52 static int first_time = TRUE;
53 struct inode *root;
54 int r;
55
56 if (first_time) {
57 /*
58 * Initialize some state. If we are incompatible with the kernel,
59 * exit immediately.
60 */
61 if ((r = init_tree()) != OK)
62 panic("init_tree failed!");
63
64 root = get_root_inode();
65
66 construct_tree(root, root_files);
67
68 service_init();
69
70 first_time = FALSE;
71 }
72 }
73
74 /*
75 * ProcFS entry point.
76 */
main(void)77 int main(void)
78 {
79 static struct inode_stat stat;
80
81 /* Properties of the root directory. */
82 stat.mode = DIR_ALL_MODE;
83 stat.uid = SUPER_USER;
84 stat.gid = SUPER_USER;
85 stat.size = 0;
86 stat.dev = NO_DEV;
87
88 /* Run VTreeFS. */
89 run_vtreefs(&hooks, NR_INODES, 0, &stat, NR_PROCS + NR_TASKS,
90 BUF_SIZE);
91
92 return 0;
93 }
94