Merge branch 'doc-sysstat-and-syssocket' into 'master'

Doc sysstat and syssocket

See merge request redox-os/relibc!773
This commit is contained in:
Jeremy Soller
2025-12-03 07:47:14 -07:00
2 changed files with 60 additions and 4 deletions
+36 -2
View File
@@ -1,11 +1,18 @@
//! socket implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xns/syssocket.h.html
//! `sys/socket.h` implementation.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
use core::{mem, ptr};
use crate::{
error::ResultExt,
header::sys_uio::iovec,
platform::{PalSocket, Sys, types::*},
platform::{
PalSocket, Sys,
types::{
c_char, c_int, c_long, c_uchar, c_uint, c_void, gid_t, pid_t, size_t, ssize_t, uid_t,
},
},
};
pub mod constants;
@@ -13,6 +20,7 @@ pub mod constants;
pub type sa_family_t = u16;
pub type socklen_t = u32;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[repr(C)]
#[derive(Default, CheckVsLibcCrate)]
pub struct linger {
@@ -23,6 +31,7 @@ pub struct linger {
#[unsafe(no_mangle)]
pub extern "C" fn _cbindgen_export_linger(linger: linger) {}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[repr(C)]
#[derive(Debug, CheckVsLibcCrate)]
pub struct msghdr {
@@ -35,6 +44,7 @@ pub struct msghdr {
pub msg_flags: c_int,
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[repr(C)]
#[derive(Debug, CheckVsLibcCrate)]
pub struct cmsghdr {
@@ -55,6 +65,7 @@ pub struct ucred {
#[unsafe(no_mangle)]
pub extern "C" fn _cbindgen_export_cmsghdr(cmsghdr: cmsghdr) {}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[repr(C)]
#[derive(Default, CheckVsLibcCrate)]
pub struct sockaddr {
@@ -67,6 +78,7 @@ const _SS_MAXSIZE: usize = 128;
// Align to pointer width
const _SS_PADDING: usize = _SS_MAXSIZE - mem::size_of::<sa_family_t>() - mem::size_of::<usize>();
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
/// Opaque storage large enough to hold any protocol specific address structure.
///
/// ## Implementation notes
@@ -97,11 +109,13 @@ pub unsafe extern "C" fn __MHDR_END(mhdr: *const msghdr) -> *mut c_uchar {
unsafe { ((*mhdr).msg_control as *mut c_uchar).offset((*mhdr).msg_controllen as isize) }
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
unsafe { (cmsg as *mut c_uchar).offset(CMSG_ALIGN(mem::size_of::<cmsghdr>()) as isize) }
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
if cmsg.is_null() {
@@ -121,6 +135,7 @@ pub unsafe extern "C" fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr)
}
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
unsafe {
@@ -137,17 +152,20 @@ pub unsafe extern "C" fn CMSG_ALIGN(len: size_t) -> size_t {
(len + mem::size_of::<size_t>() - 1) & !(mem::size_of::<size_t>() - 1)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CMSG_SPACE(len: c_uint) -> c_uint {
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(mem::size_of::<cmsghdr>())) as c_uint
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CMSG_LEN(length: c_uint) -> c_uint {
(CMSG_ALIGN(mem::size_of::<cmsghdr>()) + length as usize) as c_uint
}
// } These must match C macros in include/bits/sys/socket.h
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/accept.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn accept(
socket: c_int,
@@ -163,6 +181,7 @@ pub unsafe extern "C" fn accept(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/bind.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bind(
socket: c_int,
@@ -180,6 +199,7 @@ pub unsafe extern "C" fn bind(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/connect.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn connect(
socket: c_int,
@@ -195,6 +215,7 @@ pub unsafe extern "C" fn connect(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpeername.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn getpeername(
socket: c_int,
@@ -212,6 +233,7 @@ pub unsafe extern "C" fn getpeername(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getsockname.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn getsockname(
socket: c_int,
@@ -229,6 +251,7 @@ pub unsafe extern "C" fn getsockname(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getsockopt.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn getsockopt(
socket: c_int,
@@ -250,6 +273,7 @@ pub unsafe extern "C" fn getsockopt(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/listen.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn listen(socket: c_int, backlog: c_int) -> c_int {
Sys::listen(socket, backlog)
@@ -257,6 +281,7 @@ pub unsafe extern "C" fn listen(socket: c_int, backlog: c_int) -> c_int {
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/recv.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn recv(
socket: c_int,
@@ -274,6 +299,7 @@ pub unsafe extern "C" fn recv(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/recvfrom.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn recvfrom(
socket: c_int,
@@ -297,6 +323,7 @@ pub unsafe extern "C" fn recvfrom(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/recvmsg.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn recvmsg(socket: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t {
Sys::recvmsg(socket, msg, flags)
@@ -304,6 +331,7 @@ pub unsafe extern "C" fn recvmsg(socket: c_int, msg: *mut msghdr, flags: c_int)
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/send.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn send(
socket: c_int,
@@ -314,6 +342,7 @@ pub unsafe extern "C" fn send(
sendto(socket, message, length, flags, ptr::null(), 0)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sendmsg.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sendmsg(socket: c_int, msg: *const msghdr, flags: c_int) -> ssize_t {
Sys::sendmsg(socket, msg, flags)
@@ -321,6 +350,7 @@ pub unsafe extern "C" fn sendmsg(socket: c_int, msg: *const msghdr, flags: c_int
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sendto.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sendto(
socket: c_int,
@@ -344,6 +374,7 @@ pub unsafe extern "C" fn sendto(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/setsockopt.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn setsockopt(
socket: c_int,
@@ -365,11 +396,13 @@ pub unsafe extern "C" fn setsockopt(
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/shutdown.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn shutdown(socket: c_int, how: c_int) -> c_int {
Sys::shutdown(socket, how).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/socket.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn socket(domain: c_int, kind: c_int, protocol: c_int) -> c_int {
trace_expr!(
@@ -381,6 +414,7 @@ pub unsafe extern "C" fn socket(domain: c_int, kind: c_int, protocol: c_int) ->
)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/socketpair.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn socketpair(
domain: c_int,
+24 -2
View File
@@ -1,4 +1,6 @@
//! stat implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html
//! `sys/stat.h` implementation.
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_stat.h.html>.
use crate::{
c_str::CStr,
@@ -8,7 +10,13 @@ use crate::{
time::timespec,
},
out::Out,
platform::{Pal, Sys, types::*},
platform::{
Pal, Sys,
types::{
blkcnt_t, blksize_t, c_char, c_int, dev_t, gid_t, ino_t, mode_t, nlink_t, off_t, uid_t,
useconds_t,
},
},
};
pub const S_IFMT: c_int = 0o0_170_000;
@@ -47,6 +55,7 @@ pub const S_ISVTX: c_int = 0o1_000;
pub const UTIME_NOW: useconds_t = (1 << 30) - 1;
pub const UTIME_OMIT: useconds_t = (1 << 30) - 2;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_stat.h.html>.
#[repr(C)]
#[derive(Default)]
pub struct stat {
@@ -71,17 +80,20 @@ pub struct stat {
pub _pad: [c_char; 24],
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/chmod.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn chmod(path: *const c_char, mode: mode_t) -> c_int {
let path = CStr::from_ptr(path);
Sys::chmod(path, mode).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/fchmod.html>.
#[unsafe(no_mangle)]
pub extern "C" fn fchmod(fildes: c_int, mode: mode_t) -> c_int {
Sys::fchmod(fildes, mode).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/fchmodat.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn fchmodat(
dirfd: c_int,
@@ -95,12 +107,14 @@ pub unsafe extern "C" fn fchmodat(
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/fstat.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn fstat(fildes: c_int, buf: *mut stat) -> c_int {
let buf = Out::nonnull(buf);
Sys::fstat(fildes, buf).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/fstatat.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn fstatat(
fildes: c_int,
@@ -120,11 +134,13 @@ pub unsafe extern "C" fn __fxstat(_ver: c_int, fildes: c_int, buf: *mut stat) ->
fstat(fildes, buf)
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/futimens.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn futimens(fd: c_int, times: *const timespec) -> c_int {
Sys::futimens(fd, times).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/lstat.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int {
let path = CStr::from_ptr(path);
@@ -144,24 +160,28 @@ pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int {
res
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/mkdir.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
let path = CStr::from_ptr(path);
Sys::mkdir(path, mode).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/mkfifo.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mkfifo(path: *const c_char, mode: mode_t) -> c_int {
let path = CStr::from_ptr(path);
Sys::mkfifo(path, mode).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/mknod.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mknod(path: *const c_char, mode: mode_t, dev: dev_t) -> c_int {
let path = CStr::from_ptr(path);
Sys::mknod(path, mode, dev).map(|()| 0).or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/mknodat.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mknodat(
dirfd: c_int,
@@ -175,6 +195,7 @@ pub unsafe extern "C" fn mknodat(
.or_minus_one_errno()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/stat.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn stat(file: *const c_char, buf: *mut stat) -> c_int {
let file = CStr::from_ptr(file);
@@ -194,6 +215,7 @@ pub unsafe extern "C" fn stat(file: *const c_char, buf: *mut stat) -> c_int {
res
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/umask.html>.
#[unsafe(no_mangle)]
pub extern "C" fn umask(mask: mode_t) -> mode_t {
Sys::umask(mask)