From e7ccd1a71eb9fe3425d92a0070eb6c6b60286cd4 Mon Sep 17 00:00:00 2001 From: Dmitry Savintsev Date: Tue, 10 Jan 2023 15:34:44 +0100 Subject: [PATCH] include and use clap only if needed Signed-off-by: Dmitry Savintsev --- cargo-generate.toml | 3 +++ pre-script.rhai | 2 ++ {{project-name}}-common/src/lib.rs | 2 +- {{project-name}}/Cargo.toml | 4 +++- {{project-name}}/src/main.rs | 26 +++++++++++--------------- 5 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 pre-script.rhai diff --git a/cargo-generate.toml b/cargo-generate.toml index b4c4e31..a977b33 100644 --- a/cargo-generate.toml +++ b/cargo-generate.toml @@ -73,3 +73,6 @@ regex = "^[a-z_]+$" type = "string" prompt = "Which lsm hook? (e.g file_open, task_alloc) You can find a list of hooks in include/linux/lsm_hooks.h in the kernel source tree." regex = "^[a-z_]+$" + +[hooks] +pre = ["pre-script.rhai"] diff --git a/pre-script.rhai b/pre-script.rhai new file mode 100644 index 0000000..65fa2b5 --- /dev/null +++ b/pre-script.rhai @@ -0,0 +1,2 @@ +let program_types_with_opts = ["classifier", "cgroup_skb", "cgroup_sockopt", "cgroup_sysctl", "sock_ops", "uprobe", "uretprobe", "xdp"]; +variable::set("program_types_with_opts", program_types_with_opts); diff --git a/{{project-name}}-common/src/lib.rs b/{{project-name}}-common/src/lib.rs index 4100b5e..1bc3620 100644 --- a/{{project-name}}-common/src/lib.rs +++ b/{{project-name}}-common/src/lib.rs @@ -12,4 +12,4 @@ pub struct SockKey { #[cfg(feature = "user")] unsafe impl aya::Pod for SockKey {} -{%- endif -%} +{%- endif %} diff --git a/{{project-name}}/Cargo.toml b/{{project-name}}/Cargo.toml index 14f7f80..18be02f 100644 --- a/{{project-name}}/Cargo.toml +++ b/{{project-name}}/Cargo.toml @@ -6,10 +6,12 @@ publish = false [dependencies] aya = { version = ">=0.11", features=["async_tokio"] } +{% if program_types_with_opts contains program_type -%} +clap = { version = "4.0", features = ["derive"] } +{% endif -%} aya-log = "0.1" {{project-name}}-common = { path = "../{{project-name}}-common", features=["user"] } anyhow = "1.0.42" -clap = { version = "4.0", features = ["derive"] } env_logger = "0.10" {%- if program_type == "uprobe" %} libc = "0.2" diff --git a/{{project-name}}/src/main.rs b/{{project-name}}/src/main.rs index 07a95fc..ebb3ad8 100644 --- a/{{project-name}}/src/main.rs +++ b/{{project-name}}/src/main.rs @@ -1,4 +1,3 @@ -use aya::{include_bytes_aligned, Bpf}; {% case program_type -%} {%- when "kprobe", "kretprobe" -%} use aya::programs::KProbe; @@ -41,38 +40,35 @@ use aya::programs::SocketFilter; {%- when "raw_tracepoint" -%} use aya::programs::RawTracePoint; {%- endcase %} +use aya::{include_bytes_aligned, Bpf}; use aya_log::BpfLogger; +{% if program_types_with_opts contains program_type -%} use clap::Parser; +{% endif -%} use log::{info, warn}; use tokio::signal; -{% case program_type %} -{%- when - "xdp", "classifier", "sock_ops", "cgroup_skb", "cgroup_sysctl", "cgroup_sockopt", "uprobe", "uretprobe" -%} +{% if program_types_with_opts contains program_type -%} #[derive(Debug, Parser)] struct Opt { - {% if program_type == "xdp" or program_type == "classifier" -%} +{%- if program_type == "xdp" or program_type == "classifier" %} #[clap(short, long, default_value = "eth0")] iface: String, - {%- elsif program_type == "sock_ops" or program_type == "cgroup_skb" or program_type == "cgroup_sysctl" or program_type == "cgroup_sockopt" -%} +{% elsif program_type == "sock_ops" or program_type == "cgroup_skb" or program_type == "cgroup_sysctl" or program_type == "cgroup_sockopt" %} #[clap(short, long, default_value = "/sys/fs/cgroup/unified")] cgroup_path: String, - {%- elsif program_type == "uprobe" or program_type == "uretprobe" -%} +{% elsif program_type == "uprobe" or program_type == "uretprobe" %} #[clap(short, long)] pid: Option - {%- endif %} +{% endif -%} } -{%- endcase %} +{% endif -%} #[tokio::main] async fn main() -> Result<(), anyhow::Error> { - {% case program_type %} - {%- when - "xdp", "classifier", "sock_ops", "cgroup_skb", "cgroup_sysctl", "cgroup_sockopt", "uprobe", "uretprobe" -%} +{%- if program_types_with_opts contains program_type %} let opt = Opt::parse(); - - {%- endcase %} - +{% endif %} env_logger::init(); // This will include your eBPF object file as raw bytes at compile-time and load it at