Extract as a function.

This commit is contained in:
r-vdp 2023-03-21 16:47:59 +01:00
parent 43a2af24ae
commit 743be9537c
No known key found for this signature in database

View file

@ -178,21 +178,11 @@ fn verify_systemd_dir(ephemeral: bool) -> Result<()> {
} }
pub fn deactivate() -> Result<()> { pub fn deactivate() -> Result<()> {
restore_ephemeral_system_dir()?;
let old_services = read_saved_services()?; let old_services = read_saved_services()?;
log::debug!("{:?}", old_services); log::debug!("{:?}", old_services);
// If we turned the ephemeral systemd system dir under /run into a symlink,
// then systemd crashes in a very difficult to understand way.
// To avoid this, we always check if this directory exists and is correct,
// and we recreate it if needed.
let ephemeral_systemd_system_dir = systemd_system_dir(true);
if !ephemeral_systemd_system_dir.exists() {
if ephemeral_systemd_system_dir.is_symlink() {
fs::remove_file(&ephemeral_systemd_system_dir)?;
}
fs::create_dir_all(&ephemeral_systemd_system_dir)?;
}
let service_manager = systemd::ServiceManager::new_session()?; let service_manager = systemd::ServiceManager::new_session()?;
let job_monitor = service_manager.monitor_jobs_init()?; let job_monitor = service_manager.monitor_jobs_init()?;
let timeout = Some(Duration::from_secs(30)); let timeout = Some(Duration::from_secs(30));
@ -217,6 +207,22 @@ pub fn deactivate() -> Result<()> {
Ok(()) Ok(())
} }
// If we turned the ephemeral systemd system dir under /run into a symlink,
// then systemd crashes when that symlink goes broken.
// To avoid this, we always check whether this directory exists and is correct,
// and we recreate it if needed.
// NOTE: We rely on the fact that the etc files get cleaned up first, before this runs!
fn restore_ephemeral_system_dir() -> Result<()> {
let ephemeral_systemd_system_dir = systemd_system_dir(true);
if !ephemeral_systemd_system_dir.exists() {
if ephemeral_systemd_system_dir.is_symlink() {
fs::remove_file(&ephemeral_systemd_system_dir)?;
}
fs::create_dir_all(&ephemeral_systemd_system_dir)?;
}
Ok(())
}
// FIXME: we should probably lock this file to avoid concurrent writes // FIXME: we should probably lock this file to avoid concurrent writes
fn serialise_saved_services(services: &Services) -> Result<()> { fn serialise_saved_services(services: &Services) -> Result<()> {
let state_file = Path::new(SYSTEM_MANAGER_STATE_DIR).join(SERVICES_STATE_FILE_NAME); let state_file = Path::new(SYSTEM_MANAGER_STATE_DIR).join(SERVICES_STATE_FILE_NAME);