| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | wslinfo.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains wslinfo function definitions. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "common.h" |
| 16 | #include <iostream> |
| 17 | #include <assert.h> |
| 18 | #include "getopt.h" |
| 19 | #include "util.h" |
| 20 | #include "wslpath.h" |
| 21 | #include "wslinfo.h" |
| 22 | #include "lxinitshared.h" |
| 23 | #include "defs.h" |
| 24 | #include "Localization.h" |
| 25 | #include "CommandLine.h" |
| 26 | #include "../../shared/inc/lxinitshared.h" |
| 27 | |
| 28 | enum class WslInfoMode |
| 29 | { |
| 30 | GetNetworkingMode, |
| 31 | MsalProxyPath, |
| 32 | WslVersion, |
| 33 | VMId |
| 34 | }; |
| 35 | |
| 36 | int WslInfoEntry(int Argc, char* Argv[]) |
| 37 | |
| 38 | /*++ |
| 39 | |
| 40 | Routine Description: |
| 41 | |
| 42 | This routine is the entrypoint for the wslinfo binary. |
| 43 | |
| 44 | Arguments: |
| 45 | |
| 46 | Argc - Supplies the argument count. |
| 47 | |
| 48 | Argv - Supplies the command line arguments. |
| 49 | |
| 50 | Return Value: |
| 51 | |
| 52 | 0 on success, 1 on failure. |
| 53 | |
| 54 | --*/ |
| 55 | |
| 56 | { |
| 57 | using namespace wsl::shared; |
| 58 | |
| 59 | constexpr auto Usage = std::bind(Localization::MessageWslInfoUsage, Localization::Options::Default); |
| 60 | |
| 61 | std::optional<WslInfoMode> Mode; |
| 62 | bool noNewLine = false; |
| 63 | |
| 64 | ArgumentParser parser(Argc, Argv); |
| 65 | |
| 66 | parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::GetNetworkingMode>{Mode, Usage}, WSLINFO_NETWORKING_MODE); |
| 67 | parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::MsalProxyPath>{Mode, Usage}, WSLINFO_MSAL_PROXY_PATH); |
| 68 | parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::WslVersion>{Mode, Usage}, WSLINFO_WSL_VERSION); |
| 69 | parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::WslVersion>{Mode, Usage}, WSLINFO_WSL_VERSION_LEGACY); |
| 70 | parser.AddArgument(UniqueSetValue<WslInfoMode, WslInfoMode::VMId>{Mode, Usage}, WSLINFO_WSL_VMID); |
| 71 | parser.AddArgument(NoOp{}, WSLINFO_WSL_HELP); |
| 72 | parser.AddArgument(noNewLine, nullptr, WSLINFO_NO_NEWLINE); |
| 73 | |
| 74 | try |
| 75 | { |
| 76 | parser.Parse(); |
| 77 | } |
| 78 | catch (const wil::ExceptionWithUserMessage& e) |
| 79 | { |
| 80 | std::cerr << e.what() << "\n"; |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | if (!Mode.has_value()) |
| 85 | { |
| 86 | std::cerr << Usage() << "\n"; |
| 87 | return 1; |
| 88 | } |
| 89 | else if (Mode.value() == WslInfoMode::GetNetworkingMode) |
| 90 | { |
| 91 | if (UtilIsUtilityVm()) |
| 92 | { |
| 93 | auto NetworkingMode = UtilGetNetworkingMode(); |
| 94 | if (!NetworkingMode) |
| 95 | { |
| 96 | std::cerr << Localization::MessageFailedToQueryNetworkingMode() << "\n"; |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | switch (NetworkingMode.value()) |
| 101 | { |
| 102 | case LxMiniInitNetworkingModeNat: |
| 103 | std::cout << "nat"; |
| 104 | break; |
| 105 | |
| 106 | case LxMiniInitNetworkingModeBridged: |
| 107 | std::cout << "bridged"; |
| 108 | break; |
| 109 | |
| 110 | case LxMiniInitNetworkingModeMirrored: |
| 111 | std::cout << "mirrored"; |
| 112 | break; |
| 113 | |
| 114 | case LxMiniInitNetworkingModeConsomme: |
| 115 | std::cout << "consomme"; |
| 116 | break; |
| 117 | |
| 118 | default: |
| 119 | std::cout << "none"; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | std::cout << "wsl1"; |
| 126 | } |
| 127 | } |
| 128 | else if (Mode.value() == WslInfoMode::MsalProxyPath) |
| 129 | { |
| 130 | auto value = UtilGetEnvironmentVariable(LX_WSL2_INSTALL_PATH); |
| 131 | if (value.empty()) |
| 132 | { |
| 133 | std::cerr << Localization::MessageNoValueFound() << "\n"; |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | auto translatedPath = WslPathTranslate(value.data(), TRANSLATE_FLAG_ABSOLUTE, TRANSLATE_MODE_UNIX); |
| 138 | if (translatedPath.empty()) |
| 139 | { |
| 140 | std::cerr << Localization::MessageFailedToTranslate(value.data()) << "\n"; |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | std::cout << translatedPath << "/msal.wsl.proxy.exe"; |
| 145 | } |
| 146 | else if (Mode.value() == WslInfoMode::WslVersion) |
| 147 | { |
| 148 | std::cout << WSL_PACKAGE_VERSION; |
| 149 | } |
| 150 | else if (Mode.value() == WslInfoMode::VMId) |
| 151 | { |
| 152 | if (UtilIsUtilityVm()) |
| 153 | { |
| 154 | auto vmId = UtilGetVmId(); |
| 155 | if (vmId.empty()) |
| 156 | { |
| 157 | std::cerr << Localization::MessageNoValueFound() << "\n"; |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | std::cout << vmId; |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | std::cout << "wsl1"; |
| 166 | } |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | assert(false && "Unknown WslInfoMode"); |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | if (!noNewLine) |
| 175 | { |
| 176 | std::cout << '\n'; |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |