capture_system_or_process_audio_linux/
permissions.rs1#![cfg_attr(not(target_os = "linux"), allow(dead_code))]
2
3use crate::{
4 core::{
5 planner::required_permission_kinds_for_os,
6 schema::{
7 BackendId, Backends, CaptureTarget, PermissionEntry, PermissionKind, PermissionReport,
8 PermissionState, PlatformOs,
9 },
10 traits::{CapabilityError, PermissionsBackend},
11 },
12 loader::{LinuxClientPaths, probe_pipewire_server, probe_pulse_server},
13};
14use std::time::Duration;
15
16pub fn classify_linux_permission_state(
18 any_server_reachable: bool,
19 any_socket_permission_denied: bool,
20 in_flatpak_without_audio_socket: bool,
21) -> PermissionState {
22 if any_server_reachable {
23 PermissionState::Granted
24 } else if any_socket_permission_denied || in_flatpak_without_audio_socket {
25 PermissionState::Restricted
26 } else {
27 PermissionState::Unknown
28 }
29}
30
31#[cfg(target_os = "linux")]
32fn check_unix_socket_permission_denied(check_pw: bool, check_pa: bool) -> (bool, bool) {
33 use std::{io::ErrorKind, os::unix::net::UnixStream, path::Path, path::PathBuf};
34
35 let mut candidates: Vec<PathBuf> = Vec::new();
36 if check_pw {
37 if let Ok(pw_dir) = std::env::var("PIPEWIRE_RUNTIME_DIR") {
38 candidates.push(PathBuf::from(pw_dir).join("pipewire-0"));
39 } else if let Ok(xdg) = std::env::var("XDG_RUNTIME_DIR") {
40 candidates.push(PathBuf::from(xdg).join("pipewire-0"));
41 }
42 }
43 if check_pa {
44 if let Ok(pa_dir) = std::env::var("PULSE_RUNTIME_PATH") {
45 candidates.push(PathBuf::from(pa_dir).join("native"));
46 } else if let Ok(xdg) = std::env::var("XDG_RUNTIME_DIR") {
47 candidates.push(PathBuf::from(&xdg).join("pulse").join("native"));
48 }
49 }
50
51 let mut any_eacces = false;
52 let mut any_socket_exists = false;
53 for sock_path in &candidates {
54 match std::fs::symlink_metadata(sock_path) {
55 Ok(_) => {
56 any_socket_exists = true;
57 if let Err(err) = UnixStream::connect(sock_path)
58 && err.kind() == ErrorKind::PermissionDenied
59 {
60 any_eacces = true;
61 }
62 }
63 Err(err) if err.kind() == ErrorKind::PermissionDenied => {
64 any_eacces = true;
65 }
66 Err(_) => {}
67 }
68 }
69
70 let in_flatpak = Path::new("/.flatpak-info").exists();
71 (any_eacces, in_flatpak && !any_socket_exists)
72}
73
74pub(crate) fn probe_linux_permission_state_with_paths(
75 backends: &Backends,
76 paths: &LinuxClientPaths,
77) -> PermissionState {
78 let linux_backends = backends.for_os(PlatformOs::Linux);
79 let check_pw = linux_backends.contains(&BackendId::PipeWire);
80 let check_pa = linux_backends.contains(&BackendId::PulseAudio);
81
82 let pw_ok = check_pw && probe_pipewire_server(paths).is_ok();
83 let pa_ok = check_pa && probe_pulse_server(paths).is_ok();
84 let any_ok = pw_ok || pa_ok;
85
86 if any_ok {
87 return PermissionState::Granted;
88 }
89
90 #[cfg(target_os = "linux")]
91 let (eacces, flatpak_isolated) = check_unix_socket_permission_denied(check_pw, check_pa);
92 #[cfg(not(target_os = "linux"))]
93 let (eacces, flatpak_isolated) = (false, false);
94
95 classify_linux_permission_state(false, eacces, flatpak_isolated)
96}
97
98pub(crate) fn ensure_linux_socket_permissions(
99 backends: &Backends,
100 paths: &LinuxClientPaths,
101) -> Result<(), CapabilityError> {
102 if probe_linux_permission_state_with_paths(backends, paths) == PermissionState::Restricted {
103 return Err(CapabilityError::PermissionDenied);
104 }
105 Ok(())
106}
107
108fn build_linux_report(kinds: &[PermissionKind], backends: &Backends) -> PermissionReport {
109 let paths = LinuxClientPaths::default();
110 let state = probe_linux_permission_state_with_paths(backends, &paths);
111 PermissionReport::new(
112 PlatformOs::Linux,
113 PermissionEntry {
114 kind: PermissionKind::Microphone,
115 state,
116 required: kinds.contains(&PermissionKind::Microphone),
117 can_prompt: false,
118 requires_relaunch: false,
119 },
120 PermissionEntry {
121 kind: PermissionKind::SystemAudio,
122 state,
123 required: kinds.contains(&PermissionKind::SystemAudio),
124 can_prompt: false,
125 requires_relaunch: false,
126 },
127 PermissionEntry {
128 kind: PermissionKind::ScreenRecording,
129 state,
130 required: kinds.contains(&PermissionKind::ScreenRecording),
131 can_prompt: false,
132 requires_relaunch: false,
133 },
134 )
135}
136
137#[derive(Clone, Copy, Debug, Default)]
138pub struct PlatformPermissions;
139
140impl PermissionsBackend for PlatformPermissions {
141 fn check_permissions(
142 targets: &[CaptureTarget],
143 backends: &Backends,
144 ) -> Result<PermissionReport, CapabilityError> {
145 let req = required_permission_kinds_for_os(targets, backends, PlatformOs::Linux)?;
146 let kinds: Vec<PermissionKind> = req.into_iter().collect();
147 Self::check_permission_kinds(&kinds, backends)
148 }
149
150 fn request_permissions(
151 targets: &[CaptureTarget],
152 backends: &Backends,
153 timeout: Duration,
154 open_settings_if_denied: bool,
155 ) -> Result<PermissionReport, CapabilityError> {
156 let req = required_permission_kinds_for_os(targets, backends, PlatformOs::Linux)?;
157 let kinds: Vec<PermissionKind> = req.into_iter().collect();
158 Self::request_permission_kinds(&kinds, backends, timeout, open_settings_if_denied)
159 }
160
161 fn check_permission_kinds(
162 kinds: &[PermissionKind],
163 backends: &Backends,
164 ) -> Result<PermissionReport, CapabilityError> {
165 backends.validate()?;
166 Ok(build_linux_report(kinds, backends))
167 }
168
169 fn request_permission_kinds(
170 kinds: &[PermissionKind],
171 backends: &Backends,
172 _timeout: Duration,
173 _open_settings_if_denied: bool,
174 ) -> Result<PermissionReport, CapabilityError> {
175 backends.validate()?;
176 Ok(build_linux_report(kinds, backends))
177 }
178}
179
180#[cfg(test)]
181mod tests {
182 use super::*;
183 use crate::core::schema::DefaultDeviceMode;
184
185 #[test]
186 fn linux_permission_classifier_distinguishes_granted_sandbox_restricted_and_daemon_unknown() {
187 assert_eq!(
188 classify_linux_permission_state(true, false, false),
189 PermissionState::Granted
190 );
191 assert_eq!(
192 classify_linux_permission_state(false, true, false),
193 PermissionState::Restricted
194 );
195 assert_eq!(
196 classify_linux_permission_state(false, false, true),
197 PermissionState::Restricted
198 );
199 assert_eq!(
200 classify_linux_permission_state(false, false, false),
201 PermissionState::Unknown
202 );
203
204 let mic_target = CaptureTarget::microphone(DefaultDeviceMode::Follow);
205 let rep = PlatformPermissions::check_permissions(
206 std::slice::from_ref(&mic_target),
207 &Backends::default(),
208 )
209 .unwrap();
210 assert!(rep.microphone().required);
211 assert!(!rep.system_audio().required);
212 assert!(!rep.microphone().can_prompt);
213 }
214}