Various patches are applied
[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
24 #include <Ecore.h>
25 #include <Ecore_X.h>
26 #include <Evas.h>
27 #include <Ecore_Evas.h>
28 #include <glib.h>
29 #include <glib-object.h>
30 #include <aul.h>
31 #include <vconf.h>
32
33 #include <packet.h>
34 #include <dlog.h>
35
36 #include "slave_life.h"
37 #include "slave_rpc.h"
38 #include "client_life.h"
39 #include "instance.h"
40 #include "buffer_handler.h"
41 #include "script_handler.h"
42 #include "package.h"
43 #include "group.h"
44 #include "dead_monitor.h"
45 #include "conf.h"
46 #include "io.h"
47 #include "xmonitor.h"
48 #include "setting.h"
49 #include "server.h"
50 #include "util.h"
51 #include "debug.h"
52 #include "critical_log.h"
53 #include "event.h"
54 #include "shortcut_service.h"
55 #include "notification_service.h"
56 #include "badge_service.h"
57
58 #if defined(FLOG)
59 FILE *__file_log_fp;
60 #endif
61
62 static inline int app_create(void)
63 {
64         int ret;
65
66         if (access(SLAVE_LOG_PATH, R_OK|W_OK) != 0) {
67                 if (mkdir(SLAVE_LOG_PATH, 755) < 0)
68                         ErrPrint("Failed to create %s (%s)\n", SLAVE_LOG_PATH, strerror(errno));
69         }
70
71         /*!
72          * \note
73          * Dead signal handler has to be initialized before
74          * initate package or client (slave and client).
75          *
76          * Because while creating slaves for packages.
77          * It could be crashed before complete the initation stage.
78          *
79          * Then the dead callback should be invoked to handle it properly.
80          *
81          * To enable the dead signal handler,
82          * dead_init should be done before other components are initiated.
83          */
84         ret = setting_init();
85         DbgPrint("Setting initialized: %d\n", ret);
86
87         ret = client_init();
88         DbgPrint("Client initialized: %d\n", ret);
89
90         ret = dead_init();
91         DbgPrint("Dead callback is registered: %d\n", ret);
92
93         ret = group_init();
94         DbgPrint("group init: %d\n", ret);
95
96         ret = io_init();
97         DbgPrint("Init I/O: %d\n", ret);
98
99         ret = package_init();
100         DbgPrint("pkgmgr initialized: %d\n", ret);
101
102         instance_init();
103
104         ret = xmonitor_init();
105         DbgPrint("XMonitor init is done: %d\n", ret);
106
107         ret = buffer_handler_init();
108         DbgPrint("Buffer handler init is done: %d\n", ret);
109
110         /*!
111          * \note
112          * After initiate all other sub-systtems,
113          * Enable the server socket.
114          */
115         ret = server_init();
116         DbgPrint("Server initialized: %d\n", ret);
117
118         event_init();
119         return 0;
120 }
121
122 static inline int app_terminate(void)
123 {
124         int ret;
125
126         event_fini();
127
128         ret = setting_fini();
129         DbgPrint("Finalize setting : %d\n", ret);
130
131         xmonitor_fini();
132
133         instance_fini();
134
135         ret = package_fini();
136         DbgPrint("Finalize package info: %d\n", ret);
137
138         client_fini();
139
140         ret = server_fini();
141         DbgPrint("Finalize dbus: %d\n", ret);
142
143         ret = dead_fini();
144         DbgPrint("dead signal handler finalized: %d\n", ret);
145
146         ret = io_fini();
147         DbgPrint("IO finalized: %d\n", ret);
148
149         ret = group_fini();
150         DbgPrint("Group finalized: %d\n", ret);
151
152         DbgPrint("Terminated\n");
153         return 0;
154 }
155
156 static void signal_handler(int signum, siginfo_t *info, void *unused)
157 {
158         int fd;
159         CRITICAL_LOG("Terminated(SIGTERM)\n");
160         fd = creat("/tmp/.stop.provider", 0644);
161         if (fd > 0)
162                 close(fd);
163         exit(0);
164 }
165
166 int main(int argc, char *argv[])
167 {
168         struct sigaction act;
169         int ret;
170
171         /*!
172          * How could we care this return values?
173          * Is there any way to print something on the screen?
174          */
175         ret = critical_log_init(util_basename(argv[0]));
176         if (ret < 0)
177                 fprintf(stderr, "Failed to init the critical log\n");
178
179 #if defined(FLOG)
180         __file_log_fp = fopen("/tmp/live.log", "w+t");
181         if (!__file_log_fp)
182                 __file_log_fp = fdopen(1, "w+t");
183 #endif
184         /* appcore_agent_terminate */
185         if (ecore_init() <= 0) {
186                 CRITICAL_LOG("Failed to initiate ecore\n");
187                 critical_log_fini();
188                 return -EFAULT;
189         }
190
191         act.sa_sigaction = signal_handler;
192         act.sa_flags = SA_SIGINFO;
193
194         ret = sigemptyset(&act.sa_mask);
195         if (ret < 0)
196                 CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
197
198         ret = sigaddset(&act.sa_mask, SIGTERM);
199         if (ret < 0)
200                 CRITICAL_LOG("Failed to mask the SIGTERM: %s\n", strerror(errno));
201
202         ret = sigaction(SIGTERM, &act, NULL);
203         if (ret < 0)
204                 CRITICAL_LOG("Failed to add sigaction: %s\n", strerror(errno));
205
206         if (ecore_x_init(NULL) <= 0) {
207                 CRITICAL_LOG("Failed to ecore x init\n");
208                 ecore_shutdown();
209                 critical_log_fini();
210                 return -EFAULT;
211         }
212
213         ecore_app_args_set(argc, (const char **)argv);
214
215         if (evas_init() <= 0) {
216                 CRITICAL_LOG("Failed to init evas return count is below than 0\n");
217                 ecore_x_shutdown();
218                 ecore_shutdown();
219                 critical_log_fini();
220                 return -EFAULT;
221         }
222
223         if (ecore_evas_init() <= 0) {
224                 CRITICAL_LOG("Failed to init ecore_evas\n");
225                 evas_shutdown();
226                 ecore_x_shutdown();
227                 ecore_shutdown();
228                 critical_log_fini();
229                 return -EFAULT;
230         }
231
232         g_type_init();
233
234         conf_loader();
235
236         /*!
237          * \note
238          * Clear old contents files before start the master provider.
239          */
240         (void)util_unlink_files(ALWAYS_PATH);
241         (void)util_unlink_files(READER_PATH);
242         (void)util_unlink_files(IMAGE_PATH);
243         (void)util_unlink_files(SLAVE_LOG_PATH);
244
245         shortcut_service_init();
246         notification_service_init();
247         badge_service_init();
248         script_init();
249
250         app_create();
251
252         vconf_set_bool(VCONFKEY_MASTER_STARTED, 1);
253         ecore_main_loop_begin();
254         vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
255
256         app_terminate();
257
258         script_fini();
259         badge_service_fini();
260         notification_service_fini();
261         shortcut_service_fini();
262
263         ecore_evas_shutdown();
264         evas_shutdown();
265
266         ecore_x_shutdown();
267         ecore_shutdown();
268         critical_log_fini();
269
270 #if defined(FLOG)
271         if (__file_log_fp)
272                 fclose(__file_log_fp);
273 #endif
274         return 0;
275 }
276
277 /* End of a file */