I am writing an application that requires root user privileges to execute. If executed by a non root user, it exits and terminates with a perror message such as:
pthread_getschedparam: Operation not permitted
I would like to make the application more user friendly. As part of its early initialization I would like it to check if it is being executed by root or not. And if not root, it would present a message indicating that it can only be run by root, and then terminate.
getuidorgeteuidwould be the obvious choices.getuidchecks the credentials of the actual user.The added
eingeteuidstands foreffective. It checks the effective credentials.Just for example, if you use
sudoto run a program as root (superuser), your actual credentials are still your own account, but your effective credentials are those of the root account (or a member of the wheel group, etc.)For example, consider code like this:
If you run this normally,
getuid()andgeteuid()will return the same value, so it’ll say “running as self”. If you dosudo ./a.outinstead,getuid()will still return your user ID, butgeteuid()will return the credentials for root or wheel, so it’ll say “Running as somebody else”.