-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_main.cpp
More file actions
116 lines (103 loc) · 3.31 KB
/
log_main.cpp
File metadata and controls
116 lines (103 loc) · 3.31 KB
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
#include <cutils/log.h>
#include <cutils/sockets.h>
#include <string>
#define TAG "LogLocalSocket"
#define MAX 10
#define LOGD(TAG,...) if(1) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__);
struct Param {
std::string cmd;
int fd;
};
char logserver[16] = "detcserver";
std::string start = "start_logcat";//命令开始的标志
std::string stop = "stop_logcat";//命令结束的标志
int stopfd[MAX];
static void* execute_cmd(void *arg) {
Param param = *(Param *)arg;
std::string cmd = param.cmd;
int receive_fd = param.fd;
FILE *ptr = NULL;
char buf_result[1024];
if ((ptr = popen(cmd.c_str(), "r")) != NULL) {
LOGD(TAG," shell success\n");
while(fgets(buf_result, sizeof(buf_result), ptr) != NULL) {
LOGD(TAG," result = %s \n", buf_result);
if (stopfd[receive_fd] == receive_fd) {//判断命令是否需要停止
stopfd[receive_fd] = 0;
break;
}
send(receive_fd, buf_result, strlen(buf_result), 0);
memset(buf_result, 0, sizeof(buf_result));
}
pclose(ptr);
ptr = NULL;
} else {
LOGD(TAG," shell failed\n");
}
close(receive_fd);
return ((void *)0);
}
static void* recever_thread_exe(void *arg) {
int receive_fd = *(int *)arg;
int numbytes = -1;;
char buff_cmd[1024];
memset(buff_cmd, 0, sizeof(buff_cmd));
while((numbytes = recv(receive_fd, buff_cmd, sizeof(buff_cmd), 0)) != -1) {
std::string cmd(buff_cmd);
LOGD(TAG," cmd = %s \n", cmd.c_str());
if (cmd.find(start, 0) != std::string::npos) {//客户端传递的命令是否包含开始
cmd = cmd.erase(0, start.length());
LOGD(TAG," cmd start = %s \n", cmd.c_str());
Param param;
param.cmd = cmd;
param.fd = receive_fd;
pthread_t id;
pthread_create(&id, NULL, execute_cmd, ¶m);
} else if (cmd.find(stop, 0) != std::string::npos) {//客户端传递的命令是否包含停止
//接收到停止命令
stopfd[receive_fd] = receive_fd;
break;
}
}
LOGD(TAG,"recv %d\n", errno);
return ((void *)0);
}
int main() {
int socket_fd = -1;
int receive_fd = -1;
int result;
struct sockaddr addr;
socklen_t alen;
alen = sizeof(addr);
socket_fd = android_get_control_socket(logserver);
if (socket_fd < 0) {
LOGD(TAG,"Failed to get socket log_server %s errno:%d\n", logserver, errno);
exit(-1);
}
LOGD(TAG,"android_get_control_socket success\n");
result = listen(socket_fd, MAX);
if (result < 0) {
perror("listen\n");
LOGD(TAG,"listen error\n");
exit(-1);
}
LOGD(TAG,"listen success\n");
while(1) {
memset(&addr, 0, sizeof(addr));
receive_fd= accept(socket_fd, &addr, &alen);
LOGD(TAG,"Accept_fd %d\n",receive_fd);
if (receive_fd < 0 ) {
LOGD(TAG,"receive_fd %d\n",errno);
perror("accept error");
exit(-1);
}
fcntl(receive_fd, F_SETFD, FD_CLOEXEC);
pthread_t id;
if(pthread_create(&id, NULL, recever_thread_exe, &receive_fd) )
{
LOGD(TAG,"rece thread create fail\n");
}
}
close(socket_fd);
return 0;
}