|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + |
1 | 3 | //! Library for Bear interception via LD_PRELOAD |
2 | 4 | //! |
3 | 5 | //! This library provides system capability checks at build time. |
|
19 | 21 | //! fn fallback_exec() { /* fallback implementation */ } |
20 | 22 | //! ``` |
21 | 23 |
|
22 | | -use std::ffi::CStr; |
23 | | -use std::os::raw::{c_char, c_int}; |
| 24 | +// Only include Linux implementation when building for Linux |
| 25 | +#[cfg(target_os = "linux")] |
| 26 | +mod implementation; |
| 27 | + |
| 28 | +// Re-export Linux implementations when on Linux |
| 29 | +#[cfg(target_os = "linux")] |
| 30 | +pub use implementation::*; |
24 | 31 |
|
25 | 32 | /// Version information for the library |
26 | 33 | #[no_mangle] |
27 | 34 | pub static LIBEAR_VERSION: &[u8; 6] = b"4.0.0\0"; |
28 | 35 |
|
29 | | -/// Intercepted open function |
30 | | -/// |
31 | | -/// # Safety |
32 | | -/// |
33 | | -/// This is an FFI function intended for LD_PRELOAD interception. |
34 | | -#[no_mangle] |
35 | | -pub unsafe extern "C" fn open(path: *const c_char, flags: c_int, mode: libc::mode_t) -> c_int { |
36 | | - // Log the file being opened if logging is initialized |
37 | | - if !path.is_null() { |
38 | | - if let Ok(path_str) = CStr::from_ptr(path).to_str() { |
39 | | - log::debug!("libear: open called for path: {}", path_str); |
40 | | - |
41 | | - // Here we could implement additional logic for interception |
42 | | - // For example, recording file access for compilation database |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - // Call the real open function |
47 | | - libc::open(path, flags, mode) |
48 | | -} |
49 | | - |
50 | | -/// Intercepted execve function |
51 | | -/// |
52 | | -/// # Safety |
53 | | -/// |
54 | | -/// This is an FFI function intended for LD_PRELOAD interception. |
55 | | -#[no_mangle] |
56 | | -pub unsafe extern "C" fn execve( |
57 | | - path: *const c_char, |
58 | | - argv: *const *const c_char, |
59 | | - envp: *const *const c_char, |
60 | | -) -> c_int { |
61 | | - // Log the process being executed |
62 | | - if !path.is_null() { |
63 | | - if let Ok(path_str) = CStr::from_ptr(path).to_str() { |
64 | | - log::info!("libear: intercepted execution of: {}", path_str); |
65 | | - |
66 | | - // Here we could implement command interception logic |
67 | | - // For example, recording compiler invocations |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - // Call the real execve function |
72 | | - libc::execve(path, argv, envp) |
73 | | -} |
74 | | - |
75 | 36 | #[cfg(test)] |
76 | 37 | mod tests { |
77 | 38 | use super::*; |
| 39 | + use std::ffi::CStr; |
| 40 | + use std::os::raw::c_char; |
78 | 41 |
|
79 | 42 | #[test] |
80 | 43 | fn test_version() { |
|
0 commit comments