Initialize Tizen 2.3
[framework/appfw/aul-1.git] / am_daemon / amd_main.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/inotify.h>
26 #include <fcntl.h>
27 #include <Ecore_X.h>
28 #include <Ecore_Input.h>
29 #include <utilX.h>
30 #include <Ecore.h>
31 #include <Evas.h>
32 #include <aul.h>
33 #include <vconf.h>
34 #include <ail.h>
35 #include <glib.h>
36
37 #include "amd_config.h"
38 #include "simple_util.h"
39 #include "aul_util.h"
40 #include "amd_appinfo.h"
41 #include "amd_cgutil.h"
42 #include "amd_key.h"
43 #include "amd_status.h"
44 #include "amd_launch.h"
45 #include "amd_request.h"
46
47 #ifndef MOUNT_PATH
48 #  define MOUNT_PATH "/sys/fs/cgroup"
49 #endif
50
51 #ifndef AGENT_PATH
52 #  define AGENT_PATH "/usr/bin/daemon-manager-release-agent"
53 #endif
54
55 #define WINDOW_READY    "/tmp/.wm_ready"
56
57 typedef struct _window_watch {
58         int watch_fd;
59         int win_watch_wd;
60         Ecore_Fd_Handler *win_watch_ewd;
61 } _window_watch_t;
62 static _window_watch_t *win_info_t = NULL;
63
64 static int __app_dead_handler(int pid, void *data);
65 static int __init();
66
67 extern int _status_init(struct amdmgr* amd);
68
69 static int __app_dead_handler(int pid, void *data)
70 {
71         _unregister_key_event(pid);
72         _status_remove_app_info_list(pid);
73         return 0;
74 }
75
76 static void __start_cb(void *user_data,
77                 const char *filename, const struct appinfo *ai)
78 {
79         /*struct amdmgr *amd = user_data;*/
80         const char *componet = NULL;
81         int r;
82
83         componet = appinfo_get_value(ai, AIT_COMPTYPE);
84
85         r = appinfo_get_boolean(ai, AIT_ONBOOT);
86
87         if (r == 1 && strncmp(componet, "svcapp", 6) == 0)
88         {
89                 const char *pkgid = appinfo_get_value(ai, AIT_PKGID);
90                 _D("start service - %s", pkgid);
91
92                 _start_srv(ai, NULL);
93         }
94 }
95
96 static void _start_services(struct amdmgr *amd)
97 {
98         appinfo_foreach(amd->af, __start_cb, amd);
99 }
100
101 static int __booting_done_handler(int pid, void *data)
102 {
103         _start_services((struct amdmgr*)data);
104
105         return 0;
106 }
107
108 static gboolean _check_window_ready(void)
109 {
110         if (access(WINDOW_READY, R_OK) == 0)
111                 return true;
112         else
113                 return false;
114 }
115
116 static void __window_init(void)
117 {
118         ecore_x_init(NULL);
119         _set_atom_effect();
120 #ifndef __i386__
121         _key_init();
122 #endif
123 }
124
125 static Eina_Bool _window_cb(void *data, Ecore_Fd_Handler * fd_handler)
126 {
127         int fd;
128         char buf[FILENAME_MAX];
129         ssize_t len = 0;
130         struct inotify_event* event;
131
132         if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR)) {
133                 _E("An error has occurred. Stop watching this fd and quit");
134                 return ECORE_CALLBACK_CANCEL;
135         }
136
137         fd = ecore_main_fd_handler_fd_get(fd_handler);
138         if(fd < 0) {
139                 _E("ecore_main_fd_handler_fd_get error");
140                 return ECORE_CALLBACK_CANCEL;
141         }
142         len = read(fd, buf, FILENAME_MAX);
143
144         event = (struct inotify_event*) &buf[0];
145
146         _D("filename : %s", event->name);
147
148         if (access(WINDOW_READY, R_OK) == 0) {
149                 __window_init();
150                 if (win_info_t) {
151                         ecore_main_fd_handler_del(win_info_t->win_watch_ewd);
152                         inotify_rm_watch(win_info_t->watch_fd, win_info_t->win_watch_wd);
153                         free(win_info_t);
154                         win_info_t = NULL;
155                 }
156         }
157
158         return ECORE_CALLBACK_RENEW;
159 }
160
161 static void _register_window_init(void)
162 {
163         win_info_t = malloc(sizeof(_window_watch_t));
164         if (!win_info_t) {
165                 _E("Unable to allocate memory. don't init widow\n");
166                 return;
167         }
168         win_info_t->watch_fd = inotify_init();
169         win_info_t->win_watch_wd = inotify_add_watch(win_info_t->watch_fd, "/tmp", IN_CREATE);
170         win_info_t->win_watch_ewd = ecore_main_fd_handler_add(win_info_t->watch_fd,
171                                                     ECORE_FD_READ, _window_cb, NULL, NULL, NULL);
172 }
173
174 static void _window_init(void)
175 {
176         if (_check_window_ready())
177                 __window_init();
178         else
179                 _register_window_init();
180 }
181
182 static int __init()
183 {
184         struct amdmgr amd;
185
186         ecore_init();
187         evas_init();
188         ecore_event_init();
189
190         appinfo_init(&amd.af);
191         cgutil_create(MOUNT_PATH, AGENT_PATH, &amd.cg);
192         _requset_init(&amd);
193         _launch_init(&amd);
194         _status_init(&amd);
195         _window_init();
196
197         aul_listen_app_dead_signal(__app_dead_handler, NULL);
198
199         aul_listen_booting_done_signal(__booting_done_handler, &amd);
200
201         return 0;
202 }
203
204 gboolean  __amd_ready(gpointer user_data)
205 {
206         int handle;
207
208         handle = creat("/tmp/amd_ready", S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
209         if (handle != -1)
210                 close(handle);
211
212         return FALSE;
213 }
214
215 int main(int argc, char *argv[])
216 {
217         if (__init() != 0){
218                 _E("AMD Initialization failed!\n");
219                 return -1;
220         }
221
222         g_idle_add(__amd_ready, NULL);
223
224         ecore_main_loop_begin();
225
226         return 0;
227 }