busybox_info.cpp 3.8 KB

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