summaryrefslogtreecommitdiff
path: root/src/slave_life.c
blob: eac009d3b11fb083a924b6349c514ac2ddafe2d9 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (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 <string.h> /* strerror */
#include <errno.h> /* errno */
#include <unistd.h> /* pid_t */
#include <stdlib.h> /* free */
#include <pthread.h>
#include <malloc.h>
#include <sys/time.h>

#include <Eina.h>
#include <Ecore.h>

#include <aul.h> /* aul_launch_app */
#include <dlog.h>
#include <bundle.h>

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

#include "slave_life.h"
#include "slave_rpc.h"
#include "client_life.h"
#include "fault_manager.h"
#include "debug.h"
#include "conf.h"
#include "setting.h"
#include "util.h"
#include "abi.h"
#include "xmonitor.h"

int errno;

struct slave_node {
	char *name;
	char *abi;
	char *pkgname;
	int secured;	/* Only A package(livebox) is loaded for security requirements */
	int refcnt;
	int fault_count;
	int critical_fault_count;
	enum slave_state state;
	int network;

	int loaded_instance;
	int loaded_package;

	int reactivate_instances;
	int reactivate_slave;

	pid_t pid;

	Eina_List *event_activate_list;
	Eina_List *event_deactivate_list;
	Eina_List *event_delete_list;
	Eina_List *event_fault_list;
	Eina_List *event_pause_list;
	Eina_List *event_resume_list;

	Eina_List *data_list;

	Ecore_Timer *ttl_timer; /* Time to live */
	Ecore_Timer *activate_timer; /* Waiting hello packet for this time */

	struct timeval activated_at;
};

struct event {
	struct slave_node *slave;

	int (*evt_cb)(struct slave_node *, void *);
	void *cbdata;
};

struct priv_data {
	char *tag;
	void *data;
};

static struct {
	Eina_List *slave_list;
} s_info = {
	.slave_list = NULL,
};

static Eina_Bool slave_ttl_cb(void *data)
{
	struct slave_node *slave = (struct slave_node *)data;

	/*!
	 * \note
	 * ttl_timer must has to be set to NULL before deactivate the slave
	 * It will be used for making decision of the expired TTL timer or the fault of a livebox.
	 */
	slave->ttl_timer = NULL;

	slave_set_reactivation(slave, 0);
	slave_set_reactivate_instances(slave, 1);

	slave = slave_deactivate(slave);
	DbgPrint("Slave is deactivated(%p)\n", slave);

	/*! To recover all instances state it is activated again */
	return ECORE_CALLBACK_CANCEL;
}

static inline int xmonitor_pause_cb(void *data)
{
	slave_pause(data);
	return LB_STATUS_SUCCESS;
}

static inline int xmonitor_resume_cb(void *data)
{
	slave_resume(data);
	return LB_STATUS_SUCCESS;
}

static inline struct slave_node *create_slave_node(const char *name, int is_secured, const char *abi, const char *pkgname, int network)
{
	struct slave_node *slave;

	slave = calloc(1, sizeof(*slave));
	if (!slave) {
		ErrPrint("Heap: %s\n", strerror(errno));
		return NULL;
	}

	slave->name = strdup(name);
	if (!slave->name) {
		ErrPrint("Heap: %s\n", strerror(errno));
		DbgFree(slave);
		return NULL;
	}

	slave->abi = strdup(abi);
	if (!slave->abi) {
		ErrPrint("Heap: %s\n", strerror(errno));
		DbgFree(slave->name);
		DbgFree(slave);
		return NULL;
	}

	slave->pkgname = strdup(pkgname);
	if (!slave->pkgname) {
		ErrPrint("Heap: %s\n", strerror(errno));
		DbgFree(slave->abi);
		DbgFree(slave->name);
		DbgFree(slave);
		return NULL;
	}

	slave->secured = is_secured;
	slave->pid = (pid_t)-1;
	slave->state = SLAVE_TERMINATED;
	slave->network = network;

	xmonitor_add_event_callback(XMONITOR_PAUSED, xmonitor_pause_cb, slave);
	xmonitor_add_event_callback(XMONITOR_RESUMED, xmonitor_resume_cb, slave);

	s_info.slave_list = eina_list_append(s_info.slave_list, slave);
	DbgPrint("slave data is created %p\n", slave);
	return slave;
}

static inline void invoke_delete_cb(struct slave_node *slave)
{
	Eina_List *l;
	Eina_List *n;
	struct event *event;
	int ret;

	EINA_LIST_FOREACH_SAFE(slave->event_delete_list, l, n, event) {
		ret = event->evt_cb(event->slave, event->cbdata);
		if (ret < 0) {
			if (eina_list_data_find(slave->event_delete_list, event)) {
				slave->event_delete_list = eina_list_remove(slave->event_delete_list, event);
				DbgFree(event);
			}
		}
	}
}

static inline void destroy_slave_node(struct slave_node *slave)
{
	struct event *event;
	struct priv_data *priv;

	if (slave->pid != (pid_t)-1) {
		ErrPrint("Slave is not deactivated\n");
		return;
	}

	DbgPrint("Slave data is destroyed %p\n", slave);

	xmonitor_del_event_callback(XMONITOR_PAUSED, xmonitor_pause_cb, slave);
	xmonitor_del_event_callback(XMONITOR_RESUMED, xmonitor_resume_cb, slave);

	invoke_delete_cb(slave);
	slave_rpc_fini(slave); /*!< Finalize the RPC after handling all delete callbacks */

	EINA_LIST_FREE(slave->event_delete_list, event) {
		DbgFree(event);
	}

	EINA_LIST_FREE(slave->event_activate_list, event) {
		DbgFree(event);
	}

	EINA_LIST_FREE(slave->event_deactivate_list, event) {
		DbgFree(event);
	}

	EINA_LIST_FREE(slave->event_fault_list, event) {
		DbgFree(event);
	}

	EINA_LIST_FREE(slave->data_list, priv) {
		DbgFree(priv->tag);
		DbgFree(priv);
	}

	s_info.slave_list = eina_list_remove(s_info.slave_list, slave);

	if (slave->ttl_timer)
		ecore_timer_del(slave->ttl_timer);

	if (slave->activate_timer)
		ecore_timer_del(slave->activate_timer);

	DbgFree(slave->abi);
	DbgFree(slave->name);
	DbgFree(slave->pkgname);
	DbgFree(slave);
	return;
}

static inline struct slave_node *find_slave(const char *name)
{
	struct slave_node *slave;
	Eina_List *l;

	EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
		if (!strcmp(slave->name, name))
			return slave;
	}
	
	return NULL;
}

HAPI int slave_expired_ttl(struct slave_node *slave)
{
	if (!slave)
		return 0;

	if (!slave->secured)
		return 0;

	return !!slave->ttl_timer;
}

HAPI struct slave_node *slave_ref(struct slave_node *slave)
{
	if (!slave)
		return NULL;

	slave->refcnt++;
	return slave;
}

HAPI struct slave_node *slave_unref(struct slave_node *slave)
{
	if (!slave)
		return NULL;

	if (slave->refcnt == 0) {
		ErrPrint("Slave refcnt is not valid\n");
		return NULL;
	}

	slave->refcnt--;
	if (slave->refcnt == 0) {
		destroy_slave_node(slave);
		slave = NULL;
	}

	return slave;
}

HAPI const int const slave_refcnt(struct slave_node *slave)
{
	return slave->refcnt;
}

HAPI struct slave_node *slave_create(const char *name, int is_secured, const char *abi, const char *pkgname, int network)
{
	struct slave_node *slave;

	slave = find_slave(name);
	if (slave) {
		if (slave->secured != is_secured)
			ErrPrint("Exists slave and creating slave's security flag is not matched\n");
		return slave;
	}

	slave = create_slave_node(name, is_secured, abi, pkgname, network);
	if (!slave)
		return NULL;

	slave_ref(slave);
	slave_rpc_init(slave);

	return slave;
}

/*!
 * \note
 * Before destroying slave object,
 * you should check the RPC(slave_async_XXX) state and Private data field (slave_set_data)
 */
HAPI void slave_destroy(struct slave_node *slave)
{
	slave_unref(slave);
}

static inline void invoke_fault_cb(struct slave_node *slave)
{
	Eina_List *l;
	Eina_List *n;
	struct event *event;
	int ret;

	EINA_LIST_FOREACH_SAFE(slave->event_fault_list, l, n, event) {
		ret = event->evt_cb(event->slave, event->cbdata);
		if (ret < 0) {
			if (eina_list_data_find(slave->event_fault_list, event)) {
				slave->event_fault_list = eina_list_remove(slave->event_fault_list, event);
				DbgFree(event);
			}
		}
	}
}

static inline void invoke_activate_cb(struct slave_node *slave)
{
	Eina_List *l;
	Eina_List *n;
	struct event *event;
	int ret;

	EINA_LIST_FOREACH_SAFE(slave->event_activate_list, l, n, event) {
		ret = event->evt_cb(event->slave, event->cbdata);
		if (ret < 0) {
			if (eina_list_data_find(slave->event_activate_list, event)) {
				slave->event_activate_list = eina_list_remove(slave->event_activate_list, event);
				DbgFree(event);
			}
		}
	}
}

static Eina_Bool activate_timer_cb(void *data)
{
	struct slave_node *slave = data;

	slave->fault_count++;
	invoke_fault_cb(slave);

	slave_set_reactivation(slave, 0);
	slave_set_reactivate_instances(slave, 0);

	slave->activate_timer = NULL;
	if (slave->pid > 0) {
		int ret;
		DbgPrint("Try to terminate PID: %d\n", slave->pid);
		ret = aul_terminate_pid(slave->pid);
		if (ret < 0)
			ErrPrint("Terminate failed, pid %d\n", slave->pid);
	}

	slave = slave_deactivated(slave);
	ErrPrint("Slave is not activated in %lf sec (slave: %p)\n", SLAVE_ACTIVATE_TIME, slave);
	return ECORE_CALLBACK_CANCEL;
}

HAPI int slave_activate(struct slave_node *slave)
{

	/*!
	 * \note
	 * This check code can replace the slave->state check code
	 * If the slave data has the PID, it means, it is activated
	 * Even if it is in the termiating sequence, it will have the PID
	 * before terminated at last.
	 * So we can use this simple code for checking the slave's last state.
	 * about it is alive? or not.
	 */
	if (slave->pid != (pid_t)-1)
		return LB_STATUS_ERROR_ALREADY;

	if (DEBUG_MODE) {
		DbgPrint("Debug Mode enabled. name[%s] secured[%d] abi[%s]\n", slave->name, slave->secured, slave->abi);
	} else {
		bundle *param;
		param = bundle_create();
		if (!param) {
			ErrPrint("Failed to create a bundle\n");
			return LB_STATUS_ERROR_FAULT;
		}

		bundle_add(param, BUNDLE_SLAVE_NAME, slave->name);
		bundle_add(param, BUNDLE_SLAVE_SECURED, slave->secured ? "true" : "false");
		bundle_add(param, BUNDLE_SLAVE_ABI, slave->abi);

		DbgPrint("Launch the slave package: %s\n", slave->pkgname);
		slave->pid = (pid_t)aul_launch_app(slave->pkgname, param);

		bundle_free(param);

		if (slave->pid < 0) {
			ErrPrint("Failed to launch a new slave %s (%d)\n", slave->name, slave->pid);
			slave->pid = (pid_t)-1;
			return LB_STATUS_ERROR_FAULT;
		}
		DbgPrint("Slave launched %d for %s\n", slave->pid, slave->name);

		slave->activate_timer = ecore_timer_add(SLAVE_ACTIVATE_TIME, activate_timer_cb, slave);
		if (!slave->activate_timer)
			ErrPrint("Failed to register an activate timer\n");
	}

	slave->state = SLAVE_REQUEST_TO_LAUNCH;
	/*!
	 * \note
	 * Increase the refcnt of a slave,
	 * To prevent from making an orphan(slave).
	 */
	slave_ref(slave);

	return LB_STATUS_SUCCESS;
}

HAPI int slave_give_more_ttl(struct slave_node *slave)
{
	double delay;

	if (!slave->secured || !slave->ttl_timer)
		return LB_STATUS_ERROR_INVALID;

	delay = SLAVE_TTL - ecore_timer_pending_get(slave->ttl_timer);
	ecore_timer_delay(slave->ttl_timer, delay);
	return LB_STATUS_SUCCESS;
}

HAPI int slave_freeze_ttl(struct slave_node *slave)
{
	if (!slave->secured || !slave->ttl_timer)
		return LB_STATUS_ERROR_INVALID;

	ecore_timer_freeze(slave->ttl_timer);
	return LB_STATUS_SUCCESS;
}

HAPI int slave_thaw_ttl(struct slave_node *slave)
{
	double delay;

	if (!slave->secured || !slave->ttl_timer)
		return LB_STATUS_ERROR_INVALID;

	ecore_timer_thaw(slave->ttl_timer);

	delay = SLAVE_TTL - ecore_timer_pending_get(slave->ttl_timer);
	ecore_timer_delay(slave->ttl_timer, delay);
	return LB_STATUS_SUCCESS;
}

HAPI int slave_activated(struct slave_node *slave)
{
	slave->state = SLAVE_RESUMED;

	if (xmonitor_is_paused())
		slave_pause(slave);

	if (slave->secured == 1) {
		DbgPrint("Slave deactivation timer is added (%s - %lf)\n", slave->name, SLAVE_TTL);
		slave->ttl_timer = ecore_timer_add(SLAVE_TTL, slave_ttl_cb, slave);
		if (!slave->ttl_timer)
			ErrPrint("Failed to create a TTL timer\n");
	}

	invoke_activate_cb(slave);

	slave_set_reactivation(slave, 0);
	slave_set_reactivate_instances(slave, 0);

	if (gettimeofday(&slave->activated_at, NULL) < 0)
		ErrPrint("Failed to get time of day: %s\n", strerror(errno));

	if (slave->activate_timer) {
		ecore_timer_del(slave->activate_timer);
		slave->activate_timer = NULL;
	}

	return LB_STATUS_SUCCESS;
}

static inline int invoke_deactivate_cb(struct slave_node *slave)
{
	Eina_List *l;
	Eina_List *n;
	struct event *event;
	int ret;
	int reactivate = 0;

	EINA_LIST_FOREACH_SAFE(slave->event_deactivate_list, l, n, event) {
		ret = event->evt_cb(event->slave, event->cbdata);
		if (ret < 0) {
			if (eina_list_data_find(slave->event_deactivate_list, event)) {
				slave->event_deactivate_list = eina_list_remove(slave->event_deactivate_list, event);
				DbgFree(event);
			}
		} else if (ret == SLAVE_NEED_TO_REACTIVATE) {
			reactivate++;
		}
	}

	return reactivate;
}

HAPI struct slave_node *slave_deactivate(struct slave_node *slave)
{
	int ret;

	if (!slave_is_activated(slave)) {
		ErrPrint("Slave is already deactivated\n");
		if (slave_loaded_instance(slave) == 0) {
			/*!
			 * \note
			 * If a slave has no more instances,
			 * Destroy it
			 */
			slave = slave_unref(slave);
		}
		return slave;
	}

	DbgPrint("Deactivate a slave: %d\n", slave->pid);
	/*!
	 * \todo
	 * check the return value of the aul_terminate_pid
	 */
	slave->state = SLAVE_REQUEST_TO_TERMINATE;

	DbgPrint("Terminate PID: %d\n", slave->pid);
	if (slave->pid > 0) {
		ret = aul_terminate_pid(slave->pid);
		if (ret < 0) {
			ErrPrint("Terminate failed. pid %d (%d)\n", slave->pid, ret);
			slave = slave_deactivated(slave);
		}
	}

	return slave;
}

HAPI struct slave_node *slave_deactivated(struct slave_node *slave)
{
	int reactivate;

	slave->pid = (pid_t)-1;
	slave->state = SLAVE_TERMINATED;

	if (slave->ttl_timer) {
		ecore_timer_del(slave->ttl_timer);
		slave->ttl_timer = NULL;
	}

	if (slave->activate_timer) {
		ecore_timer_del(slave->activate_timer);
		slave->activate_timer = NULL;
	}

	reactivate = invoke_deactivate_cb(slave);

	slave = slave_unref(slave);
	if (!slave) {
		DbgPrint("SLAVE object is destroyed\n");
		return slave;
	}

	if (reactivate && slave_need_to_reactivate(slave)) {
		int ret;

		DbgPrint("Need to reactivate a slave\n");
		ret = slave_activate(slave);
		if (ret < 0 && ret != LB_STATUS_ERROR_ALREADY)
			ErrPrint("Failed to reactivate a slave\n");
	} else if (slave_loaded_instance(slave) == 0) {
		/*!
		 * \note
		 * If a slave has no more instances,
		 * Destroy it
		 */
		slave = slave_unref(slave);
	}

	return slave;
}

HAPI struct slave_node *slave_deactivated_by_fault(struct slave_node *slave)
{
	int ret;
	struct timeval faulted_at;
	int reactivate = 1;
	int reactivate_instances = 1;

	if (!slave_is_activated(slave)) {
		DbgPrint("Deactivating in progress\n");
		if (slave_loaded_instance(slave) == 0)
			slave = slave_unref(slave);

		return slave;
	}

	slave->fault_count++;

	(void)fault_check_pkgs(slave);

	if (slave->pid > 0) {
		DbgPrint("Try to terminate PID: %d\n", slave->pid);
		ret = aul_terminate_pid(slave->pid);
		if (ret < 0) {
			ErrPrint("Terminate failed, pid %d\n", slave->pid);
		}
	}

	if (gettimeofday(&faulted_at, NULL) == 0) {
		struct timeval rtv;

		timersub(&faulted_at, &slave->activated_at, &rtv);
		if (rtv.tv_sec < MINIMUM_REACTIVATION_TIME) {
			slave->critical_fault_count++;
			if (!slave_loaded_instance(slave) || slave->critical_fault_count >= SLAVE_MAX_LOAD) {
				ErrPrint("Reactivation time is too fast and frequently occurred - Stop to auto reactivation\n");
				reactivate = 0;
				reactivate_instances = 0;
				slave->critical_fault_count = 0;
				/*!
				 * \note
				 * Fault callback can access the slave information.
				 */
				invoke_fault_cb(slave);
			}
		} else {
			slave->critical_fault_count = 0;
		}
	} else {
		ErrPrint("Failed to get time of day: %s\n", strerror(errno));
	}

	slave_set_reactivation(slave, reactivate);
	slave_set_reactivate_instances(slave, reactivate_instances);

	slave = slave_deactivated(slave);
	return slave;
}

HAPI const int const slave_is_activated(struct slave_node *slave)
{
	switch (slave->state) {
	case SLAVE_REQUEST_TO_TERMINATE:
	case SLAVE_TERMINATED:
		return 0;
	case SLAVE_REQUEST_TO_LAUNCH:
		 /* Not yet launched. but the slave incurred an unexpected error */
	case SLAVE_REQUEST_TO_PAUSE:
	case SLAVE_REQUEST_TO_RESUME:
	case SLAVE_PAUSED:
	case SLAVE_RESUMED:
		return 1;
	default:
		return slave->pid != (pid_t)-1;
	}

	/* Could not be reach to here */
	return 0;
}

HAPI int slave_event_callback_add(struct slave_node *slave, enum slave_event event, int (*cb)(struct slave_node *, void *), void *data)
{
	struct event *ev;

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

	ev->slave = slave;
	ev->cbdata = data;
	ev->evt_cb = cb;

	/*!
	 * \note
	 * Use the eina_list_prepend API.
	 * To keep the sequence of a callback invocation.
	 *
	 * Here is an example sequence.
	 *
	 * slave_event_callback_add(CALLBACK_01);
	 * slave_event_callback_add(CALLBACK_02);
	 * slave_event_callback_add(CALLBACK_03);
	 *
	 * Then the invoke_event_callback function will call the CALLBACKS as below sequence
	 *
	 * invoke_CALLBACK_03
	 * invoke_CALLBACK_02
	 * invoke_CALLBACK_01
	 */

	switch (event) {
	case SLAVE_EVENT_ACTIVATE:
		slave->event_activate_list = eina_list_prepend(slave->event_activate_list, ev);
		break;
	case SLAVE_EVENT_DELETE:
		slave->event_delete_list = eina_list_prepend(slave->event_delete_list, ev);
		break;
	case SLAVE_EVENT_DEACTIVATE:
		slave->event_deactivate_list = eina_list_prepend(slave->event_deactivate_list, ev);
		break;
	case SLAVE_EVENT_PAUSE:
		slave->event_pause_list = eina_list_prepend(slave->event_pause_list, ev);
		break;
	case SLAVE_EVENT_RESUME:
		slave->event_resume_list = eina_list_prepend(slave->event_resume_list, ev);
		break;
	case SLAVE_EVENT_FAULT:
		slave->event_fault_list = eina_list_prepend(slave->event_fault_list, ev);
		break;
	default:
		DbgFree(ev);
		return LB_STATUS_ERROR_INVALID;
	}

	return LB_STATUS_SUCCESS;
}

HAPI int slave_event_callback_del(struct slave_node *slave, enum slave_event event, int (*cb)(struct slave_node *, void *), void *data)
{
	struct event *ev;
	Eina_List *l;
	Eina_List *n;

	switch (event) {
	case SLAVE_EVENT_DEACTIVATE:
		EINA_LIST_FOREACH_SAFE(slave->event_deactivate_list, l, n, ev) {
			if (ev->evt_cb == cb && ev->cbdata == data) {
				slave->event_deactivate_list = eina_list_remove(slave->event_deactivate_list, ev);
				DbgFree(ev);
				return LB_STATUS_SUCCESS;
			}
		}
		break;
	case SLAVE_EVENT_DELETE:
		EINA_LIST_FOREACH_SAFE(slave->event_delete_list, l, n, ev) {
			if (ev->evt_cb == cb && ev->cbdata == data) {
				slave->event_delete_list = eina_list_remove(slave->event_delete_list, ev);
				DbgFree(ev);
				return LB_STATUS_SUCCESS;
			}
		}
		break;
	case SLAVE_EVENT_ACTIVATE:
		EINA_LIST_FOREACH_SAFE(slave->event_activate_list, l, n, ev) {
			if (ev->evt_cb == cb && ev->cbdata == data) {
				slave->event_activate_list = eina_list_remove(slave->event_activate_list, ev);
				DbgFree(ev);
				return LB_STATUS_SUCCESS;
			}
		}
		break;
	case SLAVE_EVENT_PAUSE:
		EINA_LIST_FOREACH_SAFE(slave->event_pause_list, l, n, ev) {
			if (ev->evt_cb == cb && ev->cbdata == data) {
				slave->event_pause_list = eina_list_remove(slave->event_pause_list, ev);
				DbgFree(ev);
				return LB_STATUS_SUCCESS;
			}
		}
		break;
	case SLAVE_EVENT_RESUME:
		EINA_LIST_FOREACH_SAFE(slave->event_resume_list, l, n, ev) {
			if (ev->evt_cb == cb && ev->cbdata == data) {
				slave->event_resume_list = eina_list_remove(slave->event_resume_list, ev);
				DbgFree(ev);
				return LB_STATUS_SUCCESS;
			}
		}
		break;
	case SLAVE_EVENT_FAULT:
		EINA_LIST_FOREACH_SAFE(slave->event_fault_list, l, n, ev) {
			if (ev->evt_cb == cb && ev->cbdata == data) {
				slave->event_fault_list = eina_list_remove(slave->event_fault_list, ev);
				DbgFree(ev);
				return LB_STATUS_SUCCESS;
			}
		}
		break;
	default:
		return LB_STATUS_ERROR_INVALID;
	}

	return LB_STATUS_ERROR_NOT_EXIST;
}

HAPI int slave_set_data(struct slave_node *slave, const char *tag, void *data)
{
	struct priv_data *priv;

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

	priv->tag = strdup(tag);
	if (!priv->tag) {
		ErrPrint("Heap: %s\n", strerror(errno));
		DbgFree(priv);
		return LB_STATUS_ERROR_MEMORY;
	}

	priv->data = data;
	slave->data_list = eina_list_append(slave->data_list, priv);
	return LB_STATUS_SUCCESS;
}

HAPI void *slave_del_data(struct slave_node *slave, const char *tag)
{
	struct priv_data *priv;
	void *data;
	Eina_List *l;
	Eina_List *n;

	EINA_LIST_FOREACH_SAFE(slave->data_list, l, n, priv) {
		if (!strcmp(priv->tag, tag)) {
			slave->data_list = eina_list_remove(slave->data_list, priv);

			data = priv->data;
			DbgFree(priv->tag);
			DbgFree(priv);
			return data;
		}
	}

	return NULL;
}

HAPI void *slave_data(struct slave_node *slave, const char *tag)
{
	struct priv_data *priv;
	Eina_List *l;

	EINA_LIST_FOREACH(slave->data_list, l, priv) {
		if (!strcmp(priv->tag, tag))
			return priv->data;
	}

	return NULL;
}

HAPI struct slave_node *slave_find_by_pid(pid_t pid)
{
	Eina_List *l;
	struct slave_node *slave;

	EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
		if (slave->pid == pid)
			return slave;
	}

	return NULL;
}

HAPI struct slave_node *slave_find_by_name(const char *name)
{
	Eina_List *l;
	struct slave_node *slave;

	EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
		if (!strcmp(slave->name, name))
			return slave;
	}

	return NULL;
}

HAPI struct slave_node *slave_find_available(const char *abi, int secured, int network)
{
	Eina_List *l;
	struct slave_node *slave;

	EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
		if (slave->secured != secured)
			continue;

		if (slave->state == SLAVE_REQUEST_TO_TERMINATE && slave->loaded_instance == 0) {
			/*!
			 * \note
			 * If a slave is in request_to_terminate state,
			 * and the slave object has no more intances,
			 * the slave object will be deleted soon.
			 * so we cannot reuse it.
			 *
			 * This object is not usable.
			 */
			continue;
		}

		if (strcasecmp(slave->abi, abi))
			continue;

		if (slave->secured) {
			DbgPrint("Found secured slave - has no instances (%s)\n", slave_name(slave));
			if (slave->loaded_package == 0)
				return slave;
		} else if (slave->network == network) {
			DbgPrint("slave[%s] %d (net: %d)\n", slave_name(slave), slave->loaded_package, slave->network);
			if (!strcasecmp(abi, DEFAULT_ABI)) {
				if (slave->loaded_package < SLAVE_MAX_LOAD)
					return slave;
			} else {
				return slave;
			}
		}
	}

	return NULL;
}

HAPI struct slave_node *slave_find_by_pkgname(const char *pkgname)
{
	Eina_List *l;
	struct slave_node *slave;

	EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
		if (!strcmp(slave->pkgname, pkgname)) {
			if (slave->pid == (pid_t)-1) {
				return slave;
			}
		}
	}

	return NULL;
}

HAPI struct slave_node *slave_find_by_rpc_handle(int handle)
{
	Eina_List *l;
	struct slave_node *slave;

	if (handle <= 0) {
		ErrPrint("Invalid RPC handle: %d\n", handle);
		return NULL;
	}

	EINA_LIST_FOREACH(s_info.slave_list, l, slave) {
		if (slave_rpc_handle(slave) == handle)
			return slave;
	}

	/* Not found */
	return NULL;
}

HAPI void slave_load_package(struct slave_node *slave)
{
	slave->loaded_package++;
}

HAPI void slave_unload_package(struct slave_node *slave)
{
	if (!slave || slave->loaded_package == 0) {
		ErrPrint("Slave loaded package is not correct\n");
		return;
	}
		
	slave->loaded_package--;
}

HAPI void slave_load_instance(struct slave_node *slave)
{
	slave->loaded_instance++;
	DbgPrint("Instance: (%d)%d\n", slave->pid, slave->loaded_instance);
}

HAPI int const slave_loaded_instance(struct slave_node *slave)
{
	return slave->loaded_instance;
}

HAPI int const slave_loaded_package(struct slave_node *slave)
{
	return slave->loaded_package;
}

HAPI struct slave_node *slave_unload_instance(struct slave_node *slave)
{
	if (!slave || slave->loaded_instance == 0) {
		ErrPrint("Slave loaded instance is not correct\n");
		return slave;
	}

	slave->loaded_instance--;
	DbgPrint("Instance: (%d)%d\n", slave->pid, slave->loaded_instance);
	if (slave->loaded_instance == 0 && slave_is_activated(slave)) {
		slave_set_reactivation(slave, 0);
		slave_set_reactivate_instances(slave, 0);

		slave = slave_deactivate(slave);
	}

	return slave;
}

HAPI const int const slave_is_secured(const struct slave_node *slave)
{
	return slave->secured;
}

HAPI const char * const slave_name(const struct slave_node *slave)
{
	return slave->name;
}

HAPI const char * const slave_abi(const struct slave_node *slave)
{
	return slave->abi;
}

HAPI const pid_t const slave_pid(const struct slave_node *slave)
{
	return slave->pid;
}

HAPI int slave_set_pid(struct slave_node *slave, pid_t pid)
{
	if (!slave)
		return LB_STATUS_ERROR_INVALID;

	DbgPrint("Slave PID is updated to %d from %d\n", pid, slave->pid);

	slave->pid = pid;
	return LB_STATUS_SUCCESS;
}

static inline void invoke_resumed_cb(struct slave_node *slave)
{
	Eina_List *l;
	Eina_List *n;
	struct event *event;
	int ret;

	EINA_LIST_FOREACH_SAFE(slave->event_resume_list, l, n, event) {
		ret = event->evt_cb(event->slave, event->cbdata);
		if (ret < 0) {
			if (eina_list_data_find(slave->event_resume_list, event)) {
				slave->event_resume_list = eina_list_remove(slave->event_resume_list, event);
				DbgFree(event);
			}
		}
	}
}

static void resume_cb(struct slave_node *slave, const struct packet *packet, void *data)
{
	int ret;

	if (slave->state == SLAVE_REQUEST_TO_TERMINATE) {
		DbgPrint("Slave is terminating now. ignore resume result\n");
		return;
	}

	if (!packet) {
		ErrPrint("Failed to change the state of the slave\n");
		slave->state = SLAVE_PAUSED;
		return;
	}

	if (packet_get(packet, "i", &ret) != 1) {
		ErrPrint("Invalid parameter\n");
		return;
	}

	if (ret == 0) {
		slave->state = SLAVE_RESUMED;
		slave_rpc_ping_thaw(slave);
		invoke_resumed_cb(slave);
	}
}

static inline void invoke_paused_cb(struct slave_node *slave)
{
	Eina_List *l;
	Eina_List *n;
	struct event *event;
	int ret;

	EINA_LIST_FOREACH_SAFE(slave->event_pause_list, l, n, event) {
		ret = event->evt_cb(event->slave, event->cbdata);
		if (ret < 0) {
			if (eina_list_data_find(slave->event_pause_list, event)) {
				slave->event_pause_list = eina_list_remove(slave->event_pause_list, event);
				DbgFree(event);
			}
		}
	}
}

static void pause_cb(struct slave_node *slave, const struct packet *packet, void *data)
{
	int ret;

	if (slave->state == SLAVE_REQUEST_TO_TERMINATE) {
		DbgPrint("Slave is terminating now. ignore pause result\n");
		return;
	}

	if (!packet) {
		ErrPrint("Failed to change the state of the slave\n");
		slave->state = SLAVE_RESUMED;
		return;
	}

	if (packet_get(packet, "i", &ret) != 1) {
		ErrPrint("Invalid parameter\n");
		return;
	}

	if (ret == 0) {
		slave->state = SLAVE_PAUSED;
		slave_rpc_ping_freeze(slave);
		invoke_paused_cb(slave);
	}
}

HAPI int slave_resume(struct slave_node *slave)
{
	double timestamp;
	struct packet *packet;

	switch (slave->state) {
	case SLAVE_REQUEST_TO_LAUNCH:
	case SLAVE_REQUEST_TO_TERMINATE:
	case SLAVE_TERMINATED:
		return LB_STATUS_ERROR_INVALID;
	case SLAVE_RESUMED:
	case SLAVE_REQUEST_TO_RESUME:
		return LB_STATUS_SUCCESS;
	default:
		break;
	}

	timestamp = util_timestamp();

	packet = packet_create("resume", "d", timestamp);
	if (!packet) {
		ErrPrint("Failed to prepare param\n");
		return LB_STATUS_ERROR_FAULT;
	}

	slave->state = SLAVE_REQUEST_TO_RESUME;
	return slave_rpc_async_request(slave, NULL, packet, resume_cb, NULL, 0);
}

HAPI int slave_pause(struct slave_node *slave)
{
	double timestamp;
	struct packet *packet;

	switch (slave->state) {
	case SLAVE_REQUEST_TO_LAUNCH:
	case SLAVE_REQUEST_TO_TERMINATE:
	case SLAVE_TERMINATED:
		return LB_STATUS_ERROR_INVALID;
	case SLAVE_PAUSED:
	case SLAVE_REQUEST_TO_PAUSE:
		return LB_STATUS_SUCCESS;
	default:
		break;
	}

	timestamp = util_timestamp();

	packet = packet_create("pause", "d", timestamp);
	if (!packet) {
		ErrPrint("Failed to prepare param\n");
		return LB_STATUS_ERROR_FAULT;
	}

	slave->state = SLAVE_REQUEST_TO_PAUSE;
	return slave_rpc_async_request(slave, NULL, packet, pause_cb, NULL, 0);
}

HAPI const char *slave_pkgname(const struct slave_node *slave)
{
	return slave ? slave->pkgname : NULL;
}

HAPI enum slave_state slave_state(const struct slave_node *slave)
{
	return slave ? slave->state : SLAVE_ERROR;
}

HAPI const char *slave_state_string(const struct slave_node *slave)
{
	switch (slave->state) {
	case SLAVE_REQUEST_TO_LAUNCH:
		return "RequestToLaunch";
	case SLAVE_REQUEST_TO_TERMINATE:
		return "RequestToTerminate";
	case SLAVE_TERMINATED:
		return "Terminated";
	case SLAVE_REQUEST_TO_PAUSE:
		return "RequestToPause";
	case SLAVE_REQUEST_TO_RESUME:
		return "RequestToResume";
	case SLAVE_PAUSED:
		return "Paused";
	case SLAVE_RESUMED:
		return "Resumed";
	case SLAVE_ERROR:
		return "Error";
	default:
		break;
	}

	return "Unknown";
}

HAPI const void *slave_list(void)
{
	return s_info.slave_list;
}

HAPI int const slave_fault_count(const struct slave_node *slave)
{
	return slave->fault_count;
}

HAPI double const slave_ttl(const struct slave_node *slave)
{
	if (!slave->ttl_timer)
		return 0.0f;

	return ecore_timer_pending_get(slave->ttl_timer);
}

HAPI void slave_set_reactivate_instances(struct slave_node *slave, int reactivate)
{
	slave->reactivate_instances = reactivate;
}

HAPI int slave_need_to_reactivate_instances(struct slave_node *slave)
{
	return slave->reactivate_instances;
}

HAPI void slave_set_reactivation(struct slave_node *slave, int flag)
{
	slave->reactivate_slave = flag;
}

HAPI int slave_need_to_reactivate(struct slave_node *slave)
{
	return slave->reactivate_slave;
}

HAPI int slave_network(const struct slave_node *slave)
{
	return slave->network;
}

HAPI void slave_set_network(struct slave_node *slave, int network)
{
	slave->network = network;
}

/* End of a file */