Skip to content

Commit 6607f10

Browse files
authored
Merge pull request #1440 from jmarrero/revert-fallout
Revert "blockdev: implement signal-safe loopback device cleanup helper"
2 parents ed787b4 + fe36420 commit 6607f10

File tree

5 files changed

+1
-113
lines changed

5 files changed

+1
-113
lines changed

Cargo.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/blockdev/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ anyhow = { workspace = true }
1111
bootc-utils = { package = "bootc-internal-utils", path = "../utils", version = "0.0.0" }
1212
camino = { workspace = true, features = ["serde1"] }
1313
fn-error-context = { workspace = true }
14-
libc = { workspace = true }
1514
regex = "1.10.4"
16-
rustix = { workspace = true }
1715
serde = { workspace = true, features = ["derive"] }
1816
serde_json = { workspace = true }
19-
tempfile = { workspace = true }
20-
tokio = { workspace = true, features = ["signal"] }
2117
tracing = { workspace = true }
2218

2319
[dev-dependencies]

crates/blockdev/src/blockdev.rs

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,6 @@ pub fn partitions_of(dev: &Utf8Path) -> Result<PartitionTable> {
181181

182182
pub struct LoopbackDevice {
183183
pub dev: Option<Utf8PathBuf>,
184-
// Handle to the cleanup helper process
185-
cleanup_handle: Option<LoopbackCleanupHandle>,
186-
}
187-
188-
/// Handle to manage the cleanup helper process for loopback devices
189-
struct LoopbackCleanupHandle {
190-
/// Child process handle
191-
child: std::process::Child,
192184
}
193185

194186
impl LoopbackDevice {
@@ -216,15 +208,7 @@ impl LoopbackDevice {
216208
.run_get_string()?;
217209
let dev = Utf8PathBuf::from(dev.trim());
218210
tracing::debug!("Allocated loopback {dev}");
219-
220-
// Try to spawn cleanup helper process - if it fails, make it fatal
221-
let cleanup_handle = Self::spawn_cleanup_helper(dev.as_str())
222-
.context("Failed to spawn loopback cleanup helper")?;
223-
224-
Ok(Self {
225-
dev: Some(dev),
226-
cleanup_handle: Some(cleanup_handle),
227-
})
211+
Ok(Self { dev: Some(dev) })
228212
}
229213

230214
// Access the path to the loopback block device.
@@ -233,49 +217,13 @@ impl LoopbackDevice {
233217
self.dev.as_deref().unwrap()
234218
}
235219

236-
/// Spawn a cleanup helper process that will clean up the loopback device
237-
/// if the parent process dies unexpectedly
238-
fn spawn_cleanup_helper(device_path: &str) -> Result<LoopbackCleanupHandle> {
239-
use std::process::{Command, Stdio};
240-
241-
// Get the path to our own executable
242-
let self_exe =
243-
std::fs::read_link("/proc/self/exe").context("Failed to read /proc/self/exe")?;
244-
245-
// Create the helper process
246-
let mut cmd = Command::new(self_exe);
247-
cmd.args(["loopback-cleanup-helper", "--device", device_path]);
248-
249-
// Set environment variable to indicate this is a cleanup helper
250-
cmd.env("BOOTC_LOOPBACK_CLEANUP_HELPER", "1");
251-
252-
// Set up stdio to redirect to /dev/null
253-
cmd.stdin(Stdio::null());
254-
cmd.stdout(Stdio::null());
255-
cmd.stderr(Stdio::null());
256-
257-
// Spawn the process
258-
let child = cmd
259-
.spawn()
260-
.context("Failed to spawn loopback cleanup helper")?;
261-
262-
Ok(LoopbackCleanupHandle { child })
263-
}
264-
265220
// Shared backend for our `close` and `drop` implementations.
266221
fn impl_close(&mut self) -> Result<()> {
267222
// SAFETY: This is the only place we take the option
268223
let Some(dev) = self.dev.take() else {
269224
tracing::trace!("loopback device already deallocated");
270225
return Ok(());
271226
};
272-
273-
// Kill the cleanup helper since we're cleaning up normally
274-
if let Some(mut cleanup_handle) = self.cleanup_handle.take() {
275-
// Send SIGTERM to the child process
276-
let _ = cleanup_handle.child.kill();
277-
}
278-
279227
Command::new("losetup").args(["-d", dev.as_str()]).run()
280228
}
281229

@@ -292,46 +240,6 @@ impl Drop for LoopbackDevice {
292240
}
293241
}
294242

295-
/// Main function for the loopback cleanup helper process
296-
/// This function does not return - it either exits normally or via signal
297-
pub async fn run_loopback_cleanup_helper(device_path: &str) -> Result<()> {
298-
// Check if we're running as a cleanup helper
299-
if std::env::var("BOOTC_LOOPBACK_CLEANUP_HELPER").is_err() {
300-
anyhow::bail!("This function should only be called as a cleanup helper");
301-
}
302-
303-
// Set up death signal notification - we want to be notified when parent dies
304-
rustix::process::set_parent_process_death_signal(Some(rustix::process::Signal::TERM))
305-
.context("Failed to set parent death signal")?;
306-
307-
// Wait for SIGTERM (either from parent death or normal cleanup)
308-
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
309-
.expect("Failed to create signal stream")
310-
.recv()
311-
.await;
312-
313-
// Clean up the loopback device
314-
let status = std::process::Command::new("losetup")
315-
.args(["-d", device_path])
316-
.status();
317-
318-
match status {
319-
Ok(exit_status) if exit_status.success() => {
320-
// Log to systemd journal instead of stderr
321-
tracing::info!("Cleaned up leaked loopback device {}", device_path);
322-
std::process::exit(0);
323-
}
324-
Ok(_) => {
325-
tracing::error!("Failed to clean up loopback device {}", device_path);
326-
std::process::exit(1);
327-
}
328-
Err(e) => {
329-
tracing::error!("Error cleaning up loopback device {}: {}", device_path, e);
330-
std::process::exit(1);
331-
}
332-
}
333-
}
334-
335243
/// Parse key-value pairs from lsblk --pairs.
336244
/// Newer versions of lsblk support JSON but the one in CentOS 7 doesn't.
337245
fn split_lsblk_line(line: &str) -> HashMap<String, String> {

crates/lib/src/cli.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,6 @@ pub(crate) enum InternalsOpts {
462462
#[clap(allow_hyphen_values = true)]
463463
args: Vec<OsString>,
464464
},
465-
/// Loopback device cleanup helper (internal use only)
466-
LoopbackCleanupHelper {
467-
/// Device path to clean up
468-
#[clap(long)]
469-
device: String,
470-
},
471465
/// Invoked from ostree-ext to complete an installation.
472466
BootcInstallCompletion {
473467
/// Path to the sysroot
@@ -1269,9 +1263,6 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
12691263
let rootfs = &Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
12701264
crate::install::completion::run_from_ostree(rootfs, &sysroot, &stateroot).await
12711265
}
1272-
InternalsOpts::LoopbackCleanupHelper { device } => {
1273-
crate::blockdev::run_loopback_cleanup_helper(&device).await
1274-
}
12751266
#[cfg(feature = "rhsm")]
12761267
InternalsOpts::PublishRhsmFacts => crate::rhsm::publish_facts(&root).await,
12771268
},

crates/lib/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,3 @@ mod kernel;
3838

3939
#[cfg(feature = "rhsm")]
4040
mod rhsm;
41-
42-
// Re-export blockdev crate for internal use
43-
pub(crate) use bootc_blockdev as blockdev;

0 commit comments

Comments
 (0)