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