refactor
This commit is contained in:
		| @@ -37,6 +37,14 @@ inline const std::array<std::string, 4U> attribute_namespaces = { | ||||
| }; | ||||
| #endif | ||||
|  | ||||
| #if defined(__APPLE__) | ||||
| enum class launchctl_type : std::uint8_t { | ||||
|   bootout, | ||||
|   bootstrap, | ||||
|   kickstart, | ||||
| }; | ||||
| #endif // defined(__APPLE__) | ||||
|  | ||||
| [[nodiscard]] auto create_daemon(std::function<int()> main_func) -> int; | ||||
|  | ||||
| [[nodiscard]] auto from_api_error(api_error err) -> int; | ||||
| @@ -56,6 +64,9 @@ generate_launchd_plist(const std::string &label, std::string plist_path, | ||||
|                        const std::string &stdout_log = "/tmp/stdout.log", | ||||
|                        const std::string &stderr_log = "/tmp/stderr.log") | ||||
|     -> bool; | ||||
|  | ||||
| [[nodiscard]] auto launchctl_command(std::string_view label, | ||||
|                                      launchctl_type type) -> int; | ||||
| #endif // defined(__APPLE__) | ||||
| } // namespace repertory::utils | ||||
|  | ||||
|   | ||||
| @@ -437,7 +437,7 @@ auto fuse_base::mount([[maybe_unused]] std::vector<std::string> orig_args, | ||||
|                        provider_type_to_string(prov), unique_id); | ||||
|   if (not foreground_) { | ||||
|     if (not utils::file::change_to_process_directory()) { | ||||
|       std::cerr << "Failed to change to process directory" << std::endl; | ||||
|       std::cerr << "FATAL: Failed to change to process directory" << std::endl; | ||||
|       return -1; | ||||
|     } | ||||
|  | ||||
| @@ -451,33 +451,29 @@ auto fuse_base::mount([[maybe_unused]] std::vector<std::string> orig_args, | ||||
|                         provider_type_to_string(prov), unique_id), | ||||
|             fmt::format("/tmp/repertory_{}_{}.err", | ||||
|                         provider_type_to_string(prov), unique_id))) { | ||||
|       std::cerr << fmt::format("Failed to generate plist|{}", label_) | ||||
|       std::cerr << fmt::format("FATAL: Failed to generate plist|{}", label_) | ||||
|                 << std::endl; | ||||
|       return -1; | ||||
|     } | ||||
|  | ||||
|     system(fmt::format("launchctl bootout gui/{} '{}' 1>/dev/null 2>&1", | ||||
|                        getuid(), | ||||
|                        utils::path::combine("~", | ||||
|                                             { | ||||
|                                                 "/Library/LaunchAgents", | ||||
|                                                 fmt::format("{}.plist", label_), | ||||
|                                             })) | ||||
|                .c_str()); | ||||
|  | ||||
|     system(fmt::format("launchctl bootstrap gui/{} '{}' 1>/dev/null 2>&1", | ||||
|                        getuid(), | ||||
|                        utils::path::combine("~", | ||||
|                                             { | ||||
|                                                 "/Library/LaunchAgents", | ||||
|                                                 fmt::format("{}.plist", label_), | ||||
|                                             })) | ||||
|                .c_str()); | ||||
|  | ||||
|     ret = system( | ||||
|         fmt::format("launchctl kickstart gui/{}/{}", getuid(), label_).c_str()); | ||||
|     ret = utils::launchctl_command(launchctl_type::bootout, label_); | ||||
|     if (ret != 0) { | ||||
|       std::cerr << fmt::format("Failed to kickstart {}/{}", getuid(), label_) | ||||
|       std::cout << fmt::format("WARN: Failed to bootout {}/{}", getuid(), | ||||
|                                label_) | ||||
|                 << std::endl; | ||||
|     } | ||||
|  | ||||
|     ret = utils::launchctl_command(launchctl_type::bootstrap, label_); | ||||
|     if (ret != 0) { | ||||
|       std::cont << fmt::format("WARN: Failed to bootstrap {}/{}", getuid(), | ||||
|                                label_) | ||||
|                 << std::endl; | ||||
|     } | ||||
|  | ||||
|     ret = = utils::launchctl_command(launchctl_type::kickstart, label_); | ||||
|     if (ret != 0) { | ||||
|       std::cerr << fmt::format("FATAL: Failed to kickstart {}/{}", getuid(), | ||||
|                                label_) | ||||
|                 << std::endl; | ||||
|     } | ||||
|  | ||||
| @@ -704,7 +700,8 @@ auto fuse_base::parse_args(std::vector<std::string> &args) -> int { | ||||
|               gid = getgrgid(utils::string::to_uint32(parts[1U])); | ||||
|             } | ||||
|             if ((getgid() != 0) && (gid->gr_gid == 0)) { | ||||
|               std::cerr << "'gid=0' requires running as root" << std::endl; | ||||
|               std::cerr << "FATAL: 'gid=0' requires running as root" | ||||
|                         << std::endl; | ||||
|               return -1; | ||||
|             } | ||||
|  | ||||
| @@ -720,7 +717,8 @@ auto fuse_base::parse_args(std::vector<std::string> &args) -> int { | ||||
|               uid = getpwuid(utils::string::to_uint32(parts[1])); | ||||
|             } | ||||
|             if ((getuid() != 0) && (uid->pw_uid == 0)) { | ||||
|               std::cerr << "'uid=0' requires running as root" << std::endl; | ||||
|               std::cerr << "FATAL: 'uid=0' requires running as root" | ||||
|                         << std::endl; | ||||
|               return -1; | ||||
|             } | ||||
|  | ||||
| @@ -737,7 +735,8 @@ auto fuse_base::parse_args(std::vector<std::string> &args) -> int { | ||||
|  | ||||
|               forced_umask_ = utils::string::to_uint32(parts[1]); | ||||
|             } catch (...) { | ||||
|               std::cerr << ("'" + option + "' invalid syntax") << std::endl; | ||||
|               std::cerr << ("FATAL: '" + option + "' invalid syntax") | ||||
|                         << std::endl; | ||||
|               return -1; | ||||
|             } | ||||
|           } | ||||
| @@ -923,14 +922,7 @@ auto fuse_base::unmount(const std::string &mount_location) -> int { | ||||
|   if (utils::file::file{ | ||||
|           utils::path::combine("~", {"/Library/LaunchAgents", label_})} | ||||
|           .exists()) { | ||||
|     return system( | ||||
|         fmt::format("launchctl bootout gui/{} '{}' 1>/dev/null 2>&1", getuid(), | ||||
|                     utils::path::combine("~", | ||||
|                                          { | ||||
|                                              "/Library/LaunchAgents", | ||||
|                                              fmt::format("{}.plist", label_), | ||||
|                                          })) | ||||
|             .c_str()); | ||||
|     return utils::launchctl_command(launchctl::bootout, label_); | ||||
|   } | ||||
|  | ||||
|   auto cmd = "umount \"" + mount_location + "\" >/dev/null 2>&1"; | ||||
|   | ||||
| @@ -361,6 +361,36 @@ auto generate_launchd_plist(const std::string &label, std::string plist_path, | ||||
|       utils::path::combine(plist_path, {label + ".plist"}).c_str(), "  ", | ||||
|       pugi::format_indent | pugi::format_write_bom); | ||||
| } | ||||
|  | ||||
| auto launchctl_command(std::string_view label, launchctl_type type) -> int { | ||||
|   switch (type) { | ||||
|   case launchctl_type::bootout: | ||||
|     return system( | ||||
|         fmt::format("launchctl bootout gui/{} '{}' 1>/dev/null 2>&1", getuid(), | ||||
|                     utils::path::combine("~", | ||||
|                                          { | ||||
|                                              "/Library/LaunchAgents", | ||||
|                                              fmt::format("{}.plist", label), | ||||
|                                          })) | ||||
|             .c_str()); | ||||
|  | ||||
|   case launchctl_type::bootstrap: | ||||
|     return system( | ||||
|         fmt::format("launchctl bootstrap gui/{} '{}' 1>/dev/null 2>&1", | ||||
|                     getuid(), | ||||
|                     utils::path::combine("~", | ||||
|                                          { | ||||
|                                              "/Library/LaunchAgents", | ||||
|                                              fmt::format("{}.plist", label), | ||||
|                                          })) | ||||
|             .c_str()); | ||||
|  | ||||
|   case launchctl_type::kickstart: | ||||
|     return system( | ||||
|         fmt::format("launchctl kickstart gui/{}/{}", getuid(), label).c_str()); | ||||
|   } | ||||
|   return -1; | ||||
| } | ||||
| #endif // defined(__APPLE__) | ||||
| } // namespace repertory::utils | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user