#include <sys/stat.h> // for mkdir
#include <sys/eventfd.h> // for eventfd
#include <sys/epoll.h> // for epoll apis
+#include <sys/timerfd.h> // for timerfd
#include <unistd.h> // for access, sleep
#include <attr/xattr.h> // for fsetxattr
#include <linux/input.h>
#define EPOLL_SIZE 10
#define MAX_CONNECT_SIZE 12
+#define MAX_APP_LAUNCH_TIME 6
#define INPUT_ID_TOUCH 0
#define INPUT_ID_KEY 1
#else
get_executable(manager.appPath, execPath, PATH_MAX);
#endif
- if(exec_app(execPath, get_app_type(manager.appPath)))
+ if(samplingStart() < 0)
+ return -1;
+
+ if(exec_app(execPath, get_app_type(manager.appPath)) == 0)
{
- if(samplingStart() < 0)
- {
- return -1;
- }
- LOGI("Timer Started\n");
- }
- else
+ samplingStop();
return -1;
+ }
+
+ LOGI("Timer Started\n");
return 0;
}
// return 0 if normal case
// return plus value if non critical error occur
// return minus value if critical error occur
-static int hostMessageHandler(msg_t* log)
+static int hostMessageHandler(int efd, msg_t* log)
{
int ret = 0;
long flag = 0;
{
char command[PATH_MAX];
+ struct epoll_event ev;
//save app install path
mkdir(DA_WORK_DIR, 0775);
sendACKCodeToHost(MSG_NOTOK, ERR_CANNOT_START_PROFILING);
return -1;
}
+
+ manager.app_launch_timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
+ if(manager.app_launch_timerfd > 0)
+ {
+ struct itimerspec ctime;
+ ctime.it_value.tv_sec = MAX_APP_LAUNCH_TIME;
+ ctime.it_value.tv_nsec = 0;
+ ctime.it_interval.tv_sec = 0;
+ ctime.it_interval.tv_nsec = 0;
+ if(0 > timerfd_settime(manager.app_launch_timerfd, 0, &ctime, NULL))
+ {
+ LOGE("fail to set app launch timer\n");
+ close(manager.app_launch_timerfd);
+ manager.app_launch_timerfd = -1;
+ }
+ else
+ {
+ // add event fd to epoll list
+ ev.events = EPOLLIN;
+ ev.data.fd = manager.app_launch_timerfd;
+ if(epoll_ctl(efd, EPOLL_CTL_ADD, manager.app_launch_timerfd, &ev) < 0)
+ {
+ // fail to add event fd
+ LOGE("fail to add app launch timer fd to epoll list\n");
+ close(manager.app_launch_timerfd);
+ manager.app_launch_timerfd = -1;
+ }
+ }
+ }
}
sendACKStrToHost(MSG_OK, NULL);
break;
log.length = sprintf(log.data, "%d`,%Lu", manager.target[index].pid, manager.target[index].starttime);
}
- manager.target[index].initial_log = 1;
sendDataToHost(&log);
+ manager.target[index].initial_log = 1;
}
if(msg & EVENT_STOP || msg & EVENT_ERROR)
goto TARGET_CONNECT_FAIL;
}
+ if(manager.app_launch_timerfd >= 0)
+ {
+ epoll_ctl(efd, EPOLL_CTL_DEL, manager.app_launch_timerfd, NULL);
+ close(manager.app_launch_timerfd);
+ manager.app_launch_timerfd = -1;
+ }
+
LOGI("target connected = %d(running %d target)\n",
manager.target[index].socket, manager.target_count + 1);
// return plus value if non critical error occur
// return minus value if critical error occur
// return -11 if socket closed
-static int controlSocketHandler()
+static int controlSocketHandler(int efd)
{
ssize_t recvLen;
char recvBuf[DA_MSG_MAX];
}
// host msg command handling
- return hostMessageHandler(&log);
+ return hostMessageHandler(efd, &log);
}
else // close request from HOST
{
// control message from host
else if(events[i].data.fd == manager.host.control_socket)
{
- int result = controlSocketHandler();
+ int result = controlSocketHandler(efd);
if(result == -11) // socket close
{
// close target and host socket and quit
LOGW("host message from data socket %d\n", recvLen);
}
+ // check for application launch timerfd
+ else if(events[i].data.fd == manager.app_launch_timerfd)
+ {
+ // send to host timeout error message for launching application
+ terminate_error("Failed to launch application", 1);
+ epoll_ctl(efd, EPOLL_CTL_DEL, manager.app_launch_timerfd, NULL);
+ close(manager.app_launch_timerfd);
+ manager.app_launch_timerfd = -1;
+ ret = -1;
+ goto END_EFD;
+ }
// unknown socket
else
{
// initialize global variable
__da_manager manager =
{
- NULL, -1, -1, // portfile pointer, host / target server socket
+ NULL, -1, -1, // portfile pointer, host / target server socket
0, // target count
0, // config_flag
+ -1, // app launch timerfd
-1, // sampling_thread handle
{-1, -1, PTHREAD_MUTEX_INITIALIZER}, // host
{{0L, }, }, // target
static int initializeManager()
{
int i;
+ sigset_t newsigmask;
atexit(_close_server_socket);
writeToPortfile(i);
}
+ // initialize signal mask
+ sigemptyset(&newsigmask);
+ sigaddset(&newsigmask, SIGALRM);
+ sigaddset(&newsigmask, SIGUSR1);
+ if(pthread_sigmask(SIG_BLOCK, &newsigmask, NULL) != 0)
+ {
+ writeToPortfile(ERR_SIGNAL_MASK_SETTING_FAILED);
+ return -1;
+ }
+
LOGI("SUCCESS to write port\n");
// initialize target client sockets
// return plus value if non-critical error
int samplingStart()
{
- sigset_t newsigmask;
struct itimerval timerval;
if(manager.sampling_thread != -1) // already started
return 1;
- sigemptyset(&newsigmask);
- sigaddset(&newsigmask, SIGALRM);
- sigaddset(&newsigmask, SIGUSR1);
- if(pthread_sigmask(SIG_BLOCK, &newsigmask, NULL) != 0)
- {
- LOGE("Failed to signal masking for main thread\n");
- return -1;
- }
-
if(pthread_create(&(manager.sampling_thread), NULL, samplingThread, NULL) < 0)
{
LOGE("Failed to create sampling thread\n");
if(manager.sampling_thread != -1)
{
struct itimerval stopval;
-// int status;
-// sigset_t oldsigmask;
-// sigemptyset(&oldsigmask);
-// sigaddset(&oldsigmask, SIGALRM);
-// sigaddset(&oldsigmask, SIGUSR1);
// stop timer
stopval.it_interval.tv_sec = 0;
pthread_kill(manager.sampling_thread, SIGUSR1);
pthread_join(manager.sampling_thread, NULL);
- // this code commented because this phrase occurs an error
-// if(sigprocmask(SIG_UNBLOCK, &oldsigmask, NULL) < 0)
-// {
-// LOGE("Failed to pthread_sigmask\n");
-// }
manager.sampling_thread = -1;
}