Add support for systemd socket activation for notification service
[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 "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         exit(0);
167 }
168
169 int main(int argc, char *argv[])
170 {
171         struct sigaction act;
172         int ret;
173
174         /*!
175          * How could we care this return values?
176          * Is there any way to print something on the screen?
177          */
178         ret = critical_log_init(util_basename(argv[0]));
179         if (ret < 0)
180                 ErrPrint("Failed to init the critical log\n");
181
182 #if defined(FLOG)
183         __file_log_fp = fopen("/tmp/live.log", "w+t");
184         if (!__file_log_fp)
185                 __file_log_fp = fdopen(1, "w+t");
186 #endif
187         /* appcore_agent_terminate */
188         if (ecore_init() <= 0) {
189                 CRITICAL_LOG("Failed to initiate ecore\n");
190                 critical_log_fini();
191                 return -EFAULT;
192         }
193
194         act.sa_sigaction = signal_handler;
195         act.sa_flags = SA_SIGINFO;
196
197         ret = sigemptyset(&act.sa_mask);
198         if (ret < 0)
199                 CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
200
201         ret = sigaddset(&act.sa_mask, SIGTERM);
202         if (ret < 0)
203                 CRITICAL_LOG("Failed to mask the SIGTERM: %s\n", strerror(errno));
204
205         ret = sigaction(SIGTERM, &act, NULL);
206         if (ret < 0)
207                 CRITICAL_LOG("Failed to add sigaction: %s\n", strerror(errno));
208
209         if (ecore_x_init(NULL) <= 0) {
210                 CRITICAL_LOG("Failed to ecore x init\n");
211                 ecore_shutdown();
212                 critical_log_fini();
213                 return -EFAULT;
214         }
215
216         ecore_app_args_set(argc, (const char **)argv);
217
218         if (evas_init() <= 0) {
219                 CRITICAL_LOG("Failed to init evas return count is below than 0\n");
220                 ecore_x_shutdown();
221                 ecore_shutdown();
222                 critical_log_fini();
223                 return -EFAULT;
224         }
225
226         if (ecore_evas_init() <= 0) {
227                 CRITICAL_LOG("Failed to init ecore_evas\n");
228                 evas_shutdown();
229                 ecore_x_shutdown();
230                 ecore_shutdown();
231                 critical_log_fini();
232                 return -EFAULT;
233         }
234
235 #if !GLIB_CHECK_VERSION (2, 36, 0)
236         g_type_init();
237 #endif
238
239         conf_loader();
240
241         /*!
242          * \note
243          * Clear old contents files before start the master provider.
244          */
245         (void)util_unlink_files(ALWAYS_PATH);
246         (void)util_unlink_files(READER_PATH);
247         (void)util_unlink_files(IMAGE_PATH);
248         (void)util_unlink_files(SLAVE_LOG_PATH);
249
250         shortcut_service_init();
251         notification_service_init();
252         badge_service_init();
253         utility_service_init();
254         script_init();
255
256         app_create();
257
258         ecore_main_loop_begin();
259
260         app_terminate();
261
262         script_fini();
263         utility_service_fini();
264         badge_service_fini();
265         notification_service_fini();
266         shortcut_service_fini();
267
268         ecore_evas_shutdown();
269         evas_shutdown();
270
271         ecore_x_shutdown();
272         ecore_shutdown();
273         critical_log_fini();
274
275 #if defined(FLOG)
276         if (__file_log_fp)
277                 fclose(__file_log_fp);
278 #endif
279         return 0;
280 }
281
282 /* End of a file */