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