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

Conversation

matthri
Copy link

@matthri matthri commented Dec 21, 2023

Hi,
I this PR should add support for the sched_attr config struct and the related sched_getattr and sched_setattr syscalls.
Since this is my first PR to this repo and I'm not that experienced with the different standard libraries I encountered some problems and wanted to ask for some assistance/ hints on how this best implemented in the interests of this project.

  1. Header file conflict: The sched_attr struct is defined in linux/sched/types.h but when I add this file to the includes in the build.rs file I get some build errors since the file additionally defines the sched_param struct which is also defined by the bits/types/struct_sched_param.h header which is included by pthread.h.
    Can you point me in the right direction how this is best resolved?
  2. Multiple struct versions: There exist multiple versions of the sched_attr struct which are distinguished by the size field of the struct.
    Whats in your opinion the best way to represent this in this crate?

Many thanks in advance and sorry for the wall of text above :D

@rustbot
Copy link
Collaborator

rustbot commented Dec 21, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @JohnTitor (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

Comment on lines +4539 to +4551
// 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
}
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants