Skip to content

Commit 394dd7d

Browse files
authored
Support init_module etc. in the libc backend. (#1018)
And, fix the return types of `init_module` and friends to be `io::Result<()>`, as they can succeed and return.
1 parent 8edaeca commit 394dd7d

File tree

7 files changed

+83
-32
lines changed

7 files changed

+83
-32
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ jobs:
437437
rustup target add ${{ matrix.target }}
438438
if: matrix.target != ''
439439

440-
- uses: actions/cache@v3
440+
- uses: actions/cache@v4
441441
with:
442442
path: ${{ runner.tool_cache }}/qemu
443443
key: qemu-${{ matrix.target }}-${{ env.QEMU_BUILD_VERSION }}-patched
@@ -573,7 +573,7 @@ jobs:
573573
rustup target add ${{ matrix.target }}
574574
if: matrix.target != ''
575575

576-
- uses: actions/cache@v3
576+
- uses: actions/cache@v4
577577
with:
578578
path: ${{ runner.tool_cache }}/qemu
579579
key: qemu-${{ matrix.target }}-${{ env.QEMU_BUILD_VERSION }}-patched
@@ -665,7 +665,7 @@ jobs:
665665
rustup target add ${{ matrix.target }}
666666
if: matrix.target != ''
667667

668-
- uses: actions/cache@v3
668+
- uses: actions/cache@v4
669669
with:
670670
path: ${{ runner.tool_cache }}/qemu
671671
key: qemu-${{ matrix.target }}-${{ env.QEMU_BUILD_VERSION }}-patched

src/backend/libc/system/syscalls.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ use crate::backend::c;
66
use crate::backend::conv::ret_infallible;
77
#[cfg(target_os = "linux")]
88
use crate::system::RebootCommand;
9-
#[cfg(linux_kernel)]
10-
use crate::system::Sysinfo;
119
use core::mem::MaybeUninit;
10+
#[cfg(linux_kernel)]
11+
use {
12+
crate::backend::conv::c_str, crate::fd::BorrowedFd, crate::ffi::CStr, crate::system::Sysinfo,
13+
};
1214
#[cfg(not(any(
1315
target_os = "emscripten",
1416
target_os = "espidf",
@@ -65,3 +67,55 @@ pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> {
6567
pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
6668
unsafe { ret(c::reboot(cmd as i32)) }
6769
}
70+
71+
#[cfg(linux_kernel)]
72+
#[inline]
73+
pub(crate) fn init_module(image: &[u8], param_values: &CStr) -> io::Result<()> {
74+
syscall! {
75+
fn init_module(
76+
module_image: *const c::c_void,
77+
len: c::c_ulong,
78+
param_values: *const c::c_char
79+
) via SYS_init_module -> c::c_int
80+
}
81+
82+
unsafe {
83+
ret(init_module(
84+
image.as_ptr().cast(),
85+
image.len() as _,
86+
c_str(param_values),
87+
))
88+
}
89+
}
90+
91+
#[cfg(linux_kernel)]
92+
#[inline]
93+
pub(crate) fn finit_module(
94+
fd: BorrowedFd<'_>,
95+
param_values: &CStr,
96+
flags: c::c_int,
97+
) -> io::Result<()> {
98+
use crate::fd::AsRawFd;
99+
100+
syscall! {
101+
fn finit_module(
102+
fd: c::c_int,
103+
param_values: *const c::c_char,
104+
flags: c::c_int
105+
) via SYS_finit_module -> c::c_int
106+
}
107+
108+
unsafe { ret(finit_module(fd.as_raw_fd(), c_str(param_values), flags)) }
109+
}
110+
111+
#[cfg(linux_kernel)]
112+
#[inline]
113+
pub(crate) fn delete_module(name: &CStr, flags: c::c_int) -> io::Result<()> {
114+
syscall! {
115+
fn delete_module(
116+
name: *const c::c_char,
117+
flags: c::c_int
118+
) via SYS_delete_module -> c::c_int
119+
}
120+
unsafe { ret(delete_module(c_str(name), flags)) }
121+
}

src/backend/linux_raw/conv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use super::c;
1717
use super::fd::{AsRawFd, BorrowedFd, FromRawFd, RawFd};
18-
#[cfg(any(feature = "event", feature = "runtime"))]
18+
#[cfg(any(feature = "event", feature = "runtime", feature = "system"))]
1919
use super::io::errno::try_decode_error;
2020
#[cfg(target_pointer_width = "64")]
2121
use super::io::errno::try_decode_u64;
@@ -874,7 +874,7 @@ pub(super) unsafe fn ret(raw: RetReg<R0>) -> io::Result<()> {
874874
///
875875
/// The caller must ensure that this is the return value of a syscall which
876876
/// doesn't return on success.
877-
#[cfg(any(feature = "event", feature = "runtime"))]
877+
#[cfg(any(feature = "event", feature = "runtime", feature = "system"))]
878878
#[inline]
879879
pub(super) unsafe fn ret_error(raw: RetReg<R0>) -> io::Errno {
880880
try_decode_error(raw)

src/backend/linux_raw/io/errno.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub(in crate::backend) unsafe fn try_decode_void<Num: RetNumber>(
236236
/// # Safety
237237
///
238238
/// This must only be used with syscalls which do not return on success.
239-
#[cfg(any(feature = "event", feature = "runtime"))]
239+
#[cfg(any(feature = "event", feature = "runtime", feature = "system"))]
240240
#[inline]
241241
pub(in crate::backend) unsafe fn try_decode_error<Num: RetNumber>(raw: RetReg<Num>) -> io::Errno {
242242
debug_assert!(raw.is_in_range(-4095..0));

src/backend/linux_raw/net/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub(crate) mod addr;
22
pub(crate) mod msghdr;
3-
#[cfg(linux_kernel)]
43
pub(crate) mod netdevice;
54
pub(crate) mod read_sockaddr;
65
pub(crate) mod send_recv;

src/backend/linux_raw/system/syscalls.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
use super::types::RawUname;
99
use crate::backend::c;
10-
use crate::backend::conv::{c_int, ret, ret_error, ret_infallible, slice};
11-
#[cfg(feature = "fs")]
10+
use crate::backend::conv::{c_int, ret, ret_infallible, slice};
11+
#[cfg(any(target_os = "linux", target_os = "android"))]
1212
use crate::fd::BorrowedFd;
1313
use crate::ffi::CStr;
1414
use crate::io;
@@ -51,12 +51,11 @@ pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
5151
}
5252
}
5353

54-
#[cfg(any(target_os = "linux", target_os = "android"))]
5554
#[inline]
56-
pub(crate) fn init_module(image: &[u8], param_values: &CStr) -> io::Errno {
55+
pub(crate) fn init_module(image: &[u8], param_values: &CStr) -> io::Result<()> {
5756
let (image, len) = slice(image);
5857
unsafe {
59-
ret_error(syscall_readonly!(
58+
ret(syscall_readonly!(
6059
__NR_init_module,
6160
image,
6261
len,
@@ -65,12 +64,14 @@ pub(crate) fn init_module(image: &[u8], param_values: &CStr) -> io::Errno {
6564
}
6665
}
6766

68-
#[cfg(any(target_os = "linux", target_os = "android"))]
69-
#[cfg(feature = "fs")]
7067
#[inline]
71-
pub(crate) fn finit_module(fd: BorrowedFd<'_>, param_values: &CStr, flags: c::c_int) -> io::Errno {
68+
pub(crate) fn finit_module(
69+
fd: BorrowedFd<'_>,
70+
param_values: &CStr,
71+
flags: c::c_int,
72+
) -> io::Result<()> {
7273
unsafe {
73-
ret_error(syscall_readonly!(
74+
ret(syscall_readonly!(
7475
__NR_finit_module,
7576
fd,
7677
param_values,
@@ -79,9 +80,7 @@ pub(crate) fn finit_module(fd: BorrowedFd<'_>, param_values: &CStr, flags: c::c_
7980
}
8081
}
8182

82-
#[cfg(any(target_os = "linux", target_os = "android"))]
83-
#[cfg(feature = "fs")]
8483
#[inline]
85-
pub(crate) fn delete_module(name: &CStr, flags: c::c_int) -> io::Errno {
86-
unsafe { ret_error(syscall_readonly!(__NR_delete_module, name, c_int(flags))) }
84+
pub(crate) fn delete_module(name: &CStr, flags: c::c_int) -> io::Result<()> {
85+
unsafe { ret(syscall_readonly!(__NR_delete_module, name, c_int(flags))) }
8786
}

src/system.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![allow(unsafe_code)]
88

99
use crate::backend;
10-
#[cfg(any(linux_raw, target_os = "linux"))]
10+
#[cfg(linux_kernel)]
1111
use crate::backend::c;
1212
use crate::ffi::CStr;
1313
#[cfg(not(any(target_os = "espidf", target_os = "emscripten", target_os = "vita")))]
@@ -17,9 +17,9 @@ use core::fmt;
1717
#[cfg(linux_kernel)]
1818
pub use backend::system::types::Sysinfo;
1919

20-
#[cfg(linux_raw)]
20+
#[cfg(linux_kernel)]
2121
use crate::fd::AsFd;
22-
#[cfg(linux_raw)]
22+
#[cfg(linux_kernel)]
2323
use c::c_int;
2424

2525
/// `uname()`—Returns high-level information about the runtime OS and
@@ -231,8 +231,8 @@ pub fn reboot(cmd: RebootCommand) -> io::Result<()> {
231231
///
232232
/// [Linux]: https://man7.org/linux/man-pages/man2/init_module.2.html
233233
#[inline]
234-
#[cfg(linux_raw)]
235-
pub fn init_module(image: &[u8], param_values: &CStr) -> io::Errno {
234+
#[cfg(linux_kernel)]
235+
pub fn init_module(image: &[u8], param_values: &CStr) -> io::Result<()> {
236236
backend::system::syscalls::init_module(image, param_values)
237237
}
238238

@@ -243,9 +243,8 @@ pub fn init_module(image: &[u8], param_values: &CStr) -> io::Errno {
243243
///
244244
/// [Linux]: https://man7.org/linux/man-pages/man2/finit_module.2.html
245245
#[inline]
246-
#[cfg(linux_raw)]
247-
#[cfg(feature = "fs")]
248-
pub fn finit_module<Fd: AsFd>(fd: Fd, param_values: &CStr, flags: c_int) -> io::Errno {
246+
#[cfg(linux_kernel)]
247+
pub fn finit_module<Fd: AsFd>(fd: Fd, param_values: &CStr, flags: c_int) -> io::Result<()> {
249248
backend::system::syscalls::finit_module(fd.as_fd(), param_values, flags)
250249
}
251250

@@ -256,7 +255,7 @@ pub fn finit_module<Fd: AsFd>(fd: Fd, param_values: &CStr, flags: c_int) -> io::
256255
///
257256
/// [Linux]: https://man7.org/linux/man-pages/man2/delete_module.2.html
258257
#[inline]
259-
#[cfg(linux_raw)]
260-
pub fn delete_module(name: &CStr, flags: c_int) -> io::Errno {
258+
#[cfg(linux_kernel)]
259+
pub fn delete_module(name: &CStr, flags: c_int) -> io::Result<()> {
261260
backend::system::syscalls::delete_module(name, flags)
262261
}

0 commit comments

Comments
 (0)