-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sadly the loopdev crates is unmaintained and this patch needs some upstream functionality beyond latest crates.io release.
- Loading branch information
Felix Obenhuber
committed
Sep 12, 2023
1 parent
514334a
commit 6dad08d
Showing
7 changed files
with
630 additions
and
122 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,102 @@ | ||
fn main() { | ||
use anyhow::{Context, Result}; | ||
use std::{env, path::PathBuf}; | ||
|
||
use bindgen::Builder; | ||
|
||
fn main() -> Result<()> { | ||
loopdev()?; | ||
|
||
#[cfg(feature = "seccomp")] | ||
generate_seccomp(); | ||
generate_seccomp()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "seccomp")] | ||
fn generate_seccomp() { | ||
use std::{env, fs, io::Write, path}; | ||
fn generate_seccomp() -> Result<()> { | ||
use std::{fs, io::Write}; | ||
|
||
fn generate() -> anyhow::Result<()> { | ||
let lines = bindgen::Builder::default() | ||
.header_contents("syscall.h", "#include <sys/syscall.h>") | ||
.allowlist_var("SYS_[0-9a-zA-Z_]+") | ||
.generate() | ||
.expect("failed to generate syscall bindings") | ||
.to_string(); | ||
let lines: Vec<&str> = lines | ||
.lines() | ||
.filter(|s| s.starts_with("pub const SYS_")) | ||
.collect(); | ||
let lines = bindgen::Builder::default() | ||
.header_contents("syscall.h", "#include <sys/syscall.h>") | ||
.allowlist_var("SYS_[0-9a-zA-Z_]+") | ||
.generate() | ||
.context("failed to generate syscall bindings")? | ||
.to_string(); | ||
let lines: Vec<&str> = lines | ||
.lines() | ||
.filter(|s| s.starts_with("pub const SYS_")) | ||
.collect(); | ||
|
||
let out_path = path::PathBuf::from(env::var("OUT_DIR")?); | ||
let mut f = fs::File::create(out_path.join("syscall_bindings.rs"))?; | ||
let out_path = PathBuf::from(env::var("OUT_DIR")?); | ||
let mut f = fs::File::create(out_path.join("syscall_bindings.rs"))?; | ||
|
||
f.write_all(lines.join("\n").as_bytes())?; | ||
writeln!(f)?; | ||
writeln!(f)?; | ||
f.write_all(lines.join("\n").as_bytes())?; | ||
writeln!(f)?; | ||
writeln!(f)?; | ||
|
||
// Write static map that associates syscall strings with syscall numbers | ||
writeln!(f, "lazy_static::lazy_static! {{")?; | ||
writeln!( | ||
f, | ||
" pub(super) static ref SYSCALL_MAP: std::collections::HashMap<&'static str, u32> = {{" | ||
)?; | ||
writeln!(f, " let mut map = std::collections::HashMap::new();")?; | ||
lines.iter().try_for_each(|l| { | ||
let mut split = l.split_ascii_whitespace(); | ||
let var = split.nth(2).unwrap().trim_end_matches(':'); | ||
let name = var.replace("SYS_", ""); | ||
writeln!(f, " map.insert(\"{name}\", {var});")?; | ||
std::io::Result::Ok(()) | ||
})?; | ||
writeln!(f, " map")?; | ||
writeln!(f, " }};")?; | ||
writeln!(f, "}}")?; | ||
// Write static map that associates syscall strings with syscall numbers | ||
writeln!(f, "lazy_static::lazy_static! {{")?; | ||
writeln!( | ||
f, | ||
" pub(super) static ref SYSCALL_MAP: std::collections::HashMap<&'static str, u32> = {{" | ||
)?; | ||
writeln!(f, " let mut map = std::collections::HashMap::new();")?; | ||
lines.iter().try_for_each(|l| { | ||
let mut split = l.split_ascii_whitespace(); | ||
let var = split.nth(2).unwrap().trim_end_matches(':'); | ||
let name = var.replace("SYS_", ""); | ||
writeln!(f, " map.insert(\"{name}\", {var});")?; | ||
std::io::Result::Ok(()) | ||
})?; | ||
writeln!(f, " map")?; | ||
writeln!(f, " }};")?; | ||
writeln!(f, "}}")?; | ||
|
||
let out_path = std::path::PathBuf::from(env::var("OUT_DIR")?); | ||
bindgen::Builder::default() | ||
.header_contents( | ||
"seccomp.h", | ||
r#"#include <linux/seccomp.h> | ||
let out_path = PathBuf::from(env::var("OUT_DIR")?); | ||
bindgen::Builder::default() | ||
.header_contents( | ||
"seccomp.h", | ||
r#"#include <linux/seccomp.h> | ||
#include <linux/filter.h> | ||
#include <linux/audit.h>"#, | ||
) | ||
.layout_tests(false) | ||
.allowlist_type("seccomp_data") | ||
.allowlist_type("sock_fprog") | ||
.allowlist_var("BPF_ABS") | ||
.allowlist_var("BPF_AND") | ||
.allowlist_var("BPF_ALU") | ||
.allowlist_var("BPF_IMM") | ||
.allowlist_var("BPF_IND") | ||
.allowlist_var("BPF_JEQ") | ||
.allowlist_var("BPF_JMP") | ||
.allowlist_var("BPF_NEG") | ||
.allowlist_var("BPF_K") | ||
.allowlist_var("BPF_LD") | ||
.allowlist_var("BPF_LDX") | ||
.allowlist_var("BPF_MEM") | ||
.allowlist_var("BPF_OR") | ||
.allowlist_var("BPF_RET") | ||
.allowlist_var("BPF_ST") | ||
.allowlist_var("BPF_W") | ||
.allowlist_var("BPF_MAXINSNS") | ||
.allowlist_var("AUDIT_ARCH_X86_64") | ||
.allowlist_var("AUDIT_ARCH_AARCH64") | ||
.generate() | ||
.expect("failed to generate seccomp bindings") | ||
.write_to_file(out_path.join("seccomp_bindings.rs"))?; | ||
Ok(()) | ||
} | ||
) | ||
.layout_tests(false) | ||
.allowlist_type("seccomp_data") | ||
.allowlist_type("sock_fprog") | ||
.allowlist_var("BPF_ABS") | ||
.allowlist_var("BPF_AND") | ||
.allowlist_var("BPF_ALU") | ||
.allowlist_var("BPF_IMM") | ||
.allowlist_var("BPF_IND") | ||
.allowlist_var("BPF_JEQ") | ||
.allowlist_var("BPF_JMP") | ||
.allowlist_var("BPF_NEG") | ||
.allowlist_var("BPF_K") | ||
.allowlist_var("BPF_LD") | ||
.allowlist_var("BPF_LDX") | ||
.allowlist_var("BPF_MEM") | ||
.allowlist_var("BPF_OR") | ||
.allowlist_var("BPF_RET") | ||
.allowlist_var("BPF_ST") | ||
.allowlist_var("BPF_W") | ||
.allowlist_var("BPF_MAXINSNS") | ||
.allowlist_var("AUDIT_ARCH_X86_64") | ||
.allowlist_var("AUDIT_ARCH_AARCH64") | ||
.generate() | ||
.context("failed to generate seccomp bindings")? | ||
.write_to_file(out_path.join("seccomp_bindings.rs"))?; | ||
Ok(()) | ||
} | ||
|
||
fn loopdev() -> Result<()> { | ||
let out_path = PathBuf::from(env::var("OUT_DIR")?); | ||
let bindings = Builder::default() | ||
.header_contents("loopdev.h", "#include <linux/loop.h>") | ||
.derive_default(true) | ||
.generate() | ||
.context("failed to generated loopdev bindings")?; | ||
|
||
generate().expect("failed to generate seccomp bindings"); | ||
bindings | ||
.write_to_file(out_path.join("loopdev.rs")) | ||
.context("failed to generated loopdev bindings") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Michael Daffin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Oops, something went wrong.