change usb control process name to usb-server
[framework/system/system-server.git] / ss_core.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17
18 #include <sysman.h>
19 #include "include/ss_data.h"
20 #include "ss_queue.h"
21 #include "ss_log.h"
22 #include "ss_predefine.h"
23 #include "ss_core.h"
24
25 enum ss_core_cmd_type {
26         SS_CORE_ACT_RUN,
27         SS_CORE_ACT_CLEAR
28 };
29
30 struct _internal_msg {
31         int type;
32         int pid;
33 };
34
35 static int core_pipe[2];
36
37 static int _ss_core_action_run(void *user_data,
38                                struct ss_run_queue_entry *rq_entry)
39 {
40         struct ss_action_entry *act_entry = rq_entry->action_entry;
41         int ret;
42         char tmp[128];
43
44         rq_entry->state = SS_STATE_RUNNING;
45         ret = act_entry->predefine_action(rq_entry->argc, rq_entry->argv);
46         if (ret <= 0) {
47                 if (ret < 0)
48                         PRT_TRACE_ERR("[SYSMAN] predefine action failed");
49                 goto fast_done;
50         } else {
51                 snprintf(tmp, sizeof(tmp), "/proc/%d/status", ret);
52                 if (access(tmp, R_OK) == 0)
53                         rq_entry->forked_pid = ret;
54                 else
55                         goto fast_done;
56         }
57         return 0;
58
59  fast_done:
60         rq_entry->forked_pid = -1;
61         rq_entry->state = SS_STATE_DONE;
62         ss_core_action_clear(-1);
63         return 0;
64 }
65
66 static int core_pipe_cb(void *userdata, Ecore_Fd_Handler * fd_handler)
67 {
68         struct ss_main_data *ad = (struct ss_main_data *)userdata;
69         struct _internal_msg p_msg;
70
71         if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
72                 PRT_TRACE_ERR
73                     ("ecore_main_fd_handler_active_get error , return\n");
74                 return 1;
75         }
76
77         read(core_pipe[0], &p_msg, sizeof(struct _internal_msg));
78
79         switch (p_msg.type) {
80         case SS_CORE_ACT_RUN:
81                 ss_run_queue_run(SS_STATE_INIT, _ss_core_action_run, ad);
82                 break;
83         case SS_CORE_ACT_CLEAR:
84                 ss_run_queue_del_bypid(p_msg.pid);
85                 break;
86         }
87         return 1;
88 }
89
90 int ss_core_action_run()
91 {
92         struct _internal_msg p_msg;
93
94         p_msg.type = SS_CORE_ACT_RUN;
95         p_msg.pid = 0;
96         write(core_pipe[1], &p_msg, sizeof(struct _internal_msg));
97
98         return 0;
99 }
100
101 int ss_core_action_clear(int pid)
102 {
103         struct _internal_msg p_msg;
104
105         p_msg.type = SS_CORE_ACT_CLEAR;
106         p_msg.pid = pid;
107         write(core_pipe[1], &p_msg, sizeof(struct _internal_msg));
108
109         return 0;
110 }
111
112 int ss_core_init(struct ss_main_data *ad)
113 {
114         if (pipe(core_pipe) < 0) {
115                 PRT_TRACE_ERR("pipe cannot create");
116                 exit(1);
117         }
118
119         ecore_main_fd_handler_add(core_pipe[0], ECORE_FD_READ,
120                                   core_pipe_cb, ad, NULL, NULL);
121         return 0;
122 }