From a476f7daf17c1e48400f2496521aabd5a003d6af Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Mon, 23 Oct 2023 02:19:38 +0900 Subject: [PATCH] helpers, types: Switch to panic for errors We're not doing explicit error handling, since the kernel is in charge of safety if we crash. Just panic on anything. Signed-off-by: Hector Martin --- src/helpers.rs | 38 +++++++------------------------------- src/main.rs | 7 ++++--- src/types.rs | 4 +--- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 3bb0846..01c5400 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -5,23 +5,11 @@ use alsa; use alsa::mixer::MilliBel; use configparser::ini::Ini; -/** - Failsafe: Limit speaker volume massively and bail. - - TODO: enable TAS safe mode with IOCTL. -*/ -pub fn fail() { - println!("A catastrophic error has occurred."); - std::process::exit(1); -} - pub fn open_card(card: &str) -> alsa::ctl::Ctl { let ctldev: alsa::ctl::Ctl = match alsa::ctl::Ctl::new(card, false) { Ok(ctldev) => ctldev, Err(e) => { - println!("{}: Could not open sound card! Error: {}", card, e); - fail(); - std::process::exit(1); + panic!("{}: Could not open sound card! Error: {}", card, e); } }; @@ -104,9 +92,7 @@ pub fn new_elemvalue(t: alsa::ctl::ElemType) -> alsa::ctl::ElemValue { let val = match alsa::ctl::ElemValue::new(t) { Ok(val) => val, Err(_e) => { - println!("Could not open a handle to an element!"); - fail(); - std::process::exit(1); + panic!("Could not open a handle to an element!"); } }; @@ -121,12 +107,10 @@ pub fn read_ev(card: &alsa::ctl::Ctl, ev: &mut alsa::ctl::ElemValue, name: &str) // alsa:Result<()> Ok(val) => val, Err(e) => { - println!( + panic!( "Could not read elem value {}. alsa-lib error: {:?}", name, e ); - fail(); - std::process::exit(1); } }; } @@ -139,12 +123,10 @@ pub fn write_ev(card: &alsa::ctl::Ctl, ev: &alsa::ctl::ElemValue, name: &str) { // alsa:Result<()> Ok(val) => val, Err(e) => { - println!( + panic!( "Could not write elem value {}. alsa-lib error: {:?}", name, e ); - fail(); - std::process::exit(1); } }; } @@ -157,9 +139,7 @@ pub fn lock_el(card: &alsa::ctl::Ctl, el: &alsa::ctl::ElemId, name: &str) { // alsa:Result<()> Ok(val) => val, Err(e) => { - println!("Could not lock elem {}. alsa-lib error: {:?}", name, e); - fail(); - std::process::exit(1); + panic!("Could not lock elem {}. alsa-lib error: {:?}", name, e); } }; } @@ -168,12 +148,10 @@ pub fn int_to_db(card: &alsa::ctl::Ctl, id: &alsa::ctl::ElemId, val: i32) -> Mil let db = match card.convert_to_db(id, val.into()) { Ok(inner) => inner, Err(e) => { - println!( + panic!( "Could not convert val {} to dB! alsa-lib error: {:?}", val, e ); - fail(); - std::process::exit(1); } }; @@ -185,12 +163,10 @@ pub fn db_to_int(card: &alsa::ctl::Ctl, id: &alsa::ctl::ElemId, val: f32) -> i32 let new_int = match card.convert_from_db(id, mb, alsa::Round::Floor) { Ok(inner) => inner as i32, Err(e) => { - println!( + panic!( "Could not convert MilliBel {:?} to int! alsa-lib error: {:?}", val, e ); - fail(); - std::process::exit(1); } }; diff --git a/src/main.rs b/src/main.rs index 845a143..843522b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,10 @@ // (C) 2022 The Asahi Linux Contributors /*! Handles speaker safety on Apple Silicon machines. This code is designed to - fail safe. The speaker should not be enabled until this daemon has successfully - initialised. If at any time we run into an unrecoverable error (we shouldn't), - we gracefully bail and use an IOCTL to shut off the speakers. + fail safe. The kernel keeps the speakers capped at a low volume level until + this daemon initializes. If at any time we run into an unrecoverable error + or a timeout, we panic and let the kernel put the speakers back into a safe + state. */ use std::collections::BTreeMap; use std::fs; diff --git a/src/types.rs b/src/types.rs index f5b654c..15f4c72 100644 --- a/src/types.rs +++ b/src/types.rs @@ -148,9 +148,7 @@ impl Mixer { match self.level.val.set_integer(0, new_val) { Some(_) => {} None => { - println!("Could not set level for {}", self.drv); - helpers::fail(); - std::process::exit(1); + panic!("Could not set level for {}", self.drv); } };