use core::cmp::Ordering;
use core::fmt;
use core::sync::atomic::{self, AtomicU64};
use core::time::Duration;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;
use serde::{Deserialize, Serialize};
use tokio::sync::{mpsc, oneshot};
use crate::listen::ListenSockets;
use shep_core::config::{
AppConfig, ApplyGroup, DeclaredApp, DogTable, NormalizeError, ResetDepth, ResolvedApp,
apply_group, normalize, reaches_running,
};
use shep_core::overrides::{self, AppOverrides};
use shep_core::paths::ShepPaths;
use shep_core::protocol::{
ActionOutcome, ActionReply, BusEvent, DogSource, EnvValue, ExitInfo, LineOutcome, LineReply,
ProcessEventKind, ProcessInfo, SheepConfigView, SheepDrift, SignalOutcome, SignalReply, Smit,
sort_flock,
};
use shep_core::secrets::SecretView;
use shep_core::selector::ProcessSelector;
use shep_core::signals::OperatorSignal;
use shep_core::status::ProcStatus;
use shep_core::values::MemSize;
use crate::assemble::{AssembleError, assemble, describe, instance_slots, through_dog};
use crate::backoff::restart_delay;
use crate::brain::{Decision, SANDBOX_DOG, decide_on_exit, is_sandbox_refusal};
use crate::bus::{Bus, SharedEvent};
use crate::channel::{ChildMessage, ShepherdMessage};
use crate::entry::{ProcessEntry, ReloadState, RestartBudget};
use crate::extras::{Extras, ExtrasRegistry};
#[cfg(unix)]
use crate::handover::adopt::{AdoptedListeners, AdoptedSheep};
#[cfg(unix)]
use crate::handover::reap::AdoptedReaper;
#[cfg(unix)]
use crate::handover::{
Candidate, CarriedFds, CarriedSheep, Counters, DaemonFds, Fitness, Handover, HeldListener,
OwnedCandidate, fitness,
};
use crate::kill::kill_process;
use crate::privilege::{self, Credentials, PrivilegeError, SpawnIdentity};
use crate::probes::Prober;
use crate::probes::os::OsProber;
use crate::probes::ready::{Readiness, ReadinessSource, await_ready};
#[cfg(unix)]
use crate::runner::AdoptSpec;
use crate::runner::{
ExitOutcome, FlushError, LogCtl, LogOpen, Preflight, ProcIo, ProcessRunner, PtyError,
PtyHandle, ReopenError, RunnerError, RunningProcess, SpawnSpec, StdinWrite, check_log_ancestry,
cwd_advisory, log_path_advisory, open_log_path,
};
use crate::secrets::ProviderSecrets;
mod actor_actions;
mod actor_config;
mod actor_core;
mod actor_disable;
mod actor_dog_tables;
mod actor_env_copy;
mod actor_exit;
mod actor_lifecycle;
mod actor_listen;
mod actor_pane;
mod actor_pty;
mod actor_reload;
mod actor_reload_done;
mod actor_reset;
mod actor_scale;
mod actor_spawn;
mod builder;
mod command;
mod config_merge;
mod error;
mod handle;
#[cfg(unix)]
mod handover;
mod logs;
mod manual;
mod reload;
mod sheep;
mod slot;
mod tasks;
mod types;
mod wire;
#[cfg(target_os = "linux")]
use actor_spawn::cpu_quota_of;
pub(crate) use builder::SupervisorBuilder;
pub use builder::spawn_supervisor;
use command::ReplyKind;
pub(crate) use command::{Command, Msg};
#[cfg(target_os = "linux")]
use config_merge::CGROUP_FIELDS;
use config_merge::{
EXTRAS_FIELDS, dog_config_refusal, env_override_map, merge_declared, pending_fields,
reached_spec, try_with_count, with_count,
};
#[cfg(unix)]
pub(crate) use error::AdoptError;
pub use error::SupervisorError;
pub use handle::SupervisorHandle;
#[cfg(all(test, unix))]
use handover::REPORT_DEADLINE;
#[cfg(unix)]
use handover::{HandoverDraft, Snapshot, spawn_handover_task};
#[cfg(test)]
use logs::{flush_logs, reopen_logs, truncate_log};
use logs::{spawn_flush_task, spawn_reopen_task};
use manual::{ActionWaits, PendingAction};
pub(crate) use manual::{CommandOrigin, ManualKind, PendingManual};
#[cfg(unix)]
pub(crate) use reload::CarriedReload;
use reload::{LadderCap, ReloadJob};
pub(crate) use reload::{ReloadMode, ReloadPhase, ReloadSwap};
#[cfg(test)]
use sheep::run_sheep;
use sheep::{SignalRequest, spawn_sheep_task};
use slot::SheepSlot;
use tasks::{
spawn_action_task, spawn_readiness_task, spawn_send_line_task, spawn_signal_task,
spawn_trigger_task, spec_prober,
};
pub(crate) use types::{
Applied, BatchPolicy, ConnId, DEFAULT_ENVIRONMENT, EnvBatch, EnvCopy, FieldSet, PtyAttached,
SheepDogRead,
};
use types::{PendingReply, Registration, Scaled, SheepCtl, Smits};
use wire::{reload_eligible, restored_status, send_reply, swap_budget, to_info};
const MAILBOX_CAPACITY: usize = 256;
const SHEEP_CTL_CAPACITY: usize = 4;
const SIGNAL_CAPACITY: usize = 16;
const RELOAD_DEADLINE_SLACK: Duration = Duration::from_secs(5);
const STDIN_WRITE_TIMEOUT: Duration = Duration::from_secs(2);
const MAX_ABANDONED_ACTION_REPLIES: usize = 64;
struct Actor<R: ProcessRunner> {
runner: R,
paths: ShepPaths,
host_environment: String,
dog_binaries: BTreeMap<String, String>,
provider_secrets: Arc<ProviderSecrets>,
events: Bus,
tx: mpsc::Sender<Msg>,
sheep: HashMap<u32, SheepSlot>,
next_id: u32,
next_deadline: u64,
next_action_stamp: u64,
pending: Vec<PendingReply>,
shutting_down: bool,
extras: Option<Extras>,
registry: ExtrasRegistry,
smits: Smits,
reloads: HashMap<String, ReloadJob>,
disabled: BTreeSet<String>,
listeners: BTreeMap<String, Arc<ListenSockets>>,
listeners_before: BTreeMap<String, Option<Arc<ListenSockets>>>,
cgroup_base: Option<PathBuf>,
cgroup_unavailable: Option<String>,
oom_baselines: HashMap<u32, u64>,
}
#[cfg(test)]
mod tests;