How can I tell if the script is being run as super user?
I've written a fan control script in Lua, and I'm running Linux. The fan control needs to write to /sys/ so I need to be super user.
I want to inform the user if they are NOT super user开发者_如何学C. What's the best way to go about doing this?
The only certain test is to check whether the POSIX geteuid() call returns zero.
For this you may have to use a Lua library like luaposix.
Or you can shell out and run id -u
to see if it outputs 0
.
Since it's Linux, you can try this, which is stock Lua and does not need additional libraries:
function running_as_root()
local f=io.open"/root"
if f==nil then return false else io.close(f) return true end
end
But why not just try to write to /sys/ and report failure if that happens?
best thing to do is just report failure: you could have a system where other users than root have access to sysfs.
fd = assert(io.open("/sys/...."))
精彩评论