Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linux support for sched_attr and related syscalls #3494

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3448,6 +3448,7 @@ fn test_linux(target: &str) {
"linux/reboot.h",
"linux/rtnetlink.h",
"linux/sched.h",
"linux/sched/types.h",
"linux/sctp.h",
"linux/seccomp.h",
"linux/sock_diag.h",
Expand Down
5 changes: 5 additions & 0 deletions libc-test/semver/linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3741,6 +3741,11 @@ sched_getcpu
sched_getparam
sched_getscheduler
sched_param
sched_attr
sched_getattr
sched_setattr
SCHED_ATTR_SIZE_VER0
SCHED_ATTR_SIZE_VER1
sched_rr_get_interval
sched_setaffinity
sched_setparam
Expand Down
30 changes: 30 additions & 0 deletions src/unix/linux_like/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,19 @@ s_no_extra_traits! {
pub d_type: ::c_uchar,
pub d_name: [::c_char; 256],
}

pub struct sched_attr {
pub size: ::c_uint,
pub sched_policy: ::c_uint,
pub sched_flags: ::c_ulonglong,
pub sched_nice: ::c_int,
pub sched_priority: ::c_uint,
pub sched_runtime: ::c_ulonglong,
pub sched_deadline: ::c_ulonglong,
pub sched_period: ::c_ulonglong,
pub sched_util_min: ::c_uint,
pub sched_util_max: ::c_uint,
}
}

s_no_extra_traits! {
Expand Down Expand Up @@ -2012,6 +2025,9 @@ pub const SCHED_IDLE: ::c_int = 5;

pub const SCHED_RESET_ON_FORK: ::c_int = 0x40000000;

pub const SCHED_ATTR_SIZE_VER0: ::c_uint = 48;
pub const SCHED_ATTR_SIZE_VER1: ::c_uint = 56;

pub const CLONE_PIDFD: ::c_int = 0x1000;

// netinet/in.h
Expand Down Expand Up @@ -4520,6 +4536,20 @@ pub const NET_DCCP: ::c_int = 20;
pub const NET_IRDA: ::c_int = 412;

f! {
// This is a workaround since glibc provides no wrappers for these syscalls.
// Therefore we implement them directly through `syscall`
pub fn sched_getattr(
pid: ::pid_t,
attr: *mut ::sched_attr,
size: ::c_uint,
flags: ::c_uint
) -> ::c_int {
::syscall(::SYS_sched_getattr, pid, attr, size, flags) as ::c_int
}
pub fn sched_setattr(pid: ::pid_t, attr: *mut ::sched_attr, flags: ::c_uint) -> ::c_int {
::syscall(::SYS_sched_setattr, pid, attr, flags) as ::c_int
}
Comment on lines +4539 to +4551
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The libc crate is a thin wrapper, and if glibc doesn't expose it, it shouldn't be added.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your response.
Before opening this PR I had seen that there are already some methods in the android target which are not exposed and use the syscall directly, so I hoped this might also be applicable here.

E.g.

f! {
// Sadly, Android before 5.0 (API level 21), the accept4 syscall is not
// exposed by the libc. As work-around, we implement it as raw syscall.
// Note that for x86, the `accept4` syscall is not available either,
// and we must use the `socketcall` syscall instead.
// This workaround can be removed if the minimum Android version is bumped.
// When the workaround is removed, `accept4` can be moved back
// to `linux_like/mod.rs`
pub fn accept4(
fd: ::c_int,
addr: *mut ::sockaddr,
len: *mut ::socklen_t,
flg: ::c_int
) -> ::c_int {
// Arguments are passed as array of `long int`
// (which is big enough on x86 for a pointer).
let mut args = [
fd as ::c_long,
addr as ::c_long,
len as ::c_long,
flg as ::c_long,
];
::syscall(SYS_socketcall, SYS_ACCEPT4, args[..].as_mut_ptr())
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that it's just a temporal workaround, I mean it should also be removed but it isn't now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, thanks again for your time.


pub fn NLA_ALIGN(len: ::c_int) -> ::c_int {
return ((len) + NLA_ALIGNTO - 1) & !(NLA_ALIGNTO - 1)
}
Expand Down