fn process_name_from_args()

in aziotd/src/main.rs [107:144]


fn process_name_from_args<I>(args: &mut I) -> Result<ProcessName, Error>
where
    I: Iterator,
    <I as Iterator>::Item: AsRef<std::ffi::OsStr>,
{
    let arg = args.next().ok_or_else(|| {
        ErrorKind::GetProcessName("could not extract process name from args".into())
    })?;

    // arg could be a single component like "aziot-certd", or a path that ends with "aziot-certd",
    // so parse it as a Path and get the last component. This does the right thing in either case.
    let arg = std::path::Path::new(&arg);
    let process_name = arg.file_name().ok_or_else(|| {
        ErrorKind::GetProcessName(
            format!(
                "could not extract process name from arg {:?}",
                arg.display(),
            )
            .into(),
        )
    })?;

    match process_name.to_str() {
        Some("aziot-certd") => Ok(ProcessName::Certd),
        Some("aziot-identityd") => Ok(ProcessName::Identityd),
        Some("aziot-keyd") => Ok(ProcessName::Keyd),
        Some("aziot-tpmd") => Ok(ProcessName::Tpmd),

        // The next arg is the process name
        #[cfg(debug_assertions)]
        Some("aziotd") => process_name_from_args(args),

        _ => Err(ErrorKind::GetProcessName(
            format!("unrecognized process name {process_name:?}").into(),
        )
        .into()),
    }
}