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 #ifndef __VTS_SHELL_DRIVER_H_
18 #define __VTS_SHELL_DRIVER_H_
19 
20 #include <string>
21 
22 #include "test/vts/proto/VtsDriverControlMessage.pb.h"
23 
24 using namespace std;
25 
26 namespace android {
27 namespace vts {
28 
29 struct CommandResult {
30   string stdout;
31   string stderr;
32   int exit_code;
33 };
34 
35 class VtsShellDriver {
36  public:
VtsShellDriver()37   VtsShellDriver() { socket_address_.clear(); }
38 
VtsShellDriver(const char * socket_address)39   explicit VtsShellDriver(const char* socket_address)
40       : socket_address_(socket_address) {}
41 
~VtsShellDriver()42   ~VtsShellDriver() {
43     if (!this->socket_address_.empty()) {
44       Close();
45     }
46   }
47 
48   // closes the sockets.
49   int Close();
50 
51   // start shell driver server on unix socket
52   int StartListen();
53 
54  private:
55   // socket address
56   string socket_address_;
57 
58   /*
59    * execute a given shell command and return the output file descriptor
60    * Please remember to call close_output after usage.
61    */
62   int ExecShellCommand(const string& command,
63                        VtsDriverControlResponseMessage* respond_message);
64 
65   /*
66    * Handles a socket connection. Will execute a received shell command
67    * and send back the output text.
68    */
69   int HandleShellCommandConnection(int connection_fd);
70 
71   /*
72    * Execute a shell command using popen and return a CommandResult object.
73    */
74   CommandResult* ExecShellCommandPopen(const string& command);
75 
76   /*
77    * Execute a shell command using nohup and return a CommandResult object.
78    */
79   CommandResult* ExecShellCommandNohup(const string& command);
80 
81   /*
82    * Helper method to get the size of the given file.
83    */
84   long GetFileSize(const char* filename);
85 };
86 
87 }  // namespace vts
88 }  // namespace android
89 
90 #endif  // __VTS_SHELL_DRIVER_H_
91