I'd like to write some Qt code for a KDE app that distinguishes between when a user is root vs when the user has used sudo to request elevated privileges, so the app can display a different message for each use case. Checking for uid == 0
captures both cases, but how do I distinguish between them?
You can read SUDO_USER
environment variable using QProcessEnvironment
class:
QProcessEnvironment system_env = QProcessEnvironment::systemEnvironment();
qDebug() << "USER : " << system_env.value("USER");
qDebug() << "SUDOER: " << system_env.value("SUDO_USER");
or #include <unistd.h>
and use getlogin
:
std::cout << "LOGGED IN USER: " << getlogin() << std::endl;