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