Resolve Dbus Poweroff Issue
[apps/native/starter.git] / src / mobile / starter.c
1 /*
2  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <Elementary.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <malloc.h>
26 #include <Ecore_Wl2.h>
27
28 #include <E_DBus.h>
29 #include <systemd/sd-daemon.h>
30 #include <ode/internal-encryption.h>
31
32 #include <aul.h>
33 #include <vconf.h>
34 #include <signal.h>
35
36 #include "starter.h"
37 #include "lock_mgr.h"
38 #include "home_mgr.h"
39 #include "hw_key.h"
40 #include "process_mgr.h"
41 #include "util.h"
42 #include "status.h"
43 #include "hw_key.h"
44 #include "dbus_util.h"
45
46 static void _set_starter_sequence(int val)
47 {
48         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, val);
49 }
50
51
52
53 static void _signal_handler(int signum, siginfo_t *info, void *unused)
54 {
55         _D("_signal_handler : Terminated...");
56         elm_exit();
57 }
58
59
60
61 static int _power_off_cb(status_active_key_e key, void *data)
62 {
63         int val = status_active_get()->sysman_power_off_status;
64
65         if (val > VCONFKEY_SYSMAN_POWER_OFF_NONE) {
66             _D("_power_off_cb : Terminated...");
67             elm_exit();
68         }
69
70         return 1;
71 }
72
73
74
75 static void _language_changed_cb(keynode_t *node, void *data)
76 {
77         char *lang = NULL;
78
79         ret_if(!node);
80
81         lang = vconf_keynode_get_str(node);
82         ret_if(!lang);
83
84         _D("language is changed : %s", lang);
85
86         elm_language_set(lang);
87 }
88
89
90
91 static int _set_i18n(const char *domain, const char *dir)
92 {
93         char *r = NULL;
94
95         if (domain == NULL) {
96                 errno = EINVAL;
97                 return -1;
98         }
99
100         char *lang = vconf_get_str(VCONFKEY_LANGSET);
101         r = setlocale(LC_ALL, lang);
102         if (!r) {
103                 _E("setlocale() error");
104         }
105         if (lang) {
106                 free(lang);
107         }
108
109         r = bindtextdomain(domain, dir);
110         if (!r) {
111                 _E("bindtextdomain() error");
112         }
113
114         r = textdomain(domain);
115         if (!r) {
116                 _E("textdomain() error");
117         }
118
119         if (vconf_notify_key_changed(VCONFKEY_LANGSET, _language_changed_cb, NULL) < 0) {
120                 _E("Failed to register changed cb : %s", VCONFKEY_LANGSET);
121         }
122
123         return 0;
124 }
125
126
127
128 static int _check_dead_signal(int pid, void *data)
129 {
130         int home_pid = 0;
131         int volume_pid = 0;
132         int indicator_pid = 0;
133         int quickpanel_pid = 0;
134         int lock_pid = 0;
135
136         _D("Process %d is termianted", pid);
137
138         if (pid < 0) {
139                 _E("pid : %d", pid);
140                 return 0;
141         }
142
143         /*
144          * starter try to re-launch these apps when the app is dead.
145          */
146         home_pid = home_mgr_get_home_pid();
147         volume_pid = home_mgr_get_volume_pid();
148         indicator_pid = home_mgr_get_indicator_pid();
149         quickpanel_pid = home_mgr_get_quickpanel_pid();
150         lock_pid = lock_mgr_get_lock_pid();
151
152         if (pid == home_pid) {
153                 _D("Homescreen is dead");
154                 home_mgr_relaunch_homescreen();
155         } else if (pid == volume_pid) {
156                 _D("volume is dead");
157                 home_mgr_relaunch_volume();
158         } else if (pid == indicator_pid) {
159                 _D("indicator is dead");
160                 home_mgr_relaunch_indicator();
161         } else if (pid == quickpanel_pid) {
162                 _D("quickpanel is dead");
163                 home_mgr_relaunch_quickpanel();
164         } else if (pid == lock_pid) {
165                 _D("lockscreen is dead");
166                 lock_mgr_unlock();
167         } else {
168                 _D("Unknown process, ignore it");
169         }
170
171         return 0;
172 }
173
174
175
176 static void _launch_apps(void)
177 {
178         /* Tells the service manager that service startup is finished */
179         sd_notify(0, "READY=1");
180
181         /*
182          * After user data partition mount,
183          * launch lockscreen, homescreen, etc.
184          */
185         lock_mgr_init();
186         home_mgr_init(NULL);
187
188         /*
189          * Set the starter sequence vconfkey.
190          * '1' menas that booting seqeunce is done.
191          */
192         _set_starter_sequence(1);
193 }
194
195
196
197 static void _init(void)
198 {
199         struct sigaction act;
200         char err_buf[128] = { 0, };
201         int ret = 0;
202
203         memset(&act, 0x00, sizeof(struct sigaction));
204         act.sa_sigaction = _signal_handler;
205         act.sa_flags = SA_SIGINFO;
206
207         ret = sigemptyset(&act.sa_mask);
208         if (ret < 0) {
209                 if (strerror_r(errno, err_buf, sizeof(err_buf)) == 0) {
210                         _E("Failed to sigemptyset[%d / %s]", errno, err_buf);
211                 }
212         }
213         ret = sigaddset(&act.sa_mask, SIGTERM);
214         if (ret < 0) {
215                 if (strerror_r(errno, err_buf, sizeof(err_buf)) == 0) {
216                         _E("Failed to sigaddset[%d / %s]", errno, err_buf);
217                 }
218         }
219         ret = sigaction(SIGTERM, &act, NULL);
220         if (ret < 0) {
221                 if (strerror_r(errno, err_buf, sizeof(err_buf)) == 0) {
222                         _E("Failed to sigaction[%d / %s]", errno, err_buf);
223                 }
224         }
225
226         _set_i18n(PACKAGE, LOCALEDIR);
227
228         status_register();
229         status_active_register_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb, NULL);
230
231         e_dbus_init();
232
233         hw_key_create_window();
234
235         /*
236          * Initialize starter sequence vconfkey.
237          */
238         _set_starter_sequence(0);
239
240         ret = starter_execute_ode_process(BEFORE_LOCKSCREEN);
241         if (!ret) {
242                 _E("Failed to execute ode process");
243         }
244
245         aul_listen_app_dead_signal(_check_dead_signal, NULL);
246 }
247
248
249
250 static void _fini(void)
251 {
252         home_mgr_fini();
253         lock_mgr_fini();
254
255         hw_key_destroy_window();
256
257         e_dbus_shutdown();
258
259         status_active_unregister_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb);
260         status_unregister();
261
262         if (vconf_ignore_key_changed(VCONFKEY_LANGSET, _language_changed_cb) < 0) {
263                 _E("Failed to unregister changed cb : %s", VCONFKEY_LANGSET);
264         }
265 }
266
267
268
269 int main(int argc, char *argv[])
270 {
271         int ret = 0;
272         Ecore_Wl2_Display *display = NULL;
273         struct wl_display *displ = NULL;
274
275         _D("starter is launched..!!");
276
277         ret = elm_init(argc, argv);
278         if (!ret) {
279                 _E("elm_init() failed : %d", ret);
280                 return -1;
281         }
282
283         ret = ecore_wl2_init();
284         if (!ret) {
285                 _E("ecore_wl2_init() failed : %d", ret);
286                 elm_shutdown();
287                 return -1;
288         }
289
290         displ = ecore_wl2_display_get(NULL);
291         if (!displ) {
292                 _W("There's no display");
293                 display = ecore_wl2_display_connect(NULL);
294                 if (!display) {
295                         _E("Failed to connect display");
296                         return -1;
297                 }
298         }
299
300         _init();
301
302         malloc_trim(0);
303         elm_run();
304
305         _fini();
306
307         if (display) {
308                 ecore_wl2_display_disconnect(display);
309                 ecore_wl2_shutdown();
310         }
311
312         elm_shutdown();
313
314         return 0;
315 }
316
317
318
319 static void _mount_complete_cb(void *user_data)
320 {
321         _D("Mount is successfully completed");
322 }
323
324
325
326 int starter_execute_ode_process(int booting_state)
327 {
328         _D("This call is before or after Lockscreen: %d", booting_state);
329         int ret = ODE_ERROR_NONE;
330         int ode_state = 0;
331
332         ret = ode_internal_encryption_get_state(&ode_state);
333         if (ret != ODE_ERROR_NONE) {
334                 _E("Failed to get ODE state, ret: %d", ret);
335                 return 0;
336         }
337
338         if (booting_state == BEFORE_LOCKSCREEN) {
339                 if (ode_state == ODE_STATE_ENCRYPTED) {
340                         lock_mgr_init();
341
342                         ret = ode_internal_encryption_set_mount_event_cb(_mount_complete_cb, NULL);
343                         if (ret != ODE_ERROR_NONE) {
344                                 _E("Failed to set mount event cb");
345                         }
346                 } else {
347                         _launch_apps();
348                 }
349         } else if (booting_state == AFTER_LOCKSCREEN) {
350                 if (ode_state == ODE_STATE_ENCRYPTED) {
351                         _D("ODE state is: %d, and we should mount at this time", ode_state);
352
353                         ret = ode_internal_encryption_mount();
354                         if (ret != ODE_ERROR_NONE) {
355                                 _E("Failed to mount");
356                                 return 0;
357                         }
358
359                         sd_notify(0, "READY=1");
360                         home_mgr_init(NULL);
361
362                         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
363                 } else {
364                         _D("ODE state is: %d, Do nothing", ode_state);
365                 }
366         }
367
368         return 1;
369 }