2026-02-23 13:22:21 -06:00
|
|
|
/*
|
|
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-26 12:36:56 -04:00
|
|
|
#include "ioprio.hpp"
|
|
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
2025-09-26 12:36:56 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#else
|
2025-11-01 18:04:28 -05:00
|
|
|
#pragma message "ioprio not supported on this platform"
|
2025-09-26 12:36:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
2025-09-28 01:01:26 -04:00
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
IOPRIO_WHO_PROCESS = 1
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-13 19:26:37 -05:00
|
|
|
namespace ioprio
|
|
|
|
|
{
|
|
|
|
|
std::atomic<bool> _enabled{false};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 12:36:56 -04:00
|
|
|
thread_local int ioprio::SetFrom::thread_prio = -1;
|
|
|
|
|
|
|
|
|
|
int
|
2025-09-29 00:17:51 -04:00
|
|
|
ioprio::get(const int who_)
|
2025-09-26 12:36:56 -04:00
|
|
|
{
|
|
|
|
|
#ifdef SYS_ioprio_get
|
|
|
|
|
int rv;
|
2025-09-29 00:17:51 -04:00
|
|
|
const int which = IOPRIO_WHO_PROCESS;
|
2025-09-26 12:36:56 -04:00
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
rv = syscall(SYS_ioprio_get,which,who_);
|
2025-09-26 12:36:56 -04:00
|
|
|
|
|
|
|
|
return ((rv == -1) ? -errno : rv);
|
|
|
|
|
#else
|
2025-09-29 00:17:51 -04:00
|
|
|
return -EOPNOTSUPP;
|
2025-09-26 12:36:56 -04:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2025-09-29 00:17:51 -04:00
|
|
|
ioprio::set(const int who_,
|
2025-09-26 12:36:56 -04:00
|
|
|
const int ioprio_)
|
|
|
|
|
{
|
|
|
|
|
#ifdef SYS_ioprio_set
|
|
|
|
|
int rv;
|
2025-09-29 00:17:51 -04:00
|
|
|
const int which = IOPRIO_WHO_PROCESS;
|
2025-09-26 12:36:56 -04:00
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
rv = syscall(SYS_ioprio_set,which,who_,ioprio_);
|
2025-09-26 12:36:56 -04:00
|
|
|
|
|
|
|
|
return ((rv == -1) ? -errno : rv);
|
|
|
|
|
#else
|
2025-09-29 00:17:51 -04:00
|
|
|
return -EOPNOTSUPP;
|
2025-09-26 12:36:56 -04:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ioprio::enable(const bool enable_)
|
|
|
|
|
{
|
2026-05-13 19:26:37 -05:00
|
|
|
_enabled.store(enable_,std::memory_order_relaxed);
|
2025-09-26 12:36:56 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-13 19:26:37 -05:00
|
|
|
void
|
|
|
|
|
ioprio::SetFrom::_slow_apply(const pid_t pid_)
|
2025-09-26 12:36:56 -04:00
|
|
|
{
|
|
|
|
|
int client_prio;
|
|
|
|
|
|
2025-09-29 00:17:51 -04:00
|
|
|
client_prio = ioprio::get(pid_);
|
2025-09-26 12:36:56 -04:00
|
|
|
if(client_prio < 0)
|
|
|
|
|
return;
|
|
|
|
|
if(client_prio == thread_prio)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
thread_prio = client_prio;
|
2025-09-29 00:17:51 -04:00
|
|
|
ioprio::set(0,client_prio);
|
2025-09-26 12:36:56 -04:00
|
|
|
}
|