process.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Types and functions related to child processes management.
  5. use crate::Env;
  6. use std::path::PathBuf;
  7. /// Finds the current running binary's path.
  8. ///
  9. /// With exception to any following platform-specific behavior, the path is cached as soon as
  10. /// possible, and then used repeatedly instead of querying for a new path every time this function
  11. /// is called.
  12. ///
  13. /// # Platform-specific behavior
  14. ///
  15. /// ## Linux
  16. ///
  17. /// On Linux, this function will **attempt** to detect if it's currently running from a
  18. /// valid [AppImage] and use that path instead.
  19. ///
  20. /// ## macOS
  21. ///
  22. /// On `macOS`, this function will return an error if the original path contained any symlinks
  23. /// due to less protection on macOS regarding symlinks. This behavior can be disabled by setting the
  24. /// `process-relaunch-dangerous-allow-symlink-macos` feature, although it is *highly discouraged*.
  25. ///
  26. /// # Security
  27. ///
  28. /// See [`tauri_utils::platform::current_exe`] for possible security implications.
  29. ///
  30. /// # Examples
  31. ///
  32. /// ```rust,no_run
  33. /// use tauri::{process::current_binary, Env, Manager};
  34. /// let current_binary_path = current_binary(&Env::default()).unwrap();
  35. ///
  36. /// tauri::Builder::default()
  37. /// .setup(|app| {
  38. /// let current_binary_path = current_binary(&app.env())?;
  39. /// Ok(())
  40. /// });
  41. /// ```
  42. ///
  43. /// [AppImage]: https://appimage.org/
  44. pub fn current_binary(_env: &Env) -> std::io::Result<PathBuf> {
  45. // if we are running from an AppImage, we ONLY want the set AppImage path
  46. #[cfg(target_os = "linux")]
  47. if let Some(app_image_path) = &_env.appimage {
  48. return Ok(PathBuf::from(app_image_path));
  49. }
  50. tauri_utils::platform::current_exe()
  51. }
  52. /// Restarts the currently running binary.
  53. ///
  54. /// See [`current_binary`] for platform specific behavior, and
  55. /// [`tauri_utils::platform::current_exe`] for possible security implications.
  56. ///
  57. /// # Examples
  58. ///
  59. /// ```rust,no_run
  60. /// use tauri::{process::restart, Env, Manager};
  61. ///
  62. /// tauri::Builder::default()
  63. /// .setup(|app| {
  64. /// restart(&app.env());
  65. /// Ok(())
  66. /// });
  67. /// ```
  68. pub fn restart(env: &Env) {
  69. use std::process::{exit, Command};
  70. if let Ok(path) = current_binary(env) {
  71. Command::new(path)
  72. .args(&env.args_os)
  73. .spawn()
  74. .expect("application failed to start");
  75. }
  76. exit(0);
  77. }