From cc31079a1123498faf1acfb7f12b5976c6a686af Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Mon, 4 Oct 2021 09:49:34 -0700 Subject: [PATCH] [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. --- hal/src/main/native/athena/Threads.cpp | 52 +++--------------------- hal/src/main/native/include/hal/Errors.h | 3 -- 2 files changed, 6 insertions(+), 49 deletions(-) diff --git a/hal/src/main/native/athena/Threads.cpp b/hal/src/main/native/athena/Threads.cpp index f7afa91992..aa55b56945 100644 --- a/hal/src/main/native/athena/Threads.cpp +++ b/hal/src/main/native/athena/Threads.cpp @@ -6,42 +6,9 @@ #include #include -#include - -#include -#include -#include - -#include #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(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(handle), + scheduler, &sch)) { + *status = HAL_THREAD_PRIORITY_ERROR; return false; + } else { + *status = 0; + return true; } } diff --git a/hal/src/main/native/include/hal/Errors.h b/hal/src/main/native/include/hal/Errors.h index 724a60c19d..5da75d956c 100644 --- a/hal/src/main/native/include/hal/Errors.h +++ b/hal/src/main/native/include/hal/Errors.h @@ -135,9 +135,6 @@ #define HAL_USE_LAST_ERROR_MESSAGE \ "HAL: Use HAL_GetLastError(status) to get last error" -#define HAL_SETUID_ERROR -1157 -#define HAL_SETUID_ERROR_MESSAGE "HAL: Setting the effective user ID has failed" - #define HAL_CAN_BUFFER_OVERRUN -35007 #define HAL_CAN_BUFFER_OVERRUN_MESSAGE \ "HAL: CAN Output Buffer Full. Ensure a device is attached"