mirror of
https://github.com/joleuger/vuinputd.git
synced 2026-07-18 00:45:07 +00:00
Improved action handlers; now also use anyhow
This commit is contained in:
parent
045807e848
commit
21349c40b9
6 changed files with 51 additions and 17 deletions
|
|
@ -2,6 +2,8 @@
|
|||
//
|
||||
// Author: Johannes Leupolz <dev@leupolz.eu>
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
|
@ -12,12 +14,20 @@ pub enum Action {
|
|||
path: String,
|
||||
major: u32,
|
||||
minor: u32,
|
||||
mode: u32,
|
||||
},
|
||||
|
||||
#[serde(rename = "announce-via-netlink")]
|
||||
AnnounceViaNetlink { message: String },
|
||||
#[serde(rename = "emit-udev-event")]
|
||||
EmitUdevEvent {
|
||||
netlink_message: HashMap<String, String>,
|
||||
runtime_data: Option<String>,
|
||||
major: u32,
|
||||
minor: u32,
|
||||
},
|
||||
|
||||
#[serde(rename = "remove-device")]
|
||||
RemoveDevice { path: String },
|
||||
RemoveDevice {
|
||||
path: String,
|
||||
major: u32,
|
||||
minor: u32,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@
|
|||
// Author: Johannes Leupolz <dev@leupolz.eu>
|
||||
|
||||
use super::action::Action;
|
||||
use super::input_device;
|
||||
use super::netlink_message;
|
||||
use super::runtime_data;
|
||||
|
||||
pub fn handle_cli_action(json: String) -> i32 {
|
||||
let action: Action = serde_json::from_str(&json).expect("invalid action JSON");
|
||||
|
|
@ -12,10 +15,29 @@ pub fn handle_cli_action(json: String) -> i32 {
|
|||
0
|
||||
}
|
||||
|
||||
fn handle_action(action: Action) -> Result<(), String> {
|
||||
fn handle_action(action: Action) -> anyhow::Result<()> {
|
||||
match action {
|
||||
Action::MknodDevice { .. } => Ok(()),
|
||||
Action::AnnounceViaNetlink { .. } => Ok(()),
|
||||
Action::RemoveDevice { .. } => Ok(()),
|
||||
Action::MknodDevice { path, major, minor } => {
|
||||
input_device::ensure_input_device(path, major.into(), minor.into())?;
|
||||
Ok(())
|
||||
}
|
||||
Action::EmitUdevEvent {
|
||||
netlink_message,
|
||||
runtime_data,
|
||||
major,
|
||||
minor,
|
||||
} => {
|
||||
netlink_message::send_udev_monitor_message_with_properties(netlink_message);
|
||||
runtime_data::ensure_udev_structure()?;
|
||||
match runtime_data {
|
||||
Some(data) => runtime_data::write_udev_data(&data, major.into(), minor.into())?,
|
||||
None => runtime_data::delete_udev_data(major.into(), minor.into())?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Action::RemoveDevice { path, major, minor } => {
|
||||
input_device::remove_input_device(path, major.into(), minor.into())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@
|
|||
//
|
||||
// Author: Johannes Leupolz <dev@leupolz.eu>
|
||||
|
||||
use anyhow::anyhow;
|
||||
use nix::sys::stat::{makedev, mknod, stat, Mode, SFlag};
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn ensure_input_device(dev_path: String, major: u64, minor: u64) -> Result<(), Box<dyn Error>> {
|
||||
pub fn ensure_input_device(dev_path: String, major: u64, minor: u64) -> anyhow::Result<()> {
|
||||
let input_dir = Path::new("/dev/input");
|
||||
// Create directory like `mkdir -p`
|
||||
if !input_dir.exists() {
|
||||
|
|
@ -70,23 +70,25 @@ pub fn ensure_input_device(dev_path: String, major: u64, minor: u64) -> Result<(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_input_device(dev_path: String, major: u64, minor: u64) -> Result<(), Box<dyn Error>> {
|
||||
pub fn remove_input_device(dev_path: String, major: u64, minor: u64) -> anyhow::Result<()> {
|
||||
let path = Path::new(&dev_path);
|
||||
let expected_dev = makedev(major, minor);
|
||||
|
||||
// --- Step 1: Ensure it is the correct device ---
|
||||
if !path.exists() {
|
||||
return Err("Device does not exist".into());
|
||||
return Err(anyhow!("Device does not exist"));
|
||||
}
|
||||
match stat(path) {
|
||||
Ok(st) => {
|
||||
let is_char = (st.st_mode & libc::S_IFMT as u32) == libc::S_IFCHR as u32;
|
||||
let dev_ok = st.st_rdev == expected_dev;
|
||||
if !(is_char && dev_ok) {
|
||||
return Err("Device that should be deleted has wrong major and minor".into());
|
||||
return Err(anyhow!(
|
||||
"Device that should be deleted has wrong major and minor"
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(_x) => return Err("Could not execute stat on device file".into()),
|
||||
Err(_x) => return Err(anyhow!("Could not execute stat on device file")),
|
||||
}
|
||||
|
||||
let _ = fs::remove_file(path);
|
||||
|
|
@ -4,6 +4,6 @@
|
|||
|
||||
pub mod action;
|
||||
pub mod handle_action;
|
||||
pub mod mknod_input_device;
|
||||
pub mod input_device;
|
||||
pub mod netlink_message;
|
||||
pub mod runtime_data;
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use log::debug;
|
|||
|
||||
use crate::{
|
||||
actions::{
|
||||
mknod_input_device::ensure_input_device,
|
||||
input_device::ensure_input_device,
|
||||
netlink_message::send_udev_monitor_message_with_properties,
|
||||
runtime_data::{ensure_udev_structure, read_udev_data, write_udev_data},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ use log::debug;
|
|||
|
||||
use crate::{
|
||||
actions::{
|
||||
mknod_input_device::remove_input_device,
|
||||
input_device::remove_input_device,
|
||||
netlink_message::send_udev_monitor_message_with_properties, runtime_data::delete_udev_data,
|
||||
},
|
||||
job_engine::job::{Job, JobTarget},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue