Debian Codegraph @

src/api.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // SPDX-License-Identifier: Apache-2.0 or MIT
2 //
3 // Copyright 2021 Sony Group Corporation
4 //
5
6 use super::cvt;
7 use crate::error::{Result, SeccompError};
8 use crate::version::ensure_supported_version;
9 use crate::{check_version, ScmpVersion};
10 use libseccomp_sys::*;
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_api(level: u32) -> Result<()> {
39 cvt(unsafe { seccomp_api_set(level) })?;
40
41 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_api() -> u32 {
62 unsafe { 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_api(min_level: u32, expected: ScmpVersion) -> Result<bool> {
90 let level = get_api();
91
92 if level >= min_level && check_version(expected)? {
93 Ok(true)
94 } else {
95 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(crate) fn ensure_supported_api(msg: &str, min_level: u32, expected: ScmpVersion) -> Result<()> {
114 let level = get_api();
115
116 if level >= min_level {
117 ensure_supported_version(msg, expected)
118 } else {
119 let current = ScmpVersion::current()?;
120 Err(SeccompError::with_msg(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 tests {
129 use super::*;
130
131 #[test]
•132 #[cfg(not(target_arch = "loong64"))]
133 fn test_ensure_supported_api() {
134 assert!(ensure_supported_api("test", 3, ScmpVersion::from((2, 4, 0))).is_ok());
135 assert!(ensure_supported_api("test", 100, ScmpVersion::from((2, 4, 0))).is_err());
136 }
137 }