Skip to content

Commit

Permalink
perf: more &'static str conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Jan 26, 2025
1 parent 4f5a867 commit 5265b90
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/systemd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) use version::{KernelVersion, SystemdVersion};
const START_OPTION_OUTPUT_SNIPPET: &str = "-------- Start of suggested service options --------";
const END_OPTION_OUTPUT_SNIPPET: &str = "-------- End of suggested service options --------";

pub(crate) fn report_options(opts: Vec<options::OptionWithValue>) {
pub(crate) fn report_options(opts: Vec<options::OptionWithValue<&'static str>>) {
// Report (not through logging facility because we may need to parse it back from service logs)
println!("{START_OPTION_OUTPUT_SNIPPET}");
for opt in opts {
Expand Down
16 changes: 8 additions & 8 deletions src/systemd/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) struct OptionUpdater {
pub effect:
fn(&OptionValueEffect, &ProgramAction, &HardeningOptions) -> Option<OptionValueEffect>,
/// Generate new options from the new effect
pub options: fn(&OptionValueEffect, &HardeningOptions) -> Vec<OptionWithValue>,
pub options: fn(&OptionValueEffect, &HardeningOptions) -> Vec<OptionWithValue<&'static str>>,
}

/// Systemd option with its possibles values, and their effect
Expand Down Expand Up @@ -228,12 +228,12 @@ impl DenySyscalls {

/// A systemd option with a value, as would be present in a config file
#[derive(Debug)]
pub(crate) struct OptionWithValue {
pub name: String,
pub(crate) struct OptionWithValue<T> {
pub name: T,
pub value: OptionValue,
}

impl FromStr for OptionWithValue {
impl FromStr for OptionWithValue<String> {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -249,7 +249,7 @@ impl FromStr for OptionWithValue {
}
}

impl fmt::Display for OptionWithValue {
impl<T: fmt::Display> fmt::Display for OptionWithValue<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.value {
OptionValue::Boolean(value) => {
Expand Down Expand Up @@ -1195,12 +1195,12 @@ pub(crate) fn build_options(
OptionValueEffect::DenyWrite(PathDescription::Base { base, exceptions }) => {
vec![
OptionWithValue {
name: "ReadOnlyPaths".to_owned(),
name: "ReadOnlyPaths",
#[expect(clippy::unwrap_used)] // path is from our option, so unicode safe
value: OptionValue::String(base.to_str().unwrap().to_owned()),
},
OptionWithValue {
name: "ReadWritePaths".to_owned(),
name: "ReadWritePaths",
value: OptionValue::List {
values: merge_similar_paths(
exceptions,
Expand Down Expand Up @@ -1440,7 +1440,7 @@ pub(crate) fn build_options(
unreachable!();
};
vec![OptionWithValue {
name: "SocketBindDeny".to_owned(),
name: "SocketBindDeny",
value: OptionValue::List {
values: denied_na
.af
Expand Down
12 changes: 6 additions & 6 deletions src/systemd/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl OptionValueEffect {
/// A systemd option value and its effect, altered from original
#[derive(Debug)]
pub(crate) struct ChangedOptionValueDescription {
pub new_options: Vec<OptionWithValue>,
pub new_options: Vec<OptionWithValue<&'static str>>,
pub effect: OptionValueEffect,
}

Expand Down Expand Up @@ -165,7 +165,7 @@ pub(crate) fn resolve(
opts: &Vec<OptionDescription>,
actions: &[ProgramAction],
hardening_opts: &HardeningOptions,
) -> Vec<OptionWithValue> {
) -> Vec<OptionWithValue<&'static str>> {
let mut candidates = Vec::new();
for opt in opts {
// Options are in the less to most restrictive order,
Expand All @@ -174,7 +174,7 @@ pub(crate) fn resolve(
match &opt_value_desc.desc {
OptionEffect::None => {
candidates.push(OptionWithValue {
name: opt.name.to_owned(),
name: opt.name,
value: opt_value_desc.value.clone(),
});
break;
Expand All @@ -184,7 +184,7 @@ pub(crate) fn resolve(
{
ActionOptionEffectCompatibility::Compatible => {
candidates.push(OptionWithValue {
name: opt.name.to_owned(),
name: opt.name,
value: opt_value_desc.value.clone(),
});
break;
Expand Down Expand Up @@ -230,7 +230,7 @@ pub(crate) fn resolve(
match actions_compatible(opte, actions, None, hardening_opts) {
ActionOptionEffectCompatibility::Compatible => {
match nd.new_options.iter().at_most_one() {
Ok(Some(OptionWithValue { name, value: OptionValue::List { values: new_vals, .. } })) if name == opt.name => {
Ok(Some(OptionWithValue { name, value: OptionValue::List { values: new_vals, .. } })) if *name == opt.name => {
new_vals.clone_into(&mut cur_opt_vals);
},
e => unreachable!("{e:?}"),
Expand All @@ -250,7 +250,7 @@ pub(crate) fn resolve(
}
if !compatible_opts.is_empty() || value_if_empty.is_some() {
candidates.push(OptionWithValue {
name: opt.name.to_owned(),
name: opt.name,
value: OptionValue::List {
values: compatible_opts,
value_if_empty: *value_if_empty,
Expand Down
9 changes: 6 additions & 3 deletions src/systemd/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ impl Service {
Ok(())
}

pub(crate) fn add_hardening_fragment(&self, opts: Vec<OptionWithValue>) -> anyhow::Result<()> {
pub(crate) fn add_hardening_fragment(
&self,
opts: Vec<OptionWithValue<String>>,
) -> anyhow::Result<()> {
let fragment_path = self.fragment_path(HARDENING_FRAGMENT_NAME, true);
#[expect(clippy::unwrap_used)]
fs::create_dir_all(fragment_path.parent().unwrap())?;
Expand Down Expand Up @@ -234,7 +237,7 @@ impl Service {
Ok(())
}

pub(crate) fn profiling_result(&self) -> anyhow::Result<Vec<OptionWithValue>> {
pub(crate) fn profiling_result(&self) -> anyhow::Result<Vec<OptionWithValue<String>>> {
// Start journalctl process
let mut child = Command::new("journalctl")
.args([
Expand Down Expand Up @@ -283,7 +286,7 @@ impl Service {
let opts = snippet_lines[1..snippet_lines.len() - 1]
.iter()
.rev()
.map(|l| l.parse::<OptionWithValue>())
.map(|l| l.parse::<OptionWithValue<String>>())
.collect::<anyhow::Result<_>>()?;

// Stop journalctl
Expand Down

0 comments on commit 5265b90

Please sign in to comment.