4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
18 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 * - Samsung RnD Institute Russia
31 #define __STDC_FORMAT_MACROS
33 #include <stdlib.h> // for realpath
34 #include <string.h> // for strtok, strcpy, strncpy
35 #include <limits.h> // for realpath
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
51 #include <attr/xattr.h> // for fsetxattr
52 #include <sys/smack.h>
55 #include <linux/input.h>
61 #include "da_protocol.h"
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"
71 #define MAX_APP_LAUNCH_TIME 60
72 #define MAX_CONNECT_TIMEOUT_TIME 5*60
75 #define MAX_FILENAME 128
77 #define ARRAY_END (-11)
79 input_dev g_key_dev[MAX_DEVICE];
80 input_dev g_touch_dev[MAX_DEVICE];
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)
88 if (fp != NULL && size > 0) {
89 ret = fread((void *)buffer, sizeof(char), size, fp);
95 ret = 0; // error case
101 // get input id of given input device
102 static int get_input_id(char *inputname)
104 static int query_cmd_type = 0; // 1 if /lib/udev/input_id, 2 if udevadm
106 char buffer[BUF_SIZE];
107 char command[MAX_FILENAME];
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
116 // there is not /lib/udev/input_id
120 // make command string
121 if (query_cmd_type == 1) {
122 sprintf(command, "/lib/udev/input_id /class/input/%s",
126 "udevadm info --name=input/%s --query=property",
131 cmd_fp = popen(command, "r");
132 if (_file_read(cmd_fp, buffer, BUF_SIZE) < 0) {
133 LOGE("Failed to read input_id\n");
138 // determine input id
139 if (strstr(buffer, INPUT_ID_STR_KEY)) {
142 } else if (strstr(buffer, INPUT_ID_STR_TOUCH)) {
144 ret = INPUT_ID_TOUCH;
145 } else if (strstr(buffer, INPUT_ID_STR_KEYBOARD)) {
148 } else if (strstr(buffer, INPUT_ID_STR_TABLET)) {
150 ret = INPUT_ID_TOUCH;
158 // get filename and fd of given input type devices
159 static void _get_fds(input_dev *dev, int input_id)
165 dp = opendir("/sys/class/input");
168 while ((d = readdir(dp)) != NULL) {
169 if (!strncmp(d->d_name, "event", 5)) {
170 // start with "event"
172 if (input_id == get_input_id(d->d_name)) {
173 sprintf(dev[count].fileName,
174 "/dev/input/%s", d->d_name);
176 open(dev[count].fileName,
177 O_RDWR | O_NONBLOCK);
185 dev[count].fd = ARRAY_END; // end of input_dev array
189 void _device_write(input_dev *dev, struct input_event *in_ev)
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));
201 uint64_t get_total_alloc_size()
204 uint64_t allocsize = 0;
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;
213 static int getEmptyTargetSlot()
216 for (i = 0; i < MAX_TARGET_COUNT; i++) {
217 if (manager.target[i].socket == -1)
224 static void setEmptyTargetSlot(int index)
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;
240 // =============================================================================
241 // start and terminate control functions
242 // =============================================================================
244 //start application launch timer function
245 static int start_app_launch_timer()
248 struct epoll_event ev;
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;
264 // add event fd to epoll list
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;
275 LOGI("application launch time started\n");
279 LOGE("cannot create launch timer\n");
286 //stop application launch timer
287 static int stop_app_launch_timer()
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;
297 int kill_app_by_info(const struct app_info_t *app_info)
301 if (app_info == NULL) {
302 LOGE("Cannot exec app. app_info is NULL");
306 switch (app_info->app_type) {
308 res = kill_app(app_info->exe_path);
310 case APP_TYPE_RUNNING:
311 // TODO: nothing, it's running
312 LOGI("already started\n");
314 case APP_TYPE_COMMON:
315 res = kill_app(app_info->exe_path);
318 LOGE("Unknown app type %d\n", app_info->app_type);
326 static int exec_app(const struct app_info_t *app_info)
330 if (app_info == NULL) {
331 LOGE("Cannot exec app. app_info is NULL");
335 switch (app_info->app_type) {
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);
342 case APP_TYPE_RUNNING:
343 // TODO: nothing, it's running
344 LOGI("already started\n");
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);
353 LOGE("Unknown app type %d\n", app_info->app_type);
358 if (res == 0 && app_info->app_type != APP_TYPE_RUNNING)
359 if (start_app_launch_timer() < 0)
362 LOGI("ret=%d\n", res);
366 int launch_timer_start()
368 static struct epoll_event ev;
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;
384 // add event fd to epoll list
386 ev.data.fd = manager.connect_timeout_timerfd;
387 if (epoll_ctl(manager.efd, EPOLL_CTL_ADD,
388 manager.connect_timeout_timerfd, &ev) < 0)
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;
395 LOGI("connection timeout timer started\n");
399 LOGE("cannot create connection timeout timer\n");
402 LOGI("ret=%d\n", res);
406 static void epoll_add_input_events();
407 static void epoll_del_input_events();
409 int prepare_profiling()
411 struct app_list_t *app = NULL;
412 const struct app_info_t *app_info = NULL;
414 app_info = app_info_get_first(&app);
415 if (app_info == NULL) {
416 LOGE("No app info found\n");
421 while (app_info != NULL) {
422 if (kill_app_by_info(app_info) != 0) {
423 LOGE("kill app failed\n");
426 app_info = app_info_get_next(&app);
428 //init rw for systeminfo
429 //init recv send network systeminfo
435 int start_profiling()
437 struct app_list_t *app = NULL;
438 const struct app_info_t *app_info = NULL;
441 app_info = app_info_get_first(&app);
442 if (app_info == NULL) {
443 LOGE("No app info found\n");
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",
453 smack_lsetlabel(SCREENSHOT_DIR, "*", SMACK_LABEL_ACCESS);
456 if (samplingStart() < 0) {
457 LOGE("Cannot start sampling\n");
462 if (IS_OPT_SET(FL_RECORDING))
463 epoll_add_input_events();
465 if (exec_app(app_info)) {
466 LOGE("Cannot exec app\n");
474 if (IS_OPT_SET(FL_RECORDING))
475 epoll_del_input_events();
479 LOGI("return %d\n", res);
483 void stop_profiling(void)
485 if (IS_OPT_SET(FL_RECORDING))
486 epoll_del_input_events();
490 static void reconfigure_recording(struct conf_t conf)
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;
497 if (IS_OPT_SET_IN(FL_RECORDING, to_disable)) {
498 epoll_del_input_events();
499 prof_session.conf.use_features0 &= ~FL_RECORDING;
502 if (IS_OPT_SET_IN(FL_RECORDING, to_enable)) {
503 epoll_add_input_events();
504 prof_session.conf.use_features0 |= FL_RECORDING;
509 int reconfigure(struct conf_t conf)
511 reconfigure_recording(conf);
514 memcpy(&prof_session.conf, &conf, sizeof(conf));
515 if (samplingStart() < 0) {
516 LOGE("Cannot start sampling\n");
523 // just send stop message to all target process
524 static void terminate_all_target()
528 msg_target_t sendlog;
530 sendlog.type = MSG_STOP;
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);
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);
549 // terminate all target and wait for threads
553 terminate_all_target();
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);
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)
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);
577 LOGI("cannot generate error message\n");
583 #define MAX_EVENTS_NUM 10
584 static int deviceEventHandler(input_dev *dev, int input_type)
589 struct input_event in_ev[MAX_EVENTS_NUM];
590 struct msg_data_t *log;
592 if (input_type == INPUT_ID_TOUCH || input_type == INPUT_ID_KEY) {
594 size = read(dev->fd, &in_ev[count], sizeof(*in_ev));
597 } while (count < MAX_EVENTS_NUM && size > 0);
600 LOGI("read %d %s events\n",
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);
609 LOGW("unknown input_type\n");
610 ret = 1; // it is not error
615 static int target_event_pid_handler(int index, uint64_t msg)
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");
626 while (app_info != NULL) {
627 if (is_same_app_process(app_info->exe_path,
628 manager.target[index].pid))
630 app_info = app_info_get_next(&app);
633 if (app_info == NULL) {
634 LOGE("pid %d not found in app list\n",
635 manager.target[index].pid);
639 if (start_replay() != 0) {
640 LOGE("Cannot start replay thread\n");
644 manager.target[index].initial_log = 1;
648 static int target_event_stop_handler(int epollfd, int index, uint64_t msg)
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);
654 if (index == 0) // main application
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);
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");
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)
681 err = target_event_pid_handler(index, msg);
685 if (msg & EVENT_STOP || msg & EVENT_ERROR)
686 err = target_event_stop_handler(epollfd, index, msg);
692 * return 0 if normal case
693 * return plus value if non critical error occur
694 * return minus value if critical error occur
696 static int targetServerHandler(int efd)
699 struct epoll_event ev;
701 int index = getEmptyTargetSlot();
702 if (index == MAX_TARGET_COUNT) {
703 LOGW("Max target number(8) reached, no more target can connected\n");
707 manager.target[index].socket =
708 accept(manager.target_server_socket, NULL, NULL);
710 if (manager.target[index].socket >= 0) {
712 fd_setup_smack_attributes(manager.target[index].socket);
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,
721 LOGE("fail to send data to target index(%d)\n", index);
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;
732 // add event fd to epoll list
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;
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;
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");
759 LOGI("target connected = %d(running %d target)\n",
760 manager.target[index].socket, manager.target_count + 1);
762 manager.target_count++;
766 LOGE("Failed to accept at target server socket\n");
770 if (manager.target_count == 0) {
771 // if this connection is main connection
774 // if this connection is not main connection then ignore process by error
775 setEmptyTargetSlot(index);
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)
785 static int hostserverorder = 0;
787 struct epoll_event ev;
789 if (hostserverorder > 1) // control and data socket connected already
792 csocket = accept(manager.host_server_socket, NULL, NULL);
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");
805 if (hostserverorder == 0) {
806 manager.host.control_socket = csocket;
808 LOGI("host control socket connected = %d\n", csocket);
810 manager.host.data_socket = csocket;
811 LOGI("host data socket connected = %d\n", csocket);
818 LOGE("Failed to accept from host server socket\n");
823 // return plus value if non critical error occur
824 // return minus value if critical error occur
825 // return -11 if socket closed
827 static int controlSocketHandler(int efd)
830 struct msg_t msg_head;
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;
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)
849 msg = malloc(MSG_CMD_HDR_LEN + msg_head.len);
851 LOGE("Cannot alloc msg\n");
852 sendACKToHost(msg_head.id, ERR_WRONG_MESSAGE_FORMAT, 0, 0);
855 msg->id = msg_head.id;
856 msg->len = msg_head.len;
858 // Receive payload (if exists)
859 recv_len = recv(manager.host.control_socket,
860 msg->payload, msg->len, MSG_WAITALL);
864 printBuf((char *)msg, MSG_CMD_HDR_LEN + msg->len);
865 res = host_message_handler(msg);
872 static void epoll_add_input_events()
874 struct epoll_event ev;
877 // add device fds to epoll event pool
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
885 LOGE("keyboard device file epoll_ctl error: %s\n", strerror(errno));
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,
895 g_touch_dev[i].fd, &ev) < 0
897 LOGE("touch device file epoll_ctl error: %s\n", strerror(errno));
902 static void epoll_del_input_events()
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));
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,
917 g_touch_dev[i].fd, NULL) < 0)
918 LOGE("touch device file epoll_ctl error: %s\n", strerror(errno));
921 static bool initialize_epoll_events(void)
923 struct epoll_event ev;
925 if ((manager.efd = epoll_create1(0)) < 0) {
926 LOGE("epoll creation error\n");
929 // add server sockets to epoll event pool
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");
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");
947 // return 0 for normal case
950 int return_value = 0;
951 struct epoll_event *events = malloc(EPOLL_SIZE * sizeof(*events));
953 _get_fds(g_key_dev, INPUT_ID_KEY);
954 _get_fds(g_touch_dev, INPUT_ID_TOUCH);
957 LOGE("Out of memory when allocate epoll event pool\n");
961 if (!initialize_epoll_events()) {
966 if (launch_timer_start() < 0) {
967 LOGE("Launch timer start failed\n");
972 init_prof_session(&prof_session);
978 // number of occured events
979 int numevent = epoll_wait(manager.efd, events, EPOLL_SIZE, -1);
981 LOGE("Failed to epoll_wait : num of event(%d), errno(%d)\n", numevent, errno);
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) {
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
996 if (-11 == target_event_handler(manager.efd, k, u)) {
997 LOGI("all target process is closed\n");
1005 if (k != MAX_TARGET_COUNT)
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");
1022 if (g_touch_dev[k].fd != ARRAY_END)
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");
1037 if (g_key_dev[k].fd != ARRAY_END)
1040 // connect request from target
1041 if (events[i].data.fd == manager.target_server_socket) {
1042 if (targetServerHandler(manager.efd) < 0) {
1044 terminate_error("Internal DA framework error, "
1045 "Please re-run the profiling "
1046 "(targetServerHandler)\n", 1);
1049 } else if (events[i].data.fd == manager.host_server_socket) {
1050 // connect request from host
1051 int result = hostServerHandler(manager.efd);
1053 LOGE("Internal DA framework error (hostServerHandler)\n");
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) {
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);
1068 } else if (result < 0) {
1069 LOGE("Control socket handler.\n");
1071 } else if (events[i].data.fd == manager.host.data_socket) {
1073 recvLen = recv(manager.host.data_socket, recvBuf, 32, MSG_DONTWAIT);
1075 // close data socket
1076 if (0 > epoll_ctl(manager.efd,
1078 manager.host.data_socket,
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
1086 LOGI("host message from data socket %d\n",
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");
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,
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);
1111 LOGW("Unknown socket fd (%d)\n",
1118 LOGI("close efd\n");
1122 return return_value;