4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #define PREDEFINE_ACT_FUNC_STR "predefine_action"
28 #define IS_ACCESSIBLE_FUNC_STR "is_accessible"
29 #define UI_VIEWABLE_FUNC_STR "ui_viewable"
31 static dd_list *predef_act_list;
32 static dd_list *run_queue;
34 static struct action_entry *find_action_entry(char *type)
37 struct action_entry *data;
39 DD_LIST_FOREACH(predef_act_list, tmp, data) {
40 if (!strcmp(data->type, type))
47 int register_action(char *type,
48 int (*predefine_action) (),
49 int (*ui_viewable) (),
50 int (*is_accessible) (int))
52 struct action_entry *data;
54 data = malloc(sizeof(struct action_entry));
62 if (find_action_entry(type) != NULL)
66 data->predefine_action = predefine_action;
67 if (data->predefine_action == NULL)
70 data->is_accessible = is_accessible;
71 data->ui_viewable = ui_viewable;
72 data->owner_pid = getpid();
73 data->type = strdup(type);
74 data->path = strdup("");
76 DD_LIST_PREPEND(predef_act_list, data);
78 _D("add predefine action entry suceessfully - %s",
82 if (data->type != NULL)
83 _E("adding predefine action entry failed - %s",
89 int register_msg(struct sysnoti *msg)
91 struct action_entry *data;
93 data = malloc(sizeof(struct action_entry));
100 if (find_action_entry(msg->type) != NULL)
103 data->handle = dlopen(msg->path, RTLD_LAZY);
105 _E("cannot find such library");
109 data->predefine_action = dlsym(data->handle, PREDEFINE_ACT_FUNC_STR);
110 if (data->predefine_action == NULL) {
111 _E("cannot find predefine_action symbol : %s",
112 PREDEFINE_ACT_FUNC_STR);
116 data->is_accessible = dlsym(data->handle, IS_ACCESSIBLE_FUNC_STR);
117 data->ui_viewable = dlsym(data->handle, UI_VIEWABLE_FUNC_STR);
118 data->owner_pid = msg->pid;
119 data->type = strdup(msg->type);
120 data->path = strdup(msg->path);
122 DD_LIST_PREPEND(predef_act_list, data);
124 _D("add predefine action entry suceessfully - %s",
128 _E("adding predefine action entry failed - %s", msg->type);
133 int notify_action(char *type, int argc, ...)
136 struct action_entry *data;
141 char *argv[SYSMAN_MAXARG];
143 if (argc > SYSMAN_MAXARG || type == NULL)
146 DD_LIST_FOREACH(predef_act_list, tmp, data) {
147 if (strcmp(data->type, type))
149 va_start(argptr, argc);
150 for (i = 0; i < argc; i++) {
151 args = va_arg(argptr, char *);
153 argv[i] = strdup(args);
158 ret=run_queue_add(data, argc, argv);
159 ret=core_action_run();
166 int notify_msg(struct sysnoti *msg, int sockfd)
169 struct action_entry *data;
172 DD_LIST_FOREACH(predef_act_list, tmp, data) {
173 if (strcmp(data->type, msg->type))
175 if (data->is_accessible != NULL
176 && data->is_accessible(sockfd) == 0) {
177 _E("%d cannot call that predefine module", msg->pid);
180 ret=run_queue_add(data, msg->argc, msg->argv);
181 ret=core_action_run();
185 _E("cannot found action");
189 int run_queue_add(struct action_entry *act_entry, int argc, char **argv)
191 struct run_queue_entry *rq_entry;
194 rq_entry = malloc(sizeof(struct run_queue_entry));
196 if (rq_entry == NULL) {
201 rq_entry->state = STATE_INIT;
202 rq_entry->action_entry = act_entry;
203 rq_entry->forked_pid = 0;
207 rq_entry->argc = argc;
208 for (i = 0; i < argc; i++)
209 rq_entry->argv[i] = argv[i];
212 DD_LIST_PREPEND(run_queue, rq_entry);
217 int run_queue_run(enum run_state state,
218 int (*run_func) (void *, struct run_queue_entry *),
222 struct run_queue_entry *rq_entry;
224 DD_LIST_FOREACH(run_queue, tmp, rq_entry) {
225 if (rq_entry->state == state)
226 run_func(user_data, rq_entry);
232 struct run_queue_entry *run_queue_find_bypid(int pid)
235 struct run_queue_entry *rq_entry;
237 DD_LIST_FOREACH(run_queue, tmp, rq_entry) {
238 if (rq_entry->forked_pid == pid)
245 int run_queue_del(struct run_queue_entry *entry)
248 struct run_queue_entry *rq_entry;
251 DD_LIST_FOREACH(run_queue, tmp, rq_entry) {
252 if (rq_entry == entry) {
253 DD_LIST_REMOVE(run_queue, rq_entry);
254 for (i = 0; i < rq_entry->argc; i++) {
255 if (rq_entry->argv[i])
256 free(rq_entry->argv[i]);
265 int run_queue_del_bypid(int pid)
268 struct run_queue_entry *rq_entry;
271 DD_LIST_FOREACH(run_queue, tmp, rq_entry) {
272 if (rq_entry->forked_pid == pid) {
273 DD_LIST_REMOVE(run_queue, rq_entry);
274 for (i = 0; i < rq_entry->argc; i++) {
275 if (rq_entry->argv[i])
276 free(rq_entry->argv[i]);