From ba4d9fd9339a7db7705f00ab4a1310b43970a8e2 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 3 Oct 2017 08:44:27 -0700 Subject: [PATCH] Set $HOSTNAME in adbd. bash sets this itself, but mksh doesn't. This also makes interactive and non-interactive adb shells more similar, because when we set this in mkshrc that wouldn't affect non-interactive shells. (I'm not sure whether this is an improvement or not.) This is certainly _cheaper_ than doing this in mkshrc by calling out to getprop though. Bug: N/A Test: `adb shell printenv` and `adb shell`, `printenv` Change-Id: I3ff724f19a5098313df83836253f1f7e7872d6a4 --- adb/shell_service.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/adb/shell_service.cpp b/adb/shell_service.cpp index 5b48da029..0c7e1f99b 100644 --- a/adb/shell_service.cpp +++ b/adb/shell_service.cpp @@ -95,6 +95,7 @@ #include #include +#include #include #include @@ -212,6 +213,13 @@ Subprocess::~Subprocess() { WaitForExit(); } +static std::string GetHostName() { + char buf[HOST_NAME_MAX]; + if (gethostname(buf, sizeof(buf)) != -1 && strcmp(buf, "localhost") != 0) return buf; + + return android::base::GetProperty("ro.product.device", "android"); +} + bool Subprocess::ForkAndExec(std::string* error) { unique_fd child_stdinout_sfd, child_stderr_sfd; unique_fd parent_error_sfd, child_error_sfd; @@ -250,11 +258,11 @@ bool Subprocess::ForkAndExec(std::string* error) { } if (pw != nullptr) { - // TODO: $HOSTNAME? Normally bash automatically sets that, but mksh doesn't. env["HOME"] = pw->pw_dir; + env["HOSTNAME"] = GetHostName(); env["LOGNAME"] = pw->pw_name; - env["USER"] = pw->pw_name; env["SHELL"] = pw->pw_shell; + env["USER"] = pw->pw_name; } if (!terminal_type_.empty()) {