otelite 0.1.97

Otelite: OTLP receiver, dashboard, and CLI for local OpenTelemetry observability
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
//! Service management commands for running otelite as a background daemon

use crate::error::{Error, Result};
use otelite_storage::StorageConfig;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use tracing::{info, warn};

#[cfg(target_os = "macos")]
const LAUNCHD_SERVICE_LABEL: &str = "dev.otelite.daemon";

#[cfg(target_os = "macos")]
#[derive(Debug, PartialEq, Eq)]
enum LaunchdServiceState {
    Loaded,
    Running(u32),
}

/// Get the directory for otelite runtime files (PID, logs, database).
///
/// `OTELITE_DATA_DIR` isolates the runtime files together with the
/// database — the same variable the storage layer honours — so a
/// second otelite instance (or a test) can run without touching the
/// default instance's PID file or log.
fn get_runtime_dir() -> Result<PathBuf> {
    let runtime_dir = match std::env::var("OTELITE_DATA_DIR") {
        Ok(dir) if !dir.trim().is_empty() => PathBuf::from(dir),
        _ => StorageConfig::default_data_dir(),
    };

    if !runtime_dir.exists() {
        fs::create_dir_all(&runtime_dir).map_err(|e| {
            Error::ConfigError(format!("Failed to create runtime directory: {}", e))
        })?;
    }

    Ok(runtime_dir)
}

/// The OTLP gRPC port the local daemon is expected to listen on.
/// Honours `OTELITE_OTLP_GRPC_PORT` (the same override `serve` uses to
/// bind) so discovery and stop target the right port for non-standard
/// setups; falls back to the standard 4317.
pub fn otlp_grpc_port() -> u16 {
    std::env::var("OTELITE_OTLP_GRPC_PORT")
        .ok()
        .and_then(|v| v.trim().parse().ok())
        .unwrap_or(4317)
}

/// Get the path to the PID file
fn get_pid_file() -> Result<PathBuf> {
    Ok(get_runtime_dir()?.join("otelite.pid"))
}

/// Get the path to the log file
fn get_log_file() -> Result<PathBuf> {
    Ok(get_runtime_dir()?.join("otelite.log"))
}

/// Read the PID from a PID file.
///
/// A missing file means "not started via `otelite start`". Corrupt
/// content (torn write, stale garbage) means the same, except the file
/// is removed so it cannot trip every subsequent command — port-based
/// discovery still finds a live daemon.
pub fn read_pid_file(pid_file: &Path) -> Result<Option<u32>> {
    if !pid_file.exists() {
        return Ok(None);
    }

    let content = fs::read_to_string(pid_file)
        .map_err(|e| Error::ConfigError(format!("Failed to read PID file: {}", e)))?;

    match content.trim().parse::<u32>() {
        Ok(pid) if pid != 0 => Ok(Some(pid)),
        _ => {
            warn!("Corrupt PID file at {}, removing it", pid_file.display());
            if let Err(e) = fs::remove_file(pid_file) {
                warn!(
                    "Could not remove corrupt PID file {}: {}",
                    pid_file.display(),
                    e
                );
            }
            Ok(None)
        },
    }
}

/// Read the PID from the PID file
fn read_pid() -> Result<Option<u32>> {
    read_pid_file(&get_pid_file()?)
}

/// Write the PID to a PID file atomically (temp file + fsync + rename).
///
/// A crash mid-write must not leave a torn PID file behind: the rename
/// makes the file appear either with the old or the new content, never
/// half-written.
pub fn write_pid_file(pid: u32, pid_file: &Path) -> Result<()> {
    let tmp_file = pid_file.with_extension("pid.tmp");

    {
        let mut file = fs::File::create(&tmp_file)
            .map_err(|e| Error::ConfigError(format!("Failed to create PID file: {}", e)))?;

        file.write_all(pid.to_string().as_bytes())
            .map_err(|e| Error::ConfigError(format!("Failed to write PID file: {}", e)))?;

        file.sync_all()
            .map_err(|e| Error::ConfigError(format!("Failed to sync PID file: {}", e)))?;
    }

    fs::rename(&tmp_file, pid_file).map_err(|e| {
        let _ = fs::remove_file(&tmp_file);
        Error::ConfigError(format!("Failed to move PID file into place: {}", e))
    })
}

/// Write the PID to the PID file
fn write_pid(pid: u32) -> Result<()> {
    write_pid_file(pid, &get_pid_file()?)
}

/// Remove the PID file
fn remove_pid_file() -> Result<()> {
    let pid_file = get_pid_file()?;

    if pid_file.exists() {
        fs::remove_file(&pid_file)
            .map_err(|e| Error::ConfigError(format!("Failed to remove PID file: {}", e)))?;
    }

    Ok(())
}

#[cfg(target_os = "macos")]
fn parse_launchd_service_state(output: &str) -> LaunchdServiceState {
    let is_running = output.lines().any(|line| line.trim() == "state = running");
    let pid = output.lines().find_map(|line| {
        line.trim()
            .strip_prefix("pid = ")
            .and_then(|value| value.parse::<u32>().ok())
    });

    match (is_running, pid) {
        (true, Some(pid)) => LaunchdServiceState::Running(pid),
        _ => LaunchdServiceState::Loaded,
    }
}

#[cfg(target_os = "macos")]
fn launchd_service_target() -> String {
    use nix::unistd::getuid;

    format!("gui/{}/{}", getuid().as_raw(), LAUNCHD_SERVICE_LABEL)
}

#[cfg(target_os = "macos")]
fn launchd_service_state() -> Result<Option<LaunchdServiceState>> {
    let service_target = launchd_service_target();
    let output = Command::new("launchctl")
        .args(["print", &service_target])
        .output()
        .map_err(|e| Error::ConfigError(format!("Failed to query launchd service: {}", e)))?;

    if !output.status.success() {
        return Ok(None);
    }

    Ok(Some(parse_launchd_service_state(&String::from_utf8_lossy(
        &output.stdout,
    ))))
}

#[cfg(target_os = "macos")]
fn stop_launchd_service() -> Result<()> {
    let service_target = launchd_service_target();
    let output = Command::new("launchctl")
        .args(["bootout", &service_target])
        .output()
        .map_err(|e| Error::ConfigError(format!("Failed to stop launchd service: {}", e)))?;

    if output.status.success() {
        return Ok(());
    }

    Err(Error::ConfigError(format!(
        "Failed to stop launchd service {}: {}",
        LAUNCHD_SERVICE_LABEL,
        String::from_utf8_lossy(&output.stderr).trim()
    )))
}

#[cfg(target_os = "macos")]
fn restart_launchd_service() -> Result<()> {
    let service_target = launchd_service_target();
    let output = Command::new("launchctl")
        .args(["kickstart", "-k", &service_target])
        .output()
        .map_err(|e| Error::ConfigError(format!("Failed to restart launchd service: {}", e)))?;

    if output.status.success() {
        return Ok(());
    }

    Err(Error::ConfigError(format!(
        "Failed to restart launchd service {}: {}",
        LAUNCHD_SERVICE_LABEL,
        String::from_utf8_lossy(&output.stderr).trim()
    )))
}

#[cfg(unix)]
fn is_otelite_command(command: &str) -> bool {
    Path::new(command.trim())
        .file_name()
        .is_some_and(|name| name == "otelite")
}

#[cfg(unix)]
fn is_otelite_process(pid: u32) -> Result<bool> {
    let output = Command::new("ps")
        .args(["-p", &pid.to_string(), "-o", "comm="])
        .output()
        .map_err(|e| Error::ConfigError(format!("Failed to inspect local process: {}", e)))?;

    Ok(output.status.success() && is_otelite_command(&String::from_utf8_lossy(&output.stdout)))
}

#[cfg(unix)]
fn ensure_otelite_process(pid: u32) -> Result<()> {
    if is_otelite_process(pid)? {
        return Ok(());
    }

    Err(Error::ConfigError(format!(
        "Otelite process {} exited or was replaced; refusing to signal it",
        pid
    )))
}

#[cfg(unix)]
fn pid_file_otelite_pid() -> Result<Option<u32>> {
    let Some(pid) = read_pid()? else {
        return Ok(None);
    };

    if is_process_running(pid) && is_otelite_process(pid)? {
        Ok(Some(pid))
    } else {
        Ok(None)
    }
}

/// Discover the otelite process listening on the given TCP port (default
/// OTLP gRPC: 4317). This is how `status`/`stop` find daemons started
/// without `otelite start` (no PID file): service-managed `serve` and
/// hand-run `serve` alike (issue #107).
#[cfg(unix)]
pub fn local_otelite_pid(port: u16) -> Result<Option<u32>> {
    let output = Command::new("lsof")
        .args(["-nP", "-t", &format!("-iTCP:{port}"), "-sTCP:LISTEN"])
        .output()
        .map_err(|e| {
            Error::ConfigError(format!("Failed to discover local otelite process: {}", e))
        })?;

    if !output.status.success() {
        return Ok(None);
    }

    for line in String::from_utf8_lossy(&output.stdout).lines() {
        let Ok(pid) = line.parse::<u32>() else {
            continue;
        };

        if is_otelite_process(pid)? {
            return Ok(Some(pid));
        }
    }

    Ok(None)
}

/// Check if a process with the given PID is running
fn is_process_running(pid: u32) -> bool {
    #[cfg(unix)]
    {
        use nix::sys::signal::kill;
        use nix::unistd::Pid;

        // Send signal 0 to check if process exists without delivering a signal
        match kill(Pid::from_raw(pid as i32), None) {
            Ok(_) => true,
            Err(nix::errno::Errno::ESRCH) => false, // No such process
            Err(_) => true, // Process exists but we can't signal it (permission issue)
        }
    }

    #[cfg(not(unix))]
    {
        // On non-Unix systems, just check if PID file exists
        // This is a fallback and not as reliable
        warn!("Process check not fully supported on this platform");
        true
    }
}

/// Start otelite as a background daemon
pub async fn handle_start(storage_path: Option<PathBuf>, addr: String) -> Result<()> {
    if let Some(pid) = read_pid()? {
        if is_process_running(pid) {
            return Err(Error::ConfigError(format!(
                "Otelite is already running with PID {}",
                pid
            )));
        } else {
            warn!("Stale PID file found, removing it");
            remove_pid_file()?;
        }
    }

    // The PID file says no (or said so and went stale), but a daemon
    // started by launchd, a hand-run `serve`, or `otelite start` from
    // another data dir leaves no PID file here. Discover it the same
    // way `status` does, instead of spawning a second daemon that can
    // only fail to bind the OTLP ports.
    #[cfg(unix)]
    let otlp_port = otlp_grpc_port();
    #[cfg(unix)]
    if let Some(pid) = local_otelite_pid(otlp_port)? {
        return Err(Error::ConfigError(format!(
            "Otelite is already running with PID {pid} (discovered via OTLP gRPC port {otlp_port})"
        )));
    }

    info!("Starting otelite daemon...");

    let exe_path = std::env::current_exe()
        .map_err(|e| Error::ConfigError(format!("Failed to get executable path: {}", e)))?;

    let log_file = get_log_file()?;

    let log_file_handle = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_file)
        .map_err(|e| Error::ConfigError(format!("Failed to open log file: {}", e)))?;

    let args = daemon_command_args(&addr, &log_file, &storage_path);
    let mut cmd = Command::new(&exe_path);
    for arg in &args {
        cmd.arg(arg);
    }
    let mut child =
        cmd.stdin(Stdio::null())
            .stdout(log_file_handle.try_clone().map_err(|e| {
                Error::ConfigError(format!("Failed to clone log file handle: {}", e))
            })?)
            .stderr(log_file_handle)
            .spawn()
            .map_err(|e| Error::ConfigError(format!("Failed to spawn daemon process: {}", e)))?;

    let pid = child.id();
    write_pid(pid)?;

    // A port collision makes `serve` exit within milliseconds. Confirm
    // the child is still alive before reporting success, and roll back
    // the PID file if it died — "started with PID X" must never be
    // printed for a process that is already gone.
    // A port collision makes `serve` exit within milliseconds. Confirm
    // the child is still alive before reporting success, and roll back
    // the PID file if it died — "started with PID X" must never be
    // printed for a process that is already gone.
    let deadline = std::time::Instant::now() + std::time::Duration::from_millis(750);
    let mut exited = None;
    while exited.is_none() {
        exited = child
            .try_wait()
            .map_err(|e| Error::ConfigError(format!("Failed to check daemon process: {}", e)))?;
        if exited.is_none() && std::time::Instant::now() >= deadline {
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
    }
    if let Some(status) = exited {
        remove_pid_file()?;
        return Err(Error::ConfigError(format!(
            "Daemon exited immediately after start ({status}); see the log at {}",
            log_file.display()
        )));
    }

    let storage_display = storage_path
        .as_deref()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| StorageConfig::default_data_dir().display().to_string());

    println!("✓ Otelite daemon started with PID {}", pid);
    println!("  Logs: {}.* (rotates daily)", log_file.display());
    println!("  Storage: {}", storage_display);
    println!("  Dashboard: http://{}", addr);
    println!("\nUse 'otelite stop' to stop the daemon");
    println!("Use 'otelite status' to check daemon status");

    Ok(())
}

/// Build the argument list for a spawned daemon. Factored out so the
/// wiring (in particular the daily-rotating `--log-file`) is testable
/// without spawning anything.
fn daemon_command_args(
    addr: &str,
    log_file: &Path,
    storage_path: &Option<PathBuf>,
) -> Vec<std::ffi::OsString> {
    let mut args: Vec<std::ffi::OsString> = vec![
        "serve".into(),
        "--addr".into(),
        addr.into(),
        // Route the child's tracing through the daily-rotating appender
        // instead of an ever-growing appended file. The stderr
        // redirection in `handle_start` is kept for output that happens
        // before/around tracing (panics, startup errors); it is
        // small-volume.
        "--log-file".into(),
        log_file.as_os_str().to_os_string(),
    ];
    if let Some(path) = storage_path {
        args.push("--storage-path".into());
        args.push(path.as_os_str().to_os_string());
    }
    args
}

/// Stop the otelite daemon
pub async fn handle_stop() -> Result<()> {
    #[cfg(target_os = "macos")]
    if launchd_service_state()?.is_some() {
        stop_launchd_service()?;
        println!("✓ Otelite launchd service stopped");
        return Ok(());
    }

    #[cfg(target_os = "macos")]
    let pid = pid_file_otelite_pid()?
        .or(local_otelite_pid(otlp_grpc_port())?)
        .ok_or_else(|| Error::ConfigError("Otelite daemon is not running".to_string()))?;

    #[cfg(not(target_os = "macos"))]
    let pid = {
        let mut pid = pid_file_otelite_pid()?;
        if pid.is_none() {
            pid = local_otelite_pid(otlp_grpc_port())?;
        }
        match pid {
            Some(pid) => {
                if let Some(file_pid) = read_pid()? {
                    if file_pid != pid {
                        warn!(
                            "PID file names {} but the listening daemon is {}; stopping the daemon",
                            file_pid, pid
                        );
                    }
                }
                pid
            },
            None => {
                if read_pid()?.is_some() {
                    warn!("Stale PID file found, removing it");
                    remove_pid_file()?;
                }
                return Err(Error::ConfigError(
                    "Otelite daemon is not running (no PID file and nothing listening on 4317)"
                        .to_string(),
                ));
            },
        }
    };

    info!("Stopping otelite daemon (PID {})...", pid);

    #[cfg(unix)]
    {
        use nix::sys::signal::{kill, Signal};
        use nix::unistd::Pid;

        // Send SIGTERM for graceful shutdown
        #[cfg(unix)]
        ensure_otelite_process(pid)?;
        kill(Pid::from_raw(pid as i32), Signal::SIGTERM)
            .map_err(|e| Error::ConfigError(format!("Failed to send SIGTERM to process: {}", e)))?;

        // Wait for process to exit (with timeout).
        //
        // Every iteration re-checks the process *identity*, not just its
        // existence: once otelite exits, its PID can be recycled by an
        // unrelated process. A plain `kill(pid, 0)` liveness check would
        // report that stranger as "still running", stall the loop until
        // the timeout, and then report a failed stop for a stop that had
        // already succeeded. `is_otelite_process` returns false for a
        // dead PID and for a live non-otelite process alike — both mean
        // the original daemon is gone.
        let start = std::time::Instant::now();
        let timeout = std::time::Duration::from_secs(10);

        loop {
            if !is_otelite_process(pid)? {
                break;
            }
            if start.elapsed() > timeout {
                warn!("Process did not exit gracefully, sending SIGKILL");
                // Re-verify identity immediately before the forceful
                // kill — never signal a recycled PID.
                if is_otelite_process(pid)? {
                    kill(Pid::from_raw(pid as i32), Signal::SIGKILL).map_err(|e| {
                        Error::ConfigError(format!("Failed to send SIGKILL to process: {}", e))
                    })?;
                }
                break;
            }
            tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        }
    }

    #[cfg(not(unix))]
    {
        return Err(Error::ConfigError(
            "Stop command not supported on this platform".to_string(),
        ));
    }

    if read_pid()? == Some(pid) {
        remove_pid_file()?;
    }
    println!("✓ Otelite daemon stopped");

    Ok(())
}

/// Stop the running daemon and start a fresh one
pub async fn handle_restart(storage_path: Option<PathBuf>, addr: String) -> Result<()> {
    #[cfg(target_os = "macos")]
    if launchd_service_state()?.is_some() {
        restart_launchd_service()?;
        println!("✓ Otelite launchd service restarted");
        return Ok(());
    }

    // Verify a daemon is actually running before attempting restart
    #[cfg(unix)]
    {
        let running = read_pid().ok().flatten().is_some_and(is_process_running)
            || local_otelite_pid(otlp_grpc_port()).ok().flatten().is_some();
        if !running {
            return Err(Error::ConfigError(
                "No otelite daemon is running. Use 'otelite start' to start one.".to_string(),
            ));
        }
    }
    #[cfg(not(unix))]
    match read_pid()? {
        Some(pid) if is_process_running(pid) => {},
        _ => {
            return Err(Error::ConfigError(
                "No otelite daemon is running. Use 'otelite start' to start one.".to_string(),
            ));
        },
    }

    println!("Stopping daemon...");
    handle_stop().await?;

    println!("Daemon stopped. Starting fresh...");
    handle_start(storage_path, addr).await
}

fn display_running_status(pid: u32, supervisor: Option<&str>) -> Result<()> {
    match supervisor {
        Some(supervisor) => println!("Status: Running ({})", supervisor),
        None => println!("Status: Running"),
    }
    println!("PID: {}", pid);

    // Try to get process uptime on Unix systems
    #[cfg(unix)]
    {
        if let Ok(output) = Command::new("ps")
            .args(["-p", &pid.to_string(), "-o", "etime="])
            .output()
        {
            if output.status.success() {
                if let Ok(uptime) = String::from_utf8(output.stdout) {
                    println!("Uptime: {}", uptime.trim());
                }
            }
        }
    }

    let log_file = get_log_file()?;
    println!("Logs: {}.* (rotates daily)", log_file.display());

    let runtime_dir = get_runtime_dir()?;
    println!("Runtime directory: {}", runtime_dir.display());

    Ok(())
}

/// Show the status of the otelite daemon
pub async fn handle_status() -> Result<()> {
    #[cfg(target_os = "macos")]
    if let Some(LaunchdServiceState::Running(pid)) = launchd_service_state()? {
        return display_running_status(pid, Some("launchd: dev.otelite.daemon"));
    }

    #[cfg(target_os = "macos")]
    if let Some(pid) = pid_file_otelite_pid()? {
        return display_running_status(pid, Some("local process"));
    }

    #[cfg(target_os = "macos")]
    if let Some(pid) = local_otelite_pid(otlp_grpc_port())? {
        return display_running_status(pid, Some("local process"));
    }

    #[cfg(not(target_os = "macos"))]
    {
        let mut pid = pid_file_otelite_pid()?;
        if pid.is_none() {
            pid = local_otelite_pid(otlp_grpc_port())?;
        }
        if let Some(pid) = pid {
            return display_running_status(pid, None);
        }
        if read_pid()?.is_some() {
            println!("Status: Not running (stale PID file)");
            warn!("Cleaning up stale PID file");
            remove_pid_file()?;
        } else {
            println!("Status: Not running");
        }
    }

    #[cfg(target_os = "macos")]
    if read_pid()?.is_some() {
        println!("Status: Not running (stale PID file)");
        warn!("Cleaning up stale PID file");
        remove_pid_file()?;
    } else {
        println!("Status: Not running");
    }

    Ok(())
}

/// Install otelite as a system service
pub async fn handle_service_install() -> Result<()> {
    #[cfg(target_os = "macos")]
    {
        install_launchd_service().await
    }

    #[cfg(target_os = "linux")]
    {
        install_systemd_service().await
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        Err(Error::ConfigError(
            "Service installation not supported on this platform".to_string(),
        ))
    }
}

/// Uninstall otelite from the system service manager
pub async fn handle_service_uninstall() -> Result<()> {
    #[cfg(target_os = "macos")]
    {
        uninstall_launchd_service().await
    }

    #[cfg(target_os = "linux")]
    {
        uninstall_systemd_service().await
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        Err(Error::ConfigError(
            "Service installation not supported on this platform".to_string(),
        ))
    }
}

/// Environment variables that change daemon behaviour but are not
/// inherited by launchd/systemd from the user's shell. When set at
/// install time they are baked into the unit file so the service
/// reproduces what the user was already running.
const SERVICE_ENV_VARS: &[&str] = &[
    "OTELITE_DATA_DIR",
    "OTELITE_RETENTION_DAYS",
    "OTELITE_AUTO_PURGE_ENABLED",
    "OTELITE_PURGE_SCHEDULE",
    "OTELITE_OTLP_GRPC_PORT",
    "OTELITE_OTLP_HTTP_PORT",
];

/// Collect the service-affecting environment variables that are set in
/// the current environment, in the order listed in SERVICE_ENV_VARS.
fn collect_service_env() -> Vec<(String, String)> {
    SERVICE_ENV_VARS
        .iter()
        .filter_map(|key| {
            std::env::var(key)
                .ok()
                .map(|value| (key.to_string(), value))
        })
        .collect()
}

/// Minimal XML escaping for plist string content.
#[cfg(any(target_os = "macos", test))]
fn xml_escape(value: &str) -> String {
    value
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Build the launchd plist content. Pure so it is testable on any OS
/// without touching launchctl or the filesystem.
#[cfg(any(target_os = "macos", test))]
fn build_plist(
    exe: &std::path::Path,
    log_file: &std::path::Path,
    env: &[(String, String)],
) -> String {
    let env_block = if env.is_empty() {
        String::new()
    } else {
        let entries: String = env
            .iter()
            .map(|(k, v)| {
                format!(
                    "    <key>{}</key>\n    <string>{}</string>\n",
                    xml_escape(k),
                    xml_escape(v)
                )
            })
            .collect();
        format!(
            "    <key>EnvironmentVariables</key>\n    <dict>\n{}    </dict>\n",
            entries
        )
    };

    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>dev.otelite.daemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>{exe}</string>
        <string>serve</string>
        <string>--log-file</string>
        <string>{log}</string>
    </array>
{env_block}    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>{log}</string>
    <key>StandardErrorPath</key>
    <string>{log}</string>
</dict>
</plist>
"#,
        exe = xml_escape(&exe.display().to_string()),
        log = xml_escape(&log_file.display().to_string()),
        env_block = env_block
    )
}

/// Build the systemd user unit content. Pure so it is testable on any
/// OS without touching systemctl or the filesystem.
#[cfg(any(target_os = "linux", test))]
fn build_systemd_unit(
    exe: &std::path::Path,
    log_file: &std::path::Path,
    env: &[(String, String)],
) -> String {
    let env_lines: String = env
        .iter()
        .map(|(k, v)| {
            let escaped = v.replace('\\', "\\\\").replace('"', "\\\"");
            format!(
                r#"Environment="{}={}"
"#,
                k, escaped,
            )
        })
        .collect();

    format!(
        r#"[Unit]
Description=Otelite OpenTelemetry Collector
After=network.target

[Service]
Type=simple
ExecStart={exe} serve --log-file {log}
{env}Restart=on-failure
RestartSec=5

[Install]
WantedBy=default.target
"#,
        exe = exe.display(),
        log = log_file.display(),
        env = env_lines
    )
}

/// Print the environment that was baked into the unit file (or a note
/// that the service will run with built-in defaults), so the user can
/// see what the service will and will not inherit.
fn print_env_summary(env: &[(String, String)]) {
    if env.is_empty() {
        println!(
            "
Note: no OTELITE_* environment variables are set in this"
        );
        println!("shell, so the service will run with built-in defaults and will");
        println!("NOT inherit variables you export later (launchd/systemd do");
        println!("not read your shell profile). Set them, then re-run");
        println!("`otelite service install` to carry them over.");
    } else {
        println!(
            "
Environment carried over from your shell:"
        );
        for (k, v) in env {
            println!("  {}={}", k, v);
        }
        println!("(change these variables later, then re-run `otelite service install`");
        println!(" to carry the new values over)");
    }
}

/// Install otelite as a launchd service on macOS
#[cfg(target_os = "macos")]
async fn install_launchd_service() -> Result<()> {
    let home = std::env::var("HOME")
        .map_err(|_| Error::ConfigError("HOME environment variable not set".to_string()))?;

    let launch_agents_dir = PathBuf::from(&home).join("Library/LaunchAgents");

    if !launch_agents_dir.exists() {
        fs::create_dir_all(&launch_agents_dir).map_err(|e| {
            Error::ConfigError(format!("Failed to create LaunchAgents directory: {}", e))
        })?;
    }

    let plist_path = launch_agents_dir.join("dev.otelite.daemon.plist");
    let exe_path = std::env::current_exe()
        .map_err(|e| Error::ConfigError(format!("Failed to get executable path: {}", e)))?;

    let log_file = get_log_file()?;
    let env = collect_service_env();

    let plist_content = build_plist(&exe_path, &log_file, &env);

    fs::write(&plist_path, &plist_content)
        .map_err(|e| Error::ConfigError(format!("Failed to write plist file: {}", e)))?;

    println!(
        "✓ Service configuration created at {}",
        plist_path.display()
    );
    print_env_summary(&env);

    load_launchd_service(&plist_path).await?;
    println!("✓ Service loaded — otelite is running under launchd");

    Ok(())
}

/// Load (or reload) the launchd agent from the given plist.
///
/// `launchctl bootstrap` refuses to replace a service that is already
/// loaded, so when that happens we boot the old instance out first and
/// load the new definition — reinstalling is how a user applies
/// changed settings to a running service.
#[cfg(target_os = "macos")]
async fn load_launchd_service(plist_path: &Path) -> Result<()> {
    let target = launchd_service_target();

    let output = Command::new("launchctl")
        .args(["bootstrap", &target, &plist_path.display().to_string()])
        .output()
        .map_err(|e| {
            Error::ConfigError(format!(
                "Failed to load launchd service {}: {}",
                plist_path.display(),
                e
            ))
        })?;

    if output.status.success() {
        return Ok(());
    }

    // Already loaded: reload with the fresh definition.
    if launchd_service_state()?.is_some() {
        warn!("Service was already loaded — reloading with the new configuration");
        stop_launchd_service()?;
        let retry = Command::new("launchctl")
            .args(["bootstrap", &target, &plist_path.display().to_string()])
            .output()
            .map_err(|e| Error::ConfigError(format!("Failed to reload launchd service: {}", e)))?;
        if retry.status.success() {
            return Ok(());
        }
        return Err(Error::ConfigError(format!(
            "Failed to reload launchd service {} after booting the old instance out: {}",
            target,
            String::from_utf8_lossy(&retry.stderr).trim()
        )));
    }

    Err(Error::ConfigError(format!(
        "Failed to load launchd service {} ({}): {} — check the plist at {} and          `launchctl print {}` for details",
        target,
        output.status.code().unwrap_or(-1),
        String::from_utf8_lossy(&output.stderr).trim(),
        plist_path.display(),
        target
    )))
}

/// Uninstall otelite from launchd (macOS)
#[cfg(target_os = "macos")]
async fn uninstall_launchd_service() -> Result<()> {
    let home = std::env::var("HOME")
        .map_err(|_| Error::ConfigError("HOME environment variable not set".to_string()))?;

    let plist_path = PathBuf::from(&home)
        .join("Library/LaunchAgents")
        .join(format!("{}.plist", LAUNCHD_SERVICE_LABEL));

    if !plist_path.exists() {
        return Err(Error::ConfigError(format!(
            "No service installed (no {} found). Run `otelite service install` first.",
            plist_path.display()
        )));
    }

    let target = launchd_service_target();
    let output = Command::new("launchctl")
        .args(["bootout", &target])
        .output()
        .map_err(|e| Error::ConfigError(format!("Failed to stop launchd service: {}", e)))?;

    if !output.status.success() {
        warn!(
            "Service was not loaded (bootout: {}); removing the configuration file",
            String::from_utf8_lossy(&output.stderr).trim()
        );
    }

    fs::remove_file(&plist_path).map_err(|e| {
        Error::ConfigError(format!(
            "Failed to remove plist file {}: {}",
            plist_path.display(),
            e
        ))
    })?;

    println!("✓ Service uninstalled from launchd ({})", target);
    Ok(())
}

/// Install otelite as a systemd service on Linux
#[cfg(target_os = "linux")]
async fn install_systemd_service() -> Result<()> {
    let home = std::env::var("HOME")
        .map_err(|_| Error::ConfigError("HOME environment variable not set".to_string()))?;

    let systemd_user_dir = PathBuf::from(&home).join(".config/systemd/user");

    // Create directory if it doesn't exist
    if !systemd_user_dir.exists() {
        fs::create_dir_all(&systemd_user_dir).map_err(|e| {
            Error::ConfigError(format!("Failed to create systemd user directory: {}", e))
        })?;
    }

    let unit_path = systemd_user_dir.join("otelite.service");
    let exe_path = std::env::current_exe()
        .map_err(|e| Error::ConfigError(format!("Failed to get executable path: {}", e)))?;

    let log_file = get_log_file()?;
    let env = collect_service_env();

    let unit_content = build_systemd_unit(&exe_path, &log_file, &env);

    fs::write(&unit_path, &unit_content)
        .map_err(|e| Error::ConfigError(format!("Failed to write systemd unit file: {}", e)))?;

    println!("✓ Service configuration created at {}", unit_path.display());
    print_env_summary(&env);

    for (args, what) in [
        (
            ["--user", "daemon-reload"].as_slice(),
            "reloading systemd user units",
        ),
        (
            ["--user", "enable", "--now", "otelite.service"].as_slice(),
            "enabling and starting the service",
        ),
    ] {
        let output = Command::new("systemctl")
            .args(args)
            .output()
            .map_err(|e| Error::ConfigError(format!("Failed to run systemctl: {}", e)))?;
        if !output.status.success() {
            return Err(Error::ConfigError(format!(
                "systemctl {} failed ({}): {} — the unit file was written at {}",
                what,
                output.status.code().unwrap_or(-1),
                String::from_utf8_lossy(&output.stderr).trim(),
                unit_path.display()
            )));
        }
    }
    println!("✓ Service enabled and running under systemd user units");

    Ok(())
}

/// Uninstall otelite from systemd user units (Linux)
#[cfg(target_os = "linux")]
async fn uninstall_systemd_service() -> Result<()> {
    let home = std::env::var("HOME")
        .map_err(|_| Error::ConfigError("HOME environment variable not set".to_string()))?;

    let unit_path = PathBuf::from(&home)
        .join(".config/systemd/user")
        .join("otelite.service");

    if !unit_path.exists() {
        return Err(Error::ConfigError(format!(
            "No service installed (no {} found). Run `otelite service install` first.",
            unit_path.display()
        )));
    }

    // Stop and disable; a unit that was stopped manually is not an error.
    let output = Command::new("systemctl")
        .args(["--user", "disable", "--now", "otelite.service"])
        .output()
        .map_err(|e| Error::ConfigError(format!("Failed to run systemctl: {}", e)))?;
    if !output.status.success() {
        warn!(
            "systemctl disable reported: {}",
            String::from_utf8_lossy(&output.stderr).trim()
        );
    }
    let _ = Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .output();

    fs::remove_file(&unit_path).map_err(|e| {
        Error::ConfigError(format!(
            "Failed to remove unit file {}: {}",
            unit_path.display(),
            e
        ))
    })?;

    println!("✓ Service uninstalled from systemd user units");
    Ok(())
}

/// Environment-mutating tests share the process environment across all
/// test modules in this file, so they must not run concurrently with
/// each other.
#[cfg(test)]
static ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());

#[cfg(test)]
#[cfg(unix)]
mod tests {
    use super::ENV_MUTEX;

    #[cfg(target_os = "macos")]
    use super::parse_launchd_service_state;
    use super::{is_process_running, local_otelite_pid, otlp_grpc_port};

    /// #107 regression helper: discovery must report `None` (not an error)
    /// when nothing otelite-shaped listens, and report a live PID when the
    /// real daemon owns the port. Both outcomes are asserted consistently
    /// with process liveness so the test holds on dev machines (daemon
    /// running) and CI (no daemon) alike.
    #[test]
    fn test_local_otelite_pid_consistent_with_liveness() {
        if let Some(pid) = local_otelite_pid(otlp_grpc_port()).unwrap() {
            assert!(is_process_running(pid));
        }
    }

    #[cfg(target_os = "macos")]
    use super::LaunchdServiceState;

    #[cfg(target_os = "macos")]
    #[test]
    fn test_parse_launchd_service_state_detects_running_service() {
        let output = r#"
gui/501/dev.otelite.daemon = {
    state = running
    pid = 7351
}
"#;

        assert_eq!(
            parse_launchd_service_state(output),
            LaunchdServiceState::Running(7351)
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_parse_launchd_service_state_detects_loaded_non_running_service() {
        let output = r#"
gui/501/dev.otelite.daemon = {
    state = spawn scheduled
    pid = 7351
}
"#;

        assert_eq!(
            parse_launchd_service_state(output),
            LaunchdServiceState::Loaded
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_is_otelite_command_accepts_only_otelite_executable() {
        assert!(super::is_otelite_command(
            "/Users/jonesn/.local/bin/otelite"
        ));
        assert!(!super::is_otelite_command("/usr/bin/python3"));
    }

    #[test]
    fn test_is_otelite_command_trims_and_rejects_similar_names() {
        // `ps` output can carry trailing whitespace; the name match must
        // be exact (no `otelite-old`, `otelite-cli`, ...).
        assert!(super::is_otelite_command("otelite"));
        assert!(super::is_otelite_command("otelite "));
        assert!(!super::is_otelite_command("otelite-old"));
        assert!(!super::is_otelite_command("/opt/homebrew/bin/otelite-cli"));
    }

    #[test]
    fn test_is_process_running_distinguishes_live_and_dead_pids() {
        // PID 1 (launchd on macOS, init/systemd on Linux) always exists.
        assert!(is_process_running(1));
        // Above both Linux's pid_max (4194304) and macOS's kern.pids_max
        // (99999), and positive as i32 — `u32::MAX` would cast to -1 and
        // `kill(-1, 0)` would report the whole process group as alive.
        assert!(!is_process_running(2_000_000_000));
    }

    #[test]
    fn test_is_otelite_process_is_false_for_dead_and_foreign_pids() {
        // The stop wait loop relies on this returning `Ok(false)` — not
        // an error — both for a dead PID and for a live process that is
        // not otelite (i.e. a recycled PID).
        assert!(
            !super::is_otelite_process(2_000_000_000).unwrap(),
            "a dead PID must read as 'not otelite', not as a failure"
        );

        let mut sleep = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .unwrap();
        assert!(
            !super::is_otelite_process(sleep.id()).unwrap(),
            "a live non-otelite process (e.g. a recycled PID) must read as 'not otelite'"
        );
        let _ = sleep.kill();
        let _ = sleep.wait();
    }

    #[test]
    fn test_otlp_grpc_port_env_override() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        let key = "OTELITE_OTLP_GRPC_PORT";
        let old = std::env::var(key).ok();

        std::env::remove_var(key);
        assert_eq!(otlp_grpc_port(), 4317);

        std::env::set_var(key, "14317");
        assert_eq!(otlp_grpc_port(), 14317);

        // Discovery must stay usable on a misconfigured machine: an
        // invalid value falls back to the standard port rather than
        // failing.
        std::env::set_var(key, "not-a-port");
        assert_eq!(otlp_grpc_port(), 4317);

        match old {
            Some(value) => std::env::set_var(key, value),
            None => std::env::remove_var(key),
        }
    }

    #[test]
    fn test_runtime_dir_and_pid_file_wrappers_follow_env() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        let key = "OTELITE_DATA_DIR";
        let old = std::env::var(key).ok();

        let temp = tempfile::TempDir::new().unwrap();
        std::env::set_var(key, temp.path());

        // PID file and log file live in the (env-overridden) data dir.
        assert_eq!(super::get_runtime_dir().unwrap(), temp.path());
        assert_eq!(
            super::get_pid_file().unwrap(),
            temp.path().join("otelite.pid")
        );

        // Wrapper round-trip, including removal (which is a no-op when
        // the file is missing).
        assert_eq!(super::read_pid().unwrap(), None);
        assert!(super::remove_pid_file().is_ok());
        super::write_pid(4321).unwrap();
        assert_eq!(super::read_pid().unwrap(), Some(4321));
        super::remove_pid_file().unwrap();
        assert_eq!(super::read_pid().unwrap(), None);

        match old {
            Some(value) => std::env::set_var(key, value),
            None => std::env::remove_var(key),
        }
    }

    #[test]
    fn test_get_runtime_dir_defaults_without_env() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        let key = "OTELITE_DATA_DIR";
        let old = std::env::var(key).ok();
        std::env::remove_var(key);

        let dir = super::get_runtime_dir().unwrap();
        assert!(
            dir.ends_with(".otelite/data"),
            "default runtime dir must be under ~/.otelite/data, got {dir:?}"
        );

        match old {
            Some(value) => std::env::set_var(key, value),
            None => std::env::remove_var(key),
        }
    }
}

#[test]
fn test_collect_service_env_only_returns_set_vars() {
    let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
    for key in SERVICE_ENV_VARS {
        std::env::remove_var(key);
    }
    std::env::set_var("OTELITE_DATA_DIR", "/custom/data");
    std::env::set_var("OTELITE_RETENTION_DAYS", "60");

    let env = collect_service_env();
    assert_eq!(
        env,
        vec![
            ("OTELITE_DATA_DIR".to_string(), "/custom/data".to_string()),
            ("OTELITE_RETENTION_DAYS".to_string(), "60".to_string()),
        ]
    );

    for key in SERVICE_ENV_VARS {
        std::env::remove_var(key);
    }
}

#[test]
fn test_collect_service_env_empty_when_unset() {
    let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
    for key in SERVICE_ENV_VARS {
        std::env::remove_var(key);
    }
    assert!(collect_service_env().is_empty());
}

#[cfg(test)]
mod pid_file_tests {
    use super::*;

    #[test]
    fn test_read_pid_file_corrupt_is_removed_and_treated_as_missing() {
        let temp = tempfile::TempDir::new().unwrap();
        let pid_file = temp.path().join("otelite.pid");
        std::fs::write(&pid_file, "not-a-pid\n").unwrap();

        assert_eq!(read_pid_file(&pid_file).unwrap(), None);
        assert!(
            !pid_file.exists(),
            "the corrupt file must be removed so it cannot clog later commands"
        );
    }

    #[test]
    fn test_read_pid_file_zero_pid_treated_as_missing() {
        let temp = tempfile::TempDir::new().unwrap();
        let pid_file = temp.path().join("otelite.pid");
        std::fs::write(&pid_file, "0").unwrap();

        assert_eq!(read_pid_file(&pid_file).unwrap(), None);
        assert!(!pid_file.exists());
    }

    #[test]
    fn test_read_pid_file_missing_is_none() {
        let temp = tempfile::TempDir::new().unwrap();
        assert_eq!(
            read_pid_file(&temp.path().join("otelite.pid")).unwrap(),
            None
        );
    }
}

#[cfg(test)]
mod daemon_args_tests {
    use super::*;

    #[test]
    fn test_daemon_command_args_route_logs_through_rotating_appender() {
        let args = daemon_command_args("127.0.0.1:3000", Path::new("/tmp/data/otelite.log"), &None);
        let flat: Vec<String> = args
            .iter()
            .map(|a| a.to_string_lossy().into_owned())
            .collect();
        assert_eq!(
            flat,
            vec![
                "serve",
                "--addr",
                "127.0.0.1:3000",
                "--log-file",
                "/tmp/data/otelite.log"
            ]
        );

        let storage = PathBuf::from("/tmp/data/otelite.db");
        let args = daemon_command_args(
            "127.0.0.1:3000",
            Path::new("/tmp/data/otelite.log"),
            &Some(storage),
        );
        let flat: Vec<String> = args
            .iter()
            .map(|a| a.to_string_lossy().into_owned())
            .collect();
        assert_eq!(
            flat,
            vec![
                "serve",
                "--addr",
                "127.0.0.1:3000",
                "--log-file",
                "/tmp/data/otelite.log",
                "--storage-path",
                "/tmp/data/otelite.db"
            ]
        );
    }

    #[test]
    fn test_build_plist_bakes_in_environment_variables() {
        let env = vec![
            ("OTELITE_DATA_DIR".to_string(), "/custom/data".to_string()),
            ("OTELITE_OTLP_GRPC_PORT".to_string(), "4317".to_string()),
        ];
        let plist = build_plist(
            std::path::Path::new("/usr/local/bin/otelite"),
            std::path::Path::new("/custom/data/otelite.log"),
            &env,
        );

        assert!(plist.contains("<key>EnvironmentVariables</key>"));
        assert!(plist.contains("<key>OTELITE_DATA_DIR</key>"));
        assert!(plist.contains("<string>/custom/data</string>"));
        assert!(plist.contains("<key>OTELITE_OTLP_GRPC_PORT</key>"));
        assert!(plist.contains("<string>4317</string>"));
        // well-formed-ish: dict open/close balanced
        assert_eq!(plist.matches("<dict>").count(), 2);
        assert_eq!(plist.matches("</dict>").count(), 2);
    }

    #[test]
    fn test_build_plist_omits_environment_block_when_empty() {
        let plist = build_plist(
            std::path::Path::new("/usr/local/bin/otelite"),
            std::path::Path::new("/tmp/otelite.log"),
            &[],
        );
        assert!(!plist.contains("EnvironmentVariables"));
        assert_eq!(plist.matches("<dict>").count(), 1);
    }

    #[test]
    fn test_build_plist_escapes_xml_special_characters() {
        let env = vec![("OTELITE_DATA_DIR".to_string(), "/data/a&b<c>".to_string())];
        let plist = build_plist(
            std::path::Path::new("/bin/otelite"),
            std::path::Path::new("/tmp/otelite.log"),
            &env,
        );
        assert!(plist.contains("/data/a&amp;b&lt;c&gt;"));
        assert!(!plist.contains("/data/a&b<c>"));
    }

    #[test]
    fn test_build_systemd_unit_bakes_in_environment_lines() {
        let env = vec![
            ("OTELITE_DATA_DIR".to_string(), "/custom/data".to_string()),
            ("OTELITE_PURGE_SCHEDULE".to_string(), "03:30".to_string()),
        ];
        let unit = build_systemd_unit(
            std::path::Path::new("/usr/local/bin/otelite"),
            std::path::Path::new("/custom/data/otelite.log"),
            &env,
        );

        assert!(unit.contains(r#"Environment="OTELITE_DATA_DIR=/custom/data""#));
        assert!(unit.contains(r#"Environment="OTELITE_PURGE_SCHEDULE=03:30""#));
        assert!(unit.contains(
            "ExecStart=/usr/local/bin/otelite serve --log-file /custom/data/otelite.log"
        ));
    }

    #[test]
    fn test_build_systemd_unit_escapes_quotes_in_values() {
        let env = vec![("OTELITE_DATA_DIR".to_string(), "/data/we\"ird".to_string())];
        let unit = build_systemd_unit(
            std::path::Path::new("/bin/otelite"),
            std::path::Path::new("/tmp/otelite.log"),
            &env,
        );
        assert!(unit.contains(r#"Environment="OTELITE_DATA_DIR=/data/we\"ird""#));
    }

    #[test]
    fn test_build_systemd_unit_no_environment_when_empty() {
        let unit = build_systemd_unit(
            std::path::Path::new("/bin/otelite"),
            std::path::Path::new("/tmp/otelite.log"),
            &[],
        );
        assert!(!unit.contains("Environment="));
    }

    #[test]
    fn test_uninstall_without_installed_service_is_clear_error() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        let tmp = tempfile::TempDir::new().expect("temp dir");
        let old_home = std::env::var("HOME").ok();
        std::env::set_var("HOME", tmp.path());

        // No unit file in the isolated HOME: the handler must fail with a
        // clear error BEFORE any service-manager invocation, so this test
        // is safe on machines with a live service. The handler runs on a
        // scoped runtime (not #[tokio::test]) so the env mutex guard is
        // never held across an await.
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("test runtime");
        let err = rt
            .block_on(super::handle_service_uninstall())
            .expect_err("missing unit file must be an error");
        let msg = format!("{err}");
        assert!(
            msg.contains("No service installed"),
            "error says nothing is installed: {msg}"
        );
        assert!(
            msg.contains("otelite service install"),
            "error points at the remedy: {msg}"
        );

        match old_home {
            Some(value) => std::env::set_var("HOME", value),
            None => std::env::remove_var("HOME"),
        }
    }
}