agentmux_srv/proc_name.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The name `ps`, `top` and desktop system monitors show for this process on
5//! Linux.
6//!
7//! The kernel keeps a 15-byte process name (`comm`), cut from the file name.
8//! srv ships as `agentmux-srv-<version>-linux.x64`, so without this it shows
9//! as `agentmux-srv-0.`, a name that changes with every release. Each role srv
10//! runs in sets a stable name instead
11//! (docs/reports/REPORT_PROCESS_ICONS_NAMES_AND_LABELS_2026_10_10.md ยง8.2).
12//!
13//! Only the calling thread is renamed, and the main thread's name is the
14//! process's, so call this from the main thread. Elsewhere it does nothing:
15//! Windows takes the name from the exe's version resource (`build.rs`), and
16//! macOS has no equivalent call.
17
18/// The backend itself.
19pub const MAIN: &str = "agentmux-srv";
20
21/// The child srv re-runs itself as to parse one attached document
22/// (`backend::attachments::extract`).
23pub const DOC_CHILD: &str = "agentmux-srvdoc";
24
25/// The child srv re-runs itself as to ask the system for file icons
26/// (`backend::file_icons`).
27pub const ICON_CHILD: &str = "agentmux-srvico";
28
29/// Set this thread's name. Longer than 15 bytes is cut by the kernel.
30pub fn set(name: &str) {
31 #[cfg(target_os = "linux")]
32 {
33 if let Ok(c) = std::ffi::CString::new(name) {
34 // SAFETY: PR_SET_NAME reads a NUL-terminated string, copying at
35 // most 16 bytes; `c` outlives the call.
36 unsafe {
37 libc::prctl(libc::PR_SET_NAME, c.as_ptr() as libc::c_ulong, 0, 0, 0);
38 }
39 }
40 }
41 #[cfg(not(target_os = "linux"))]
42 let _ = name;
43}
44
45#[cfg(all(test, target_os = "linux"))]
46mod tests {
47 use super::*;
48
49 fn own_thread_name() -> String {
50 std::fs::read_to_string("/proc/thread-self/comm").unwrap().trim_end().to_string()
51 }
52
53 // A spawned thread, so renaming it can't affect the test harness.
54 #[test]
55 fn sets_the_names_srv_uses() {
56 std::thread::spawn(|| {
57 for name in [MAIN, DOC_CHILD, ICON_CHILD] {
58 assert!(name.len() <= 15, "{name} would be cut by the kernel");
59 set(name);
60 assert_eq!(own_thread_name(), name);
61 }
62 })
63 .join()
64 .unwrap();
65 }
66
67 #[test]
68 fn a_long_name_is_cut_to_fifteen_bytes() {
69 std::thread::spawn(|| {
70 set("agentmux-srv-0.59.18-linux.x64");
71 assert_eq!(own_thread_name(), "agentmux-srv-0.");
72 })
73 .join()
74 .unwrap();
75 }
76}