[hal] Use setcap instead of setuid for setting thread priorities (#3613)

We originally moved to setuid admin so user programs could do other
things requiring admin if they wanted. However, these things, like
setting RT priorities of other processes, can usually be done instead as
admin during the GradleRIO 2022 deploy process, or adding commands to
the robotCommand script. By going back to setcap, we can simplify the
HAL code.
This commit is contained in:
Tyler Veness
2021-10-04 09:49:34 -07:00
committed by GitHub
parent 4676648b78
commit cc31079a11
2 changed files with 6 additions and 49 deletions

View File

@@ -6,42 +6,9 @@
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#include <system_error>
#include <fmt/format.h>
#include "hal/Errors.h"
namespace {
class UidSetter {
public:
explicit UidSetter(uid_t uid) {
m_uid = geteuid();
if (uid == 0 && setuid(uid) == -1) {
throw std::system_error(errno, std::generic_category(),
fmt::format("setuid({}) failed", uid));
} else if (uid != 0 && seteuid(uid) == -1) {
throw std::system_error(errno, std::generic_category(),
fmt::format("seteuid({}) failed", uid));
}
}
~UidSetter() noexcept(false) {
if (geteuid() != m_uid && seteuid(m_uid) == -1) {
throw std::system_error(errno, std::generic_category(),
fmt::format("seteuid({}) failed", m_uid));
}
}
private:
uid_t m_uid;
};
} // namespace
namespace hal::init {
void InitializeThreads() {}
} // namespace hal::init
@@ -104,20 +71,13 @@ HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
sch.sched_priority = 0;
}
try {
UidSetter uidSetter{0};
if (pthread_setschedparam(*reinterpret_cast<const pthread_t*>(handle),
scheduler, &sch)) {
*status = HAL_THREAD_PRIORITY_ERROR;
return false;
} else {
*status = 0;
return true;
}
} catch (const std::system_error& e) {
*status = HAL_SETUID_ERROR;
if (pthread_setschedparam(*reinterpret_cast<const pthread_t*>(handle),
scheduler, &sch)) {
*status = HAL_THREAD_PRIORITY_ERROR;
return false;
} else {
*status = 0;
return true;
}
}