summaryrefslogtreecommitdiff
path: root/src/main.c
blob: e57fc6b1479939ba514a2a519f0fd9a8403518f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <Ecore.h>
#include <Ecore_X.h>
#include <Evas.h>
#include <Ecore_Evas.h>
#include <glib.h>
#include <glib-object.h>
#include <aul.h>
#include <vconf.h>

#include <packet.h>
#include <dlog.h>

#include "slave_life.h"
#include "slave_rpc.h"
#include "client_life.h"
#include "instance.h"
#include "buffer_handler.h"
#include "script_handler.h"
#include "package.h"
#include "group.h"
#include "dead_monitor.h"
#include "conf.h"
#include "io.h"
#include "xmonitor.h"
#include "setting.h"
#include "server.h"
#include "util.h"
#include "debug.h"
#include "critical_log.h"
#include "event.h"
#include "shortcut_service.h"
#include "notification_service.h"
#include "utility_service.h"
#include "badge_service.h"

#if defined(FLOG)
FILE *__file_log_fp;
#endif

static inline int app_create(void)
{
	int ret;

	if (access(SLAVE_LOG_PATH, R_OK|W_OK) != 0) {
		if (mkdir(SLAVE_LOG_PATH, 755) < 0)
			ErrPrint("Failed to create %s (%s)\n", SLAVE_LOG_PATH, strerror(errno));
	}

	/*!
	 * \note
	 * Dead signal handler has to be initialized before
	 * initate package or client (slave and client).
	 *
	 * Because while creating slaves for packages.
	 * It could be crashed before complete the initation stage.
	 *
	 * Then the dead callback should be invoked to handle it properly.
	 *
	 * To enable the dead signal handler,
	 * dead_init should be done before other components are initiated.
	 */
	ret = setting_init();
	DbgPrint("Setting initialized: %d\n", ret);

	ret = client_init();
	DbgPrint("Client initialized: %d\n", ret);

	ret = dead_init();
	DbgPrint("Dead callback is registered: %d\n", ret);

	ret = group_init();
	DbgPrint("group init: %d\n", ret);

	ret = io_init();
	DbgPrint("Init I/O: %d\n", ret);

	ret = package_init();
	DbgPrint("pkgmgr initialized: %d\n", ret);

	instance_init();

	ret = xmonitor_init();
	DbgPrint("XMonitor init is done: %d\n", ret);

	ret = buffer_handler_init();
	DbgPrint("Buffer handler init is done: %d\n", ret);

	/*!
	 * \note
	 * After initiate all other sub-systtems,
	 * Enable the server socket.
	 */
	ret = server_init();
	DbgPrint("Server initialized: %d\n", ret);

	event_init();
	return 0;
}

static inline int app_terminate(void)
{
	int ret;

	event_fini();

	ret = setting_fini();
	DbgPrint("Finalize setting : %d\n", ret);

	xmonitor_fini();

	instance_fini();

	ret = package_fini();
	DbgPrint("Finalize package info: %d\n", ret);

	client_fini();

	ret = server_fini();
	DbgPrint("Finalize dbus: %d\n", ret);

	ret = dead_fini();
	DbgPrint("dead signal handler finalized: %d\n", ret);

	ret = io_fini();
	DbgPrint("IO finalized: %d\n", ret);

	ret = group_fini();
	DbgPrint("Group finalized: %d\n", ret);

	DbgPrint("Terminated\n");
	return 0;
}

static void signal_handler(int signum, siginfo_t *info, void *unused)
{
	int fd;

	CRITICAL_LOG("Terminated(SIGTERM)\n");
	fd = creat("/tmp/.stop.provider", 0644);
	if (fd > 0)
		close(fd);

	exit(0);
}

int main(int argc, char *argv[])
{
	struct sigaction act;
	int ret;

	/*!
	 * How could we care this return values?
	 * Is there any way to print something on the screen?
	 */
	ret = critical_log_init(util_basename(argv[0]));
	if (ret < 0)
		ErrPrint("Failed to init the critical log\n");

#if defined(FLOG)
	__file_log_fp = fopen("/tmp/live.log", "w+t");
	if (!__file_log_fp)
		__file_log_fp = fdopen(1, "w+t");
#endif
	/* appcore_agent_terminate */
	if (ecore_init() <= 0) {
		CRITICAL_LOG("Failed to initiate ecore\n");
		critical_log_fini();
		return -EFAULT;
	}

	act.sa_sigaction = signal_handler;
	act.sa_flags = SA_SIGINFO;

	ret = sigemptyset(&act.sa_mask);
	if (ret < 0)
		CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));

	ret = sigaddset(&act.sa_mask, SIGTERM);
	if (ret < 0)
		CRITICAL_LOG("Failed to mask the SIGTERM: %s\n", strerror(errno));

	ret = sigaction(SIGTERM, &act, NULL);
	if (ret < 0)
		CRITICAL_LOG("Failed to add sigaction: %s\n", strerror(errno));

	if (ecore_x_init(NULL) <= 0) {
		CRITICAL_LOG("Failed to ecore x init\n");
		ecore_shutdown();
		critical_log_fini();
		return -EFAULT;
	}

	ecore_app_args_set(argc, (const char **)argv);

	if (evas_init() <= 0) {
		CRITICAL_LOG("Failed to init evas return count is below than 0\n");
		ecore_x_shutdown();
		ecore_shutdown();
		critical_log_fini();
		return -EFAULT;
	}

	if (ecore_evas_init() <= 0) {
		CRITICAL_LOG("Failed to init ecore_evas\n");
		evas_shutdown();
		ecore_x_shutdown();
		ecore_shutdown();
		critical_log_fini();
		return -EFAULT;
	}

	g_type_init();

	conf_loader();

	/*!
	 * \note
	 * Clear old contents files before start the master provider.
	 */
	(void)util_unlink_files(ALWAYS_PATH);
	(void)util_unlink_files(READER_PATH);
	(void)util_unlink_files(IMAGE_PATH);
	(void)util_unlink_files(SLAVE_LOG_PATH);

	shortcut_service_init();
	notification_service_init();
	badge_service_init();
	utility_service_init();
	script_init();

	app_create();

	vconf_set_bool(VCONFKEY_MASTER_STARTED, 1);
	ecore_main_loop_begin();
	vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);

	app_terminate();

	script_fini();
	utility_service_fini();
	badge_service_fini();
	notification_service_fini();
	shortcut_service_fini();

	ecore_evas_shutdown();
	evas_shutdown();

	ecore_x_shutdown();
	ecore_shutdown();
	critical_log_fini();

#if defined(FLOG)
	if (__file_log_fp)
		fclose(__file_log_fp);
#endif
	return 0;
}

/* End of a file */