Merge branch 'kernel' of 106.109.8.71:/srv/git/dbi into kernel
[kernel/swap-modules.git] / parser / us_inst.c
1 #include <linux/module.h>
2 #include <linux/version.h>
3 #include <linux/errno.h>
4 #include <linux/namei.h>
5 #include <us_manager/pf/pf_group.h>
6 #include "msg_parser.h"
7 #include "us_inst.h"
8
9 /* FIXME: create get_dentry() and put_dentry() */
10 static struct dentry *dentry_by_path(const char *path)
11 {
12         struct dentry *dentry;
13 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
14         struct path st_path;
15         if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
16 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
17         struct nameidata nd;
18         if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
19 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
20                 printk("failed to lookup dentry for path %s!\n", path);
21                 return NULL;
22         }
23
24 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
25         dentry = nd.dentry;
26         path_release(&nd);
27 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
28         dentry = nd.path.dentry;
29         path_put(&nd.path);
30 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
31         dentry = st_path.dentry;
32         path_put(&st_path);
33 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
34         return dentry;
35 }
36
37
38 static int mod_func_inst(struct func_inst_data *func, struct pf_group *pfg,
39                          struct dentry *dentry, enum MOD_TYPE mt)
40 {
41         int ret;
42
43         switch (mt) {
44         case MT_ADD:
45                 ret = pf_register_probe(pfg, dentry, func->addr, func->args);
46                 break;
47         case MT_DEL:
48                 ret = pf_unregister_probe(pfg, dentry, func->addr);
49                 break;
50         default:
51                 printk("ERROR: mod_type=0x%x\n", mt);
52                 ret = -EINVAL;
53         }
54
55         return ret;
56 }
57
58 static int mod_lib_inst(struct lib_inst_data *lib, struct pf_group *pfg,
59                         enum MOD_TYPE mt)
60 {
61         int ret = 0, i;
62         struct dentry *dentry;
63
64         dentry = dentry_by_path(lib->path);
65         if (dentry == NULL) {
66                 printk("Cannot get dentry by path %s\n", lib->path);
67                 return -EINVAL;
68         }
69
70         for (i = 0; i < lib->cnt_func; ++i) {
71                 ret = mod_func_inst(lib->func[i], pfg, dentry, mt);
72                 if (ret) {
73                         printk("Cannot mod func inst, ret = %d\n", ret);
74                         return ret;
75                 }
76         }
77
78         return ret;
79 }
80
81 static int get_pfg_by_app_info(struct app_info_data *app_info, struct pf_group **pfg)
82 {
83         struct dentry *dentry;
84
85         dentry = dentry_by_path(app_info->exec_path);
86         if (dentry == NULL)
87                 return -EINVAL;
88
89         switch (app_info->app_type) {
90         case AT_PID:
91                 *pfg = get_pf_group_by_tgid(app_info->tgid, dentry);
92                 break;
93         case AT_TIZEN_NATIVE_APP:
94         case AT_COMMON_EXEC:
95                 *pfg = get_pf_group_by_dentry(dentry, dentry);
96                 break;
97         default:
98                 printk("ERROR: app_type=%0x%x\n", app_info->app_type);
99                 return -EINVAL;
100         }
101
102         return 0;
103 }
104
105 static int mod_us_app_inst(struct app_inst_data *app_inst, enum MOD_TYPE mt)
106 {
107         int ret, i;
108         struct pf_group *pfg;
109         struct dentry *dentry;
110
111         ret = get_pfg_by_app_info(app_inst->app_info, &pfg);
112         if (ret) {
113                 printk("Cannot get pfg by app info, ret = %d\n", ret);
114                 return ret;
115         }
116
117         for (i = 0; i < app_inst->cnt_func; ++i) {
118                 /* TODO: */
119                 dentry = dentry_by_path(app_inst->app_info->exec_path);
120                 if (dentry == NULL) {
121                         printk("Cannot find dentry by path %s\n",
122                                app_inst->app_info->exec_path);
123                         return -EINVAL;
124                 }
125
126                 ret = mod_func_inst(app_inst->func[i], pfg, dentry, mt);
127                 if (ret) {
128                         printk("Cannot mod func inst, ret = \n", ret);
129                         return ret;
130                 }
131         }
132
133         for (i = 0; i < app_inst->cnt_lib; ++i) {
134                 ret = mod_lib_inst(app_inst->lib[i], pfg, mt);
135                 if (ret) {
136                         printk("Cannot mod lib inst, ret = %d\n", ret);
137                         return ret;
138                 }
139         }
140
141         return 0;
142 }
143
144 int mod_us_inst(struct us_inst_data *us_inst, enum MOD_TYPE mt)
145 {
146         u32 i;
147         int ret;
148
149         for (i = 0; i < us_inst->cnt; ++i) {
150                 ret = mod_us_app_inst(us_inst->app_inst[i], mt);
151                 if (ret) {
152                         printk("Cannot mod us app inst, ret = %d\n", ret);
153                         return ret;
154                 }
155         }
156
157         return 0;
158 }