Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(macos): replace running copies without token handshake (#7624)

Ezra Ellette2cee2d4ffffc
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff.rs b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff.rs
index 258d14e58..bb9956a36 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff.rs
@@ -2,13 +2,11 @@
// https://screenpipe.com
//! A deliberate launch selects a build before either single-instance path runs.
-//! The private socket authenticates the connecting process, never a supplied PID.
+//! A selected replacement terminates the verified previous process before starting.
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
-const PROTOCOL: u32 = 1;
-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Identity {
pid: i32,
@@ -51,21 +49,6 @@ fn select(current: &Identity, candidate: &Identity) -> Selection {
}
}
-#[derive(Serialize, Deserialize)]
-#[serde(deny_unknown_fields)]
-struct Request {
- protocol: u32,
- replace: bool,
-}
-
-#[derive(Serialize, Deserialize)]
-struct Response {
- protocol: u32,
- identity: Identity,
- outcome: String,
- error: Option<String>,
-}
-
fn report_failure(root: &std::path::Path, stage: &str, error: &str) {
crate::update_diagnostics::append(
root,
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/kernel_signature.rs b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/kernel_signature.rs
index 8bc4ccab1..506dd2444 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/kernel_signature.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/kernel_signature.rs
@@ -34,13 +34,6 @@ extern "C" {
}
extern "C" {
fn csops(pid: i32, op: u32, data: *mut libc::c_void, len: usize) -> i32;
- fn csops_audittoken(
- pid: i32,
- op: u32,
- data: *mut libc::c_void,
- len: usize,
- audit: *const u32,
- ) -> i32;
}
fn word(data: &[u8], offset: usize) -> Result<usize> {
@@ -102,24 +95,9 @@ fn directory_identity(directory: &[u8], expected_hash: &str) -> Result<String> {
Ok(std::str::from_utf8(&tail[..end])?.to_owned())
}
-pub(super) fn verify(
- pid: i32,
- audit: Option<&[u32; 8]>,
- expected_identifier: &str,
- expected_cert: &[u8],
-) -> Result<()> {
- let call = |op, buffer: &mut [u8]| unsafe {
- match audit {
- Some(token) => csops_audittoken(
- pid,
- op,
- buffer.as_mut_ptr().cast(),
- buffer.len(),
- token.as_ptr(),
- ),
- None => csops(pid, op, buffer.as_mut_ptr().cast(), buffer.len()),
- }
- };
+pub(super) fn verify(pid: i32, expected_identifier: &str, expected_cert: &[u8]) -> Result<()> {
+ let call =
+ |op, buffer: &mut [u8]| unsafe { csops(pid, op, buffer.as_mut_ptr().cast(), buffer.len()) };
let mut status = [0; 4];
if call(0, &mut status) != 0 || u32::from_ne_bytes(status) & 1 == 0 {
bail!("running code is no longer valid");
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/macos.rs b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/macos.rs
index 21c9288ef..249c0f032 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/macos.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/macos.rs
@@ -1,16 +1,13 @@
// screenpipe — AI that knows everything you've seen, said, or heard
// https://screenpipe.com
-use super::{report_failure, select, Identity, Request, Response, Selection, PROTOCOL};
+use super::{report_failure, select, Identity, Selection};
use anyhow::{bail, Context, Result};
use fs2::FileExt;
use sha2::{Digest, Sha256};
use std::{
fs::{File, OpenOptions},
- os::unix::{
- fs::{DirBuilderExt, MetadataExt, OpenOptionsExt, PermissionsExt},
- io::AsRawFd,
- },
+ os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt},
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, Ordering},
@@ -19,15 +16,11 @@ use std::{
time::Duration,
};
use tauri::Manager;
-use tokio::{
- io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
- net::{UnixListener, UnixStream},
-};
-#[path = "signing.rs"]
-mod signing;
#[path = "kernel_signature.rs"]
mod kernel_signature;
+#[path = "signing.rs"]
+mod signing;
const WAIT: Duration = Duration::from_secs(30);
const EXPECTED_BUILD_ENV: &str = "SCREENPIPE_MANUAL_HANDOFF_BUILD";
@@ -39,7 +32,6 @@ static CAPTURE_BEFORE_HANDOFF: AtomicBool = AtomicBool::new(false);
struct Runtime {
identity: Identity,
requirement: String,
- socket: PathBuf,
lock: PathBuf,
root: PathBuf,
executable_stamp: (u64, u64, i64, i64, u64),
@@ -60,7 +52,7 @@ fn manual_launch() -> bool {
fn private_directory(root: &Path, identifier: &str) -> Result<PathBuf> {
let hash = Sha256::digest(format!("{}:{identifier}", root.display()).as_bytes());
- // Darwin sockaddr_un has only 104 bytes. Keep the owner-only path short.
+ // Retain the existing owner-only launch-lock directory across app versions.
let digest = format!("{hash:x}");
let directory = PathBuf::from(format!(
"/tmp/screenpipe-handoff-{}-{}",
@@ -103,86 +95,6 @@ async fn acquire(path: &Path) -> Result<File> {
}
}
-fn peer_token(stream: &UnixStream) -> Result<[u32; 8]> {
- // The kernel audit token includes pidversion, unlike LOCAL_PEERPID. Security
- // binds signature validation to the process that connected, even after PID reuse.
- let mut token = [0u32; 8];
- let mut len = std::mem::size_of_val(&token) as libc::socklen_t;
- let result = unsafe {
- libc::getsockopt(
- stream.as_raw_fd(),
- 0, /* SOL_LOCAL */
- 0x006, /* LOCAL_PEERTOKEN, sys/un.h */
- token.as_mut_ptr().cast(),
- &mut len,
- )
- };
- if result != 0 || len as usize != std::mem::size_of_val(&token) {
- bail!("could not authenticate local peer audit token");
- }
- Ok(token)
-}
-
-fn authenticated_peer(stream: &UnixStream, requirement: &str) -> Result<i32> {
- signing::verify_audit_token(&peer_token(stream)?, requirement)
-}
-
-async fn read_message<T: serde::de::DeserializeOwned>(stream: &mut UnixStream) -> Result<T> {
- let mut bytes = Vec::new();
- // A length cap prevents even a same-user local client from allocating unbounded memory.
- use tokio::io::AsyncReadExt;
- let mut reader = BufReader::new(stream.take(8193));
- tokio::time::timeout(WAIT, reader.read_until(b'\n', &mut bytes)).await??;
- if bytes.len() > 8192 || bytes.last() != Some(&b'\n') {
- bail!("invalid handoff message");
- }
- Ok(serde_json::from_slice(&bytes)?)
-}
-
-async fn write_message<T: serde::Serialize>(stream: &mut UnixStream, message: &T) -> Result<()> {
- let mut bytes = serde_json::to_vec(message)?;
- bytes.push(b'\n');
- tokio::time::timeout(WAIT, stream.write_all(&bytes)).await??;
- Ok(())
-}
-
-async fn exchange(runtime: &Runtime, replace: bool) -> Result<Option<Response>> {
- let mut stream = match UnixStream::connect(&runtime.socket).await {
- Ok(stream) => stream,
- Err(e)
- if matches!(
- e.kind(),
- std::io::ErrorKind::NotFound | std::io::ErrorKind::ConnectionRefused
- ) =>
- {
- return Ok(None)
- }
- Err(e) => return Err(e.into()),
- };
- let pid = authenticated_peer(&stream, &runtime.requirement)?;
- let stamp = signing::process_stamp(pid)?;
- let hash = signing::process_hash(pid)?;
- authenticated_peer(&stream, &runtime.requirement)?;
- write_message(
- &mut stream,
- &Request {
- protocol: PROTOCOL,
- replace,
- },
- )
- .await?;
- let response: Response = read_message(&mut stream).await?;
- if response.protocol != PROTOCOL
- || response.identity.pid != pid
- || response.identity.identifier != runtime.identity.identifier
- || (response.identity.uid, response.identity.started) != stamp
- || response.identity.hash != hash
- {
- bail!("instance identity changed during handshake");
- }
- Ok(Some(response))
-}
-
async fn wait_for_exit(old: &Identity) -> Result<()> {
wait_until_exit(old, WAIT).await?;
// Match the existing updater's ScreenCaptureKit release interval.
@@ -240,50 +152,18 @@ async fn initialize_inner(root: PathBuf) -> Result<()> {
let runtime = Runtime {
identity,
requirement,
- socket: directory.join("ipc"),
lock: directory.join("launch"),
executable_stamp,
root,
};
let lock = acquire(&runtime.lock).await?;
- let existing = exchange(&runtime, false).await?;
- if let Some(old) = existing {
- record_selection(&runtime, &old.identity, &runtime.identity);
- let mut replaced = false;
- if manual_launch() {
- match select(&old.identity, &runtime.identity) {
- Selection::Replace => {
- verify_selected_bundle(&runtime)?;
- let reply = exchange(&runtime, true)
- .await?
- .context("old instance disappeared before handoff")?;
- if reply.outcome != "exiting" {
- bail!("{}", reply.error.unwrap_or(reply.outcome));
- }
- wait_for_exit(&old.identity).await?;
- replaced = true;
- }
- Selection::Reject => {
- bail!("the opened copy is older or belongs to a different edition")
- }
- Selection::Focus => {}
- }
- }
- if !replaced {
- // A verified identical build (or background/deep-link launch) must
- // never fall through to engine initialization if focus is unavailable.
- focus_existing(&old.identity).await?;
- std::process::exit(0);
- }
- } else if manual_launch() {
- legacy_takeover(&runtime).await?;
- }
+ takeover(&runtime).await?;
let _ = RUNTIME.set(runtime);
*LAUNCH_LOCK.lock().unwrap_or_else(|e| e.into_inner()) = Some(lock);
Ok(())
}
-// Revalidate the selected bundle immediately before asking the old owner to exit.
+// Revalidate the selected bundle immediately before terminating the old owner.
fn verify_selected_bundle(runtime: &Runtime) -> Result<()> {
let verified = signing::on_disk(
&runtime.identity.executable,
@@ -338,99 +218,19 @@ fn record_selection(runtime: &Runtime, source: &Identity, target: &Identity) {
);
}
-pub(crate) fn install(app: &tauri::AppHandle) -> Result<()> {
+pub(crate) fn install(_app: &tauri::AppHandle) -> Result<()> {
let Some(runtime) = RUNTIME.get() else {
return Ok(());
};
- // The startup lock excludes other new builds. A stale socket has no live peer.
- if runtime.socket.exists() {
- std::fs::remove_file(&runtime.socket)?;
- }
- let listener = UnixListener::bind(&runtime.socket)?;
- std::fs::set_permissions(&runtime.socket, std::fs::Permissions::from_mode(0o600))?;
- let app = app.clone();
- tauri::async_runtime::spawn(async move {
- while let Ok((mut stream, _)) = listener.accept().await {
- let app = app.clone();
- tauri::async_runtime::spawn(async move {
- if let Err(error) = serve(&app, runtime, &mut stream).await {
- report_failure(&runtime.root, "request", &format!("{error:#}"));
- }
- });
- }
- });
LAUNCH_LOCK.lock().unwrap_or_else(|e| e.into_inner()).take();
crate::update_diagnostics::append(
&runtime.root,
"manual_handoff_ready",
- &format!(
- "identity={}; requirement={}",
- serde_json::to_string(&runtime.identity).unwrap_or_default(),
- runtime.requirement
- ),
+ &format!("identity={}", serde_json::to_string(&runtime.identity)?),
);
Ok(())
}
-async fn serve(app: &tauri::AppHandle, runtime: &Runtime, stream: &mut UnixStream) -> Result<()> {
- let pid = authenticated_peer(stream, &runtime.requirement)?;
- let peer = signing::running(pid, &runtime.requirement)?;
- authenticated_peer(stream, &runtime.requirement)?;
- if peer.identifier != runtime.identity.identifier {
- bail!("different app edition");
- }
- let request: Request = read_message(stream).await?;
- if request.protocol != PROTOCOL {
- bail!("unsupported handoff protocol");
- }
- let mut response = Response {
- protocol: PROTOCOL,
- identity: runtime.identity.clone(),
- outcome: "running".into(),
- error: None,
- };
- if !request.replace {
- return write_message(stream, &response).await;
- }
- if select(&runtime.identity, &peer) != Selection::Replace {
- bail!("incoming build is not a replacement");
- }
- // Also validate all on-disk resources before the healthy owner gives up its API.
- let candidate = signing::on_disk(&peer.executable, &runtime.requirement, &peer)?;
- if candidate.hash != peer.hash {
- bail!("incoming bundle changed during launch");
- }
- record_selection(runtime, &runtime.identity, &peer);
- match prepare(app).await {
- Ok(guard) => {
- if !signing::is_same_process(&peer) {
- recover(app);
- bail!("incoming process exited before handoff");
- }
- response.outcome = "exiting".into();
- if let Err(error) = write_message(stream, &response).await {
- recover(app);
- return Err(error);
- }
- crate::update_diagnostics::append(
- &runtime.root,
- "manual_handoff_drained",
- "outcome=exiting; staged_install=suppressed",
- );
- // Keep native initialization excluded until the process is gone.
- std::mem::forget(guard);
- tokio::time::sleep(Duration::from_millis(100)).await;
- crate::process_exit::force_process_exit(0);
- }
- Err(error) => {
- response.outcome = "failed".into();
- response.error = Some(format!("{error:#}"));
- write_message(stream, &response).await?;
- Err(error)
- }
- }
-}
-
async fn prepare(app: &tauri::AppHandle) -> Result<tokio::sync::RwLockWriteGuard<'static, ()>> {
if PENDING
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
@@ -475,7 +275,7 @@ async fn prepare(app: &tauri::AppHandle) -> Result<tokio::sync::RwLockWriteGuard
let drain_started_at = std::time::Instant::now();
// The five-second forced-exit budget is too short for an active
// recorder (capture + durable database close took 6.5s in acceptance).
- // Use the existing IPC reply deadline instead. Exceeding it cancels
+ // Use the existing handoff deadline instead. Exceeding it cancels
// this replacement, but the owned stop task still finishes and the
// previous owner recovers without abandoning pending writes.
match wait_for_drain(stop, WAIT).await {
@@ -629,7 +429,7 @@ fn show_error(message: &str) {
.args(["-e", "on run argv\ndisplay alert \"Screenpipe couldn't switch versions\" message (item 1 of argv)\nend run", "--", message]).spawn();
}
-async fn legacy_takeover(runtime: &Runtime) -> Result<()> {
+async fn takeover(runtime: &Runtime) -> Result<()> {
let name = runtime
.identity
.executable
@@ -638,7 +438,8 @@ async fn legacy_takeover(runtime: &Runtime) -> Result<()> {
for pid in signing::candidates(name)? {
if let Err(error) = signing::verify_process(pid, &runtime.requirement) {
if signing::claimed_identifier(pid).as_deref() == Some(&runtime.identity.identifier)
- || signing::process_path(pid).ok().as_ref() == Some(&runtime.identity.executable) {
+ || signing::process_path(pid).ok().as_ref() == Some(&runtime.identity.executable)
+ {
bail!("cannot authenticate the existing Screenpipe process {pid}: {error:#}");
}
continue;
@@ -648,6 +449,10 @@ async fn legacy_takeover(runtime: &Runtime) -> Result<()> {
continue;
}
record_selection(runtime, &old, &runtime.identity);
+ if !manual_launch() {
+ focus_existing(&old).await?;
+ std::process::exit(0);
+ }
match select(&old, &runtime.identity) {
Selection::Focus if owns_listener(pid, focus_port()).await? => {
focus_existing(&old).await?;
@@ -656,48 +461,9 @@ async fn legacy_takeover(runtime: &Runtime) -> Result<()> {
Selection::Reject => bail!("the opened copy is older than the running build"),
Selection::Focus | Selection::Replace => verify_selected_bundle(runtime)?,
}
- let settings = crate::store::read_startup_store(&runtime.root.join("store.bin"))
- .map_err(anyhow::Error::msg)?;
- let settings = settings.get("settings").unwrap_or(&settings);
- let settings: crate::store::SettingsStore = serde_json::from_value(settings.clone())?;
- let data_dir = crate::config::selected_recording_data_dir(&settings.data_dir)?;
- let mut api = crate::recording::LocalApiContext {
- port: std::env::var("SCREENPIPE_PORT")
- .ok()
- .and_then(|p| p.parse().ok())
- .unwrap_or(settings.recording.port),
- api_key: std::env::var("SCREENPIPE_API_KEY")
- .ok()
- .filter(|k| !k.is_empty())
- .or_else(|| {
- (!settings.recording.api_key.is_empty()).then_some(settings.recording.api_key)
- }),
- };
- // An old app can survive after its engine/listener exits (or another
- // copy takes the port). Its signature above already establishes which
- // process we may retire. Do not send credentials to a different owner
- // or require a dead API to prove that this process can be replaced.
- if retire_legacy_without_listener(&runtime.root, &old, api.port).await? {
- continue;
- }
- if api.api_key.is_none() {
- api.api_key =
- screenpipe_engine::auth_key::find_api_auth_key_for_data_dir(&data_dir).await;
- }
- if api.api_key.is_none() {
- bail!("cannot authenticate the legacy search API without its existing key");
- }
- verify_listener(pid, api.port).await?;
- verify_legacy_stopped(&api).await?;
- // Recheck both immediately before targeting the exact process. Never kill by name/port.
- verify_listener(pid, api.port).await?;
- verify_legacy_stopped(&api).await?;
- terminate_legacy(&old).await?;
- crate::update_diagnostics::append(
- &runtime.root,
- "manual_handoff_legacy_exited",
- "outcome=selected_copy_starting; capture=stopped",
- );
+ // Opening another copy explicitly selects it. Do not require the old
+ // app's socket, API key, or recording state to cooperate with replacement.
+ retire_previous(&runtime.root, &old).await?;
}
Ok(())
}
@@ -709,35 +475,36 @@ async fn verify_listener(pid: i32, port: u16) -> Result<()> {
Ok(())
}
-async fn retire_legacy_without_listener(root: &Path, old: &Identity, port: u16) -> Result<bool> {
- if owns_listener(old.pid, port).await? {
- return Ok(false);
- }
+async fn retire_previous(root: &Path, old: &Identity) -> Result<()> {
crate::update_diagnostics::append(
root,
- "manual_handoff_legacy_listener_missing",
+ "manual_handoff_terminate",
&format!(
- "pid={}; port={port}; cause=verified_process_does_not_own_api_listener; action=terminate_verified_process; capture=unknown",
- old.pid,
+ "pid={}; cause=opened_copy_selected; action=terminate_verified_process",
+ old.pid
),
);
- terminate_legacy(old).await?;
+ terminate_previous(old).await?;
crate::update_diagnostics::append(
root,
- "manual_handoff_legacy_exited",
- "outcome=selected_copy_starting; cause=missing_api_listener; capture=unknown",
+ "manual_handoff_previous_exited",
+ "outcome=selected_copy_starting; cause=opened_copy_selected",
);
- Ok(true)
+ Ok(())
}
-// Call only after authenticating the process and verifying the selected bundle.
-async fn terminate_legacy(old: &Identity) -> Result<()> {
+// Call only after verifying the process and the selected bundle. Recheck the
+// process generation immediately before signalling; never terminate by name/port.
+async fn terminate_previous(old: &Identity) -> Result<()> {
if !signing::is_same_process(old) {
- bail!("legacy process changed before termination; pid={}", old.pid);
+ bail!(
+ "previous process changed before termination; pid={}",
+ old.pid
+ );
}
if unsafe { libc::kill(old.pid, libc::SIGTERM) } != 0 {
return Err(std::io::Error::last_os_error())
- .with_context(|| format!("legacy termination failed; pid={}", old.pid));
+ .with_context(|| format!("previous process termination failed; pid={}", old.pid));
}
wait_for_exit(old).await
}
@@ -778,53 +545,6 @@ async fn owns_listener(pid: i32, port: u16) -> Result<bool> {
Ok(!stdout.trim().is_empty() && stdout.lines().all(|line| line.trim() == expected))
}
-async fn verify_legacy_stopped(api: &crate::recording::LocalApiContext) -> Result<()> {
- let client = reqwest::Client::builder()
- .no_proxy()
- .timeout(Duration::from_secs(3))
- .build()?;
- let unauthenticated = client.get(api.url("/pipes")).send().await?;
- if !matches!(unauthenticated.status().as_u16(), 401 | 403) {
- bail!("legacy API does not enforce authentication; cannot verify search-only ownership");
- }
- // This read-only route is blocked by the shipped search-only middleware.
- let response = api.apply_auth(client.get(api.url("/pipes"))).send().await?;
- if response.status() != reqwest::StatusCode::CONFLICT
- || response.json::<serde_json::Value>().await?["error"] != "search_only"
- {
- bail!("legacy app is not in search-only mode");
- }
- for (route, field) in [
- ("/audio/device/status", "is_running"),
- ("/vision/device/status", "active"),
- ] {
- let response = api
- .apply_auth(client.get(api.url(route)))
- .send()
- .await?
- .error_for_status()?
- .json::<serde_json::Value>()
- .await?;
- let devices = response
- .as_array()
- .context("capture status is unavailable")?;
- if devices.iter().any(|d| d[field].as_bool() != Some(false)) {
- bail!("legacy capture has not stopped");
- }
- }
- let health = api
- .apply_auth(client.get(api.url("/health")))
- .send()
- .await?
- .error_for_status()?
- .json::<serde_json::Value>()
- .await?;
- if health["ui_recorder"]["running"].as_bool() != Some(false) {
- bail!("legacy UI capture has not been verified stopped");
- }
- Ok(())
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -843,29 +563,46 @@ mod tests {
}
#[tokio::test]
- async fn missing_listener_retires_only_the_selected_process() {
+ async fn replacement_retires_only_the_selected_process_without_ipc() {
let root = tempfile::tempdir().unwrap();
// This process owns the port; the stale process must not send it an
// API key or terminate it merely because it has the configured port.
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let owner = test_process_identity(std::process::id() as i32);
- assert!(!retire_legacy_without_listener(root.path(), &owner, port)
- .await
- .unwrap());
-
- let mut child = tokio::process::Command::new("/bin/sleep")
- .arg("60")
+ let ready = root.path().join("ready");
+ let mut child = tokio::process::Command::new(std::env::current_exe().unwrap())
+ .args([
+ "--exact",
+ "manual_handoff::macos::tests::replacement_process_fixture",
+ ])
+ .env("SCREENPIPE_HANDOFF_TEST_READY", &ready)
.kill_on_drop(true)
.spawn()
.unwrap();
+ tokio::time::timeout(WAIT, async {
+ while !ready.exists() {
+ assert!(
+ child.try_wait().unwrap().is_none(),
+ "fixture exited before ready"
+ );
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .unwrap();
+ let active_port: u16 = std::fs::read_to_string(&ready).unwrap().parse().unwrap();
let old = test_process_identity(child.id().unwrap() as i32);
+ assert!(owns_listener(old.pid, active_port).await.unwrap());
let exited = tokio::spawn(async move { child.wait().await.unwrap() });
- assert!(retire_legacy_without_listener(root.path(), &old, port)
- .await
- .unwrap());
+ retire_previous(root.path(), &old).await.unwrap();
use std::os::unix::process::ExitStatusExt;
assert_eq!(exited.await.unwrap().signal(), Some(libc::SIGTERM));
+ // The selected process's live listener is released; the new copy can
+ // own it even though the previous process never answered IPC or HTTP.
+ let _replacement_listener = tokio::net::TcpListener::bind(("127.0.0.1", active_port))
+ .await
+ .unwrap();
assert!(owns_listener(owner.pid, port).await.unwrap());
assert!(signing::is_same_process(&owner));
@@ -881,34 +618,50 @@ mod tests {
let report = crate::diagnostic_logs::collect_redacted_from_dirs(&[root.path().into()])
.await
.unwrap();
- assert!(report.contains("verified_process_does_not_own_api_listener"));
+ assert!(report.contains("opened_copy_selected"));
assert!(report.contains(&format!("pid={}", old.pid)));
- assert!(report.contains(&format!("port={port}")));
assert!(report.contains("outcome=selected_copy_starting"));
- assert!(report.contains("capture=unknown"));
drop(listener);
assert!(!owns_listener(owner.pid, port).await.unwrap());
}
+ #[test]
+ fn replacement_process_fixture() {
+ let Some(ready) = std::env::var_os("SCREENPIPE_HANDOFF_TEST_READY") else {
+ return;
+ };
+ let _listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
+ let ready = PathBuf::from(ready);
+ let staged = ready.with_extension("pending");
+ std::fs::write(&staged, _listener.local_addr().unwrap().port().to_string()).unwrap();
+ std::fs::rename(staged, ready).unwrap();
+ loop {
+ std::thread::park();
+ }
+ }
+
#[tokio::test]
async fn stale_identity_never_terminates_the_reused_pid_and_failure_reaches_support() {
let root = tempfile::tempdir().unwrap();
- let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
- let port = listener.local_addr().unwrap().port();
- drop(listener);
let mut old = test_process_identity(std::process::id() as i32);
old.started -= 1;
- let error = retire_legacy_without_listener(root.path(), &old, port)
- .await
- .unwrap_err();
+ let error = retire_previous(root.path(), &old).await.unwrap_err();
report_failure(root.path(), "launch", &format!("{error:#}"));
+ for day in 1..=7 {
+ std::fs::write(
+ root.path()
+ .join(format!("screenpipe-app.2026-10-{day:02}.log")),
+ "restart\n",
+ )
+ .unwrap();
+ }
let report = crate::diagnostic_logs::collect_redacted_from_dirs(&[root.path().into()])
.await
.unwrap();
- assert!(report.contains("legacy process changed before termination"));
+ assert!(report.contains("previous process changed before termination"));
assert!(report.contains("outcome=selected_copy_not_started"));
- assert!(!report.contains("manual_handoff_legacy_exited"));
+ assert!(!report.contains("manual_handoff_previous_exited"));
}
#[tokio::test]
@@ -952,37 +705,6 @@ mod tests {
assert!(flushed.load(Ordering::SeqCst));
}
- #[tokio::test]
- async fn socket_uses_os_peer_and_rejects_supplied_pid() {
- let (mut left, right) = UnixStream::pair().unwrap();
- let requirement = signing::own_requirement().unwrap();
- assert_eq!(
- authenticated_peer(&right, &requirement).unwrap(),
- std::process::id() as i32
- );
- left.write_all(b"{\"protocol\":1,\"replace\":true,\"pid\":1}\n")
- .await
- .unwrap();
- let mut right = right;
- assert!(read_message::<Request>(&mut right).await.is_err());
- }
-
- #[tokio::test]
- async fn stale_peer_process_generation_is_rejected() {
- let (_left, right) = UnixStream::pair().unwrap();
- let mut token = peer_token(&right).unwrap();
- token[7] = token[7].wrapping_add(1);
- assert!(signing::verify_audit_token(&token, &signing::own_requirement().unwrap()).is_err());
- }
-
- #[tokio::test]
- async fn oversized_request_is_rejected() {
- let (mut left, mut right) = UnixStream::pair().unwrap();
- let writer = tokio::spawn(async move { left.write_all(&vec![b'x'; 8193]).await });
- assert!(read_message::<Request>(&mut right).await.is_err());
- writer.await.unwrap().unwrap();
- }
-
#[tokio::test]
async fn concurrent_launches_cannot_hold_the_same_lease() {
let dir = tempfile::tempdir().unwrap();
@@ -1038,75 +760,4 @@ mod tests {
std::fs::rename(&replacement, &path).unwrap();
assert_ne!(before, file_stamp(&path).unwrap());
}
-
- #[tokio::test]
- async fn legacy_takeover_requires_authenticated_search_only_and_stopped_devices() {
- use axum::{routing::get, Json, Router};
- use serde_json::json;
- use std::sync::Arc;
- let active = Arc::new(AtomicBool::new(false));
- let audio_active = active.clone();
- let router =
- Router::new()
- .route(
- "/pipes",
- get(|headers: axum::http::HeaderMap| async move {
- if headers.get("authorization").and_then(|v| v.to_str().ok())
- == Some("Bearer handoff-test")
- {
- (
- axum::http::StatusCode::CONFLICT,
- Json(json!({"error":"search_only"})),
- )
- } else {
- (
- axum::http::StatusCode::UNAUTHORIZED,
- Json(json!({"error":"unauthorized"})),
- )
- }
- }),
- )
- .route(
- "/audio/device/status",
- get(move || {
- let audio_active = audio_active.clone();
- async move {
- Json(json!([{ "is_running": audio_active.load(Ordering::SeqCst) }]))
- }
- }),
- )
- .route(
- "/vision/device/status",
- get(|| async { Json(json!([{ "active":false }])) }),
- )
- .route(
- "/health",
- get(|| async { Json(json!({ "ui_recorder": {"running":false} })) }),
- );
- let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
- let port = listener.local_addr().unwrap().port();
- let listener = listener.into_std().unwrap();
- let server = tokio::spawn(async move {
- axum::Server::from_tcp(listener)
- .unwrap()
- .serve(router.into_make_service())
- .await
- .unwrap()
- });
- let mut api = crate::recording::LocalApiContext {
- port,
- api_key: Some("handoff-test".into()),
- };
- verify_legacy_stopped(&api).await.unwrap();
- active.store(true, Ordering::SeqCst);
- assert!(verify_legacy_stopped(&api)
- .await
- .unwrap_err()
- .to_string()
- .contains("capture has not stopped"));
- active.store(false, Ordering::SeqCst);
- api.api_key = Some("wrong-key".into());
- assert!(verify_legacy_stopped(&api).await.is_err());
- server.abort();
- }
}
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/signing.rs b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/signing.rs
index 236382ce9..9b9dffa2d 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/signing.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/manual_handoff/signing.rs
@@ -163,36 +163,17 @@ pub(super) fn verify_process(pid: i32, requirement: &str) -> Result<()> {
{
return Ok(());
}
- verify_replaced_process(pid, None)
+ verify_replaced_process(pid)
}
-fn verify_replaced_process(pid: i32, audit: Option<&[u32; 8]>) -> Result<()> {
+fn verify_replaced_process(pid: i32) -> Result<()> {
let (identifier, certificate) = SIGNER
.get()
.context("verified app signing certificate unavailable")?;
- super::kernel_signature::verify(pid, audit, identifier, certificate)
+ super::kernel_signature::verify(pid, identifier, certificate)
.context("authenticate original running signature after bundle replacement")
}
-pub(super) fn verify_audit_token(token: &[u32; 8], requirement: &str) -> Result<i32> {
- // audit_token_t fields: auid, euid, egid, ruid, rgid, pid, asid, pidversion.
- let pid = token[5] as i32;
- if token[1] != unsafe { libc::geteuid() } || pid <= 0 {
- bail!("different local user or invalid peer process");
- }
- let bytes: Vec<u8> = token.iter().flat_map(|word| word.to_ne_bytes()).collect();
- let data = CFData::from_buffer(&bytes);
- let mut attrs = GuestAttributes::new();
- attrs.set_audit_token(data.as_concrete_TypeRef());
- let code = SecCode::copy_guest_with_attribues(None, &attrs, Flags::NONE)
- .context("resolve authenticated peer process")?;
- let requirement: SecRequirement = requirement.parse()?;
- if code.check_validity(Flags::NONE, &requirement).is_err() {
- verify_replaced_process(pid, Some(token))?;
- }
- Ok(pid)
-}
-
pub(super) fn own_requirement() -> Result<String> {
let code = SecCode::for_self(Flags::NONE)?;
unsafe {
@@ -252,7 +233,7 @@ pub(super) fn running(pid: i32, requirement: &str) -> Result<Identity> {
let (identifier, version) = match metadata {
Ok((identifier, signed_hash, version)) if hash == signed_hash => (identifier, version),
_ => {
- verify_replaced_process(pid, None)?;
+ verify_replaced_process(pid)?;
let identifier = SIGNER.get().context("missing verified signer")?.0.clone();
let version = registered_process_version(pid, &identifier)?;
(identifier, version)