• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "TcpServerForRunner.h"
18 
19 #include <errno.h>
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include <dirent.h>
27 
28 #include <netdb.h>
29 #include <netinet/in.h>
30 
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 
34 #include <utils/RefBase.h>
35 
36 #include <iostream>
37 #include <sstream>
38 
39 #include "AgentRequestHandler.h"
40 #include "BinderClientToDriver.h"
41 #include "test/vts/proto/AndroidSystemControlMessage.pb.h"
42 
43 using namespace std;
44 
45 namespace android {
46 namespace vts {
47 
48 // Starts to run a TCP server (foreground).
StartTcpServerForRunner(const char * spec_dir_path,const char * fuzzer_path32,const char * fuzzer_path64,const char * shell_path32,const char * shell_path64)49 int StartTcpServerForRunner(const char* spec_dir_path,
50                             const char* fuzzer_path32,
51                             const char* fuzzer_path64, const char* shell_path32,
52                             const char* shell_path64) {
53   int sockfd;
54   socklen_t clilen;
55   struct sockaddr_in serv_addr;
56   struct sockaddr_in cli_addr;
57 
58   sockfd = socket(AF_INET, SOCK_STREAM, 0);
59   if (sockfd < 0) {
60     cerr << "Can't open the socket." << endl;
61     return -1;
62   }
63 
64   bzero((char*)&serv_addr, sizeof(serv_addr));
65   serv_addr.sin_family = AF_INET;
66   serv_addr.sin_addr.s_addr = INADDR_ANY;
67   serv_addr.sin_port = htons(0);
68 
69   if (::bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1) {
70     cerr << __func__ << " binding failed. errno = " << errno << endl;
71     cerr << __func__ << " " << strerror(errno) << endl;
72     return -1;
73   }
74 
75   socklen_t sa_len = sizeof(serv_addr);
76   if (getsockname(sockfd, (struct sockaddr*) &serv_addr, &sa_len) == -1) {
77     cerr << __func__ << " getsockname failed. errno = " << errno << endl;
78     cerr << __func__ << " " << strerror(errno) << endl;
79     return -1;
80   }
81 
82   cout << "[agent] TCP server port is " << (int) ntohs(serv_addr.sin_port)
83        << endl;
84   FILE* fp = fopen("/data/local/tmp/vts_tcp_server_port", "wt");
85   if (!fp) {
86     cerr << __func__ << " can't write to "
87          << "/data/local/tmp/vts_tcp_server_port" << endl;
88     return -1;
89   }
90   fprintf(fp, "%d", (int) ntohs(serv_addr.sin_port));
91   fclose(fp);
92 
93   cout << "[agent] listening" << endl;
94   if (listen(sockfd, 5) == -1) {
95     cerr << __func__ << " listen failed." << endl;
96     return -1;
97   }
98   clilen = sizeof(cli_addr);
99   while (true) {
100     cout << "[agent] accepting" << endl;
101     int newsockfd = ::accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
102     if (newsockfd < 0) {
103       cerr << __func__ << " accept failed" << endl;
104       return -1;
105     }
106 
107     cout << "[runner->agent] NEW SESSION" << endl;
108     cout << "[runner->agent] ===========" << endl;
109     pid_t pid = fork();
110     if (pid == 0) {  // child
111       close(sockfd);
112       cout << "[agent] process for a runner - pid = " << getpid() << endl;
113       AgentRequestHandler handler(spec_dir_path, fuzzer_path32, fuzzer_path64,
114                                   shell_path32, shell_path64);
115       handler.SetSockfd(newsockfd);
116       while (handler.ProcessOneCommand())
117         ;
118       exit(-1);
119     } else if (pid < 0) {
120       cerr << "can't fork a child process to handle a session." << endl;
121       return -1;
122     } else {
123       close(newsockfd);
124     }
125   }
126   return 0;
127 }
128 
129 }  // namespace vts
130 }  // namespace android
131