greggd 1.0.14

Lightweight Linux, macOS, and Windows metrics daemon that exposes a read-only JSON API for the gregg client.
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
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
//! Install errors, atomic writes, privilege guidance, install dispatch, instruction rendering, and restart coordination.

use super::cron::ShellQuoteError;
use super::cron::{cron_block, install_cron, CRON_MANAGED_MARKER};
use super::launchd::{install_launchd, restart_launchd};
use super::method::{
    launchd_label, resolve_startup_method, standard_launchd_binary, standard_launchd_config,
    standard_launchd_plist_path, standard_systemd_binary, standard_systemd_config,
    standard_systemd_unit_path, StartupMethod, StartupMethodArg,
};
#[cfg(unix)]
use super::process::DIRECT_RESTART_TIMEOUT;
#[cfg(test)]
use super::state::systemd_state_with;
use super::systemd::{install_systemd, restart_systemd};
use crate::config::Config;
use std::fmt;
use std::fs;
use std::io::{self};
use std::path::{Path, PathBuf};
#[cfg(unix)]
use std::thread;
#[cfg(unix)]
use std::time::{Duration, Instant};

/// Process-wide counter disambiguating concurrent atomic writers in the same
/// PID that collide on PID+nanosecond with `create_new(true)`.
static TMP_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

// ── Instruction rendering ─────────────────────────────────────────────────

/// Render human-readable startup instructions for the given method.
///
/// `exe` and `config` are the current executable and resolved config path.
/// `explicit` controls whether `--config` is shown in cron lines.
#[allow(clippy::too_many_lines)]
#[allow(clippy::uninlined_format_args)]
pub fn render_instructions(
    method: StartupMethod,
    exe: &Path,
    config: &Path,
    _explicit: bool,
) -> String {
    let exe_str = exe.display().to_string();
    let cfg_str = config.display().to_string();
    match method {
        StartupMethod::Systemd => {
            let unit = standard_systemd_unit_path().display().to_string();
            let binary = standard_systemd_binary().display().to_string();
            let cfg = standard_systemd_config().display().to_string();
            format!(
                "Systemd startup (Linux)\n\
                \n\
                Standard paths:\n\
                  binary: {binary}\n\
                  config: {cfg}\n\
                  unit:   {unit}\n\
                \n\
                Install (requires root):\n\
                  sudo {exe_str} startup install --method systemd\n\
                  # or explicitly:\n\
                  sudo {exe_str} startup install --method systemd --config {cfg_str}\n\
                \n\
                The installer will:\n\
                  - verify {binary} exists\n\
                  - ensure the greggd system user/group exists\n\
                  - create {cfg} if absent (preserve existing)\n\
                  - write {unit} atomically\n\
                  - run: systemctl daemon-reload\n\
                  - run: systemctl enable greggd\n\
                  - start or restart the service\n\
                \n\
                Manual/status commands:\n\
                  systemctl status greggd\n\
                  systemctl restart greggd\n\
                  systemctl stop greggd\n\
                  journalctl -u greggd -f\n\
                \n\
                Config location: {cfg}\n"
            )
        }
        StartupMethod::Launchd => {
            let plist = standard_launchd_plist_path().display().to_string();
            let binary = standard_launchd_binary().display().to_string();
            let cfg = standard_launchd_config().display().to_string();
            let label = launchd_label();
            format!(
                "Launchd startup (macOS)\n\
                \n\
                Standard paths:\n\
                  binary: {binary}\n\
                  config: {cfg}\n\
                  plist:  {plist}\n\
                  label:  {label}\n\
                \n\
                Install (requires root):\n\
                  sudo {exe_str} startup install --method launchd\n\
                \n\
                Manual/status commands:\n\
                  sudo launchctl bootstrap system {plist}\n\
                  sudo launchctl kickstart -k system/{label}\n\
                  sudo launchctl bootout system/{label}\n\
                  log show --predicate 'process == \"greggd\"' --last 5m\n\
                \n\
                Config location: {cfg}\n"
            )
        }
        StartupMethod::Cron => {
            // Instructions always show the explicit --config form so the
            // operator can copy-paste a deterministic entry.
            let block = cron_block(exe, config, true)
                .unwrap_or_else(|e| format!("# error: {e}\n"));
            format!(
                "Cron startup (Unix, non-systemd)\n\
                \n\
                This method uses croncheck as the supervisor. No PID file is required.\n\
                The daemon is started only when the health endpoint is definitely absent\n\
                (connection refused). An ambiguous or non-Gregg listener is never\n\
                blindly replaced.\n\
                \n\
                Canonical cron entries for this host:\n\
                {block}\n\
                Install (user-local, no root required):\n\
                  {exe_str} startup install --method cron\n\
                  # with explicit config:\n\
                  {exe_str} startup install --method cron --config {cfg_str}\n\
                \n\
                Manual installation:\n\
                  crontab -l > /tmp/crontab.tmp  # or create empty if no crontab\n\
                  # append the two lines above (including the marker comment)\n\
                  crontab /tmp/crontab.tmp\n\
                  crontab -l   # verify\n\
                \n\
                The managed block is identified by:\n\
                  {marker}\n\
                Rerunning `startup install --method cron` is idempotent and preserves\n\
                unrelated crontab entries.\n\
                \n\
                If `crontab` is unavailable, add the lines through your scheduler's\n\
                native editor and ensure `greggd croncheck` runs at reboot and every minute.\n",
                marker = CRON_MANAGED_MARKER,
            )
        }
        StartupMethod::WindowsScm => {
            String::from(
                "Windows Service (SCM)\n\
                \n\
                The daemon runs as a Windows service via the Service Control Manager.\n\
                \n\
                Install (run PowerShell as Administrator):\n\
                  irm https://github.com/eggstack/gregg/releases/latest/download/install.ps1 | iex\n\
                  .\\packaging\\install.ps1 -Component Greggd\n\
                  # or via bootstrap installer:\n\
                  .\\packaging\\install.ps1 -Component Greggd -Version 1.0.11\n\
                \n\
                Service details:\n\
                  name:        greggd\n\
                  display:     Gregg Metrics Daemon\n\
                  start type:  Automatic\n\
                  account:     NT AUTHORITY\\LocalService\n\
                  config:      %ProgramData%\\gregg\\greggd.toml\n\
                  image:       \"%ProgramFiles%\\Gregg\\greggd.exe\" service --config \"%ProgramData%\\gregg\\greggd.toml\"\n\
                \n\
                Lifecycle commands:\n\
                  greggd start\n\
                  greggd stop\n\
                  greggd restart\n\
                  Get-Service greggd\n\
                  sc.exe query greggd\n\
                \n\
                Existing config at %ProgramData%\\gregg\\greggd.toml is preserved.\n\
                `greggd startup install` on Windows reports service state; SCM\n\
                registration remains owned by the PowerShell installer.\n",
            )
        }
        StartupMethod::Direct => {
            format!(
                "Direct (unmanaged)\n\
                \n\
                No system service manager detected. Run the daemon directly:\n\
                  {exe_str} run --config {cfg_str}\n\
                \n\
                For automatic startup, install via cron:\n\
                  {exe_str} startup install --method cron\n\
                Or use a system service if available:\n\
                  sudo {exe_str} startup install --method systemd   # Linux\n\
                  sudo {exe_str} startup install --method launchd   # macOS\n"
            )
        }
    }
}
// ── Atomic file write helpers ─────────────────────────────────────────────
pub(crate) fn write_atomic_text(path: &Path, content: &str) -> io::Result<()> {
    let dir = path.parent().ok_or_else(|| {
        io::Error::new(io::ErrorKind::InvalidInput, "path has no parent directory")
    })?;
    fs::create_dir_all(dir)?;
    // Write to a temp file in the same directory.
    let tmp_name = format!(
        ".greggd-startup-{}-{}-{}.tmp",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.as_nanos()),
        TMP_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
    );
    let tmp_path = dir.join(tmp_name);
    // Ensure we clean up on failure.
    let write_res = (|| -> io::Result<()> {
        let mut options = fs::OpenOptions::new();
        options.write(true).create_new(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            options.mode(0o600);
        }
        let mut file = options.open(&tmp_path)?;
        io::Write::write_all(&mut file, content.as_bytes())?;
        // Propagate durability failures instead of silently dropping them.
        file.sync_all()?;
        drop(file);
        fs::rename(&tmp_path, path)?;
        // Sync parent directory where supported.
        #[cfg(unix)]
        {
            if let Ok(dir_file) = fs::OpenOptions::new().read(true).open(dir) {
                dir_file.sync_all()?;
            }
        }
        Ok(())
    })();
    if write_res.is_err() {
        let _ = fs::remove_file(&tmp_path);
    }
    write_res
}
// ── Privilege helpers ─────────────────────────────────────────────────────

#[cfg(unix)]
#[allow(unsafe_code)]
pub(crate) fn is_privileged() -> bool {
    // SAFETY: geteuid is a pure libc call with no side effects and is safe
    // to call at any time. It does not read or write Rust-managed memory.
    unsafe { libc::geteuid() == 0 }
}

#[cfg(not(unix))]
pub(crate) fn is_privileged() -> bool {
    // On non-Unix, attempt a privileged operation and handle PermissionDenied.
    // For Windows, check via SCM or assume not privileged if not admin.
    // We treat as privileged only if we can open SCM with create access.
    #[cfg(target_os = "windows")]
    {
        // Best-effort: try to query SCM; if AccessDenied, not privileged.
        // Use a simple check: attempt to open ServiceManager.
        // If we can't, assume not privileged.
        is_windows_admin()
    }
    #[cfg(not(target_os = "windows"))]
    {
        true
    }
}

#[cfg(target_os = "windows")]
fn is_windows_admin() -> bool {
    // Best-effort privilege probe: opening the SCM with create-service
    // access requires elevation. Success means admin; any failure (including
    // access-denied) means not privileged, and the real install attempt
    // still surfaces the true OS error. Uses the existing `windows-service`
    // dependency — no new crates.
    windows_service::service_manager::ServiceManager::local_computer(
        None::<&str>,
        windows_service::service_manager::ServiceManagerAccess::CREATE_SERVICE,
    )
    .is_ok()
}

// ── Systemd installation ──────────────────────────────────────────────────
#[derive(Debug)]
pub enum InstallError {
    Io {
        path: PathBuf,
        source: io::Error,
    },
    Permission {
        message: String,
    },
    SystemdNotDetected {
        message: String,
    },
    LaunchdNotDetected {
        message: String,
    },
    BinaryMissing {
        path: PathBuf,
    },
    UnsupportedMethod {
        method: StartupMethod,
        message: String,
    },
    ShellQuote(ShellQuoteError),
    CrontabUnavailable {
        message: String,
    },
    Other(String),
}
impl fmt::Display for InstallError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
            Self::Permission { message }
            | Self::SystemdNotDetected { message }
            | Self::LaunchdNotDetected { message }
            | Self::CrontabUnavailable { message }
            | Self::Other(message) => write!(f, "{message}"),
            Self::BinaryMissing { path } => write!(
                f,
                "required binary not found at {}: install it first (e.g., sudo install -m 755 <binary> {})",
                path.display(),
                path.display()
            ),
            Self::UnsupportedMethod { method, message } => {
                write!(f, "method {method} not supported: {message}")
            }
            Self::ShellQuote(e) => write!(f, "{e}"),
        }
    }
}
impl std::error::Error for InstallError {}
impl From<ShellQuoteError> for InstallError {
    fn from(e: ShellQuoteError) -> Self {
        Self::ShellQuote(e)
    }
}
pub(crate) fn elevated_command(exe: &Path, method: StartupMethodArg) -> String {
    let exe_str = exe.display().to_string();
    match method {
        StartupMethodArg::Auto => format!("sudo {exe_str} startup install"),
        other => format!("sudo {exe_str} startup install --method {other}"),
    }
}
pub(crate) fn ensure_config_preserved(config_path: &Path) -> io::Result<()> {
    if config_path.exists() {
        return Ok(());
    }
    if let Some(parent) = config_path.parent() {
        fs::create_dir_all(parent)?;
    }
    // Create default config atomically.
    let cfg = Config::default();
    cfg.write_atomic(config_path)
        .map_err(|e| io::Error::other(format!("failed to write default config: {e}")))?;
    Ok(())
}
/// Repair an existing system config so read-only diagnostics work for
/// unprivileged operators.
///
/// System daemon configs carry no secrets, but older installs wrote them
/// `0600 greggd:greggd`, which makes `croncheck`/`status`/`configprint`
/// fail with `Permission denied (os error 13)` for anyone except the
/// daemon user and root. The daemon user still owns the file; this only
/// relaxes the mode to world-readable and ensures the parent directory is
/// traversable. Best-effort for missing paths; hard errors for real I/O
/// failures so install surfaces them.
pub(crate) fn repair_system_config_permissions(config_path: &Path) -> io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Some(parent) = config_path.parent() {
            if parent.exists() {
                // 0755: owner can manage, everyone can traverse/read.
                // Existing operator-managed modes are intentionally
                // normalized here because a 0700 system directory would
                // still block unprivileged `croncheck` even with a 0644
                // file.
                fs::set_permissions(parent, fs::Permissions::from_mode(0o755))?;
            }
        }
        if config_path.exists() {
            fs::set_permissions(config_path, fs::Permissions::from_mode(0o644))?;
        }
        Ok(())
    }
    #[cfg(not(unix))]
    {
        let _ = config_path;
        Ok(())
    }
}
pub(crate) fn manager_error_is_permission(message: &str) -> bool {
    let message = message.to_ascii_lowercase();
    message.contains("permission")
        || message.contains("access denied")
        || message.contains("not authorized")
        || message.contains("authentication")
}
// ── Unified install dispatch ──────────────────────────────────────────────
pub fn install_startup(
    exe: &Path,
    config_path: &Path,
    explicit_config: bool,
    method_arg: StartupMethodArg,
) -> Result<(), InstallError> {
    let method = resolve_startup_method(method_arg);
    // Explicit method overrides auto; if auto selected systemd but privilege missing, do not fallback to cron.
    match method {
        StartupMethod::Systemd => install_systemd(exe, config_path),
        StartupMethod::Launchd => install_launchd(exe, config_path),
        StartupMethod::Cron | StartupMethod::Direct => {
            install_cron(exe, config_path, explicit_config)
        }
        StartupMethod::WindowsScm => {
            // On Windows, SCM registration is owned by the PowerShell installer.
            // Report state and instructions rather than duplicating sc.exe logic.
            #[cfg(target_os = "windows")]
            {
                let state = super::state::startup_state();
                println!("Windows Service (SCM) state: {state}");
                println!(
                    "{}",
                    render_instructions(method, exe, config_path, explicit_config)
                );
                // If service is not installed, instruct to use installer.
                if state == super::state::StartupState::UnmanagedOrCron {
                    println!(
                        "No greggd service found. Run as Administrator:\n  .\\packaging\\install.ps1 -Component Greggd"
                    );
                }
                Ok(())
            }
            #[cfg(not(target_os = "windows"))]
            {
                Err(InstallError::UnsupportedMethod {
                    method,
                    message: "Windows SCM is only available on Windows".into(),
                })
            }
        }
    }
}
// ── Restart ───────────────────────────────────────────────────────────────

/// Restart via systemd using `systemctl restart greggd`.
#[cfg(unix)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RestartStopState {
    Stopped,
    NotRunning,
    Uncertain,
    ControlError,
}
#[cfg(unix)]
fn restart_spawn_allowed(stop_state: RestartStopState, probe: &crate::cli::CroncheckProbe) -> bool {
    !matches!(stop_state, RestartStopState::Uncertain)
        && matches!(probe, crate::cli::CroncheckProbe::Absent)
}
#[allow(clippy::too_many_lines)]
fn restart_cron_direct(exe: &Path, config_path: &Path, explicit: bool) -> Result<(), InstallError> {
    // Use existing control socket stop + detached start via croncheck primitive.
    // 1) try stop
    #[cfg(unix)]
    {
        let outcome = crate::control::send_stop(config_path);
        let config = crate::cli::load_config(config_path, explicit).map_err(|error| {
            InstallError::Other(format!("failed to load restart config: {error}"))
        })?;
        let target = crate::cli::croncheck_target(&config);
        let stop_state = match outcome {
            Ok(crate::control::StopOutcome::Stopped { .. }) => {
                println!("greggd stopped via control socket");
                RestartStopState::Stopped
            }
            Ok(crate::control::StopOutcome::NotRunning) => {
                println!("greggd not running (control socket)");
                RestartStopState::NotRunning
            }
            Ok(crate::control::StopOutcome::Uncertain { .. }) => RestartStopState::Uncertain,
            Err(e) => {
                // If permission denied, surface it
                if let crate::control::ControlError::Io(io_e) = &e {
                    if io_e.kind() == io::ErrorKind::PermissionDenied {
                        return Err(InstallError::Permission {
                            message: format!("permission denied on stop: {e}"),
                        });
                    }
                }
                if !matches!(
                    crate::cli::probe_greggd(target),
                    crate::cli::CroncheckProbe::Absent
                ) {
                    return Err(InstallError::Other(format!(
                        "restart refused after stop error: endpoint is not definitely absent ({e})"
                    )));
                }
                RestartStopState::ControlError
            }
        };
        if matches!(stop_state, RestartStopState::Uncertain) {
            return Err(InstallError::Other(
                "restart refused: stop outcome was uncertain; daemon may still be running".into(),
            ));
        }
        wait_for_endpoint_absence(target)?;
        let probe = crate::cli::probe_greggd(target);
        if !restart_spawn_allowed(stop_state, &probe) {
            return Err(InstallError::Other(
                "restart refused: endpoint is not definitely absent".into(),
            ));
        }
        let mut cmd = crate::cli::build_daemon_command_for(exe, config_path, explicit);
        let mut child = match cmd.spawn() {
            Ok(child) => child,
            Err(e) if e.kind() == io::ErrorKind::PermissionDenied => {
                return Err(InstallError::Permission {
                    message: format!("permission denied spawning greggd: {e}"),
                });
            }
            Err(e) => {
                return Err(InstallError::Io {
                    path: PathBuf::from("greggd run"),
                    source: e,
                });
            }
        };
        let deadline = Instant::now() + DIRECT_RESTART_TIMEOUT;
        loop {
            match crate::cli::probe_greggd(target) {
                crate::cli::CroncheckProbe::Running => {
                    println!("greggd restarted (direct/cron) and passed health check");
                    return Ok(());
                }
                crate::cli::CroncheckProbe::Ambiguous if Instant::now() >= deadline => {
                    let _ = child.kill();
                    let _ = child.wait();
                    return Err(InstallError::Other(
                        "greggd restart timed out with an ambiguous endpoint".into(),
                    ));
                }
                _ if Instant::now() >= deadline => {
                    let _ = child.kill();
                    let _ = child.wait();
                    return Err(InstallError::Other(
                        "greggd restart timed out before health readiness".into(),
                    ));
                }
                _ => {
                    if let Some(status) = child.try_wait().map_err(|source| InstallError::Io {
                        path: PathBuf::from("greggd run"),
                        source,
                    })? {
                        return Err(InstallError::Other(format!(
                            "greggd restart child exited before readiness: {status}"
                        )));
                    }
                    thread::sleep(Duration::from_millis(50));
                }
            }
        }
    }
    #[cfg(not(unix))]
    {
        let _ = (exe, config_path, explicit);
        Err(InstallError::Other(
            "direct restart not supported on this platform".into(),
        ))
    }
}
#[cfg(unix)]
fn wait_for_endpoint_absence(target: std::net::SocketAddr) -> Result<(), InstallError> {
    let deadline = Instant::now() + DIRECT_RESTART_TIMEOUT;
    loop {
        match crate::cli::probe_greggd(target) {
            crate::cli::CroncheckProbe::Absent => return Ok(()),
            crate::cli::CroncheckProbe::Running if Instant::now() >= deadline => {
                return Err(InstallError::Other(
                    "restart refused: configured endpoint remained occupied".into(),
                ));
            }
            crate::cli::CroncheckProbe::Ambiguous if Instant::now() >= deadline => {
                return Err(InstallError::Other(
                    "restart refused: could not prove configured endpoint is absent".into(),
                ));
            }
            _ => thread::sleep(Duration::from_millis(50)),
        }
    }
}

/// Manager-aware restart, factoring for Plan 101 reuse.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(target_os = "windows", allow(dead_code))]
enum RestartRoute {
    Systemd,
    Launchd,
    WindowsScm,
    Direct,
}

/// Decide whether a discovered startup artifact may receive restart
/// mutation. A manager is authoritative only when its registered executable
/// targets the exact invoked binary. A foreign manager with a different,
/// known config may coexist with a config-specific direct restart; same or
/// unknown config identity fails closed.
#[cfg_attr(target_os = "windows", allow(dead_code))]
fn manager_restart_route(
    manager: StartupMethod,
    ownership: super::ArtifactOwnership,
    active: bool,
    registered_config: Option<&Path>,
    selected_config: &Path,
) -> Result<RestartRoute, String> {
    match ownership {
        super::ArtifactOwnership::Owned => Ok(match manager {
            StartupMethod::Systemd => RestartRoute::Systemd,
            StartupMethod::Launchd => RestartRoute::Launchd,
            StartupMethod::WindowsScm => RestartRoute::WindowsScm,
            StartupMethod::Cron | StartupMethod::Direct => RestartRoute::Direct,
        }),
        super::ArtifactOwnership::Absent if !active => Ok(RestartRoute::Direct),
        super::ArtifactOwnership::Absent => Err(format!(
            "{manager} is active but its executable registration is absent; refusing restart"
        )),
        super::ArtifactOwnership::Unknown => Err(format!(
            "cannot determine {manager} executable ownership; refusing restart"
        )),
        super::ArtifactOwnership::Foreign => match registered_config {
            Some(config) if !gregg_update::uninstall::paths_equivalent(config, selected_config) => {
                Ok(RestartRoute::Direct)
            }
            Some(_) => Err(format!(
                "foreign {manager} registration owns the selected config; refusing direct restart"
            )),
            None => Err(format!(
                "foreign {manager} registration has unknown config ownership; refusing restart"
            )),
        },
    }
}

fn execute_restart(
    route: RestartRoute,
    exe: &Path,
    config_path: &Path,
    explicit: bool,
) -> Result<(), InstallError> {
    match route {
        RestartRoute::Systemd => restart_systemd(exe),
        RestartRoute::Launchd => restart_launchd(exe),
        RestartRoute::WindowsScm => {
            #[cfg(target_os = "windows")]
            {
                crate::service::platform_service_manager()
                    .restart()
                    .map_err(|e| {
                        let msg = e.to_string().to_lowercase();
                        if msg.contains("access denied") || msg.contains("permission") {
                            InstallError::Permission {
                                message: format!("permission denied restarting service: {e}; rerun as Administrator"),
                            }
                        } else {
                            InstallError::Other(format!("service restart failed: {e}"))
                        }
                    })?;
                println!("greggd restarted via SCM");
                Ok(())
            }
            #[cfg(not(target_os = "windows"))]
            {
                let _ = explicit;
                Err(InstallError::Other(
                    "Windows service restart requested on non-Windows".into(),
                ))
            }
        }
        RestartRoute::Direct => restart_cron_direct(exe, config_path, explicit),
    }
}

#[cfg(target_os = "windows")]
fn windows_restart_registration_route(
    registration: &crate::service::ServiceRegistration,
    exe: &Path,
) -> Result<RestartRoute, String> {
    if registration.state == crate::service::ServiceState::NotInstalled {
        return Err("Windows SCM service is not installed; direct restart is unsupported".into());
    }
    let target = registration.executable_path.as_deref().ok_or_else(|| {
        "Windows SCM executable ownership is unknown; refusing restart".to_string()
    })?;
    if !gregg_update::uninstall::paths_equivalent(target, exe) {
        return Err(format!(
            "Windows SCM service targets foreign executable {}; refusing restart",
            target.display()
        ));
    }
    Ok(RestartRoute::WindowsScm)
}

pub fn restart_daemon(exe: &Path, config_path: &Path, explicit: bool) -> Result<(), InstallError> {
    let route = {
        #[cfg(target_os = "windows")]
        {
            let registration = crate::service::platform_service_manager()
                .query_registration()
                .map_err(|error| {
                    InstallError::Other(format!(
                        "cannot determine Windows SCM ownership; refusing restart: {error}"
                    ))
                })?;
            windows_restart_registration_route(&registration, exe).map_err(InstallError::Other)?
        }
        #[cfg(all(unix, target_os = "linux"))]
        {
            let (ownership, active, registered_config) =
                super::systemd::systemd_artifact_ownership(exe);
            manager_restart_route(
                StartupMethod::Systemd,
                ownership,
                active,
                registered_config.as_deref(),
                config_path,
            )
            .map_err(InstallError::Other)?
        }
        #[cfg(all(unix, target_os = "macos"))]
        {
            let (ownership, active, registered_config) =
                super::launchd::launchd_artifact_ownership(exe);
            manager_restart_route(
                StartupMethod::Launchd,
                ownership,
                active,
                registered_config.as_deref(),
                config_path,
            )
            .map_err(InstallError::Other)?
        }
        #[cfg(all(unix, not(any(target_os = "linux", target_os = "macos"))))]
        {
            RestartRoute::Direct
        }
    };
    execute_restart(route, exe, config_path, explicit)
}
#[cfg(test)]
mod tests {
    use super::super::state::StartupState;
    use super::*;
    use crate::startup::ArtifactOwnership;

    #[test]
    fn instructions_contain_standard_paths() {
        let exe = Path::new("/usr/local/bin/greggd");
        let cfg = Path::new("/etc/gregg/greggd.toml");
        let s = render_instructions(StartupMethod::Systemd, exe, cfg, true);
        assert!(s.contains("/usr/local/bin/greggd"));
        assert!(s.contains("/etc/gregg/greggd.toml"));
        assert!(s.contains("/etc/systemd/system/greggd.service"));

        let s2 = render_instructions(StartupMethod::Cron, exe, cfg, true);
        assert!(s2.contains(CRON_MANAGED_MARKER));
        assert!(s2.contains("croncheck"));
        assert!(s2.contains("No PID file"));

        let s3 = render_instructions(
            StartupMethod::Launchd,
            exe,
            Path::new("/Library/Application Support/gregg/greggd.toml"),
            true,
        );
        assert!(s3.contains("com.eggstack.greggd"));
        assert!(s3.contains("/Library/LaunchDaemons/com.eggstack.greggd.plist"));
    }
    #[cfg(unix)]
    #[test]
    fn restart_spawn_decision_requires_definite_absence() {
        use crate::cli::CroncheckProbe;

        assert!(restart_spawn_allowed(
            RestartStopState::Stopped,
            &CroncheckProbe::Absent
        ));
        assert!(restart_spawn_allowed(
            RestartStopState::NotRunning,
            &CroncheckProbe::Absent
        ));
        assert!(restart_spawn_allowed(
            RestartStopState::ControlError,
            &CroncheckProbe::Absent
        ));
        assert!(!restart_spawn_allowed(
            RestartStopState::Uncertain,
            &CroncheckProbe::Absent
        ));
        assert!(!restart_spawn_allowed(
            RestartStopState::Stopped,
            &CroncheckProbe::Running
        ));
        assert!(!restart_spawn_allowed(
            RestartStopState::NotRunning,
            &CroncheckProbe::Ambiguous
        ));

        let mut spawn_count = 0;
        if restart_spawn_allowed(RestartStopState::Stopped, &CroncheckProbe::Absent) {
            spawn_count += 1;
        }
        assert_eq!(spawn_count, 1);
    }
    #[test]
    fn systemd_state_maps_active_manager_for_restart() {
        // This test only verifies the helper maps correctly; it doesn't run systemctl.
        // We test that an unmanaged state would go to cron/direct path without panicking in pure helper.
        // Actual systemctl invocation is not mocked here; we just check state mapping.
        let state = systemd_state_with(true, true);
        assert_eq!(state, StartupState::SystemdActive);
    }

    #[test]
    fn owned_manager_registration_is_the_only_manager_restart_route() {
        let selected = Path::new("/etc/gregg/greggd.toml");
        assert_eq!(
            manager_restart_route(
                StartupMethod::Systemd,
                super::super::ArtifactOwnership::Owned,
                true,
                Some(selected),
                selected,
            ),
            Ok(RestartRoute::Systemd)
        );
        assert_eq!(
            manager_restart_route(
                StartupMethod::Launchd,
                super::super::ArtifactOwnership::Owned,
                false,
                Some(selected),
                selected,
            ),
            Ok(RestartRoute::Launchd)
        );
    }

    #[test]
    fn foreign_manager_with_different_config_allows_config_specific_direct_restart() {
        assert_eq!(
            manager_restart_route(
                StartupMethod::Systemd,
                super::super::ArtifactOwnership::Foreign,
                true,
                Some(Path::new("/other/greggd.toml")),
                Path::new("/selected/greggd.toml"),
            ),
            Ok(RestartRoute::Direct)
        );
    }

    #[test]
    fn foreign_launchd_with_different_config_allows_config_specific_direct_restart() {
        assert_eq!(
            manager_restart_route(
                StartupMethod::Launchd,
                ArtifactOwnership::Foreign,
                true,
                Some(Path::new("/tmp/foreign.toml")),
                Path::new("/tmp/selected.toml"),
            )
            .unwrap(),
            RestartRoute::Direct
        );
    }

    #[test]
    fn foreign_same_config_and_unknown_active_manager_fail_closed() {
        let selected = Path::new("/etc/gregg/greggd.toml");
        assert!(manager_restart_route(
            StartupMethod::Launchd,
            super::super::ArtifactOwnership::Foreign,
            true,
            Some(selected),
            selected,
        )
        .is_err());
        assert!(manager_restart_route(
            StartupMethod::Systemd,
            super::super::ArtifactOwnership::Unknown,
            true,
            None,
            selected,
        )
        .is_err());
        assert!(manager_restart_route(
            StartupMethod::Systemd,
            super::super::ArtifactOwnership::Foreign,
            true,
            None,
            selected,
        )
        .is_err());
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn windows_restart_registration_preserves_scm_ownership_states() {
        use crate::service::{ServiceRegistration, ServiceState};

        let exe = Path::new(r"C:\Program Files\Gregg\greggd.exe");
        let owned = ServiceRegistration {
            state: ServiceState::Running,
            executable_path: Some(exe.to_path_buf()),
        };
        assert_eq!(
            windows_restart_registration_route(&owned, exe),
            Ok(RestartRoute::WindowsScm)
        );
        let foreign = ServiceRegistration {
            state: ServiceState::Stopped,
            executable_path: Some(PathBuf::from(r"C:\Other\greggd.exe")),
        };
        assert!(windows_restart_registration_route(&foreign, exe).is_err());
        let unknown = ServiceRegistration {
            state: ServiceState::Running,
            executable_path: None,
        };
        assert!(windows_restart_registration_route(&unknown, exe).is_err());
        let missing = ServiceRegistration {
            state: ServiceState::NotInstalled,
            executable_path: None,
        };
        assert!(windows_restart_registration_route(&missing, exe).is_err());
    }
    #[test]
    fn manager_permission_text_is_classified() {
        assert!(manager_error_is_permission(
            "Interactive authentication required"
        ));
        assert!(manager_error_is_permission("Access denied"));
        assert!(!manager_error_is_permission("unit failed"));
    }
    #[test]
    #[cfg(unix)]
    fn repair_system_config_permissions_relaxes_old_0600() {
        use std::os::unix::fs::PermissionsExt;

        let dir =
            std::env::temp_dir().join(format!("greggd_test_repair_perms_{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        let path = dir.join("greggd.toml");
        // Simulate a pre-fix install: 0700 dir, 0600 file.
        fs::set_permissions(&dir, fs::Permissions::from_mode(0o700)).unwrap();
        fs::write(
            &path,
            "name = \"greggd\"\nhost = \"0.0.0.0\"\nport = 11310\nsample_interval_ms = 1000\nstale_after_ms = 10000\n",
        )
        .unwrap();
        fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap();

        repair_system_config_permissions(&path).unwrap();

        assert_eq!(
            fs::metadata(&path).unwrap().permissions().mode() & 0o777,
            0o644,
            "system config must become world-readable for croncheck/status"
        );
        assert_eq!(
            fs::metadata(&dir).unwrap().permissions().mode() & 0o777,
            0o755,
            "system config dir must stay traversable"
        );
        let _ = fs::remove_dir_all(&dir);
    }
}