summaryrefslogtreecommitdiff
path: root/src/utility_service.c
blob: 723d22af92413ff81df35846909dd5935959e55f (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * 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 <Eina.h>

#include <dlog.h>
#include <aul.h>

#include <livebox-errno.h>
#include <packet.h>

#include <sys/smack.h>

#include "critical_log.h"
#include "service_common.h"
#include "utility_service.h"
#include "debug.h"
#include "util.h"
#include "conf.h"

#ifndef SVC_PKG
#define SVC_PKG		"com.samsung.data-provider-slave.icon"
#endif

#ifndef LAUNCH_TIMEOUT
#define LAUNCH_TIMEOUT	10.0f
#endif

static struct info {
	Eina_List *pending_list;
	Eina_List *context_list;
	struct service_context *svc_ctx;

	struct tcb *svc_daemon;
	int svc_daemon_is_launched;

	struct service_event_item *launch_timer; 
	struct service_event_item *delay_launcher;
} s_info = {
	.pending_list = NULL,
	.context_list = NULL, /*!< \WARN: This is only used for SERVICE THREAD */
	.svc_ctx = NULL, /*!< \WARN: This is only used for MAIN THREAD */

	.svc_daemon = NULL,
	.svc_daemon_is_launched = 0,

	.launch_timer = NULL,
	.delay_launcher = NULL,
};

struct pending_item {
	struct tcb *tcb;
	struct packet *packet;
};

struct context {
	struct tcb *tcb;
	double seq;
};

static int lazy_launcher_cb(struct service_context *svc_ctx, void *data);

static inline int put_reply_tcb(struct tcb *tcb, double seq)
{
	struct context *ctx;

	ctx = malloc(sizeof(*ctx));
	if (!ctx) {
		ErrPrint("Heap: %s\n", strerror(errno));
		return LB_STATUS_ERROR_MEMORY;
	}

	ctx->tcb = tcb;
	ctx->seq = seq;

	s_info.context_list = eina_list_append(s_info.context_list, ctx);
	return LB_STATUS_SUCCESS;
}

static inline struct tcb *get_reply_tcb(double seq)
{
	Eina_List *l;
	Eina_List *n;
	struct context *ctx;
	struct tcb *tcb;

	EINA_LIST_FOREACH_SAFE(s_info.context_list, l, n, ctx) {
		if (ctx->seq != seq)
			continue;

		s_info.context_list = eina_list_remove(s_info.context_list, ctx);
		tcb = ctx->tcb;
		free(ctx);
		return tcb;
	}

	return NULL;
}

static inline int flush_pended_request(void)
{
	/*!
	 * Flush all pended requests
	 */
	struct pending_item *item;
	int ret;

	EINA_LIST_FREE(s_info.pending_list, item) {
		ret = service_common_unicast_packet(s_info.svc_daemon, item->packet);
		if (ret < 0) {
			struct packet *reply;
			reply = packet_create_reply(item->packet, "i", ret);
			if (service_common_unicast_packet(item->tcb, reply) < 0)
				ErrPrint("Unable to send packet\n");
			packet_destroy(reply);
		} else {
			put_reply_tcb(item->tcb, packet_seq(item->packet));
		}
		packet_unref(item->packet);
		free(item);
	}

	return 0;
}

static inline int put_pended_request(struct tcb *tcb, struct packet *packet)
{
	struct pending_item *item;

	item = malloc(sizeof(*item));
	if (!item) {
		ErrPrint("Heap: %s\n", strerror(errno));
		return LB_STATUS_ERROR_MEMORY;
	}

	item->tcb = tcb;
	item->packet = packet_ref(packet);
	if (!item->packet) {
		free(item);
		ErrPrint("Unable to ref packet\n");
		return LB_STATUS_ERROR_FAULT;
	}

	s_info.pending_list = eina_list_append(s_info.pending_list, item);
	return 0;
}

static int launch_timeout_cb(struct service_context *svc_ctx, void *data)
{
	struct pending_item *item;
	struct packet *reply;

	EINA_LIST_FREE(s_info.pending_list, item) {
		reply = packet_create_reply(item->packet, "i", -EFAULT);
		if (!reply) {
			ErrPrint("Unable to create a packet\n");
		} else {
			int ret;

			ret = service_common_unicast_packet(item->tcb, reply);
			if (ret < 0)
				ErrPrint("Failed to send reply packet: %d\n", ret);

			packet_destroy(reply);
		}

		packet_unref(item->packet);
		free(item);
	}

	s_info.launch_timer = NULL;
	s_info.svc_daemon_is_launched = 0;
	return -ECANCELED; /* Delete this timer */
}

static inline int launch_svc(struct service_context *svc_ctx)
{
	pid_t pid;
	int ret = LB_STATUS_SUCCESS;

	pid = aul_launch_app(SVC_PKG, NULL);
	if (pid > 0) {
		s_info.svc_daemon_is_launched = 1;
		s_info.launch_timer = service_common_add_timer(svc_ctx, LAUNCH_TIMEOUT, launch_timeout_cb, NULL);
		if (!s_info.launch_timer)
			ErrPrint("Unable to create launch timer\n");
	} else if (pid == AUL_R_ETIMEOUT || pid == AUL_R_ECOMM) {
		s_info.svc_daemon_is_launched = 1;
		CRITICAL_LOG("SVC launch failed with timeout(%d), But waiting response\n", pid);
		s_info.launch_timer = service_common_add_timer(svc_ctx, LAUNCH_TIMEOUT, launch_timeout_cb, NULL);
		if (!s_info.launch_timer)
			ErrPrint("Unable to create launch timer\n");
	} else if (pid == AUL_R_ETERMINATING) {
		/* Need time to launch app again */
		ErrPrint("Terminating now, try to launch this after few sec later\n");
		s_info.svc_daemon_is_launched = 1;
		s_info.delay_launcher = service_common_add_timer(svc_ctx, LAUNCH_TIMEOUT, lazy_launcher_cb, NULL);
		if (!s_info.delay_launcher) {
			ErrPrint("Unable to add delay launcher\n");
			ret = LB_STATUS_ERROR_FAULT;
		}
	} else {
		ErrPrint("Failed to launch an app: %s(%d)\n", SVC_PKG, pid);
		ret = LB_STATUS_ERROR_FAULT;
	}

	return ret;
}

static int lazy_launcher_cb(struct service_context *svc_ctx, void *data)
{
	s_info.svc_daemon_is_launched = launch_svc(svc_ctx) == LB_STATUS_SUCCESS;
	s_info.delay_launcher = NULL;
	return -ECANCELED;
}

static int service_thread_main(struct tcb *tcb, struct packet *packet, void *data)
{
	struct packet *reply;
	const char *cmd;
	int ret;

	if (!packet) {
		DbgPrint("TCB %p is terminated (NIL packet)\n", tcb);

		if (tcb == s_info.svc_daemon) {
			s_info.svc_daemon = NULL;
			s_info.svc_daemon_is_launched = 0;
		}

		return LB_STATUS_SUCCESS;
	}

	cmd = packet_command(packet);
	if (!cmd) {
		ErrPrint("Invalid packet\n");
		return LB_STATUS_ERROR_INVALID;
	}

	switch (packet_type(packet)) {
	case PACKET_REQ:
		if (!s_info.svc_daemon_is_launched) {
			ret = launch_svc(tcb_svc_ctx(tcb));
			if (ret != LB_STATUS_SUCCESS)
				goto reply_out;
		}

		if (!s_info.svc_daemon) {
			ret = put_pended_request(tcb, packet);
			if (ret < 0)
				goto reply_out;
		} else {
			ret = service_common_unicast_packet(s_info.svc_daemon, packet);
			if (ret <0)
				goto reply_out;

			put_reply_tcb(tcb, packet_seq(packet));
		}

		break;
	case PACKET_REQ_NOACK:
		if (!strcmp(cmd, "service_register")) {
			if (!s_info.svc_daemon_is_launched) {
				ErrPrint("Service daemon is not launched. but something tries to register a service\n");
				return LB_STATUS_ERROR_INVALID;
			}

			if (s_info.svc_daemon) {
				ErrPrint("Service daemon is already prepared\n");
				return LB_STATUS_ERROR_INVALID;
			}

			if (s_info.launch_timer) {
				service_common_del_timer(tcb_svc_ctx(tcb), s_info.launch_timer);
				s_info.launch_timer = NULL;
			}

			s_info.svc_daemon = tcb;
			flush_pended_request();
		}
		break;
	case PACKET_ACK:
		tcb = get_reply_tcb(packet_seq(packet));
		if (!tcb) {
			ErrPrint("Unable to find reply tcb\n");
		} else {
			ret = service_common_unicast_packet(tcb, packet);
			if (ret < 0)
				ErrPrint("Unable to forward the reply packet\n");
		}
		break;
	default:
		ErrPrint("Packet type is not valid[%s]\n", cmd);
		return LB_STATUS_ERROR_INVALID;
	}

	return LB_STATUS_SUCCESS;

reply_out:
	ErrPrint("Error: %d\n", ret);
	reply = packet_create_reply(packet, "i", ret);
	if (service_common_unicast_packet(tcb, reply) < 0)
		ErrPrint("Unable to send reply packet\n");
	packet_destroy(reply);
	return ret;
}

int utility_service_init(void)
{
	if (s_info.svc_ctx) {
		ErrPrint("Already initialized\n");
		return LB_STATUS_ERROR_ALREADY;
	}

	s_info.svc_ctx = service_common_create(UTILITY_SOCKET, service_thread_main, NULL);
	if (!s_info.svc_ctx) {
		ErrPrint("Unable to activate service thread\n");
		return LB_STATUS_ERROR_FAULT;
	}

	if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), UTILITY_SMACK_LABEL, SMACK_LABEL_IPOUT) != 0) {
		if (errno != EOPNOTSUPP) {
			ErrPrint("Unable to set SMACK label(%d)\n", errno);
			service_common_destroy(s_info.svc_ctx);
			s_info.svc_ctx = NULL;
			return LB_STATUS_ERROR_FAULT;
		}
	}

	if (smack_fsetlabel(service_common_fd(s_info.svc_ctx), UTILITY_SMACK_LABEL, SMACK_LABEL_IPIN) != 0) {
		if (errno != EOPNOTSUPP) {
			ErrPrint("Unable to set SMACK label(%d)\n", errno);
			service_common_destroy(s_info.svc_ctx);
			s_info.svc_ctx = NULL;
			return LB_STATUS_ERROR_FAULT;
		}
	}

	DbgPrint("Successfully initiated\n");
	return LB_STATUS_SUCCESS;
}

int utility_service_fini(void)
{
	if (!s_info.svc_ctx)
		return LB_STATUS_ERROR_INVALID;

	service_common_destroy(s_info.svc_ctx);
	s_info.svc_ctx = NULL;
	DbgPrint("Successfully Finalized\n");
	return LB_STATUS_SUCCESS;
}

/* End of a file */