lmcpp 0.1.1

Rust bindings for llama.cpp's server with managed toolchain, typed endpoints, and UDS/HTTP support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Server Process - Kill
//! =====================
//!
//! Cross-platform helpers for terminating *server instances* started by this
//! crate.  The public surface consists of two primary entry points:
//!
//! * [`kill_by_hostname`] – shut down **exactly one** instance that was launched
//!   with `--host <hostname>` (or `-h <hostname>`).
//! * [`kill_all_servers`] – blanket kill for **all** running copies of the same
//!   executable.
//!
//! Internally the module follows a three-step escalation ladder:
//!
//! 1. **PID-file fast path** – If the server created a PID-file we trust it and
//!    attempt a *polite* signal (`SIGTERM`, `CTRL_BREAK`, or the platform
//!    equivalent) against the recorded PID.  
//! 2. **Command-line scan fallback** – When the PID-file is missing or stale we
//!    search the process list for an argument pair matching `--host <hostname>`
//!    or `-h <hostname>` (same logic used by the launcher).  
//! 3. **Force-kill escalation** – If the target is still alive after the
//!    configurable *grace period* (`POLITE_WAIT`) we deliver an unconditional
//!    kill (`SIGKILL` / `TerminateProcess`).  
//!
//! The helper returns **as soon as every targeted PID is gone** or the
//! force-kill timeout elapses, whichever happens first.  Any stubborn PIDs are
//! propagated back in a [`ProcessError::TerminationTimeout`] so that higher
//! layers can decide whether to retry, log, or treat it as fatal.
//!
//! ## Error semantics
//!
//! *All* public functions return [`crate::server::process::error::Result`].  The
//! errors are designed to be actionable; for instance, [`ProcessError::PermissionDenied`]
//! will not be silently swallowed.
//!
//! ## Constants
//!
//! Timing knobs such as `POLITE_WAIT`, `POLL_INTERVAL_MS`, and
//! `FORCE_KILL_TIMEOUT_SECS` live in the *parent* module so that they are shared
//! across all process-control utilities.  They are referenced here but not
//! re-exported.
//!
//! ## Thread safety
//!
//! The functions perform blocking I/O (`std::fs`, `nix`, Win32 API) and sleep
//! loops.  They are therefore *synchronous* and expected to run on a dedicated
//! management thread or as part of a CLI command—not in an async runtime.

use core::str;
use std::{
    path::Path,
    time::{Duration, Instant},
};

use super::{error::*, pid::*, *};

pub fn kill_by_client(pidfile_path: &Path, host: &str) -> Result<()> {
    // ── 1 ▪ PID-file fast path ────────────────────────────────────────────
    let pid: Option<u32> = std::fs::read_to_string(pidfile_path)
        .ok()
        .and_then(|s| s.trim().parse().ok());
    if let Some(pid) = pid {
        match pid_alive(pid) {
            // a) recognised & alive ⇒ killfs
            Ok(true) => {
                crate::info!("Killing server at {host} (PID {pid}) via pid-file");
                let res = kill_pids(&[pid], POLITE_WAIT);

                if res.is_ok() {
                    if let Err(e) = std::fs::remove_file(pidfile_path) {
                        crate::warn!("Failed to remove pid-file for host {host}: {e}");
                    }
                }
                return res;
            }
            // b) recognised & dead ⇒ stale file → delete & fall through
            Ok(false) => match std::fs::remove_file(pidfile_path) {
                Ok(_) => (),
                Err(e) => crate::warn!("Failed to remove stale pid-file for host {host}: {e}"),
            },
            // c) could not check (permissions, EACCES, etc.) ⇒ warn & fall back
            Err(e) => {
                crate::warn!("pid_alive({pid}) failed: {e}. Falling back to argv scan…");
            }
        }
    } else {
        if let Err(e) = std::fs::remove_file(pidfile_path) {
            crate::warn!("Failed to remove malformed pid-file for host {host}: {e}");
        }
    };

    // ── 2 ▪ Fallback: argv scan for --host / -h ───────────────────────────
    let patterns: &[&[&str]] = &[&["--host", host], &["-h", host]];
    if let Some(pid) = get_server_pid_by_cmd_args(patterns) {
        crate::info!("Killing server at {host} (PID {pid}) via argv scan");
        match kill_pids(&[pid], POLITE_WAIT) {
            Ok(()) => {
                match std::fs::remove_file(pidfile_path) {
                    Ok(_) => (),
                    Err(e) => {
                        crate::warn!("Failed to remove stale pid-file for host {host}: {e}")
                    }
                }
                return Ok(());
            }
            Err(e) => {
                crate::warn!("Failed to kill server at {host} (PID {pid}): {e}");
            }
        }
    }

    // ── 3 ▪ Nothing matched ───────────────────────────────────────────────
    Err(ProcessError::NoSuchProcess {
        query: format!("host={host}"),
    })
}

pub fn kill_all_servers(executable_name: &str) -> Result<()> {
    crate::info!("Killing all {executable_name} processes");

    // ── 1 ▪ kill-by-path / name (old behaviour) ────────────────
    let pids = get_all_server_pids(executable_name);
    let mut errors = Vec::new();

    if !pids.is_empty() {
        if let Err(e) = kill_pids(&pids, POLITE_WAIT) {
            errors.push(e);
        }
    }

    // ── 2 ▪ sweep pid-files we just invalidated ────────────────
    for pid in pids {
        // If the process is gone (or we just killed it), delete the file.
        match pid_alive(pid) {
            Ok(false) => crate::info!("PID {pid} shut down"),
            Ok(true) => crate::warn!("PID {pid} still alive, but we tried to kill it"),
            Err(e) => crate::warn!("Could not probe PID {pid}: {e}"),
        }
    }

    // ── 3 ▪ summarise result ───────────────────────────────────
    if errors.is_empty() {
        Ok(())
    } else {
        // bubble up the first error but log the rest
        for e in &errors[1..] {
            crate::warn!("Additional error while killing servers: {e}");
        }
        Err(errors.remove(0))
    }
}

fn kill_pids(pids: &[u32], polite_wait: Duration) -> Result<()> {
    let mut seen = std::collections::HashSet::with_capacity(pids.len());
    let uniq: Vec<u32> = pids.iter().copied().filter(|p| seen.insert(*p)).collect();
    if uniq.is_empty() {
        return Ok(());
    }
    let start = Instant::now();
    // phase 1 ▪ TERM / taskkill /T
    for pid in &uniq {
        match pid_alive(*pid) {
            Ok(true) => match kill_pid(*pid) {
                Ok(()) => crate::info!("Sent TERM to PID {}", pid),
                Err(e) => crate::error!("Failed to send TERM to PID {}: {}", pid, e),
            },
            Ok(false) => (),
            Err(e) => crate::error!("Failed to check PID {}: {}", pid, e),
        }
    }

    // phase 2 ▪ wait a little, bailing early if all gone
    let polite_deadline = Instant::now() + polite_wait;
    let mut probe_failures: Vec<(u32, ProcessError)> = Vec::new();

    while Instant::now() < polite_deadline {
        let all_dead = pids.iter().all(|&pid| match pid_alive(pid) {
            Ok(alive) => !alive,
            Err(e) => {
                // record only once; keep treating PID as "alive"
                if !probe_failures.iter().any(|(p, _)| *p == pid) {
                    probe_failures.push((pid, e));
                }
                false
            }
        });

        if all_dead {
            break;
        }
        std::thread::sleep(Duration::from_millis(POLL_INTERVAL_MS));
    }

    // phase 3 ▪ KILL / taskkill /F
    for &pid in pids {
        if let Err(e) = force_kill_pid(pid) {
            crate::error!("Failed to force-kill PID {pid}: {e}");
        }
    }

    if !probe_failures.is_empty() {
        for (pid, err) in &probe_failures {
            crate::warn!("Never obtained status for PID {pid}: {err}");
        }
    }
    let force_kill_deadline = Instant::now() + Duration::from_secs(FORCE_KILL_TIMEOUT_SECS);
    while Instant::now() < force_kill_deadline {
        if pids
            .iter()
            .all(|&pid| matches!(pid_alive(pid), Ok(false) | Err(_)))
        {
            break;
        }
        std::thread::sleep(Duration::from_millis(POLL_INTERVAL_MS));
    }

    #[cfg(target_os = "macos")]
    for &pid in &uniq {
        use nix::sys::wait::{waitpid, WaitPidFlag};
        let _ = nix::unistd::Pid::from_raw(pid as i32);
        // Try to reap our own children; ignore errors & non‑children.
        let _ = waitpid(
            nix::unistd::Pid::from_raw(pid as i32),
            Some(WaitPidFlag::WNOHANG),
        );
    }

    // return any stubborn PIDs so callers can escalate or log
    let leftovers: Vec<u32> = pids
        .iter()
        .copied()
        .filter(|&pid| match pid_alive(pid) {
            Ok(alive) => alive,
            Err(_) => true, // treat unknowns as alive
        })
        .collect();

    let elapsed = start.elapsed();
    if leftovers.is_empty() {
        Ok(())
    } else {
        Err(ProcessError::TerminationTimeout {
            operation: "kill_pids",
            elapsed,
            leftovers,
        })
    }
}

#[cfg(unix)]
pub fn kill_pid(pid: u32) -> Result<()> {
    use nix::{
        errno::Errno,
        sys::signal::{kill, Signal},
        unistd::Pid,
    };
    match kill(Pid::from_raw(pid as i32), Signal::SIGTERM) {
        Ok(_) | Err(Errno::ESRCH) => Ok(()), // gone already → success

        Err(Errno::EPERM) => Err(ProcessError::PermissionDenied {
            action: "send SIGTERM",
            source: "operation not permitted".into(),
        }),

        Err(e) => Err(ProcessError::CommandFailed {
            action: "send SIGTERM",
            source: e.into(),
        }),
    }
}

#[cfg(unix)]
fn force_kill_pid(pid: u32) -> Result<()> {
    use nix::{
        errno::Errno,
        sys::signal::{kill, Signal},
        unistd::Pid,
    };
    match kill(Pid::from_raw(pid as i32), Signal::SIGKILL) {
        Ok(_) | Err(Errno::ESRCH) => Ok(()),

        Err(Errno::EPERM) => Err(ProcessError::PermissionDenied {
            action: "send SIGKILL",
            source: "operation not permitted".into(),
        }),

        Err(e) => Err(ProcessError::CommandFailed {
            action: "send SIGKILL",
            source: e.into(),
        }),
    }
}

#[cfg(windows)]
pub fn kill_pid(pid: u32) -> Result<()> {
    use windows::Win32::{
        Foundation::CloseHandle,
        System::Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE},
    };

    unsafe {
        // Open the process with termination rights
        let handle = OpenProcess(PROCESS_TERMINATE, false, pid).map_err(|e| {
            ProcessError::CommandFailed {
                action: "OpenProcess",
                source: Box::new(e),
            }
        })?;

        if handle.is_invalid() {
            // Process already gone
            return Ok(());
        }

        // Terminate the process (this is already "forceful" on Windows)
        let result = TerminateProcess(handle, 1);
        let _ = CloseHandle(handle);

        result.map_err(|e| ProcessError::CommandFailed {
            action: "TerminateProcess",
            source: Box::new(e),
        })
    }
}

#[cfg(windows)]
pub fn force_kill_pid(pid: u32) -> Result<()> {
    use windows::Win32::{
        Foundation::CloseHandle,
        System::Threading::{OpenProcess, TerminateProcess, PROCESS_TERMINATE},
    };
    fn win32_error(action: &'static str) -> ProcessError {
        // windows::core::Error already wraps GetLastError + FormatMessageW.
        let err = windows::core::Error::from_win32();
        match err.code().0 {
            5 => ProcessError::PermissionDenied {
                /* ERROR_ACCESS_DENIED */
                action,
                source: Box::new(err),
            },
            _ => ProcessError::CommandFailed {
                action,
                source: Box::new(err),
            },
        }
    }

    unsafe {
        let h = OpenProcess(PROCESS_TERMINATE, false, pid).map_err(|e| {
            ProcessError::CommandFailed {
                action: "force-kill (OpenProcess)",
                source: e.into(),
            }
        })?;
        if h.is_invalid() {
            let err = windows::core::Error::from_win32();
            return match err.code().0 {
                87 => Ok(()), // already gone
                _ => Err(win32_error("force-kill (OpenProcess)")),
            };
        }
        match TerminateProcess(h, 1) {
            Ok(_) => CloseHandle(h).map_err(|e| ProcessError::CommandFailed {
                action: "force-kill (CloseHandle)",
                source: e.into(),
            }),
            Err(_) => {
                let _ = CloseHandle(h);
                Err(win32_error("force-kill (TerminateProcess)"))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use serial_test::serial;
    use tempfile::tempdir;

    use super::*;
    use crate::server::process::tests_helpers::*;

    // ─────────────────────────────────────────────────────────────────────
    //  kill_pids – unchanged edge-case matrix
    // ─────────────────────────────────────────────────────────────────────
    #[test]
    #[serial]
    fn kill_pids_scenarios() {
        use ProcessError::*;

        // 1 ▪ empty slice is always Ok
        assert!(kill_pids(&[], Duration::from_millis(10)).is_ok());

        // 2 ▪ “all-dead” slice: spawn a short-lived child, wait for it to exit,
        //     then point kill_pids at that *real* but now-dead PID.
        let dead_pid = {
            let mut child = short_cmd().spawn().unwrap();
            let pid = child.id();
            let _ = child.wait();
            pid
        };
        match kill_pids(&[dead_pid], Duration::from_millis(200)) {
            Ok(()) | Err(TerminationTimeout { .. }) => {} // both are fine
            Err(e) => panic!("unexpected error on all-dead slice: {e:?}"),
        }

        // 3 ▪ Live-process variants ------------------------------------------------
        fn spawn_and_kill(wait: Duration, duplicate: bool) {
            let mut child = long_cmd().spawn().unwrap();
            let pid = child.id();
            let pids = if duplicate { vec![pid, pid] } else { vec![pid] };

            // Accept success or the timeout error – either means *we tried*.
            match kill_pids(&pids, wait) {
                Ok(()) | Err(TerminationTimeout { .. }) => {}
                Err(e) => panic!("kill_pids failed unexpectedly: {e:?}"),
            }

            let _ = child.wait();
            assert!(
                !pid_alive(pid).unwrap_or(true),
                "child {pid} still alive after kill_pids(wait={wait:?}, dup={duplicate})"
            );
        }

        // duplicate-PID case
        spawn_and_kill(Duration::from_secs(2), true);

        // polite-wait vs. force-kill
        for &d in &[Duration::from_secs(2), Duration::from_secs(0)] {
            spawn_and_kill(d, false);
        }

        // 4 ▪ Mixed live / dead ----------------------------------------------------
        let mut child_live = long_cmd().spawn().unwrap();
        let pid_live = child_live.id();

        let mut child_dead = long_cmd().spawn().unwrap();
        let pid_dead = child_dead.id();
        kill_pid(pid_dead).unwrap(); // terminate it immediately
        let _ = child_dead.wait(); // reap

        match kill_pids(&[pid_dead, pid_live], Duration::from_millis(500)) {
            Ok(()) | Err(TerminationTimeout { .. }) => {}
            Err(e) => panic!("mixed kill failed unexpectedly: {e:?}"),
        }
        let _ = child_live.wait();
        assert!(
            !pid_alive(pid_live).unwrap_or(true),
            "live child {pid_live} survived mixed-status kill"
        );
    }

    // ─────────────────────────────────────────────────────────────────────
    //  kill_by_client – permutations covering pid-file & argv scan paths
    // ─────────────────────────────────────────────────────────────────────
    #[test]
    #[serial]
    fn kill_by_client_scenarios() {
        use sanitize_filename::sanitize; // kept local to honour “no extra imports” request

        struct Case<'a> {
            name: &'a str,
            pidfile_raw: Option<&'a str>, // None ⇒ no pid-file
            spawn_child: bool,            // spawn a long_cmd() child?
            expect_ok: bool,              // expect Ok(())
            pf_removed: bool,             // should pid-file be gone?
            argv_scan: bool,              // child must advertise --host
        }

        let cases = [
            Case {
                name: "no_match",
                pidfile_raw: None,
                spawn_child: false,
                expect_ok: false,
                pf_removed: false,
                argv_scan: false,
            },
            Case {
                name: "corrupt_pidfile",
                pidfile_raw: Some("not-a-number"),
                spawn_child: false,
                expect_ok: false,
                pf_removed: true,
                argv_scan: false,
            },
            Case {
                name: "stale_pidfile",
                pidfile_raw: Some("999999"), // dead PID
                spawn_child: false,
                expect_ok: false,
                pf_removed: true,
                argv_scan: false,
            },
            Case {
                name: "pidfile_happy",
                pidfile_raw: None, // will be filled with real PID below
                spawn_child: true,
                expect_ok: true,
                pf_removed: true,
                argv_scan: false,
            },
            Case {
                name: "argv_scan",
                pidfile_raw: None,
                spawn_child: true,
                expect_ok: true,
                pf_removed: false,
                argv_scan: true,
            },
        ];

        for Case {
            name,
            pidfile_raw,
            spawn_child,
            expect_ok,
            pf_removed,
            argv_scan,
        } in cases
        {
            let td = tempdir().unwrap();
            let host = name; // host string kept short to stay well below 240 chars

            // Construct the pid-file path *exactly* the way production code does.
            // We mimic the “*_unix_*” layout for simplicity; the exact flavour
            // (unix / http / tcp) is irrelevant to the termination logic.
            let pid_id = sanitize(format!("{TEST_EXE}_unix_{host}").to_ascii_lowercase());
            let pidfile_path = td.path().join(format!("{pid_id}.pid"));

            // maybe spawn a child
            let child = if spawn_child {
                #[cfg(unix)]
                {
                    let mut c = std::process::Command::new("sh");
                    c.args(["-c", "sleep 30"]);
                    if argv_scan {
                        c.arg("--host").arg(host);
                    }
                    Some(c.spawn().unwrap())
                }
                #[cfg(windows)]
                {
                    let mut c = std::process::Command::new("cmd");
                    c.args(["/C", "timeout", "/T", "30", "/NOBREAK"]);
                    if argv_scan {
                        c.arg("--host").arg(host);
                    }
                    Some(c.spawn().unwrap())
                }
            } else {
                None
            };

            // create / tweak pid-file if required
            if let Some(contents) = pidfile_raw {
                std::fs::write(&pidfile_path, contents.as_bytes()).unwrap();
            } else if spawn_child && !argv_scan {
                // happy-path pid-file with live PID
                let pid = child.as_ref().unwrap().id();
                std::fs::write(&pidfile_path, pid.to_string()).unwrap();
            }

            // -------- exercise ------------------------------------------------
            let result = kill_by_client(&pidfile_path, host);

            // -------- assertions ---------------------------------------------
            if expect_ok {
                result.unwrap();
            } else {
                matches!(
                    result.expect_err("should fail"),
                    ProcessError::NoSuchProcess { .. }
                );
            }

            // pid-file may have been removed by kill_by_client
            let expect_exists = pidfile_raw.is_some() && !pf_removed;
            assert_eq!(
                pidfile_path.exists(),
                expect_exists,
                "[{name}] pid-file existence mismatch (expected {expect_exists})"
            );

            if let Some(mut ch) = child {
                let _ = ch.wait();
                assert!(
                    !pid_alive(ch.id()).unwrap_or(true),
                    "[{name}] child process not killed"
                );
            }
        }
    }
}