busybox_info.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/install/busybox_info.h"
  5. #include <iterator>
  6. #include "common/exe_path.h"
  7. #include "llvm/ADT/StringRef.h"
  8. namespace Carbon {
  9. // The mode is set to the initial filename used for `argv[0]`.
  10. static auto GetMode(const std::filesystem::path& argv0)
  11. -> std::optional<std::string> {
  12. std::string filename = argv0.filename();
  13. if (filename != "carbon" && filename != "carbon-busybox") {
  14. return filename;
  15. }
  16. return std::nullopt;
  17. }
  18. auto GetBusyboxInfo(const char* argv0) -> ErrorOr<BusyboxInfo> {
  19. // Need storage due to `unsetenv` affecting `getenv` lifetime; using `path`
  20. // for `GetMode`.
  21. std::filesystem::path argv0_path = argv0;
  22. // Check for an override of `argv[0]` from the environment and apply it.
  23. if (const char* argv0_override = getenv(Argv0OverrideEnv)) {
  24. argv0_path = argv0_override;
  25. unsetenv(Argv0OverrideEnv);
  26. }
  27. BusyboxInfo info = {.bin_path = FindExecutablePath(argv0_path.c_str()),
  28. .mode = GetMode(argv0_path)};
  29. if (info.bin_path.filename() == "carbon-busybox") {
  30. // Check for bazel structure. For example, this makes work:
  31. // /bin/sh -c "exec -a carbon ./bazel-bin/toolchain/carbon"
  32. // /bin/sh -c "exec -a llvm-symbolizer ./bazel-bin/toolchain/carbon"
  33. if (std::filesystem::exists(info.bin_path.string() + ".runfiles")) {
  34. std::string prefix_root = info.bin_path.parent_path().string() +
  35. "/prefix_root/lib/carbon/carbon-busybox";
  36. if (std::filesystem::exists(prefix_root)) {
  37. info.bin_path = prefix_root;
  38. }
  39. }
  40. return info;
  41. }
  42. // Now search through any symlinks to locate the installed busybox binary.
  43. while (true) {
  44. // If we've not already reached the busybox, look for it relative to the
  45. // current binary path. This can help more immediately locate an
  46. // installation tree, and avoids walking through a final layer of symlinks
  47. // which may point to content-addressed storage or other parts of a build
  48. // output tree.
  49. //
  50. // We break this into two cases we need to handle:
  51. // - Carbon's CLI will be: `<prefix>/bin/carbon`
  52. // - Other tools will be: `<prefix>/lib/carbon/<group>/bin/<tool>`
  53. //
  54. // We also check that the current path is within a `bin` directory to
  55. // provide best-effort checking for accidentally walking up from symlinks
  56. // that aren't within an installation-shaped tree.
  57. auto parent_path = info.bin_path.parent_path();
  58. // Strip any `.` path components at the end to simplify processing.
  59. while (parent_path.filename() == ".") {
  60. parent_path = parent_path.parent_path();
  61. }
  62. if (parent_path.filename() == "bin") {
  63. auto lib_path = info.bin_path.filename() == "carbon"
  64. ? parent_path / ".." / "lib" / "carbon"
  65. : parent_path / ".." / "..";
  66. auto busybox_path = lib_path / "carbon-busybox";
  67. std::error_code ec;
  68. if (std::filesystem::exists(busybox_path, ec)) {
  69. info.bin_path = busybox_path;
  70. return info;
  71. }
  72. }
  73. // Try to walk through another layer of symlinks and see if we can find the
  74. // installation there or are linked directly to the busybox.
  75. std::error_code ec;
  76. auto symlink_target = std::filesystem::read_symlink(info.bin_path, ec);
  77. if (ec) {
  78. return ErrorBuilder()
  79. << "expected carbon-busybox symlink at `" << info.bin_path << "`";
  80. }
  81. // Do a path join, to handle relative symlinks.
  82. info.bin_path = parent_path / symlink_target;
  83. if (info.bin_path.filename() == "carbon-busybox") {
  84. return info;
  85. }
  86. }
  87. }
  88. } // namespace Carbon