super-voice-daemon/src/self_check.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! Asking macOS about one of Super Voice's permissions in a process of its |
3 | | //! own. |
4 | | //! |
5 | | //! macOS keeps the first answer a process gets about a permission for the |
6 | | //! life of that process, for the microphone (`AVCaptureDevice`) and for |
7 | | //! Device Control (`AXIsProcessTrusted`) alike, and the daemon starts before |
8 | | //! the user answers the app's prompt. So the daemon asks in a new process |
9 | | //! each time: itself, started with the question's argument, which answers in |
10 | | //! its exit code and exits. A process the daemon starts is the daemon's to |
11 | | //! macOS, so it gets the daemon's answer, as it stands now. |
12 | | |
13 | | #[cfg(target_os = "macos")] |
14 | | use std::sync::Mutex; |
15 | | #[cfg(target_os = "macos")] |
16 | | use std::time::{Duration, Instant}; |
17 | | |
18 | | /// In a daemon started with `arg`, the exit code that answers: 0 while |
19 | | /// `allowed` says yes, 1 while it says no. `None` in a daemon started to |
20 | | /// serve, which goes on. |
21 | | #[must_use] |
22 | 402 | pub fn answer(arg: &str, allowed: impl FnOnce() -> bool) -> Option<i32> { |
23 | 402 | if std::env::args().nth(1).as_deref() != Some(arg) { |
24 | 402 | return None; |
25 | 0 | } |
26 | 0 | Some(i32::from(!allowed())) |
27 | 402 | } |
28 | | |
29 | | /// One question the daemon asks in a process of its own, with its last |
30 | | /// answer, kept for a second so that clients reading the status at once |
31 | | /// start one process between them. |
32 | | #[cfg(target_os = "macos")] |
33 | | pub struct Question { |
34 | | arg: &'static str, |
35 | | last: Mutex<Option<(Instant, bool)>>, |
36 | | } |
37 | | |
38 | | #[cfg(target_os = "macos")] |
39 | | impl Question { |
40 | | const KEPT_FOR: Duration = Duration::from_secs(1); |
41 | | const WAIT_FOR: Duration = Duration::from_secs(5); |
42 | | |
43 | | #[must_use] |
44 | | pub const fn new(arg: &'static str) -> Self { |
45 | | Self { |
46 | | arg, |
47 | | last: Mutex::new(None), |
48 | | } |
49 | | } |
50 | | |
51 | | /// The answer as it stands now. If the process can't be started, or |
52 | | /// doesn't answer in time, `here` answers in the daemon's own process, |
53 | | /// which may be out of date. |
54 | | pub async fn ask(&self, here: impl FnOnce() -> bool) -> bool { |
55 | | if let Some((at, allowed)) = *self.lock() |
56 | | && at.elapsed() < Self::KEPT_FOR |
57 | | { |
58 | | return allowed; |
59 | | } |
60 | | let asked = async { |
61 | | tokio::process::Command::new(std::env::current_exe().ok()?) |
62 | | .arg(self.arg) |
63 | | .stdin(std::process::Stdio::null()) |
64 | | .stdout(std::process::Stdio::null()) |
65 | | .stderr(std::process::Stdio::null()) |
66 | | .kill_on_drop(true) |
67 | | .status() |
68 | | .await |
69 | | .ok() |
70 | | }; |
71 | | let allowed = match tokio::time::timeout(Self::WAIT_FOR, asked).await { |
72 | | Ok(Some(status)) if matches!(status.code(), Some(0 | 1)) => status.code() == Some(0), |
73 | | outcome => { |
74 | | log::warn!( |
75 | | "couldn't ask {} in a process of its own ({outcome:?}); answering from the daemon's own, which may be out of date", |
76 | | self.arg |
77 | | ); |
78 | | here() |
79 | | } |
80 | | }; |
81 | | *self.lock() = Some((Instant::now(), allowed)); |
82 | | allowed |
83 | | } |
84 | | |
85 | | fn lock(&self) -> std::sync::MutexGuard<'_, Option<(Instant, bool)>> { |
86 | | self.last |
87 | | .lock() |
88 | | .unwrap_or_else(std::sync::PoisonError::into_inner) |
89 | | } |
90 | | } |