Created
December 1, 2022 12:10
-
-
Save valmat/a9db9cba91b636e0a4c21cbf2e3c9eaf to your computer and use it in GitHub Desktop.
function to change process owner (if required)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
// true if current user is not root | |
// or true if owner changing is not required (targ_uid == 0) | |
// or true if current user is root and uid changed | |
// false otherwise (owner changing was unsuccessful) | |
bool change_owner(unsigned int targ_uid) noexcept | |
{ | |
return | |
(0u != getuid()) || // made sure that src_uid == 0 (source is root) | |
(0u == targ_uid) || // made sure that targ_uid != 0 (target is not root) | |
(0 == (setgid(targ_uid) & setuid(targ_uid))); // set the gid and then the uid and then checked the result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment