ptp-helper: Set a process priority / nice value of -5 on UNIX platforms

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3889>
This commit is contained in:
Sebastian Dröge 2023-03-31 23:15:30 +03:00 committed by GStreamer Marge Bot
parent 572d344482
commit 9d25a5075e
3 changed files with 25 additions and 2 deletions

View file

@ -178,6 +178,8 @@ pub mod unix {
#[cfg(not(any(target_os = "linux", target_os = "solaris", target_os = "illumos")))]
pub const IF_NAMESIZE: usize = 16;
pub const PRIO_PROCESS: c_int = 0;
extern "C" {
#[cfg_attr(
all(target_os = "macos", target_arch = "x86"),
@ -220,6 +222,8 @@ pub mod unix {
pub fn getifaddrs(ifap: *mut *mut ifaddrs) -> c_int;
pub fn freeifaddrs(ifa: *mut ifaddrs);
pub fn setpriority(which: c_int, who: c_int, prio: c_int) -> c_int;
}
#[cfg(any(target_os = "linux", target_os = "solaris", target_os = "illumos"))]

View file

@ -17,8 +17,8 @@ case "$with_ptp_helper_permissions" in
ls -l "$ptp_helper"
;;
capabilities)
echo "Calling $setcap cap_net_bind_service,cap_net_admin+ep $ptp_helper"
$setcap cap_net_bind_service,cap_net_admin+ep "$ptp_helper" || true
echo "Calling $setcap cap_sys_nice,cap_net_bind_service,cap_net_admin+ep $ptp_helper"
$setcap cap_sys_nice,cap_net_bind_service,cap_net_admin+ep "$ptp_helper" || true
;;
none)
echo "No perms/caps to set for $ptp_helper"

View file

@ -11,6 +11,25 @@
use crate::error::Error;
pub fn set_priority() -> Result<(), Error> {
#[cfg(unix)]
{
use std::io;
use crate::{bail, ffi::unix::*};
// SAFETY: Setting the process priority can happen at any time. A negative
// priority require special permissions, which should've been given to the process.
//
// On error it returns a negative value.
unsafe {
if setpriority(PRIO_PROCESS, 0, -5) < 0 {
bail!(
source: io::Error::last_os_error(),
"Failed to set process priority"
);
}
}
}
#[cfg(windows)]
{
use std::io;