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 "SocketServerForDriver.h"
18
19 #include <errno.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <sys/un.h>
28 #include <unistd.h>
29
30 #include <iostream>
31 #include <sstream>
32
33 #include <VtsDriverCommUtil.h>
34
35 #include "test/vts/proto/AndroidSystemControlMessage.pb.h"
36
37 using namespace std;
38
39 namespace android {
40 namespace vts {
41
42 static const int kCallbackServerPort = 5010;
43
RpcCallToRunner(const AndroidSystemCallbackRequestMessage & message)44 void SocketServerForDriver::RpcCallToRunner(
45 const AndroidSystemCallbackRequestMessage& message) {
46 cout << __func__ << ":" << __LINE__ << " " << message.id() << endl;
47 struct sockaddr_in serv_addr;
48 struct hostent* server;
49
50 int sockfd;
51 sockfd = socket(AF_INET, SOCK_STREAM, 0);
52 if (sockfd < 0) {
53 cerr << __func__ << " ERROR opening socket" << endl;
54 exit(-1);
55 return;
56 }
57 server = gethostbyname("127.0.0.1");
58 if (server == NULL) {
59 cerr << __func__ << " ERROR can't resolve the host name, localhost" << endl;
60 exit(-1);
61 return;
62 }
63 bzero((char*)&serv_addr, sizeof(serv_addr));
64 serv_addr.sin_family = AF_INET;
65 bcopy((char*)server->h_addr, (char*)&serv_addr.sin_addr.s_addr,
66 server->h_length);
67 serv_addr.sin_port = htons(runner_port_);
68
69 if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
70 cerr << __func__ << " ERROR connecting" << endl;
71 exit(-1);
72 return;
73 }
74
75 VtsDriverCommUtil util(sockfd);
76 if (!util.VtsSocketSendMessage(message)) return;
77 }
78
Start()79 void SocketServerForDriver::Start() {
80 AndroidSystemCallbackRequestMessage message;
81 if (!VtsSocketRecvMessage(&message)) return;
82 cout << __func__ << " Callback ID: " << message.id() << endl;
83 RpcCallToRunner(message);
84 Close();
85 }
86
StartSocketServerForDriver(const string & callback_socket_name,int runner_port)87 int StartSocketServerForDriver(const string& callback_socket_name,
88 int runner_port) {
89 struct sockaddr_un serv_addr;
90 int pid = fork();
91 if (pid < 0) {
92 cerr << __func__ << " ERROR on fork" << endl;
93 return -1;
94 } else if (pid > 0) {
95 return 0;
96 }
97
98 if (runner_port == -1) {
99 runner_port = kCallbackServerPort;
100 }
101 // only child process continues;
102 int sockfd;
103 sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
104 if (sockfd < 0) {
105 cerr << __func__ << " ERROR opening socket" << endl;
106 return -1;
107 }
108
109 bzero((char*) &serv_addr, sizeof(serv_addr));
110 serv_addr.sun_family = AF_UNIX;
111 strcpy(serv_addr.sun_path, callback_socket_name.c_str());
112 cout << "[agent] callback server at " << callback_socket_name << endl;
113
114 if (::bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
115 int error_save = errno;
116 cerr << getpid() << " " << __func__ << " ERROR on binding "
117 << callback_socket_name << " errno = " << error_save << " "
118 << strerror(error_save) << endl;
119 return -1;
120 }
121
122 if (listen(sockfd, 5) < 0) {
123 cerr << __func__ << " ERROR on listening" << endl;
124 return -1;
125 }
126
127 while (1) {
128 int newsockfd;
129 struct sockaddr_in cli_addr;
130 socklen_t clilen;
131
132 clilen = sizeof(cli_addr);
133 newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
134 if (newsockfd < 0) {
135 cerr << __func__ << " ERROR on accept " << strerror(errno) << endl;
136 break;
137 }
138 cout << "[agent] new callback connection." << endl;
139 pid = fork();
140 if (pid == 0) {
141 close(sockfd);
142 SocketServerForDriver server(newsockfd, runner_port);
143 server.Start();
144 exit(0);
145 } else if (pid > 0) {
146 close(newsockfd);
147 } else {
148 cerr << __func__ << " ERROR on fork" << endl;
149 break;
150 }
151 }
152 close(sockfd);
153 exit(0);
154 }
155
156 } // namespace vts
157 } // namespace android
158