process.rs 2.6 KB

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