1 /*
2 * Copyright (C) 2022 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 #pragma once
18
19 #include <sys/types.h>
20
21 #include <optional>
22 #include <sstream>
23 #include <unordered_map>
24 #include <vector>
25
26 #include "common/libs/utils/result.h"
27 #include "cvd_server.pb.h"
28 #include "host/commands/cvd/types.h"
29
30 namespace cuttlefish {
31
32 // utility struct for std::variant uses
33 template <typename... Ts>
34 struct Overload : Ts... {
35 using Ts::operator()...;
36 };
37
38 template <typename... Ts>
39 Overload(Ts...) -> Overload<Ts...>;
40
41 struct MakeRequestForm {
42 cvd_common::Args cmd_args;
43 cvd_common::Envs env;
44 cvd_common::Args selector_args;
45 std::optional<std::string> working_dir;
46 };
47
48 cvd::Request MakeRequest(const MakeRequestForm& request_form,
49 const cvd::WaitBehavior wait_behavior);
50
51 cvd::Request MakeRequest(const MakeRequestForm& request_form);
52
53 // name of environment variable to mark the launch_cvd initiated by the cvd
54 // server
55 static constexpr char kCvdMarkEnv[] = "_STARTED_BY_CVD_SERVER_";
56
57 constexpr char kServerExecPath[] = "/proc/self/exe";
58
59 // The name of environment variable that points to the host out directory
60 constexpr char kAndroidHostOut[] = "ANDROID_HOST_OUT";
61 // kAndroidHostOut for old branches
62 constexpr char kAndroidSoongHostOut[] = "ANDROID_SOONG_HOST_OUT";
63 constexpr char kAndroidProductOut[] = "ANDROID_PRODUCT_OUT";
64 constexpr char kLaunchedByAcloud[] = "LAUNCHED_BY_ACLOUD";
65
66 template <typename Ostream, typename... Args>
ConcatToStream(Ostream & out,Args &&...args)67 Ostream& ConcatToStream(Ostream& out, Args&&... args) {
68 (out << ... << std::forward<Args>(args));
69 return out;
70 }
71
72 template <typename... Args>
ConcatToString(Args &&...args)73 std::string ConcatToString(Args&&... args) {
74 std::stringstream concatenator;
75 return ConcatToStream(concatenator, std::forward<Args>(args)...).str();
76 }
77
78 // given /a/b/c/d/e, ensures
79 // all directories from /a through /a/b/c/d/e exist
80 Result<void> EnsureDirectoryExistsAllTheWay(const std::string& dir);
81
82 struct InputPathForm {
83 /** If nullopt, uses the process' current working dir
84 * But if there is no preceding .. or ., this field is not used.
85 */
86 std::optional<std::string> current_working_dir;
87 /** If nullopt, use SystemWideUserHome()
88 * But, if there's no preceding ~, this field is not used.
89 */
90 std::optional<std::string> home_dir;
91 std::string path_to_convert;
92 bool follow_symlink;
93 };
94
95 /**
96 * Returns emulated absolute path with a different process'/thread's
97 * context.
98 *
99 * This is useful when daemon(0, 0)-started server process wants to
100 * figure out a relative path that came from its client.
101 *
102 * The call mostly succeeds. It fails only if:
103 * home_dir isn't given so supposed to relies on the local SystemWideUserHome()
104 * but SystemWideUserHome() call fails.
105 */
106 Result<std::string> EmulateAbsolutePath(const InputPathForm& path_info);
107
108 } // namespace cuttlefish
109