Home | History | Annotate | Download | only in stop_cvd
      1 /*
      2  * Copyright (C) 2018 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 <limits.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <sys/types.h>
     21 #include <sys/stat.h>
     22 #include <sys/wait.h>
     23 #include <fcntl.h>
     24 #include <unistd.h>
     25 
     26 #include <algorithm>
     27 #include <cstdlib>
     28 #include <fstream>
     29 #include <iomanip>
     30 #include <memory>
     31 #include <sstream>
     32 #include <string>
     33 #include <vector>
     34 
     35 #include <gflags/gflags.h>
     36 #include <glog/logging.h>
     37 
     38 #include "host/libs/config/cuttlefish_config.h"
     39 #include "host/libs/vm_manager/libvirt_manager.h"
     40 
     41 namespace {
     42 void RunCommand(const char* command) {
     43   int rval = std::system(command);
     44   if (rval) {
     45     LOG(ERROR) << "Unable to execute command: " << command
     46                << ". Exit code: " << rval;
     47   }
     48 }
     49 }  // anonymous namespace
     50 
     51 int main(int argc, char** argv) {
     52   ::android::base::InitLogging(argv, android::base::StderrLogger);
     53   google::ParseCommandLineFlags(&argc, &argv, true);
     54 
     55   int exit_code = 0;
     56 
     57   // TODO(b/78512938): Should ask the monitor to do the shutdown instead
     58   vm_manager::LibvirtManager libvirt_manager;
     59   if (!libvirt_manager.Stop()) {
     60     LOG(ERROR)
     61         << "Error when stopping guest virtual machine. Is it still running?";
     62     exit_code = 1;
     63   }
     64 
     65   auto config = vsoc::CuttlefishConfig::Get();
     66 
     67   // TODO(b/78512938): Shouldn't need sudo to shut down
     68   std::string run_files = config->PerInstancePath("*");
     69   std::string fuser_cmd = "sudo fuser -k ";
     70   fuser_cmd += run_files;
     71   fuser_cmd += " ";
     72   fuser_cmd += config->mempath();
     73   RunCommand(fuser_cmd.c_str());
     74   std::string delete_cmd = "rm -f ";
     75   delete_cmd += run_files;
     76   RunCommand(delete_cmd.c_str());
     77 
     78   return exit_code;
     79 }
     80