Skip to content

Commit

Permalink
[hal] Use setcap instead of setuid for setting thread priorities (#3613)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
calcmogul authored Oct 4, 2021
1 parent 4676648 commit cc31079
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 49 deletions.
52 changes: 6 additions & 46 deletions hal/src/main/native/athena/Threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down
3 changes: 0 additions & 3 deletions hal/src/main/native/include/hal/Errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit cc31079

Please sign in to comment.