Merge branch 'tizen_2.1' into tizen_2.2
[platform/framework/web/data-provider-master.git] / src / main.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <sys/signalfd.h>
24
25 #include <Ecore.h>
26 #include <Ecore_X.h>
27 #include <Evas.h>
28 #include <Ecore_Evas.h>
29 #include <glib.h>
30 #include <glib-object.h>
31 #include <aul.h>
32 #include <vconf.h>
33
34 #include <packet.h>
35 #include <dlog.h>
36
37 #include "slave_life.h"
38 #include "slave_rpc.h"
39 #include "client_life.h"
40 #include "instance.h"
41 #include "buffer_handler.h"
42 #include "script_handler.h"
43 #include "package.h"
44 #include "group.h"
45 #include "dead_monitor.h"
46 #include "conf.h"
47 #include "io.h"
48 #include "xmonitor.h"
49 #include "setting.h"
50 #include "server.h"
51 #include "util.h"
52 #include "debug.h"
53 #include "critical_log.h"
54 #include "event.h"
55 #include "shortcut_service.h"
56 #include "notification_service.h"
57 #include "utility_service.h"
58 #include "badge_service.h"
59
60 #if defined(FLOG)
61 FILE *__file_log_fp;
62 #endif
63
64 static inline int app_create(void)
65 {
66         int ret;
67
68         if (access(SLAVE_LOG_PATH, R_OK|W_OK) != 0) {
69                 if (mkdir(SLAVE_LOG_PATH, 755) < 0)
70                         ErrPrint("Failed to create %s (%s)\n", SLAVE_LOG_PATH, strerror(errno));
71         }
72
73         /*!
74          * \note
75          * Dead signal handler has to be initialized before
76          * initate package or client (slave and client).
77          *
78          * Because while creating slaves for packages.
79          * It could be crashed before complete the initation stage.
80          *
81          * Then the dead callback should be invoked to handle it properly.
82          *
83          * To enable the dead signal handler,
84          * dead_init should be done before other components are initiated.
85          */
86         ret = setting_init();
87         DbgPrint("Setting initialized: %d\n", ret);
88
89         ret = client_init();
90         DbgPrint("Client initialized: %d\n", ret);
91
92         ret = dead_init();
93         DbgPrint("Dead callback is registered: %d\n", ret);
94
95         ret = group_init();
96         DbgPrint("group init: %d\n", ret);
97
98         ret = io_init();
99         DbgPrint("Init I/O: %d\n", ret);
100
101         ret = package_init();
102         DbgPrint("pkgmgr initialized: %d\n", ret);
103
104         instance_init();
105
106         ret = xmonitor_init();
107         DbgPrint("XMonitor init is done: %d\n", ret);
108
109         ret = buffer_handler_init();
110         DbgPrint("Buffer handler init is done: %d\n", ret);
111
112         /*!
113          * \note
114          * After initiate all other sub-systtems,
115          * Enable the server socket.
116          */
117         ret = server_init();
118         DbgPrint("Server initialized: %d\n", ret);
119
120         event_init();
121
122         shortcut_service_init();
123         notification_service_init();
124         badge_service_init();
125         utility_service_init();
126         script_init();
127
128         return 0;
129 }
130
131 static inline int app_terminate(void)
132 {
133         int ret;
134
135         ret = server_fini();
136         DbgPrint("Finalize server: %d\n", ret);
137
138         ret = dead_fini();
139         DbgPrint("dead signal handler finalized: %d\n", ret);
140
141         ret = utility_service_fini();
142         DbgPrint("utility: %d\n", ret);
143
144         ret = badge_service_fini();
145         DbgPrint("badge: %d\n", ret);
146
147         ret = notification_service_fini();
148         DbgPrint("noti: %d\n", ret);
149
150         ret = shortcut_service_fini();
151         DbgPrint("shortcut: %d\n", ret);
152
153         ret = event_fini();
154         DbgPrint("event: %d\n", ret);
155
156         ret = setting_fini();
157         DbgPrint("Finalize setting : %d\n", ret);
158
159         ret = instance_fini();
160         DbgPrint("Finalizing instances: %d\n", ret);
161
162         ret = package_fini();
163         DbgPrint("Finalize package info: %d\n", ret);
164
165         ret = script_fini();
166         DbgPrint("script: %d\n", ret);
167
168         ret = buffer_handler_fini();
169         DbgPrint("buffer handler: %d\n", ret);
170
171         xmonitor_fini();
172
173         client_fini();
174
175         ret = io_fini();
176         DbgPrint("IO finalized: %d\n", ret);
177
178         ret = group_fini();
179         DbgPrint("Group finalized: %d\n", ret);
180
181         DbgPrint("Terminated\n");
182         return 0;
183 }
184
185 static Eina_Bool signal_cb(void *data, Ecore_Fd_Handler *handler)
186 {
187         struct signalfd_siginfo fdsi;
188         ssize_t size;
189         int fd;
190
191         fd = ecore_main_fd_handler_fd_get(handler);
192         if (fd < 0) {
193                 ErrPrint("Unable to get FD\n");
194                 return ECORE_CALLBACK_CANCEL;
195         }
196
197         size = read(fd, &fdsi, sizeof(fdsi));
198         if (size != sizeof(fdsi)) {
199                 ErrPrint("Unable to get siginfo: %s\n", strerror(errno));
200                 return ECORE_CALLBACK_CANCEL;
201         }
202
203         if (fdsi.ssi_signo == SIGTERM) {
204                 int cfd;
205
206                 CRITICAL_LOG("Terminated(SIGTERM)\n");
207
208                 cfd = creat("/tmp/.stop.provider", 0644);
209                 if (cfd < 0 || close(cfd) < 0)
210                         ErrPrint("stop.provider: %s\n", strerror(errno));
211
212                 vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
213                 //exit(0);
214                 ecore_main_loop_quit();
215         } else if (fdsi.ssi_signo == SIGUSR1) {
216                 /*!
217                  * Turn off auto-reactivation
218                  * Terminate all slaves
219                  */
220                 CRITICAL_LOG("USRS1, Deactivate ALL\n");
221                 slave_deactivate_all(0, 1);
222         } else if (fdsi.ssi_signo == SIGUSR2) {
223                 /*!
224                  * Turn on auto-reactivation
225                  * Launch all slaves again
226                  */
227                 CRITICAL_LOG("USR2, Activate ALL\n");
228                 slave_activate_all();
229         } else {
230                 CRITICAL_LOG("Unknown SIG[%d] received\n", fdsi.ssi_signo);
231         }
232
233         return ECORE_CALLBACK_RENEW;
234 }
235
236 int main(int argc, char *argv[])
237 {
238         int ret;
239         sigset_t mask;
240         Ecore_Fd_Handler *signal_handler = NULL;
241
242         /*!
243          * \note
244          * Clear old contents files before start the master provider.
245          */
246         (void)util_unlink_files(ALWAYS_PATH);
247         (void)util_unlink_files(READER_PATH);
248         (void)util_unlink_files(IMAGE_PATH);
249         (void)util_unlink_files(SLAVE_LOG_PATH);
250
251         /*!
252          * How could we care this return values?
253          * Is there any way to print something on the screen?
254          */
255         ret = critical_log_init(util_basename(argv[0]));
256         if (ret < 0)
257                 ErrPrint("Failed to init the critical log\n");
258
259 #if defined(FLOG)
260         __file_log_fp = fopen("/tmp/live.log", "w+t");
261         if (!__file_log_fp)
262                 __file_log_fp = fdopen(1, "w+t");
263 #endif
264         /* appcore_agent_terminate */
265         if (ecore_init() <= 0) {
266                 CRITICAL_LOG("Failed to initiate ecore\n");
267                 critical_log_fini();
268                 return -EFAULT;
269         }
270
271         sigemptyset(&mask);
272
273         ret = sigaddset(&mask, SIGTERM);
274         if (ret < 0)
275                 CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
276
277         ret = sigaddset(&mask, SIGUSR1);
278         if (ret < 0)
279                 CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
280
281         ret = sigaddset(&mask, SIGUSR2);
282         if (ret < 0)
283                 CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
284
285         ret = sigprocmask(SIG_BLOCK, &mask, NULL);
286         if (ret < 0)
287                 CRITICAL_LOG("Failed to mask the SIGTERM: %s\n", strerror(errno));
288
289         ret = signalfd(-1, &mask, 0);
290         if (ret < 0) {
291                 CRITICAL_LOG("Failed to initiate the signalfd: %s\n", strerror(errno));
292         } else {
293                 signal_handler = ecore_main_fd_handler_add(ret, ECORE_FD_READ, signal_cb, NULL, NULL, NULL);
294                 CRITICAL_LOG("Signal handler initiated: %d\n", ret);
295         }
296
297         if (ecore_x_init(NULL) <= 0) {
298                 CRITICAL_LOG("Failed to ecore x init\n");
299                 ecore_shutdown();
300                 critical_log_fini();
301                 return -EFAULT;
302         }
303
304         ecore_app_args_set(argc, (const char **)argv);
305
306         if (evas_init() <= 0) {
307                 CRITICAL_LOG("Failed to init evas return count is below than 0\n");
308                 ecore_x_shutdown();
309                 ecore_shutdown();
310                 critical_log_fini();
311                 return -EFAULT;
312         }
313
314         if (ecore_evas_init() <= 0) {
315                 CRITICAL_LOG("Failed to init ecore_evas\n");
316                 evas_shutdown();
317                 ecore_x_shutdown();
318                 ecore_shutdown();
319                 critical_log_fini();
320                 return -EFAULT;
321         }
322
323         g_type_init();
324
325         conf_loader();
326
327         app_create();
328
329         vconf_set_bool(VCONFKEY_MASTER_STARTED, 1);
330         ecore_main_loop_begin();
331         vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
332
333         app_terminate();
334
335         evas_shutdown();
336         ecore_evas_shutdown();
337
338         ecore_x_shutdown();
339
340         if (signal_handler)
341                 ecore_main_fd_handler_del(signal_handler);
342
343         ecore_shutdown();
344         critical_log_fini();
345
346 #if defined(FLOG)
347         if (__file_log_fp)
348                 fclose(__file_log_fp);
349 #endif
350         return 0;
351 }
352
353 /* End of a file */