Skip to main content

proc_cli/commands/
thaw.rs

1//! `proc thaw` - Resume frozen processes with SIGCONT
2//!
3//! Examples:
4//!   proc thaw node              # Resume the frozen processes named node
5//!   proc thaw node --cmd        # ... or with node anywhere in their command line
6//!   proc thaw :3000             # Resume process on port 3000
7//!   proc thaw :3000 :8080       # Resume several targets: separate arguments
8//!   proc thaw node --dry-run    # Show what would be resumed
9//!   proc thaw :3000 --yes       # Skip confirmation (a port or PID, not a name)
10//!
11//! Commas still work (`proc thaw :3000,:8080`) but are deprecated.
12
13#[cfg(unix)]
14use crate::core::{
15    apply_filters, apply_to_each, flatten_targets, resolve_targets_excluding_self, NameMatch,
16    Process,
17};
18use crate::error::{ProcError, Result};
19#[cfg(unix)]
20use crate::ui::Printer;
21use clap::Args;
22
23/// Resume frozen process(es) with SIGCONT
24#[derive(Args, Debug)]
25pub struct ThawCommand {
26    /// Target(s): process name, PID, or :port (separate arguments for several)
27    #[arg(required = true, num_args = 1.., value_parser = crate::core::validate_target_arg)]
28    pub target: Vec<String>,
29
30    /// Skip confirmation prompt
31    #[arg(long, short = 'y')]
32    pub yes: bool,
33
34    /// Show what would be resumed without actually resuming
35    #[arg(long)]
36    pub dry_run: bool,
37
38    /// Output as JSON
39    #[arg(long, short = 'j')]
40    pub json: bool,
41
42    /// Show verbose output
43    #[arg(long, short = 'v')]
44    pub verbose: bool,
45
46    /// Filter by directory; takes the next word as the directory unless it starts with '-'
47    /// (bare --in is the current directory, --in=DIR is explicit)
48    #[arg(long = "in", short = 'i', num_args = 0..=1, default_missing_value = ".")]
49    pub in_dir: Option<String>,
50
51    /// Filter by process name
52    #[arg(long = "by", short = 'b')]
53    pub by_name: Option<String>,
54
55    /// Match name targets and --by as a substring of the process name or full command line (like pkill -f)
56    #[arg(long)]
57    pub cmd: bool,
58}
59
60impl ThawCommand {
61    /// Executes the thaw command, resuming frozen processes with SIGCONT.
62    #[cfg(unix)]
63    pub fn execute(&self) -> Result<()> {
64        use nix::sys::signal::Signal;
65
66        let printer = Printer::from_flags(self.json, self.verbose);
67
68        let targets = flatten_targets(&self.target);
69        let mode = NameMatch::from_flag(self.cmd);
70        let resolved = resolve_targets_excluding_self(&targets, mode);
71
72        if let Some(warning) = resolved.warning() {
73            printer.warning(&warning);
74        }
75        // the JSON results list the targets that matched nothing
76        let printer = printer.with_not_found(&resolved.not_found);
77        let mut processes = resolved.processes;
78
79        // Apply --in and --by filters
80        apply_filters(&mut processes, &self.in_dir, &self.by_name, mode);
81
82        // Real CPU readings whenever the list is shown or returned (dry run, the
83        // confirmation prompt, or the error for a missing `--yes`); only `--yes`
84        // acts at once, without the wait
85        if self.dry_run || !self.yes {
86            Process::measure_cpu(&mut processes);
87        }
88
89        if processes.is_empty() {
90            return Err(ProcError::ProcessNotFound(self.target.join(" ")));
91        }
92
93        if self.dry_run {
94            printer.print_dry_run("thaw", &processes);
95            return Ok(());
96        }
97
98        if !printer.ask_confirm("thaw", &processes, self.yes)? {
99            return Ok(());
100        }
101
102        let (succeeded, failed) =
103            apply_to_each(&processes, |proc| proc.send_signal(Signal::SIGCONT));
104
105        printer.print_action_result("thaw", &succeeded, &failed)
106    }
107
108    /// Windows stub
109    #[cfg(not(unix))]
110    pub fn execute(&self) -> Result<()> {
111        Err(ProcError::NotSupported(
112            "thaw (SIGCONT) is not supported on Windows".to_string(),
113        ))
114    }
115}