[REFACTOR] Make `get_total_alloc_size` return int of known width
[platform/core/system/swap-manager.git] / daemon / daemon.c
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
12  * Nikita Kalyazin    <n.kalyazin@samsung.com>
13  *
14  * Licensed under the Apache License, Version 2.0 (the "License");
15  * you may not use this file except in compliance with the License.
16  * You may obtain a copy of the License at
17  *
18  * http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  * - Samsung RnD Institute Russia
29  *
30  */
31 #define __STDC_FORMAT_MACROS
32 #include <stdio.h>
33 #include <stdlib.h>             // for realpath
34 #include <string.h>             // for strtok, strcpy, strncpy
35 #include <limits.h>             // for realpath
36 #include <inttypes.h>
37
38 #include <errno.h>              // for errno
39 #include <sys/types.h>          // for accept, mkdir, opendir, readdir
40 #include <sys/socket.h>         // for accept
41 #include <sys/stat.h>           // for mkdir
42 #include <sys/eventfd.h>        // for eventfd
43 #include <sys/epoll.h>          // for epoll apis
44 #include <sys/timerfd.h>        // for timerfd
45 #include <unistd.h>             // for access, sleep
46 #include <stdbool.h>
47
48 #include <ctype.h>
49
50 #ifndef LOCALTEST
51 #include <attr/xattr.h>         // for fsetxattr
52 #include <sys/smack.h>
53 #endif
54
55 #include <linux/input.h>
56 #include <dirent.h>
57 #include <fcntl.h>
58 #include "daemon.h"
59 #include "sys_stat.h"
60 #include "utils.h"
61 #include "da_protocol.h"
62 #include "da_inst.h"
63 #include "da_data.h"
64 #include "debug.h"
65
66 #define DA_WORK_DIR                     "/home/developer/sdk_tools/da/"
67 #define DA_READELF_PATH                 "/home/developer/sdk_tools/da/readelf"
68 #define SCREENSHOT_DIR                  "/tmp/da"
69
70 #define EPOLL_SIZE                      10
71 #define MAX_APP_LAUNCH_TIME             60
72 #define MAX_CONNECT_TIMEOUT_TIME        5*60
73
74 #define MAX_DEVICE                      10
75 #define MAX_FILENAME                    128
76 #define BUF_SIZE                        1024
77 #define ARRAY_END                       (-11)
78
79 input_dev g_key_dev[MAX_DEVICE];
80 input_dev g_touch_dev[MAX_DEVICE];
81
82 // return bytes size of readed data
83 // return 0 if no data readed or error occurred
84 static int _file_read(FILE *fp, char *buffer, int size)
85 {
86         int ret = 0;
87
88         if (fp != NULL && size > 0) {
89                 ret = fread((void *)buffer, sizeof(char), size, fp);
90                 buffer[ret] = '\0';
91         } else {
92                 // fp is null
93                 if (size > 0)
94                         buffer[0] = '\0';
95                 ret = 0;        // error case
96         }
97
98         return ret;
99 }
100
101 // get input id of given input device
102 static int get_input_id(char *inputname)
103 {
104         static int query_cmd_type = 0;  // 1 if /lib/udev/input_id, 2 if udevadm
105         FILE *cmd_fp = NULL;
106         char buffer[BUF_SIZE];
107         char command[MAX_FILENAME];
108         int ret = -1;
109
110         // determine input_id query command
111         if (unlikely(query_cmd_type == 0)) {
112                 if (access("/lib/udev/input_id", F_OK) == 0) {
113                         // there is /lib/udev/input_id
114                         query_cmd_type = 1;
115                 } else {
116                         // there is not /lib/udev/input_id
117                         query_cmd_type = 2;
118                 }
119         }
120         // make command string
121         if (query_cmd_type == 1) {
122                 sprintf(command, "/lib/udev/input_id /class/input/%s",
123                         inputname);
124         } else {
125                 sprintf(command,
126                         "udevadm info --name=input/%s --query=property",
127                         inputname);
128         }
129
130         // run command
131         cmd_fp = popen(command, "r");
132         if (_file_read(cmd_fp, buffer, BUF_SIZE) < 0) {
133                 LOGE("Failed to read input_id\n");
134                 if (cmd_fp != NULL)
135                         pclose(cmd_fp);
136                 return ret;
137         }
138         // determine input id
139         if (strstr(buffer, INPUT_ID_STR_KEY)) {
140                 // key
141                 ret = INPUT_ID_KEY;
142         } else if (strstr(buffer, INPUT_ID_STR_TOUCH)) {
143                 // touch
144                 ret = INPUT_ID_TOUCH;
145         } else if (strstr(buffer, INPUT_ID_STR_KEYBOARD)) {
146                 // keyboard
147                 ret = INPUT_ID_KEY;
148         } else if (strstr(buffer, INPUT_ID_STR_TABLET)) {
149                 // touch (emulator)
150                 ret = INPUT_ID_TOUCH;
151         }
152
153         if (cmd_fp != NULL)
154                 pclose(cmd_fp);
155         return ret;
156 }
157
158 // get filename and fd of given input type devices
159 static void _get_fds(input_dev *dev, int input_id)
160 {
161         DIR *dp;
162         struct dirent *d;
163         int count = 0;
164
165         dp = opendir("/sys/class/input");
166
167         if (dp != NULL) {
168                 while ((d = readdir(dp)) != NULL) {
169                         if (!strncmp(d->d_name, "event", 5)) {
170                                 // start with "event"
171                                 // event file
172                                 if (input_id == get_input_id(d->d_name)) {
173                                         sprintf(dev[count].fileName,
174                                                 "/dev/input/%s", d->d_name);
175                                         dev[count].fd =
176                                             open(dev[count].fileName,
177                                                  O_RDWR | O_NONBLOCK);
178                                         count++;
179                                 }
180                         }
181                 }
182
183                 closedir(dp);
184         }
185         dev[count].fd = ARRAY_END;      // end of input_dev array
186 }
187
188 //static
189 void _device_write(input_dev *dev, struct input_event *in_ev)
190 {
191         int i;
192         for (i = 0; dev[i].fd != ARRAY_END; i++) {
193                 if (dev[i].fd >= 0) {
194                         write(dev[i].fd, in_ev, sizeof(struct input_event));
195                         LOGI("write(%d, %d, %d)\n",
196                              dev[i].fd, (int)in_ev, sizeof(struct input_event));
197                 }
198         }
199 }
200
201 uint64_t get_total_alloc_size()
202 {
203         int i;
204         uint64_t allocsize = 0;
205
206         for (i = 0; i < MAX_TARGET_COUNT; i++) {
207                 if (manager.target[i].socket != -1 && manager.target[i].allocmem > 0)
208                    allocsize += manager.target[i].allocmem;
209         }
210         return allocsize;
211 }
212
213 static int getEmptyTargetSlot()
214 {
215         int i;
216         for (i = 0; i < MAX_TARGET_COUNT; i++) {
217                 if (manager.target[i].socket == -1)
218                         break;
219         }
220
221         return i;
222 }
223
224 static void setEmptyTargetSlot(int index)
225 {
226         if (index >= 0 && index < MAX_TARGET_COUNT) {
227                 manager.target[index].pid = -1;
228                 manager.target[index].recv_thread = -1;
229                 manager.target[index].allocmem = 0;
230                 manager.target[index].initial_log = 0;
231                 if (manager.target[index].event_fd != -1)
232                         close(manager.target[index].event_fd);
233                 manager.target[index].event_fd = -1;
234                 if (manager.target[index].socket != -1)
235                         close(manager.target[index].socket);
236                 manager.target[index].socket = -1;
237         }
238 }
239
240 // =============================================================================
241 // start and terminate control functions
242 // =============================================================================
243
244 //start application launch timer function
245 static int start_app_launch_timer()
246 {
247         int res = 0;
248         struct epoll_event ev;
249
250         manager.app_launch_timerfd =
251             timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
252         if (manager.app_launch_timerfd > 0) {
253                 struct itimerspec ctime;
254                 ctime.it_value.tv_sec = MAX_APP_LAUNCH_TIME;
255                 ctime.it_value.tv_nsec = 0;
256                 ctime.it_interval.tv_sec = 0;
257                 ctime.it_interval.tv_nsec = 0;
258                 if (timerfd_settime(manager.app_launch_timerfd, 0, &ctime, NULL) < 0) {
259                         LOGE("fail to set app launch timer\n");
260                         close(manager.app_launch_timerfd);
261                         manager.app_launch_timerfd = -1;
262                         res = -1;
263                 } else {
264                         // add event fd to epoll list
265                         ev.events = EPOLLIN;
266                         ev.data.fd = manager.app_launch_timerfd;
267                         if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
268                                       manager.app_launch_timerfd, &ev) < 0) {
269                                 // fail to add event fd
270                                 LOGE("fail to add app launch timer fd to epoll list\n");
271                                 close(manager.app_launch_timerfd);
272                                 manager.app_launch_timerfd = -1;
273                                 res = -2;
274                         } else {
275                                 LOGI("application launch time started\n");
276                         }
277                 }
278         } else {
279                 LOGE("cannot create launch timer\n");
280                 res = -3;
281         }
282
283         return res;
284 }
285
286 //stop application launch timer
287 static int stop_app_launch_timer()
288 {
289         if (0 > epoll_ctl(manager.efd, EPOLL_CTL_DEL,
290                           manager.app_launch_timerfd, NULL))
291                 LOGW("fail to EPOLL DEL of app launch timerfd\n");
292         close(manager.app_launch_timerfd);
293         manager.app_launch_timerfd = -1;
294         return 0;
295 }
296
297 int kill_app_by_info(const struct app_info_t *app_info)
298 {
299         int res = 0;
300
301         if (app_info == NULL) {
302                 LOGE("Cannot exec app. app_info is NULL");
303                 return -1;
304         }
305
306         switch (app_info->app_type) {
307         case APP_TYPE_TIZEN:
308                 res = kill_app(app_info->exe_path);
309                 break;
310         case APP_TYPE_RUNNING:
311                 // TODO: nothing, it's running
312                 LOGI("already started\n");
313                 break;
314         case APP_TYPE_COMMON:
315                 res = kill_app(app_info->exe_path);
316                 break;
317         default:
318                 LOGE("Unknown app type %d\n", app_info->app_type);
319                 res = -1;
320                 break;
321         }
322
323         return res;
324 }
325
326 static int exec_app(const struct app_info_t *app_info)
327 {
328         int res = 0;
329
330         if (app_info == NULL) {
331                 LOGE("Cannot exec app. app_info is NULL");
332                 return -1;
333         }
334
335         switch (app_info->app_type) {
336         case APP_TYPE_TIZEN:
337                 if (exec_app_tizen(app_info->app_id, app_info->exe_path)) {
338                         LOGE("Cannot exec tizen app %s\n", app_info->app_id);
339                         res = -1;
340                 }
341                 break;
342         case APP_TYPE_RUNNING:
343                 // TODO: nothing, it's running
344                 LOGI("already started\n");
345                 break;
346         case APP_TYPE_COMMON:
347                 if (exec_app_common(app_info->exe_path)) {
348                         LOGE("Cannot exec common app %s\n", app_info->exe_path);
349                         res = -1;
350                 }
351                 break;
352         default:
353                 LOGE("Unknown app type %d\n", app_info->app_type);
354                 res = -1;
355                 break;
356         }
357
358         if (res == 0 && app_info->app_type != APP_TYPE_RUNNING)
359                 if (start_app_launch_timer() < 0)
360                         res = -1;
361
362         LOGI("ret=%d\n", res);
363         return res;
364 }
365
366 int launch_timer_start()
367 {
368         static struct epoll_event ev;
369         int res = 0;
370
371         manager.connect_timeout_timerfd =
372             timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
373         if (manager.connect_timeout_timerfd > 0) {
374                 struct itimerspec ctime;
375                 ctime.it_value.tv_sec = MAX_CONNECT_TIMEOUT_TIME;
376                 ctime.it_value.tv_nsec = 0;
377                 ctime.it_interval.tv_sec = 0;
378                 ctime.it_interval.tv_nsec = 0;
379                 if (timerfd_settime(manager.connect_timeout_timerfd, 0, &ctime, NULL) < 0) {
380                         LOGE("fail to set connect timeout timer\n");
381                         close(manager.connect_timeout_timerfd);
382                         manager.connect_timeout_timerfd = -1;
383                 } else {
384                         // add event fd to epoll list
385                         ev.events = EPOLLIN;
386                         ev.data.fd = manager.connect_timeout_timerfd;
387                         if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
388                                       manager.connect_timeout_timerfd, &ev) < 0)
389                         {
390                                 // fail to add event fd
391                                 LOGE("fail to add app connection timeout timer fd to epoll list\n");
392                                 close(manager.connect_timeout_timerfd);
393                                 manager.connect_timeout_timerfd = -1;
394                         } else {
395                                 LOGI("connection timeout timer started\n");
396                         }
397                 }
398         } else {
399                 LOGE("cannot create connection timeout timer\n");
400         }
401
402         LOGI("ret=%d\n", res);
403         return res;
404 }
405
406 static void epoll_add_input_events();
407 static void epoll_del_input_events();
408
409 int prepare_profiling()
410 {
411         struct app_list_t *app = NULL;
412         const struct app_info_t *app_info = NULL;
413
414         app_info = app_info_get_first(&app);
415         if (app_info == NULL) {
416                 LOGE("No app info found\n");
417                 return -1;
418         }
419
420         //all apps
421         while (app_info != NULL) {
422                 if (kill_app_by_info(app_info) != 0) {
423                         LOGE("kill app failed\n");
424                         return -1;
425                 }
426                 app_info = app_info_get_next(&app);
427         }
428         //init rw for systeminfo
429         //init recv send network systeminfo
430         sys_stat_prepare();
431         return 0;
432
433 }
434
435 int start_profiling()
436 {
437         struct app_list_t *app = NULL;
438         const struct app_info_t *app_info = NULL;
439         int res = 0;
440
441         app_info = app_info_get_first(&app);
442         if (app_info == NULL) {
443                 LOGE("No app info found\n");
444                 return -1;
445         }
446         // remove previous screen capture files
447         remove_indir(SCREENSHOT_DIR);
448         if (mkdir(SCREENSHOT_DIR, 0777) == -1 && errno != EEXIST)
449                 LOGW("Failed to create directory for screenshot : %s\n",
450                      strerror(errno));
451
452 #ifndef LOCALTEST
453         smack_lsetlabel(SCREENSHOT_DIR, "*", SMACK_LABEL_ACCESS);
454 #endif
455
456         if (samplingStart() < 0) {
457                 LOGE("Cannot start sampling\n");
458                 res = -1;
459                 goto exit;
460         }
461
462         if (IS_OPT_SET(FL_RECORDING))
463                 epoll_add_input_events();
464
465         if (exec_app(app_info)) {
466                 LOGE("Cannot exec app\n");
467                 res = -1;
468                 goto recording_stop;
469         }
470
471         goto exit;
472
473  recording_stop:
474         if (IS_OPT_SET(FL_RECORDING))
475                 epoll_del_input_events();
476         samplingStop();
477
478  exit:
479         LOGI("return %d\n", res);
480         return res;
481 }
482
483 void stop_profiling(void)
484 {
485         if (IS_OPT_SET(FL_RECORDING))
486                 epoll_del_input_events();
487         samplingStop();
488 }
489
490 static void reconfigure_recording(struct conf_t conf)
491 {
492         uint64_t old_features = prof_session.conf.use_features0;
493         uint64_t new_features = conf.use_features0;
494         uint64_t to_enable = (new_features ^ old_features) & new_features;
495         uint64_t to_disable = (new_features ^ old_features) & old_features;
496
497         if (IS_OPT_SET_IN(FL_RECORDING, to_disable)) {
498                 epoll_del_input_events();
499                 prof_session.conf.use_features0 &= ~FL_RECORDING;
500         }
501
502         if (IS_OPT_SET_IN(FL_RECORDING, to_enable)) {
503                 epoll_add_input_events();
504                 prof_session.conf.use_features0 |= FL_RECORDING;
505         }
506
507 }
508
509 int reconfigure(struct conf_t conf)
510 {
511         reconfigure_recording(conf);
512
513         samplingStop();
514         memcpy(&prof_session.conf, &conf, sizeof(conf));
515         if (samplingStart() < 0) {
516                 LOGE("Cannot start sampling\n");
517                 return -1;
518         }
519
520         return 0;
521 }
522
523 // just send stop message to all target process
524 static void terminate_all_target()
525 {
526         int i;
527         ssize_t sendlen;
528         msg_target_t sendlog;
529
530         sendlog.type = MSG_STOP;
531         sendlog.length = 0;
532
533         for (i = 0; i < MAX_TARGET_COUNT; i++) {
534                 if (manager.target[i].socket != -1) {
535                         sendlen = send(manager.target[i].socket, &sendlog,
536                                        sizeof(sendlog.type) +
537                                        sizeof(sendlog.length), MSG_NOSIGNAL);
538                         if (sendlen != -1) {
539                                 // send to only first main target proces
540                                 LOGI("TERMINATE send exit msg (socket %d) "
541                                      "by terminate_all_target()\n",
542                                      manager.target[i].socket);
543                                 break;
544                         }
545                 }
546         }
547 }
548
549 // terminate all target and wait for threads
550 void terminate_all()
551 {
552         int i;
553         terminate_all_target();
554
555         // wait for all other thread exit
556         for (i = 0; i < MAX_TARGET_COUNT; i++) {
557                 if (manager.target[i].recv_thread != -1) {
558                         LOGI("join recv thread [%d] is started\n", i);
559                         pthread_join(manager.target[i].recv_thread, NULL);
560                         LOGI("join recv thread %d. done\n", i);
561                 }
562         }
563 }
564
565 // terminate all profiling by critical error
566 // TODO: don't send data to host
567 static void terminate_error(char *errstr, int send_to_host)
568 {
569         LOGE("termination all with err '%s'\n", errstr);
570         struct msg_data_t *msg = NULL;
571         if (send_to_host != 0) {
572                 msg = gen_message_error(errstr);
573                 if (msg) {
574                         write_to_buf(msg);
575                         free_msg_data(msg);
576                 } else {
577                         LOGI("cannot generate error message\n");
578                 }
579         }
580         terminate_all();
581 }
582
583 #define MAX_EVENTS_NUM 10
584 static int deviceEventHandler(input_dev *dev, int input_type)
585 {
586         int ret = 0;
587         ssize_t size = 0;
588         int count = 0;
589         struct input_event in_ev[MAX_EVENTS_NUM];
590         struct msg_data_t *log;
591
592         if (input_type == INPUT_ID_TOUCH || input_type == INPUT_ID_KEY) {
593                 do {
594                         size = read(dev->fd, &in_ev[count], sizeof(*in_ev));
595                         if (size > 0)
596                                 count++;
597                 } while (count < MAX_EVENTS_NUM && size > 0);
598
599                 if (count) {
600                         LOGI("read %d %s events\n",
601                              count,
602                              input_type == INPUT_ID_KEY ? STR_KEY : STR_TOUCH);
603                         log = gen_message_event(in_ev, count, input_type);
604                         printBuf((char *)log, MSG_DATA_HDR_LEN + log->len);
605                         write_to_buf(log);
606                         free_msg_data(log);
607                 }
608         } else {
609                 LOGW("unknown input_type\n");
610                 ret = 1;        // it is not error
611         }
612         return ret;
613 }
614
615 static int target_event_pid_handler(int index, uint64_t msg)
616 {
617         struct app_list_t *app = NULL;
618         struct app_info_t *app_info = NULL;
619         if (index == 0) {       // main application
620                 app_info = app_info_get_first(&app);
621                 if (app_info == NULL) {
622                         LOGE("No app info found\n");
623                         return -1;
624                 }
625
626                 while (app_info != NULL) {
627                         if (is_same_app_process(app_info->exe_path,
628                                                 manager.target[index].pid))
629                                 break;
630                         app_info = app_info_get_next(&app);
631                 }
632
633                 if (app_info == NULL) {
634                         LOGE("pid %d not found in app list\n",
635                              manager.target[index].pid);
636                         return -1;
637                 }
638
639                 if (start_replay() != 0) {
640                         LOGE("Cannot start replay thread\n");
641                         return -1;
642                 }
643         }
644         manager.target[index].initial_log = 1;
645         return 0;
646 }
647
648 static int target_event_stop_handler(int epollfd, int index, uint64_t msg)
649 {
650         LOGI("target close, socket(%d), pid(%d) : (remaining %d target)\n",
651              manager.target[index].socket, manager.target[index].pid,
652              manager.target_count - 1);
653
654         if (index == 0)         // main application
655                 stop_replay();
656
657         if (0 > epoll_ctl(epollfd, EPOLL_CTL_DEL,
658                           manager.target[index].event_fd, NULL))
659                 LOGW("fail to EPOLL DEL of event fd(%d)\n", index);
660
661         setEmptyTargetSlot(index);
662         // all target client are closed
663         if (0 == __sync_sub_and_fetch(&manager.target_count, 1)) {
664                 LOGI("all targets are stopped\n");
665                 if (stop_all() != ERR_NO)
666                         LOGE("Stop failed\n");
667                 return -11;
668         }
669
670         return 0;
671 }
672
673 // return 0 if normal case
674 // return plus value if non critical error occur
675 // return minus value if critical error occur
676 // return -11 if all target process closed
677 static int target_event_handler(int epollfd, int index, uint64_t msg)
678 {
679         int err = 0;
680         if (msg & EVENT_PID)
681                 err = target_event_pid_handler(index, msg);
682         if (err)
683                 return err;
684
685         if (msg & EVENT_STOP || msg & EVENT_ERROR)
686                 err = target_event_stop_handler(epollfd, index, msg);
687
688         return err;
689 }
690
691 /**
692  * return 0 if normal case
693  * return plus value if non critical error occur
694  * return minus value if critical error occur
695  */
696 static int targetServerHandler(int efd)
697 {
698         msg_target_t log;
699         struct epoll_event ev;
700
701         int index = getEmptyTargetSlot();
702         if (index == MAX_TARGET_COUNT) {
703                 LOGW("Max target number(8) reached, no more target can connected\n");
704                 return 1;
705         }
706
707         manager.target[index].socket =
708             accept(manager.target_server_socket, NULL, NULL);
709
710         if (manager.target[index].socket >= 0) {
711                 /* accept succeed */
712                 fd_setup_smack_attributes(manager.target[index].socket);
713
714                 /* send config message to target process */
715                 log.type = MSG_OPTION;
716                 log.length = sprintf(log.data, "%lu",
717                                      (unsigned long int)prof_session.conf.use_features0);
718                 if (0 > send(manager.target[index].socket, &log,
719                              sizeof(log.type) + sizeof(log.length) + log.length,
720                              MSG_NOSIGNAL))
721                         LOGE("fail to send data to target index(%d)\n", index);
722
723                 // make event fd
724                 manager.target[index].event_fd = eventfd(0, EFD_NONBLOCK);
725                 if (manager.target[index].event_fd == -1) {
726                         // fail to make event fd
727                         LOGE("fail to make event fd for socket (%d)\n",
728                              manager.target[index].socket);
729                         goto TARGET_CONNECT_FAIL;
730                 }
731
732                 // add event fd to epoll list
733                 ev.events = EPOLLIN;
734                 ev.data.fd = manager.target[index].event_fd;
735                 if (epoll_ctl(efd, EPOLL_CTL_ADD, manager.target[index].event_fd, &ev) < 0) {
736                         // fail to add event fd
737                         LOGE("fail to add event fd to epoll list for socket (%d)\n",
738                              manager.target[index].socket);
739                         goto TARGET_CONNECT_FAIL;
740                 }
741
742                 // make recv thread for target
743                 if (makeRecvThread(index) != 0) {
744                         // fail to make recv thread
745                         LOGE("fail to make recv thread for socket (%d)\n",
746                              manager.target[index].socket);
747                         if (0 > epoll_ctl(efd, EPOLL_CTL_DEL,
748                                           manager.target[index].event_fd, NULL))
749                                 LOGW("fail to EPOLL DEL of event fd(%d)\n", index);
750                         goto TARGET_CONNECT_FAIL;
751                 }
752
753                 if (manager.app_launch_timerfd >= 0) {
754                         LOGI("release launch timer\n");
755                         if (stop_app_launch_timer() < 0)
756                                 LOGE("cannot stop app launch timer\n");
757                 }
758
759                 LOGI("target connected = %d(running %d target)\n",
760                      manager.target[index].socket, manager.target_count + 1);
761
762                 manager.target_count++;
763                 return 0;
764         } else {
765                 // accept error
766                 LOGE("Failed to accept at target server socket\n");
767         }
768
769  TARGET_CONNECT_FAIL:
770         if (manager.target_count == 0) {
771                 // if this connection is main connection
772                 return -1;
773         } else {
774                 // if this connection is not main connection then ignore process by error
775                 setEmptyTargetSlot(index);
776                 return 1;
777         }
778 }
779
780 // return 0 if normal case
781 // return plus value if non critical error occur
782 // return minus value if critical error occur
783 static int hostServerHandler(int efd)
784 {
785         static int hostserverorder = 0;
786         int csocket;
787         struct epoll_event ev;
788
789         if (hostserverorder > 1)        // control and data socket connected already
790                 return 1;               // ignore
791
792         csocket = accept(manager.host_server_socket, NULL, NULL);
793
794         if (csocket >= 0) {
795                 // accept succeed
796                 ev.events = EPOLLIN;
797                 ev.data.fd = csocket;
798                 if (epoll_ctl(efd, EPOLL_CTL_ADD, csocket, &ev) < 0) {
799                         // consider as accept fail
800                         LOGE("Failed to add socket fd to epoll list\n");
801                         close(csocket);
802                         return -1;
803                 }
804
805                 if (hostserverorder == 0) {
806                         manager.host.control_socket = csocket;
807                         unlink_portfile();
808                         LOGI("host control socket connected = %d\n", csocket);
809                 } else {
810                         manager.host.data_socket = csocket;
811                         LOGI("host data socket connected = %d\n", csocket);
812                 }
813
814                 hostserverorder++;
815                 return 0;
816         } else {
817                 // accept error
818                 LOGE("Failed to accept from host server socket\n");
819                 return -1;
820         }
821 }
822
823 // return plus value if non critical error occur
824 // return minus value if critical error occur
825 // return -11 if socket closed
826
827 static int controlSocketHandler(int efd)
828 {
829         ssize_t recv_len;
830         struct msg_t msg_head;
831         struct msg_t *msg;
832         int res = 0;
833
834         if (manager.connect_timeout_timerfd >= 0) {
835                 LOGI("release connect timeout timer\n");
836                 if (0 > epoll_ctl(efd, EPOLL_CTL_DEL,
837                                   manager.connect_timeout_timerfd, NULL))
838                         LOGW("fail to EPOLL DEL of timeout timer fd\n");
839                 close(manager.connect_timeout_timerfd);
840                 manager.connect_timeout_timerfd = -1;
841         }
842         // Receive header
843         recv_len = recv(manager.host.control_socket,
844                         &msg_head, MSG_CMD_HDR_LEN, 0);
845         // error or close request from host
846         if (recv_len == -1 || recv_len == 0)
847                 return -11;
848         else {
849                 msg = malloc(MSG_CMD_HDR_LEN + msg_head.len);
850                 if (!msg) {
851                         LOGE("Cannot alloc msg\n");
852                         sendACKToHost(msg_head.id, ERR_WRONG_MESSAGE_FORMAT, 0, 0);
853                         return -1;
854                 }
855                 msg->id = msg_head.id;
856                 msg->len = msg_head.len;
857                 if (msg->len > 0) {
858                         // Receive payload (if exists)
859                         recv_len = recv(manager.host.control_socket,
860                                         msg->payload, msg->len, MSG_WAITALL);
861                         if (recv_len == -1)
862                                 return -11;
863                 }
864                 printBuf((char *)msg, MSG_CMD_HDR_LEN + msg->len);
865                 res = host_message_handler(msg);
866                 free(msg);
867         }
868
869         return res;
870 }
871
872 static void epoll_add_input_events()
873 {
874         struct epoll_event ev;
875         int i;
876
877         // add device fds to epoll event pool
878         ev.events = EPOLLIN;
879         for (i = 0; g_key_dev[i].fd != ARRAY_END; i++) {
880                 if (g_key_dev[i].fd >= 0) {
881                         ev.data.fd = g_key_dev[i].fd;
882                         if (epoll_ctl(manager.efd,
883                                       EPOLL_CTL_ADD, g_key_dev[i].fd, &ev) < 0
884                                       && errno != EEXIST)
885                                 LOGE("keyboard device file epoll_ctl error: %s\n", strerror(errno));
886                 }
887         }
888
889         ev.events = EPOLLIN;
890         for (i = 0; g_touch_dev[i].fd != ARRAY_END; i++) {
891                 if (g_touch_dev[i].fd >= 0) {
892                         ev.data.fd = g_touch_dev[i].fd;
893                         if (epoll_ctl(manager.efd,
894                                       EPOLL_CTL_ADD,
895                                       g_touch_dev[i].fd, &ev) < 0
896                                       && errno != EEXIST)
897                                 LOGE("touch device file epoll_ctl error: %s\n", strerror(errno));
898                 }
899         }
900 }
901
902 static void epoll_del_input_events()
903 {
904         int i;
905
906         // remove device fds from epoll event pool
907         for (i = 0; g_key_dev[i].fd != ARRAY_END; i++)
908                 if (g_key_dev[i].fd >= 0)
909                         if (epoll_ctl(manager.efd,
910                                       EPOLL_CTL_DEL, g_key_dev[i].fd, NULL) < 0)
911                                 LOGE("keyboard device file epoll_ctl error: %s\n", strerror(errno));
912
913         for (i = 0; g_touch_dev[i].fd != ARRAY_END; i++)
914                 if (g_touch_dev[i].fd >= 0)
915                         if (epoll_ctl(manager.efd,
916                                       EPOLL_CTL_DEL,
917                                       g_touch_dev[i].fd, NULL) < 0)
918                                 LOGE("touch device file epoll_ctl error: %s\n", strerror(errno));
919 }
920
921 static bool initialize_epoll_events(void)
922 {
923         struct epoll_event ev;
924
925         if ((manager.efd = epoll_create1(0)) < 0) {
926                 LOGE("epoll creation error\n");
927                 return false;
928         }
929         // add server sockets to epoll event pool
930         ev.events = EPOLLIN;
931         ev.data.fd = manager.host_server_socket;
932         if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
933                       manager.host_server_socket, &ev) < 0) {
934                 LOGE("Host server socket epoll_ctl error\n");
935                 return false;
936         }
937         ev.events = EPOLLIN;
938         ev.data.fd = manager.target_server_socket;
939         if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
940                       manager.target_server_socket, &ev) < 0) {
941                 LOGE("Target server socket epoll_ctl error\n");
942                 return false;
943         }
944         return true;
945 }
946
947 // return 0 for normal case
948 int daemonLoop()
949 {
950         int return_value = 0;
951         struct epoll_event *events = malloc(EPOLL_SIZE * sizeof(*events));
952
953         _get_fds(g_key_dev, INPUT_ID_KEY);
954         _get_fds(g_touch_dev, INPUT_ID_TOUCH);
955
956         if (!events) {
957                 LOGE("Out of memory when allocate epoll event pool\n");
958                 return_value = -1;
959                 goto END_EVENT;
960         }
961         if (!initialize_epoll_events()) {
962                 return_value = -1;
963                 goto END_EFD;
964         }
965
966         if (launch_timer_start() < 0) {
967                 LOGE("Launch timer start failed\n");
968                 return_value = -1;
969                 goto END_EFD;
970         }
971
972         init_prof_session(&prof_session);
973
974         // handler loop
975         while (1) {
976                 int i, k;
977                 ssize_t recvLen;
978                 // number of occured events
979                 int numevent = epoll_wait(manager.efd, events, EPOLL_SIZE, -1);
980                 if (numevent <= 0) {
981                         LOGE("Failed to epoll_wait : num of event(%d), errno(%d)\n", numevent, errno);
982                         continue;
983                 }
984
985                 for (i = 0; i < numevent; i++) {
986                         // check for request from event fd
987                         for (k = 0; k < MAX_TARGET_COUNT; k++) {
988                                 if (manager.target[k].socket != -1 &&
989                                     events[i].data.fd == manager.target[k].event_fd) {
990                                         uint64_t u;
991                                         recvLen = read(manager.target[k].event_fd, &u, sizeof(uint64_t));
992                                         if (recvLen != sizeof(uint64_t)) {
993                                                 // maybe closed, but ignoring is more safe then
994                                                 // removing fd from epoll list
995                                         } else {
996                                                 if (-11 == target_event_handler(manager.efd, k, u)) {
997                                                         LOGI("all target process is closed\n");
998                                                         continue;
999                                                 }
1000                                         }
1001                                         break;
1002                                 }
1003                         }
1004
1005                         if (k != MAX_TARGET_COUNT)
1006                                 continue;
1007
1008                         // check for request from device fd
1009                         for (k = 0; g_touch_dev[k].fd != ARRAY_END; k++) {
1010                                 if (g_touch_dev[k].fd >= 0 &&
1011                                     events[i].data.fd == g_touch_dev[k].fd) {
1012                                         if (deviceEventHandler(&g_touch_dev[k],
1013                                             INPUT_ID_TOUCH) < 0) {
1014                                                 LOGE("Internal DA framework error, "
1015                                                      "Please re-run the profiling (touch dev)\n");
1016                                                 continue;
1017                                         }
1018                                         break;
1019                                 }
1020                         }
1021
1022                         if (g_touch_dev[k].fd != ARRAY_END)
1023                                 continue;
1024
1025                         for (k = 0; g_key_dev[k].fd != ARRAY_END; k++) {
1026                                 if (g_key_dev[k].fd >= 0 &&
1027                                     events[i].data.fd == g_key_dev[k].fd) {
1028                                         if (deviceEventHandler(&g_key_dev[k], INPUT_ID_KEY) < 0) {
1029                                                 LOGE("Internal DA framework error, "
1030                                                      "Please re-run the profiling (key dev)\n");
1031                                                 continue;
1032                                         }
1033                                         break;
1034                                 }
1035                         }
1036
1037                         if (g_key_dev[k].fd != ARRAY_END)
1038                                 continue;
1039
1040                         // connect request from target
1041                         if (events[i].data.fd == manager.target_server_socket) {
1042                                 if (targetServerHandler(manager.efd) < 0) {
1043                                         // critical error
1044                                         terminate_error("Internal DA framework error, "
1045                                                         "Please re-run the profiling "
1046                                                         "(targetServerHandler)\n", 1);
1047                                         continue;
1048                                 }
1049                         } else if (events[i].data.fd == manager.host_server_socket) {
1050                         // connect request from host
1051                                 int result = hostServerHandler(manager.efd);
1052                                 if (result < 0) {
1053                                         LOGE("Internal DA framework error (hostServerHandler)\n");
1054                                         continue;
1055                                 }
1056                         } else if (events[i].data.fd == manager.host.control_socket) {
1057                         // control message from host
1058                                 int result = controlSocketHandler(manager.efd);
1059                                 if (result == -11) {
1060                                         // socket close
1061                                         //if the host disconnected.
1062                                         //In all other cases daemon must report an error and continue the loop
1063                                         //close connect_timeoutt and host socket and quit
1064                                         LOGI("Connection closed. Termination. (%d)\n",
1065                                               manager.host.control_socket);
1066                                         return_value = 0;
1067                                         goto END_EFD;
1068                                 } else if (result < 0) {
1069                                         LOGE("Control socket handler.\n");
1070                                 }
1071                         } else if (events[i].data.fd == manager.host.data_socket) {
1072                                 char recvBuf[32];
1073                                 recvLen = recv(manager.host.data_socket, recvBuf, 32, MSG_DONTWAIT);
1074                                 if (recvLen == 0) {
1075                                         // close data socket
1076                                         if (0 > epoll_ctl(manager.efd,
1077                                                           EPOLL_CTL_DEL,
1078                                                           manager.host.data_socket,
1079                                                           NULL))
1080                                                 LOGW("fail to EPOLL DEL of host data socket\n");
1081                                         close(manager.host.data_socket);
1082                                         manager.host.data_socket = -1;
1083                                         // TODO: finish transfer thread
1084                                 }
1085
1086                                 LOGI("host message from data socket %d\n",
1087                                      recvLen);
1088                         } else if (events[i].data.fd == manager.app_launch_timerfd) {
1089                                 // check for application launch timerfd
1090                                 // send to host timeout error message for launching application
1091                                 LOGE("Failed to launch application\n");
1092                                 if (stop_app_launch_timer() < 0)
1093                                         LOGE("cannot stop app launch timer\n");
1094                                 continue;
1095                         } else if (events[i].data.fd == manager.connect_timeout_timerfd) {
1096                                 // check for connection timeout timerfd
1097                                 // send to host timeout error message for launching application
1098                                 terminate_error("no incoming connections", 1);
1099                                 if (0 > epoll_ctl(manager.efd, EPOLL_CTL_DEL,
1100                                                   manager.connect_timeout_timerfd,
1101                                                   NULL))
1102                                         LOGW("fail to EPOLL DEL of timeout timer fd\n");
1103                                 close(manager.connect_timeout_timerfd);
1104                                 manager.connect_timeout_timerfd = -1;
1105                                 LOGE("No connection in %d sec. shutdown.\n",
1106                                      MAX_CONNECT_TIMEOUT_TIME);
1107                                 goto END_EFD;
1108                         } else {
1109                                 // unknown socket
1110                                 // never happened
1111                                 LOGW("Unknown socket fd (%d)\n",
1112                                      events[i].data.fd);
1113                         }
1114                 }
1115         }
1116
1117  END_EFD:
1118         LOGI("close efd\n");
1119         close(manager.efd);
1120  END_EVENT:
1121         free(events);
1122         return return_value;
1123 }