Move the VSoC client socket into /var/run

Change-Id: I9573134e3ae97987a04ad1213ac06b34ad4cfec2
diff --git a/Android.bp b/Android.bp
index 561c9cd..73d7264 100644
--- a/Android.bp
+++ b/Android.bp
@@ -69,6 +69,7 @@
     static_libs: [
         "libcuttlefish_fs",
         "libbase",
+	"libgflags",
     ],
     defaults: ["cuttlefish_host"],
 }
@@ -136,6 +137,7 @@
         "libbase",
         "cuttlefish_auto_resources",
         "libcuttlefish_fs",
+	"libgflags",
     ],
     defaults: ["cuttlefish_host"],
 }
diff --git a/common/vsoc/framebuffer/Android.bp b/common/vsoc/framebuffer/Android.bp
index 50ad9ea..95196c5 100644
--- a/common/vsoc/framebuffer/Android.bp
+++ b/common/vsoc/framebuffer/Android.bp
@@ -35,6 +35,7 @@
         "libbase",
         "cuttlefish_auto_resources",
         "libcuttlefish_fs",
+	"libgflags",
     ],
     defaults: ["cuttlefish_host"],
 }
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index f9d2ccd..5634483 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -46,7 +46,6 @@
 }  // namespace
 
 DEFINE_string(cache_image, "", "Location of the cache partition image.");
-DEFINE_string(clientsocket, "/tmp/ivshmem_socket_client", "Client socket path");
 DEFINE_int32(cpus, 2, "Virtual CPU count.");
 DEFINE_string(data_image, "", "Location of the data partition image.");
 DEFINE_bool(disable_app_armor_security, false,
@@ -55,8 +54,8 @@
             "Disable DAC security in libvirt. For debug only.");
 DEFINE_string(extra_kernel_command_line, "",
               "Additional flags to put on the kernel command line");
+DECLARE_int32(instance);
 DEFINE_string(initrd, "", "Location of cuttlefish initrd file.");
-DEFINE_int32(instance, 1, "Instance number. Must be unique.");
 DEFINE_string(kernel, "", "Location of cuttlefish kernel file.");
 DEFINE_string(kernel_command_line, "",
               "Location of a text file with the kernel command line.");
@@ -81,7 +80,6 @@
 
 DEFINE_string(usbipsocket, "android_usbip", "Name of the USB/IP socket.");
 DEFINE_string(uuid, "", "UUID to use for the device. Random if not specified");
-DEFINE_string(vsoc_domain, vsoc::DEFAULT_DOMAIN, "Client socket path");
 
 namespace {
 Json::Value LoadLayoutFile(const std::string& file) {
@@ -148,7 +146,8 @@
  public:
   IVServerManager(const Json::Value& json_root)
       : server_(ivserver::IVServerOptions(FLAGS_layout, FLAGS_mempath,
-                                          FLAGS_qemusocket, FLAGS_vsoc_domain),
+                                          FLAGS_qemusocket,
+                                          vsoc::GetShmClientSocketPath()),
                 json_root) {}
 
   ~IVServerManager() = default;
@@ -250,8 +249,7 @@
       .SetDisableDACSecurity(FLAGS_disable_dac_security)
       .SetDisableAppArmorSecurity(FLAGS_disable_app_armor_security)
       .SetUUID(FLAGS_uuid);
-  cfg.SetUSBV1SocketName(std::string("/tmp/") + cfg.GetInstanceName() +
-                         "-usb");
+  cfg.SetUSBV1SocketName(std::string("/tmp/") + cfg.GetInstanceName() + "-usb");
 
   std::string xml = cfg.Build();
   if (FLAGS_log_xml) {
diff --git a/host/frontend/vnc_server/main.cpp b/host/frontend/vnc_server/main.cpp
index 02a5f84..c47a687 100644
--- a/host/frontend/vnc_server/main.cpp
+++ b/host/frontend/vnc_server/main.cpp
@@ -19,20 +19,19 @@
 
 #include <gflags/gflags.h>
 
-#include "host/vsoc/lib/region_control.h"
+#include "common/libs/glog/logging.h"
 #include "host/frontend/vnc_server/vnc_server.h"
 #include "host/frontend/vnc_server/vnc_utils.h"
-#include "common/libs/glog/logging.h"
+#include "host/vsoc/lib/region_control.h"
 
 DEFINE_bool(agressive, false, "Whether to use agressive server");
 DEFINE_int32(port, 6444, "Port where to listen for connections");
-DEFINE_string(vsoc_domain, vsoc::DEFAULT_DOMAIN, "Client socket path");
 
 int main(int argc, char* argv[]) {
   using ::android::base::ERROR;
   ::android::base::InitLogging(argv, android::base::StderrLogger);
   ::gflags::ParseCommandLineFlags(&argc, &argv, true);
-  if (!avd::vnc::GetFBBroadcastRegionView()->Open(FLAGS_vsoc_domain.c_str())) {
+  if (!avd::vnc::GetFBBroadcastRegionView()->Open()) {
     LOG(FATAL) << "Unable to open FBBroadcastRegion";
   }
   avd::vnc::VncServer vnc_server(FLAGS_port, FLAGS_agressive);
diff --git a/host/libs/ivserver/options.h b/host/libs/ivserver/options.h
index 48be179..53a51e2 100644
--- a/host/libs/ivserver/options.h
+++ b/host/libs/ivserver/options.h
@@ -24,11 +24,7 @@
 const uint16_t kIVServerMajorVersion = 1;
 const uint16_t kIVServerMinorVersion = 0;
 const uint32_t kIVServerDefaultShmSizeInMiB = 4;
-const std::string kIVServerDefaultShmFile = "ivshmem";
 const std::string kIVServerDefaultLayoutFile = "vsoc_mem.json";
-const std::string kIVServerDefaultQemuSocketPath = "/tmp/ivshmem_socket";
-const std::string kIVServerDefaultClientSocketPath =
-    "/tmp/ivshmem_client_socket";
 
 //
 // structure that contains the various options to start the server.
diff --git a/host/vsoc/lib/region_control.cpp b/host/vsoc/lib/region_control.cpp
index f8b5c09..8f53508 100644
--- a/host/vsoc/lib/region_control.cpp
+++ b/host/vsoc/lib/region_control.cpp
@@ -13,8 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "common/vsoc/lib/region_view.h"
 #include "host/vsoc/lib/region_control.h"
+#include "common/vsoc/lib/region_view.h"
 
 #define LOG_TAG "vsoc: region_host"
 
@@ -26,10 +26,12 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <iomanip>
 #include <sstream>
 #include <thread>
 #include <vector>
 
+#include <gflags/gflags.h>
 #include <glog/logging.h>
 
 #include "common/libs/fs/shared_fd.h"
@@ -37,7 +39,14 @@
 
 using avd::SharedFD;
 
+DEFINE_int32(instance, 1, "Instance number. Must be unique.");
+DEFINE_string(vsoc_run_dir, "/var/run/cvd-",
+              "Path to the directory holding vsoc resources");
+DEFINE_string(vsoc_shm_client_sock, "ivshmem_socket_client",
+              "Name of the VSoC client socket");
+
 namespace {
+
 class HostRegionControl : public vsoc::RegionControl {
  public:
   HostRegionControl(const char* region_name,
@@ -172,12 +181,21 @@
 }
 }  // namespace
 
+std::string vsoc::GetShmClientSocketPath() {
+  std::ostringstream stream;
+  stream << FLAGS_vsoc_run_dir << std::setfill('0') << std::setw(2)
+         << FLAGS_instance << "/" << FLAGS_vsoc_shm_client_sock;
+  return stream.str();
+}
+
 std::shared_ptr<vsoc::RegionControl> vsoc::RegionControl::Open(
     const char* region_name, const char* domain) {
   AutoFreeBuffer msg;
+  std::string owned_domain;
 
   if (!domain) {
-    domain = DEFAULT_DOMAIN;
+    owned_domain = GetShmClientSocketPath();
+    domain = owned_domain.c_str();
   }
   SharedFD region_server =
       SharedFD::SocketLocalClient(domain, false, SOCK_STREAM);
diff --git a/host/vsoc/lib/region_control.h b/host/vsoc/lib/region_control.h
index e7d3e5c..a661dd6 100644
--- a/host/vsoc/lib/region_control.h
+++ b/host/vsoc/lib/region_control.h
@@ -18,6 +18,8 @@
 
 /* Host-specific values associated with RegionControl. */
 
+#include <string>
+
 namespace vsoc {
-const char* const DEFAULT_DOMAIN{"/tmp/ivshmem_socket_client"};
+std::string GetShmClientSocketPath();
 }  // namespace vsoc