root launch

Linux specific forum
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 153
Joined: Thu Dec 28, 2023 9:04 pm

root launch

Post by rndrei »

How do I know if a program is running as an administrator, superuser?
User avatar
NicTheQuick
Addict
Addict
Posts: 1519
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: root launch

Post by NicTheQuick »

There are four function you can use to get real and effective user and group IDs:

Code: Select all

OpenConsole()
PrintN("Real user ID: " + Str(getuid_()))
PrintN("Effective user ID: " + Str(geteuid_()))
PrintN("Real group ID: " + Str(getgid_()))
PrintN("Effective group ID: " + Str(getegid_()))
CloseConsole()
Here's an example output:
$ ./tmp/showuserid
Real user ID: 1000
Effective user ID: 1000
Real group ID: 1000
Effective group ID: 1000
$ sudo ./tmp/showuserid
[sudo] Passwort für nicolas:
Real user ID: 0
Effective user ID: 0
Real group ID: 0
Effective group ID: 0
And if the executable owner is root and the setuid bit is set it can look like this:
$ sudo chown root:root tmp/showuserid
$ sudo chmod +s ./tmp/showuserid
$ ./tmp/showuserid
Real user ID: 1000
Effective user ID: 0
Real group ID: 1000
Effective group ID: 0
In this case you can differentiate between the user that executed the program and the one who is the owner of the executable.
You can use `setuid_(uid)` to switch between the user with id 0 and 1000 (in this case), depending on what you want to do.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 153
Joined: Thu Dec 28, 2023 9:04 pm

Re: root launch

Post by rndrei »

Just what you need, thank you!
Post Reply