| 1 |
// SPDX-License-Identifier: Apache-2.0 or MIT |
| 2 |
// |
| 3 |
// Copyright 2021 Sony Group Corporation |
| 4 |
// |
| 5 |
|
| 6 |
use superrust-analyzer cargo libseccomp 0.4.0 crate/::cvtrust-analyzer cargo libseccomp 0.4.0 cvt().; |
| 7 |
use craterust-analyzer cargo libseccomp 0.4.0 crate/::errorrust-analyzer cargo libseccomp 0.4.0 error/::{Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#, SeccompErrorrust-analyzer cargo libseccomp 0.4.0 error/SeccompError#}; |
| 8 |
use craterust-analyzer cargo libseccomp 0.4.0 crate/::versionrust-analyzer cargo libseccomp 0.4.0 version/::ensure_supported_versionrust-analyzer cargo libseccomp 0.4.0 version/ensure_supported_version().; |
| 9 |
use craterust-analyzer cargo libseccomp 0.4.0 crate/::{check_versionrust-analyzer cargo libseccomp 0.4.0 version/check_version()., ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#}; |
| 10 |
use libseccomp_sysrust-analyzer cargo libseccomp-sys 0.3.0 crate/::*; |
| 11 |
|
| 12 |
/// Sets the API level forcibly. |
| 13 |
/// |
| 14 |
/// General use of this function is strongly discouraged. |
| 15 |
/// See the [`seccomp_api_get(3)`] man page for details on available API levels. |
| 16 |
/// |
| 17 |
/// [`seccomp_api_get(3)`]: https://www.man7.org/linux/man-pages/man3/seccomp_api_get.3.html |
| 18 |
/// |
| 19 |
/// This function corresponds to |
| 20 |
/// [`seccomp_api_set`](https://www.man7.org/linux/man-pages/man3/seccomp_api_set.3.html). |
| 21 |
/// |
| 22 |
/// # Arguments |
| 23 |
/// |
| 24 |
/// * `level` - The API level |
| 25 |
/// |
| 26 |
/// # Errors |
| 27 |
/// |
| 28 |
/// If the API level can not be detected due to the library being older than v2.4.0, |
| 29 |
/// an error will be returned. |
| 30 |
/// |
| 31 |
/// # Examples |
| 32 |
/// |
| 33 |
/// ``` |
| 34 |
/// # use libseccomp::*; |
| 35 |
/// set_api(1)?; |
| 36 |
/// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 37 |
/// ``` |
| 38 |
pub fn set_apirust-analyzer cargo libseccomp 0.4.0 api/set_api().(level: u32) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<()> { |
| 39 |
cvtrust-analyzer cargo libseccomp 0.4.0 cvt().(unsafe { seccomp_api_setrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_api_set().(level) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 40 |
|
| 41 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 42 |
} |
| 43 |
|
| 44 |
/// Gets the API level supported by the system. |
| 45 |
/// |
| 46 |
/// See the [`seccomp_api_get(3)`] man page for details on available API levels. |
| 47 |
/// |
| 48 |
/// [`seccomp_api_get(3)`]: https://www.man7.org/linux/man-pages/man3/seccomp_api_get.3.html |
| 49 |
/// |
| 50 |
/// This function corresponds to |
| 51 |
/// [`seccomp_api_get`](https://www.man7.org/linux/man-pages/man3/seccomp_api_get.3.html). |
| 52 |
/// |
| 53 |
/// # Examples |
| 54 |
/// |
| 55 |
/// ``` |
| 56 |
/// # use libseccomp::*; |
| 57 |
/// set_api(1)?; |
| 58 |
/// assert_eq!(get_api(), 1); |
| 59 |
/// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 60 |
/// ``` |
| 61 |
pub fn get_apirust-analyzer cargo libseccomp 0.4.0 api/get_api().() -> u32 { |
| 62 |
unsafe { seccomp_api_getrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_api_get().() } |
| 63 |
} |
| 64 |
|
| 65 |
/// Checks that both the libseccomp API level and the libseccomp version being |
| 66 |
/// used are equal to or greater than the specified API level and version. |
| 67 |
/// |
| 68 |
/// This function returns `Ok(true)` if both the libseccomp API level and the |
| 69 |
/// libseccomp version are equal to or greater than the specified API level and |
| 70 |
/// version, `Ok(false)` otherwise. |
| 71 |
/// |
| 72 |
/// # Arguments |
| 73 |
/// |
| 74 |
/// * `min_level` - The libseccomp API level you want to check |
| 75 |
/// * `expected` - The libseccomp version you want to check |
| 76 |
/// |
| 77 |
/// # Errors |
| 78 |
/// |
| 79 |
/// If an issue is encountered getting the current API level or version, |
| 80 |
/// an error will be returned. |
| 81 |
/// |
| 82 |
/// # Examples |
| 83 |
/// |
| 84 |
/// ``` |
| 85 |
/// # use libseccomp::*; |
| 86 |
/// assert!(check_api(3, ScmpVersion::from((2, 4, 0)))?); |
| 87 |
/// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 88 |
/// ``` |
| 89 |
pub fn check_apirust-analyzer cargo libseccomp 0.4.0 api/check_api().(min_level: u32, expected: ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<bool> { |
| 90 |
let level = get_apirust-analyzer cargo libseccomp 0.4.0 api/get_api().(); |
| 91 |
|
| 92 |
if level rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().min_level && check_versionrust-analyzer cargo libseccomp 0.4.0 version/check_version().(expected)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 93 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true) |
| 94 |
} else { |
| 95 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/// Ensures that both the libseccomp API level and the libseccomp version are |
| 100 |
/// equal to or greater than the specified API level and version. |
| 101 |
/// |
| 102 |
/// # Arguments |
| 103 |
/// |
| 104 |
/// * `msg` - An arbitrary non-empty operation description, used as a part |
| 105 |
/// of the error message returned. |
| 106 |
/// * `min_level` - The libseccomp API level you want to check |
| 107 |
/// * `expected` - The libseccomp version you want to check |
| 108 |
/// |
| 109 |
/// # Errors |
| 110 |
/// |
| 111 |
/// If the libseccomp API level and the libseccomp version being used are less than |
| 112 |
/// the specified version, an error will be returned. |
| 113 |
pub(craterust-analyzer cargo libseccomp 0.4.0 crate/) fn ensure_supported_apirust-analyzer cargo libseccomp 0.4.0 api/ensure_supported_api().(msg: &str, min_level: u32, expected: ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<()> { |
| 114 |
let level = get_apirust-analyzer cargo libseccomp 0.4.0 api/get_api().(); |
| 115 |
|
| 116 |
if level rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]ge().min_level { |
| 117 |
ensure_supported_versionrust-analyzer cargo libseccomp 0.4.0 version/ensure_supported_version().(msg, expected) |
| 118 |
} else { |
| 119 |
let current = ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#::currentrust-analyzer cargo libseccomp 0.4.0 version/impl#[ScmpVersion]current().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 120 |
Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SeccompErrorrust-analyzer cargo libseccomp 0.4.0 error/SeccompError#::with_msgrust-analyzer cargo libseccomp 0.4.0 error/impl#[SeccompError]with_msg().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 121 |
"{} requires libseccomp >= {} and API level >= {} (current version: {}, API level: {})", |
| 122 |
msg, expected, min_level, current, level |
| 123 |
))) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[cfg(test)] |
| 128 |
mod testsrust-analyzer cargo libseccomp 0.4.0 api/tests/ { |
| 129 |
use superrust-analyzer cargo libseccomp 0.4.0 api/::*; |
| 130 |
|
| 131 |
#[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| •132 |
#[cfg(not(target_arch = "loong64"))] |
| 133 |
fn test_ensure_supported_apirust-analyzer cargo libseccomp 0.4.0 api/tests/test_ensure_supported_api().() { |
| 134 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(ensure_supported_apirust-analyzer cargo libseccomp 0.4.0 api/ensure_supported_api().("test", 3, ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#::fromrust-analyzer cargo libseccomp 0.4.0 version/impl#[ScmpVersion][`From<(u32, u32, u32)>`]from().((2, 4, 0))).is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 135 |
assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(ensure_supported_apirust-analyzer cargo libseccomp 0.4.0 api/ensure_supported_api().("test", 100, ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#::fromrust-analyzer cargo libseccomp 0.4.0 version/impl#[ScmpVersion][`From<(u32, u32, u32)>`]from().((2, 4, 0))).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 136 |
} |
| 137 |
} |