Skip to main content

term_session/
lib.rs

1pub mod auto_spawn;
2
3use std::io;
4use std::sync::Arc;
5
6use muxio_tokio_rpc_ipc_client::RpcCallPrebuffered;
7use term_session_muxio_service_definitions::{
8    KillChannel, KillClient, ListChannels, ListChannelsResponse, ShutdownGateway,
9};
10
11// TODO: Rename to TERM_SESSION_CHANNEL
12pub const CHANNEL_ENV_VAR: &str = "TERM_WM_CHANNEL";
13pub const DEFAULT_CHANNEL: &str = "default/main";
14
15/// Resolve the channel from an optional CLI arg, falling back to the env var,
16/// then the default.
17pub fn resolve_channel(cli_channel: Option<String>) -> String {
18    cli_channel
19        .or_else(|| std::env::var(CHANNEL_ENV_VAR).ok())
20        .unwrap_or_else(|| DEFAULT_CHANNEL.to_string())
21}
22
23/// Seconds per minute, used by [`format_unix_relative`].
24const SECS_PER_MIN: u64 = 60;
25/// Seconds per hour, used by [`format_unix_relative`].
26const SECS_PER_HOUR: u64 = 3600;
27/// Seconds per day, used by [`format_unix_relative`].
28const SECS_PER_DAY: u64 = 86400;
29
30/// Format a unix timestamp as a relative human string ("2s ago", "5m ago", …),
31/// always in elapsed units regardless of age ("2d 5h" for ages beyond a day).
32pub fn format_unix_relative(ts: u64) -> String {
33    let now = std::time::SystemTime::now()
34        .duration_since(std::time::UNIX_EPOCH)
35        .map(|d| d.as_secs())
36        .unwrap_or(0);
37    format_unix_relative_at(ts, now)
38}
39
40/// Format a unix timestamp relative to an explicit `now` in unix seconds.
41///
42/// Elapsed durations are always rendered in relative units: seconds, minutes,
43/// hours, then combined days + hours. A zero timestamp renders as `-`.
44/// Timestamps newer than `now` saturate to the seconds tier.
45pub fn format_unix_relative_at(ts: u64, now: u64) -> String {
46    if ts == 0 {
47        return "-".to_string();
48    }
49    let diff = now.saturating_sub(ts);
50    if diff < SECS_PER_MIN {
51        format!("{diff}s")
52    } else if diff < SECS_PER_HOUR {
53        format!("{}m", diff / SECS_PER_MIN)
54    } else if diff < SECS_PER_DAY {
55        format!("{}h", diff / SECS_PER_HOUR)
56    } else {
57        format!(
58            "{}d {}h",
59            diff / SECS_PER_DAY,
60            (diff % SECS_PER_DAY) / SECS_PER_HOUR
61        )
62    }
63}
64
65/// Connect to the gateway daemon and run `op` with a live client. The tokio
66/// runtime that hosts the muxio connection is kept alive for the whole `op`,
67/// so RPCs complete (dropping it early would tear down the connection and
68/// hang the call). `op` receives an owned `Arc` and runs on that runtime.
69pub fn with_gateway<F, Fut, T>(op: F) -> io::Result<T>
70where
71    F: FnOnce(Arc<muxio_tokio_rpc_ipc_client::RpcIpcClient>) -> Fut,
72    Fut: std::future::Future<Output = T>,
73{
74    let gateway = term_session_muxio_service_definitions::gateway_channel_name();
75    let rt =
76        tokio::runtime::Runtime::new().map_err(|e| io::Error::other(format!("runtime: {e}")))?;
77    rt.block_on(async {
78        let client = muxio_tokio_rpc_ipc_client::RpcIpcClient::new(&gateway.to_string())
79            .await
80            .map_err(|e| {
81                io::Error::new(
82                    io::ErrorKind::ConnectionRefused,
83                    format!(
84                        "No gateway daemon is running on '{gateway}'. Start one with `term-session --channel <name>` or `term-session --daemon` first.\n  cause: {e}"
85                    ),
86                )
87            })?;
88        Ok(op(client).await)
89    })
90}
91
92/// List channels from the gateway, including the daemon PID + socket name.
93pub fn list_channels() -> io::Result<ListChannelsResponse> {
94    with_gateway(|client| async move { ListChannels::call(&*client, ()).await })?
95        .map_err(|e| io::Error::other(format!("list: {e}")))
96}
97
98/// Kill a channel's session and detach all its sockets.
99pub fn kill_channel(channel: &str) -> io::Result<()> {
100    with_gateway(|client| async move { KillChannel::call(&*client, channel.to_string()).await })?
101        .map_err(|e| io::Error::other(format!("kill channel: {e}")))
102}
103
104/// Detach a single client socket from a channel by `conn_id`.
105pub fn kill_client(channel: &str, conn_id: usize) -> io::Result<()> {
106    with_gateway(|client| async move {
107        KillClient::call(&*client, (channel.to_string(), conn_id)).await
108    })?
109    .map_err(|e| io::Error::other(format!("kill client: {e}")))
110}
111
112/// Stop the gateway daemon.
113///
114/// The daemon refuses to shut down while any live session is running unless
115/// `force` is true (see `RPC_ERROR_LIVE_SESSIONS`).
116pub fn stop_gateway(force: bool) -> io::Result<()> {
117    with_gateway(|client| async move { ShutdownGateway::call(&*client, force).await })?
118        .map_err(|e| io::Error::other(format!("shutdown: {e}")))
119}
120
121/// Run the gateway daemon: rename the process, detach from the controlling
122/// terminal, and serve until `ShutdownGateway`. `selfcheck_marker` is a
123/// test-only path written with the platform's detachment proof once bound.
124pub fn run_daemon(selfcheck_marker: Option<std::path::PathBuf>) -> io::Result<()> {
125    tracing_subscriber::fmt::init();
126
127    // Make the daemon recognizable in process managers: every `term-session`
128    // process is the same binary, so rename this one so `ps`/`top`/Task
129    // Manager show `term-session-daemon` instead of generic `term-session`.
130    set_daemon_process_name();
131
132    // Self-detach: a `--daemon` that was not already started detached (e.g.
133    // spawned directly by a test or wrapper, not via
134    // `auto_spawn::connect_or_spawn_server`) detaches itself from the
135    // launching terminal so Ctrl+C / SIGHUP never reach it.
136    //
137    // - Unix: `setsid()` starts a new session and process group and drops the
138    //   controlling terminal. It fails with EPERM if the process is already a
139    //   process-group leader, which is exactly the already-detached case — so
140    //   ignore that error.
141    // - Windows: `FreeConsole()` detaches from the launching console so no
142    //   console control events (Ctrl+C, Ctrl+Close) are ever delivered to the
143    //   daemon. It reports failure when there is no console to detach from,
144    //   which is the already-detached `auto_spawn` case — so ignore that too.
145    #[cfg(unix)]
146    unsafe {
147        libc::setsid();
148    }
149    #[cfg(windows)]
150    unsafe {
151        let _ = windows_sys::Win32::System::Console::FreeConsole();
152    }
153
154    let gateway = term_session_muxio_service_definitions::gateway_channel_name();
155
156    // Test-only: as soon as the gateway socket is reachable, write the
157    // platform's detachment proof to the marker, then exit the probe thread.
158    if let Some(ref marker) = selfcheck_marker {
159        let gw = gateway.clone();
160        let marker = marker.clone();
161        std::thread::Builder::new()
162            .name("daemon-selfcheck".into())
163            .spawn(move || {
164                for _ in 0..200 {
165                    if term_session_muxio_service_definitions::probe_ipc_endpoint(&gw) {
166                        write_selfcheck_marker(&marker);
167                        return;
168                    }
169                    std::thread::sleep(std::time::Duration::from_millis(25));
170                }
171                let _ = std::fs::write(&marker, "bound-timeout");
172            })?;
173    }
174
175    let rt =
176        tokio::runtime::Runtime::new().map_err(|e| io::Error::other(format!("runtime: {e}")))?;
177    rt.block_on(term_session_server::run_gateway(gateway.clone()))
178        .map_err(|e| io::Error::other(format!("gateway error: {e}")))?;
179    Ok(())
180}
181
182/// Rename the running process so process managers can distinguish the gateway
183/// daemon from interactive `term-session` clients. Best-effort and cosmetic:
184/// a failure is ignored and never affects functionality.
185///
186/// Platform behavior (and limitations):
187/// - **Linux:** `PR_SET_NAME` sets the process comm (capped at 15 bytes →
188///   `term-session-d`), so `ps -comm`, `top`, and `htop` show the renamed
189///   value. This is the most complete rename on any platform.
190/// - **macOS:** `pthread_setname_np` sets the **thread** name, not the process
191///   comm — `ps -o comm` and Activity Monitor's process list still show
192///   `term-session`. The renamed value is only visible in Activity Monitor's
193///   per-thread view (and `sample`). This is an OS limitation: macOS has no
194///   portable user-space API to rename the process comm. Daemon disambiguation
195///   on macOS therefore relies primarily on the `--daemon` argv flag and the
196///   `Gateway Daemon PID` header printed by `term-session list`.
197/// - **Windows:** `SetThreadDescription` sets the thread description, which
198///   Process Explorer / Process Hacker show in the **Description** column.
199pub fn set_daemon_process_name() {
200    #[cfg(target_os = "linux")]
201    {
202        use std::ffi::CString;
203        if let Ok(name) = CString::new("term-session-d") {
204            unsafe {
205                libc::prctl(libc::PR_SET_NAME, name.as_ptr() as usize, 0, 0, 0);
206            }
207        }
208    }
209    #[cfg(target_os = "macos")]
210    {
211        use std::ffi::CString;
212        if let Ok(name) = CString::new("term-session-daemon") {
213            unsafe {
214                libc::pthread_setname_np(name.as_ptr());
215            }
216        }
217    }
218    #[cfg(windows)]
219    {
220        use windows_sys::Win32::System::Threading::{GetCurrentThread, SetThreadDescription};
221        let wide: Vec<u16> = "term-session-daemon"
222            .encode_utf16()
223            .chain(std::iter::once(0))
224            .collect();
225        unsafe {
226            SetThreadDescription(GetCurrentThread(), wide.as_ptr());
227        }
228    }
229}
230
231/// Write the platform's detachment proof to the marker (test-only).
232fn write_selfcheck_marker(marker: &std::path::Path) {
233    #[cfg(windows)]
234    let proof = {
235        use windows_sys::Win32::System::Console::{
236            GetConsoleProcessList, GetStdHandle, STD_INPUT_HANDLE,
237        };
238        let mut pids = [0u32; 4];
239        let count = unsafe {
240            let _handle = GetStdHandle(STD_INPUT_HANDLE);
241            GetConsoleProcessList(pids.as_mut_ptr(), pids.len() as u32)
242        };
243        if count == 0 {
244            "windows-no-console"
245        } else {
246            "windows-has-console"
247        }
248    };
249    #[cfg(unix)]
250    let proof = {
251        let sid = unsafe { libc::getsid(0) };
252        let pid = unsafe { libc::getpid() };
253        if sid == pid {
254            "unix-session-leader"
255        } else {
256            "unix-not-leader"
257        }
258    };
259    #[cfg(not(any(unix, windows)))]
260    let proof = "unsupported";
261    let _ = std::fs::write(marker, proof);
262}
263
264#[cfg(test)]
265mod tests {
266    use super::*;
267
268    /// Serializes tests that mutate `TERM_WM_CHANNEL`, which is process-global.
269    fn env_lock() -> std::sync::MutexGuard<'static, ()> {
270        static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
271        LOCK.lock().unwrap_or_else(|e| e.into_inner())
272    }
273
274    #[test]
275    fn cli_channel_takes_precedence_over_env() {
276        let _guard = env_lock();
277        unsafe {
278            std::env::set_var(CHANNEL_ENV_VAR, "other/chan");
279        }
280        assert_eq!(resolve_channel(Some("work/dev".to_string())), "work/dev");
281        unsafe {
282            std::env::remove_var(CHANNEL_ENV_VAR);
283        }
284    }
285
286    #[test]
287    fn falls_back_to_env_channel() {
288        let _guard = env_lock();
289        unsafe {
290            std::env::set_var(CHANNEL_ENV_VAR, "work/dev");
291        }
292        assert_eq!(resolve_channel(None), "work/dev");
293        unsafe {
294            std::env::remove_var(CHANNEL_ENV_VAR);
295        }
296    }
297
298    #[test]
299    fn falls_back_to_default_channel() {
300        let _guard = env_lock();
301        unsafe {
302            std::env::remove_var(CHANNEL_ENV_VAR);
303        }
304        assert_eq!(resolve_channel(None), DEFAULT_CHANNEL);
305    }
306
307    #[test]
308    fn format_zero_timestamp_is_dash() {
309        assert_eq!(format_unix_relative_at(0, SECS_PER_DAY), "-");
310    }
311
312    #[test]
313    fn format_under_a_minute_shows_seconds() {
314        assert_eq!(
315            format_unix_relative_at(SECS_PER_DAY - 42, SECS_PER_DAY),
316            "42s"
317        );
318    }
319
320    #[test]
321    fn format_under_an_hour_shows_minutes() {
322        assert_eq!(
323            format_unix_relative_at(SECS_PER_DAY - 3_300, SECS_PER_DAY),
324            "55m"
325        );
326    }
327
328    #[test]
329    fn format_under_a_day_shows_hours() {
330        assert_eq!(
331            format_unix_relative_at(SECS_PER_DAY - 7_200, SECS_PER_DAY),
332            "2h"
333        );
334    }
335
336    #[test]
337    fn format_older_than_a_day_shows_days_and_hours() {
338        assert_eq!(
339            format_unix_relative_at(10 * SECS_PER_DAY, 11 * SECS_PER_DAY),
340            "1d 0h"
341        );
342        assert_eq!(
343            format_unix_relative_at(10 * SECS_PER_DAY, 11 * SECS_PER_DAY + 3 * SECS_PER_HOUR),
344            "1d 3h"
345        );
346    }
347
348    #[test]
349    fn format_day_boundary_exact() {
350        assert_eq!(
351            format_unix_relative_at(10 * SECS_PER_DAY, 11 * SECS_PER_DAY),
352            "1d 0h"
353        );
354    }
355
356    #[test]
357    fn format_timestamp_newer_than_now_saturates() {
358        assert_eq!(
359            format_unix_relative_at(SECS_PER_DAY + 10, SECS_PER_DAY),
360            "0s"
361        );
362    }
363
364    #[test]
365    fn format_does_not_render_clock_time() {
366        // Regression for the military-time leak: an old timestamp rendered
367        // `ts % 86400` (UTC time-of-day). It must never produce HH:MM:SS.
368        let ts = SECS_PER_DAY * 40 + 18 * SECS_PER_HOUR + 48 * SECS_PER_MIN + 46;
369        let out = format_unix_relative_at(ts, SECS_PER_DAY * 42);
370        assert_eq!(out, "1d 5h");
371        assert!(!out.contains(':'), "clock-time format leaked: {out}");
372    }
373}