[REFACTOR] us_manager: install helper probes
[kernel/swap-modules.git] / writer / event_filter.c
1 /*
2  *  SWAP kernel features
3  *  writer/event_filter.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
22  *
23  */
24
25
26 #include <linux/module.h>
27 #include <linux/list.h>
28 #include "event_filter.h"
29
30
31 static LIST_HEAD(filter_list);
32
33 static int func_none(struct task_struct *task)
34 {
35         return 1;
36 }
37
38 static struct ev_filter filter_none = {
39         .name = "all",
40         .filter = func_none
41 };
42
43 static struct ev_filter *filter_current = &filter_none;
44
45 int check_event(struct task_struct *task)
46 {
47         return filter_current->filter(task);
48 }
49
50 static struct ev_filter *event_filter_find(const char *name)
51 {
52         struct ev_filter *f, *tmp;
53
54         list_for_each_entry_safe(f, tmp, &filter_list, list) {
55                 if (strcmp(f->name, name) == 0)
56                         return f;
57         }
58
59         return NULL;
60 }
61
62 int event_filter_register(struct ev_filter *f)
63 {
64         if (event_filter_find(f->name))
65                 return -EINVAL;
66
67         INIT_LIST_HEAD(&f->list);
68         list_add(&f->list, &filter_list);
69
70         return 0;
71 }
72 EXPORT_SYMBOL_GPL(event_filter_register);
73
74 void event_filter_unregister(struct ev_filter *f)
75 {
76         struct ev_filter *filter, *tmp;
77
78         if (filter_current == f)
79                 filter_current = &filter_none;
80
81         list_for_each_entry_safe(filter, tmp, &filter_list, list) {
82                 if (filter == f) {
83                         list_del(&filter->list);
84                         break;
85                 }
86         }
87 }
88 EXPORT_SYMBOL_GPL(event_filter_unregister);
89
90 int event_filter_set(const char *name)
91 {
92         struct ev_filter *f;
93
94         f = event_filter_find(name);
95         if (f == NULL)
96                 return -EINVAL;
97
98         filter_current = f;
99
100         return 0;
101 }
102 EXPORT_SYMBOL_GPL(event_filter_set);
103
104 const char *event_filter_get(void)
105 {
106         return filter_current->name;
107 }
108
109 void event_filter_on_each(void (*func)(struct ev_filter *, void *),
110                           void *data)
111 {
112         struct ev_filter *f, *tmp;
113
114         list_for_each_entry_safe(f, tmp, &filter_list, list)
115                 func(f, data);
116 }
117
118 int event_filter_init(void)
119 {
120         return event_filter_register(&filter_none);
121 }
122
123 void event_filter_exit(void)
124 {
125         event_filter_unregister(&filter_none);;
126 }