blob: 95f5fefe37675ac85b483ffec6d60098efbe1c36 [file] [log] [blame]
Ryan Haininge58ecd92018-07-13 15:16:14 -07001/*
2 * Copyright (C) 2017 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/* Utility that uses an adb connection as the login shell. */
18
Ryan Haining7338d352018-10-24 17:41:52 -070019#include "host/libs/config/cuttlefish_config.h"
20
Ryan Haininge58ecd92018-07-13 15:16:14 -070021#include <array>
22#include <cassert>
23#include <cstdio>
Ryan Haining7338d352018-10-24 17:41:52 -070024#include <cstdlib>
Ryan Haininge58ecd92018-07-13 15:16:14 -070025#include <cstring>
26#include <string>
27#include <vector>
Greg Hartmanfb1b5dc2018-12-21 09:58:11 -080028
29#include <errno.h>
Ryan Haininge58ecd92018-07-13 15:16:14 -070030#include <unistd.h>
31
32// Many of our users interact with CVDs via ssh. They expect to be able to
33// get an Android shell (as opposed to the host shell) with a single command.
34//
35// Our goals are to:
36//
37// * Allow the user to select which CVD to connect to
38//
39// * Avoid modifications to the host-side sshd and the protocol
40//
41// We accomplish this by using specialized accounts: vsoc-## and cvd-## and
42// specific Android serial numbers:
43//
44// The vsoc-01 account provides a host-side shell that controls the first CVD
45// The cvd-01 account is connected to the Andorid shell of the first CVD
46// The first CVD has a serial number of CUTTLEFISHCVD01
47//
48// The code in the commands/launch directory also follows these conventions by
49// default.
50//
51
52namespace {
Ryan Haining7338d352018-10-24 17:41:52 -070053std::string VsocUser() {
54 const char* user_cstring = std::getenv("USER");
55 assert(user_cstring != nullptr);
56 std::string user(user_cstring);
Ryan Haininge58ecd92018-07-13 15:16:14 -070057
Ryan Haining7338d352018-10-24 17:41:52 -070058 std::string cvd_prefix = "cvd-";
59 if (user.find(cvd_prefix) == 0) {
60 user.replace(0, cvd_prefix.size(), vsoc::kVsocUserPrefix);
Ryan Haininge58ecd92018-07-13 15:16:14 -070061 }
Ryan Haining7338d352018-10-24 17:41:52 -070062 return user;
Ryan Haininge58ecd92018-07-13 15:16:14 -070063}
64
Ryan Haining7338d352018-10-24 17:41:52 -070065std::string CuttlefishConfigLocation() {
66 return std::string("/home/") + VsocUser() +
67 "/cuttlefish_runtime/cuttlefish_config.json";
68}
69
Greg Hartmanfb1b5dc2018-12-21 09:58:11 -080070std::string CuttlefishFindAdb() {
71 std::string rval = std::string("/home/") + VsocUser() + "/bin/adb";
72 if (TEMP_FAILURE_RETRY(access(rval.c_str(), X_OK)) == -1) {
73 return "/usr/bin/adb";
74 }
75 return rval;
76}
77
Ryan Haining7338d352018-10-24 17:41:52 -070078void SetCuttlefishConfigEnv() {
79 setenv(vsoc::kCuttlefishConfigEnvVarName, CuttlefishConfigLocation().c_str(),
80 true);
81}
Ryan Haininge58ecd92018-07-13 15:16:14 -070082} // namespace
83
84int main(int argc, char* argv[]) {
Ryan Haining7338d352018-10-24 17:41:52 -070085 SetCuttlefishConfigEnv();
Cody Schuffelen47c57852019-12-13 13:50:50 -080086 auto instance = vsoc::CuttlefishConfig::Get()
87 ->ForDefaultInstance().adb_device_name();
Greg Hartmanfb1b5dc2018-12-21 09:58:11 -080088 std::string adb_path = CuttlefishFindAdb();
Ryan Haining7338d352018-10-24 17:41:52 -070089
Ryan Haininge58ecd92018-07-13 15:16:14 -070090 std::vector<char*> new_argv = {
Greg Hartmanfb1b5dc2018-12-21 09:58:11 -080091 const_cast<char*>(adb_path.c_str()), const_cast<char*>("-s"),
Ryan Haininge58ecd92018-07-13 15:16:14 -070092 const_cast<char*>(instance.c_str()), const_cast<char*>("shell"),
93 const_cast<char*>("/system/bin/sh")};
94
95 // Some important data is lost before this point, and there are
96 // no great recovery options:
97 // * ssh with no arguments comes in with 1 arg of -adbshell. The command
98 // given above does the right thing if we don't invoke the shell.
99 if (argc == 1) {
100 new_argv.back() = nullptr;
101 }
102 // * simple shell commands come in with a -c and a single string. The
103 // problem here is that adb doesn't preserve spaces, so we need
104 // to do additional escaping. The best compromise seems to be to
105 // throw double quotes around each string.
106 for (int i = 1; i < argc; ++i) {
107 size_t buf_size = std::strlen(argv[i]) + 4;
108 new_argv.push_back(new char[buf_size]);
109 std::snprintf(new_argv.back(), buf_size, "\"%s\"", argv[i]);
110 }
111 //
112 // * scp seems to be pathologically broken when paths contain spaces.
113 // spaces aren't properly escaped by gcloud, so scp will fail with
114 // "scp: with ambiguous target." We might be able to fix this with
115 // some creative parsing of the arguments, but that seems like
116 // overkill.
117 new_argv.push_back(nullptr);
118 execv(new_argv[0], new_argv.data());
119 // This never should happen
120 return 2;
121}